UniSet  2.2.1
UInterface.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00024 // --------------------------------------------------------------------------
00025 #ifndef UInterface_H_
00026 #define UInterface_H_
00027 // ---------------------------------------------------------------------------
00028 #include <memory>
00029 #include <string>
00030 #include <sstream>
00031 #include <unordered_map>
00032 #include <functional>
00033 #include <omniORB4/CORBA.h>
00034 #include "Exceptions.h"
00035 #include "UniSetTypes.h"
00036 #include "ObjectIndex.h"
00037 #include "ObjectRepository.h"
00038 #include "IOController_i.hh"
00039 #include "MessageType.h"
00040 #include "Configuration.h"
00041 
00042 // -----------------------------------------------------------------------------------------
00046 namespace UniversalIO
00047 {
00049     const unsigned int defaultTimeOut = 3;  // [сек]
00050 }
00051 
00052 // -----------------------------------------------------------------------------------------
00053 #define UI_THROW_EXCEPTIONS UniSetTypes::TimeOut,UniSetTypes::IOBadParam,UniSetTypes::ORepFailed
00054 // -----------------------------------------------------------------------------------------
00064 class UInterface
00065 {
00066     public:
00067 
00068         UInterface( const UniSetTypes::ObjectId backid, CORBA::ORB_var orb = NULL, const std::shared_ptr<UniSetTypes::ObjectIndex> oind = nullptr );
00069         UInterface( const std::shared_ptr<UniSetTypes::Configuration>& uconf = UniSetTypes::uniset_conf() );
00070         ~UInterface();
00071 
00072         // ---------------------------------------------------------------
00073         // Работа с датчиками
00074 
00076         long getValue ( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const throw(UI_THROW_EXCEPTIONS);
00077         long getValue ( const UniSetTypes::ObjectId id ) const;
00078         long getRawValue( const IOController_i::SensorInfo& si );
00079 
00081         void setValue ( const UniSetTypes::ObjectId id, long value, const UniSetTypes::ObjectId node ) const throw(UI_THROW_EXCEPTIONS);
00082         void setValue ( const UniSetTypes::ObjectId id, long value ) const;
00083         void setValue ( const IOController_i::SensorInfo& si, long value, const UniSetTypes::ObjectId supplier );
00084 
00085         // fast - это удалённый вызов "без подтверждения", он быстрее, но менее надёжен
00086         // т.к. вызывающий никогда не узнает об ошибке, если она была (датчик такой не найдён и т.п.)
00087         void fastSetValue( const IOController_i::SensorInfo& si, long value, UniSetTypes::ObjectId supplier );
00088 
00090         IOController_i::SensorInfoSeq_var getSensorSeq( const UniSetTypes::IDList& lst );
00091 
00094         UniSetTypes::IDSeq_var setOutputSeq( const IOController_i::OutSeq& lst, UniSetTypes::ObjectId sup_id );
00095 
00096         // ---------------------------------------------------------------
00097         // Заказ датчиков
00098 
00100         void askSensor( const UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd,
00101                         UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ) const;
00102 
00103         void askRemoteSensor( const UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, const UniSetTypes::ObjectId node,
00104                               UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ) const throw(UI_THROW_EXCEPTIONS);
00105 
00107         UniSetTypes::IDSeq_var askSensorsSeq( const UniSetTypes::IDList& lst, UniversalIO::UIOCommand cmd,
00108                                               UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId );
00109         // ------------------------------------------------------
00110 
00111         // установка неопределённого состояния
00112         void setUndefinedState( const IOController_i::SensorInfo& si, bool undefined, UniSetTypes::ObjectId supplier );
00113 
00114         // ---------------------------------------------------------------
00115         // Калибровка... пороги...
00116 
00118         void calibrate(const IOController_i::SensorInfo& si,
00119                        const IOController_i::CalibrateInfo& ci,
00120                        UniSetTypes::ObjectId adminId = UniSetTypes::DefaultObjectId );
00121 
00122         IOController_i::CalibrateInfo getCalibrateInfo( const IOController_i::SensorInfo& si );
00123 
00125         void askThreshold( const UniSetTypes::ObjectId sensorId, const UniSetTypes::ThresholdId tid,
00126                            UniversalIO::UIOCommand cmd,
00127                            long lowLimit, long hiLimit, bool invert = false,
00128                            UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ) const;
00129 
00130         void askRemoteThreshold( const UniSetTypes::ObjectId sensorId, const UniSetTypes::ObjectId node,
00131                                  const UniSetTypes::ThresholdId thresholdId, UniversalIO::UIOCommand cmd,
00132                                  long lowLimit, long hiLimit, bool invert = false,
00133                                  UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ) const;
00134 
00135         IONotifyController_i::ThresholdInfo getThresholdInfo( const IOController_i::SensorInfo& si, const UniSetTypes::ThresholdId tid ) const;
00136         IONotifyController_i::ThresholdInfo getThresholdInfo( const UniSetTypes::ObjectId sid, const UniSetTypes::ThresholdId tid ) const;
00137 
00138         // ---------------------------------------------------------------
00139         // Вспомогательные функции
00140 
00141         UniversalIO::IOType getIOType(const UniSetTypes::ObjectId id, UniSetTypes::ObjectId node) const throw(UI_THROW_EXCEPTIONS);
00142         UniversalIO::IOType getIOType(const UniSetTypes::ObjectId id) const;
00143 
00144         // read from xml (only for xml!) т.е. без удалённого запроса
00145         UniversalIO::IOType getConfIOType( const UniSetTypes::ObjectId id ) const;
00146 
00147         // Получение типа объекта..
00148         UniSetTypes::ObjectType getType(const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node) const throw(UI_THROW_EXCEPTIONS);
00149         UniSetTypes::ObjectType getType(const UniSetTypes::ObjectId id) const;
00150 
00151 
00153         IOController_i::ShortIOInfo getChangedTime( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const;
00154 
00156         IOController_i::ShortMapSeq* getSensors( const UniSetTypes::ObjectId id,
00157                 const UniSetTypes::ObjectId node = UniSetTypes::uniset_conf()->getLocalNode() );
00158 
00159         IOController_i::SensorInfoSeq* getSensorsMap( const UniSetTypes::ObjectId id,
00160                 const UniSetTypes::ObjectId node = UniSetTypes::uniset_conf()->getLocalNode() );
00161 
00162         IONotifyController_i::ThresholdsListSeq* getThresholdsList( const UniSetTypes::ObjectId id,
00163                 const UniSetTypes::ObjectId node = UniSetTypes::uniset_conf()->getLocalNode() );
00164         // ---------------------------------------------------------------
00165         // Работа с репозиторием
00166 
00167         //        /*! регистрация объекта в репозитории */
00168         void registered(const UniSetTypes::ObjectId id, const UniSetTypes::ObjectPtr oRef, bool force = false)const throw(UniSetTypes::ORepFailed);
00169 
00170         //        /*! разрегистрация объекта */
00171         void unregister(const UniSetTypes::ObjectId id)throw(UniSetTypes::ORepFailed);
00172 
00174         inline UniSetTypes::ObjectPtr resolve( const std::string& name ) const
00175         {
00176             return rep.resolve(name);
00177         }
00178 
00179         inline UniSetTypes::ObjectPtr resolve( const UniSetTypes::ObjectId id ) const
00180         {
00181             return rep.resolve( oind->getNameById(id) );
00182         }
00183 
00184         UniSetTypes::ObjectPtr resolve( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId nodeName, int timeoutMS = UniversalIO::defaultTimeOut) const
00185         throw(UniSetTypes::ResolveNameError, UniSetTypes::TimeOut);
00186 
00187 
00188         // Проверка доступности объекта или датчика
00189         bool isExist( const UniSetTypes::ObjectId id ) const;
00190         bool isExist( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const;
00191 
00192         // used for check 'isExist'
00193         bool waitReady( const UniSetTypes::ObjectId id, int msec, int pause = 5000,
00194                         const UniSetTypes::ObjectId node = UniSetTypes::uniset_conf()->getLocalNode() );
00195 
00196         // used for check 'getValue'
00197         bool waitWorking( const UniSetTypes::ObjectId id, int msec, int pause = 3000,
00198                           const UniSetTypes::ObjectId node = UniSetTypes::uniset_conf()->getLocalNode() );
00199 
00200         // ---------------------------------------------------------------
00201         // Работа с ID, Name
00202 
00204         inline UniSetTypes::ObjectId getIdByName( const std::string& name ) const
00205         {
00206             return oind->getIdByName(name);
00207         }
00208 
00210         inline std::string getNameById( const UniSetTypes::ObjectId id ) const
00211         {
00212             return oind->getNameById(id);
00213         }
00214 
00215         inline UniSetTypes::ObjectId getNodeId( const std::string& fullname ) const
00216         {
00217             return oind->getNodeId(fullname);
00218         }
00219 
00220         inline std::string getTextName( const UniSetTypes::ObjectId id ) const
00221         {
00222             return oind->getTextName(id);
00223         }
00224 
00225         // ---------------------------------------------------------------
00226         // Получение указателей на вспомогательные классы.
00227         inline const std::shared_ptr<UniSetTypes::ObjectIndex> getObjectIndex()
00228         {
00229             return oind;
00230         }
00231         inline const std::shared_ptr<UniSetTypes::Configuration> getConf()
00232         {
00233             return uconf;
00234         }
00235         // ---------------------------------------------------------------
00236         // Посылка сообщений
00237 
00239         void send( const UniSetTypes::ObjectId name, const UniSetTypes::TransportMessage& msg, UniSetTypes::ObjectId node ) throw(UI_THROW_EXCEPTIONS);
00240         void send( const UniSetTypes::ObjectId name, const UniSetTypes::TransportMessage& msg);
00241 
00242         // ---------------------------------------------------------------
00243         // Вспомогательный класс для кэширования ссылок на удалённые объекты
00244 
00245         inline void setCacheMaxSize( unsigned int newsize )
00246         {
00247             rcache.setMaxSize(newsize);
00248         }
00249 
00251         class CacheOfResolve
00252         {
00253             public:
00254                 CacheOfResolve( unsigned int maxsize, int cleancount = 20 ):
00255                     MaxSize(maxsize), minCallCount(cleancount) {};
00256                 ~CacheOfResolve() {};
00257 
00258                 UniSetTypes::ObjectPtr resolve( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const throw(UniSetTypes::NameNotFound);
00259                 void cache( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node, UniSetTypes::ObjectVar ptr ) const;
00260                 void erase( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const;
00261 
00262                 inline void setMaxSize( unsigned int ms )
00263                 {
00264                     MaxSize = ms;
00265                 };
00266 
00267             protected:
00268                 CacheOfResolve() {};
00269 
00270             private:
00271 
00272                 bool clean();       
00273                 inline void clear() 
00274                 {
00275                     UniSetTypes::uniset_rwmutex_wrlock l(cmutex);
00276                     mcache.clear();
00277                 };
00278 
00279                 struct Info
00280                 {
00281                     Info( const UniSetTypes::ObjectVar& ptr ): ptr(ptr), ncall(0) {}
00282                     Info(): ptr(NULL), ncall(0) {}
00283 
00284                     UniSetTypes::ObjectVar ptr;
00285                     unsigned long ncall; // счётчик обращений
00286 
00287                     bool operator<( const CacheOfResolve::Info& rhs ) const
00288                     {
00289                         return this->ncall > rhs.ncall;
00290                     }
00291                 };
00292 
00293                 typedef std::unordered_map<int, Info> CacheMap;
00294                 mutable CacheMap mcache;
00295                 mutable UniSetTypes::uniset_rwmutex cmutex;
00296                 unsigned int MaxSize;      
00297                 unsigned int minCallCount; 
00298         };
00299 
00300         void initBackId( UniSetTypes::ObjectId backid );
00301     protected:
00302         std::string set_err(const std::string& pre, const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node) const;
00303 
00304     private:
00305         void init();
00306 
00307         ObjectRepository rep;
00308         UniSetTypes::ObjectId myid;
00309         mutable CosNaming::NamingContext_var localctx;
00310         mutable CORBA::ORB_var orb;
00311         CacheOfResolve rcache;
00312         std::shared_ptr<UniSetTypes::ObjectIndex> oind;
00313         std::shared_ptr<UniSetTypes::Configuration> uconf;
00314 };
00315 // --------------------------------------------------------------------------
00316 #endif
00317 // --------------------------------------------------------------------------