UniSet  2.0.0
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 <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 ( 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         // ---------------------------------------------------------------
00160         // Работа с репозиторием
00161 
00162 //        /*! регистрация объекта в репозитории */
00163         void registered(const UniSetTypes::ObjectId id, const UniSetTypes::ObjectPtr oRef, bool force=false)const throw(UniSetTypes::ORepFailed);
00164 
00165 //        /*! разрегистрация объекта */
00166         void unregister(const UniSetTypes::ObjectId id)throw(UniSetTypes::ORepFailed);
00167 
00169         inline UniSetTypes::ObjectPtr resolve( const std::string& name ) const
00170         {
00171             return rep.resolve(name);
00172         }
00173 
00174         inline UniSetTypes::ObjectPtr resolve( const UniSetTypes::ObjectId id ) const
00175         {
00176             return rep.resolve( oind->getNameById(id) );
00177         }
00178 
00179         UniSetTypes::ObjectPtr resolve( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId nodeName, int timeoutMS=UniversalIO::defaultTimeOut) const
00180             throw(UniSetTypes::ResolveNameError, UniSetTypes::TimeOut);
00181 
00182 
00183         // Проверка доступности объекта или датчика
00184         bool isExist( const UniSetTypes::ObjectId id ) const;
00185         bool isExist( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const;
00186 
00187         bool waitReady( const UniSetTypes::ObjectId id, int msec, int pause=5000,
00188                         const UniSetTypes::ObjectId node = UniSetTypes::uniset_conf()->getLocalNode() );     // used exist
00189 
00190         bool waitWorking( const UniSetTypes::ObjectId id, int msec, int pause=3000,
00191                             const UniSetTypes::ObjectId node = UniSetTypes::uniset_conf()->getLocalNode() );     // used getValue
00192 
00193         // ---------------------------------------------------------------
00194         // Работа с ID, Name
00195 
00197         inline UniSetTypes::ObjectId getIdByName( const char* name ) const
00198         {
00199             return oind->getIdByName(name);
00200         }
00201         inline UniSetTypes::ObjectId getIdByName( const std::string& name ) const
00202         {
00203             return getIdByName(name.c_str());
00204         }
00205 
00207         inline std::string getNameById( const UniSetTypes::ObjectId id ) const
00208         {
00209             return oind->getNameById(id);
00210         }
00211 
00212         inline UniSetTypes::ObjectId getNodeId( const std::string& fullname ) const
00213         {
00214             return oind->getNodeId(fullname);
00215         }
00216 
00217         inline std::string getTextName( const UniSetTypes::ObjectId id ) const
00218         {
00219             return oind->getTextName(id);
00220         }
00221 
00222         // ---------------------------------------------------------------
00223         // Получение указателей на вспомогательные классы.
00224         inline const std::shared_ptr<UniSetTypes::ObjectIndex> getObjectIndex() { return oind; }
00225         inline const std::shared_ptr<UniSetTypes::Configuration> getConf() { return uconf; }
00226         // ---------------------------------------------------------------
00227         // Посылка сообщений
00228 
00230         void send( const UniSetTypes::ObjectId name, const UniSetTypes::TransportMessage& msg, UniSetTypes::ObjectId node ) throw(UI_THROW_EXCEPTIONS);
00231         void send( const UniSetTypes::ObjectId name, const UniSetTypes::TransportMessage& msg);
00232 
00233         // ---------------------------------------------------------------
00234         // Вспомогательный класс для кэширования ссылок на удалённые объекты
00235 
00236         inline void setCacheMaxSize( unsigned int newsize )
00237         {
00238             rcache.setMaxSize(newsize);
00239         }
00240 
00242         class CacheOfResolve
00243         {
00244             public:
00245                     CacheOfResolve( unsigned int maxsize, int cleancount=20 ):
00246                         MaxSize(maxsize), minCallCount(cleancount){};
00247                     ~CacheOfResolve(){};
00248 
00249                     UniSetTypes::ObjectPtr resolve( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const throw(UniSetTypes::NameNotFound);
00250                     void cache( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node, UniSetTypes::ObjectVar ptr ) const;
00251                     void erase( const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node ) const;
00252 
00253                     inline void setMaxSize( unsigned int ms )
00254                     {
00255                         MaxSize = ms;
00256                     };
00257 
00258             protected:
00259                     CacheOfResolve(){};
00260 
00261             private:
00262 
00263                 bool clean();       
00264                 inline void clear() 
00265                 {
00266                     UniSetTypes::uniset_rwmutex_wrlock l(cmutex);
00267                     mcache.clear();
00268                 };
00269 
00270                 struct Info
00271                 {
00272                     Info( UniSetTypes::ObjectVar ptr ):ptr(ptr),ncall(0){}
00273                     Info():ptr(NULL),ncall(0){}
00274 
00275                     UniSetTypes::ObjectVar ptr;
00276                     unsigned long ncall; // счётчик обращений
00277 
00278                     bool operator<( const CacheOfResolve::Info& rhs ) const
00279                     {
00280                         return this->ncall > rhs.ncall;
00281                     }
00282                 };
00283 
00284                 typedef std::map<int, Info> CacheMap;
00285                 mutable CacheMap mcache;
00286                 mutable UniSetTypes::uniset_rwmutex cmutex;
00287                 unsigned int MaxSize;      
00288                 unsigned int minCallCount; 
00289         };
00290 
00291         void initBackId( UniSetTypes::ObjectId backid );
00292     protected:
00293         std::string set_err(const std::string& pre, const UniSetTypes::ObjectId id, const UniSetTypes::ObjectId node) const;
00294 
00295     private:
00296         void init();
00297 
00298         ObjectRepository rep;
00299         UniSetTypes::ObjectId myid;
00300         mutable CosNaming::NamingContext_var localctx;
00301         mutable CORBA::ORB_var orb;
00302         CacheOfResolve rcache;
00303         std::shared_ptr<UniSetTypes::ObjectIndex> oind;
00304         std::shared_ptr<UniSetTypes::Configuration> uconf;
00305 };
00306 // --------------------------------------------------------------------------
00307 #endif
00308 // --------------------------------------------------------------------------