UniSet
1.4.0
|
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 UniversalInterface_H_ 00026 #define UniversalInterface_H_ 00027 // --------------------------------------------------------------------------- 00028 #include <string> 00029 #include <sstream> 00030 #include <map> 00031 #include <functional> 00032 #include <omniORB4/CORBA.h> 00033 #include "Exceptions.h" 00034 #include "UniSetTypes.h" 00035 #include "ObjectIndex.h" 00036 #include "ObjectRepository.h" 00037 #include "IOController_i.hh" 00038 #include "MessageType.h" 00039 #include "Configuration.h" 00040 00041 // ----------------------------------------------------------------------------------------- 00045 namespace UniversalIO 00046 { 00048 const unsigned int defaultTimeOut=3; // [сек] 00049 } 00050 00051 // ----------------------------------------------------------------------------------------- 00052 //#define REPEAT_TIMEOUT 100 // [мс] пауза между попытками вызвать удаленную функцию объекта 00053 //#define REPEAT_COUNT 5 // количество попыток, после которого вырабатывается TimeOut 00054 00055 #define IO_THROW_EXCEPTIONS UniSetTypes::TimeOut,UniSetTypes::IOBadParam,UniSetTypes::ORepFailed 00056 00057 // ----------------------------------------------------------------------------------------- 00065 class UniversalInterface 00066 { 00067 public: 00068 00069 UniversalInterface( UniSetTypes::ObjectId backid, CORBA::ORB_var orb=NULL, UniSetTypes::ObjectIndex* oind=NULL ); 00070 UniversalInterface( UniSetTypes::Configuration* uconf=UniSetTypes::conf ); 00071 ~UniversalInterface(); 00072 00073 inline UniSetTypes::ObjectIndex* getObjectIndex() { return oind; } 00074 inline UniSetTypes::Configuration* getConf() { return uconf; } 00075 00076 // -------- Функции работы с группой датчиков ----------- 00077 00078 // Группа должна принадлежать одному процессу! 00079 00081 IOController_i::ASensorInfoSeq_var getSensorSeq( UniSetTypes::IDList& lst ); 00082 00083 // Изменения состояния списка входов/выходов 00084 // Возвращает список не найденных идентификаторов 00085 UniSetTypes::IDSeq_var setOutputSeq( const IOController_i::OutSeq& lst, UniSetTypes::ObjectId sup_id ); 00086 00088 UniSetTypes::IDSeq_var askSensorsSeq( UniSetTypes::IDList& lst, UniversalIO::UIOCommand cmd, 00089 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ); 00090 00091 00092 // ------------------------------------------------------ 00093 00094 00096 bool getState ( UniSetTypes::ObjectId id, UniSetTypes::ObjectId node ) throw(IO_THROW_EXCEPTIONS); 00097 bool getState ( UniSetTypes::ObjectId id ); 00098 00100 long getValue ( UniSetTypes::ObjectId id, UniSetTypes::ObjectId node )throw(IO_THROW_EXCEPTIONS); 00101 long getValue ( UniSetTypes::ObjectId id ); 00102 00104 void setState ( UniSetTypes::ObjectId id, bool state, UniSetTypes::ObjectId node ) throw(IO_THROW_EXCEPTIONS); 00105 void setState ( UniSetTypes::ObjectId id, bool state ); 00106 void setState ( IOController_i::SensorInfo& si, bool state, UniSetTypes::ObjectId supplier ); 00107 00109 void setValue ( UniSetTypes::ObjectId id, long value, UniSetTypes::ObjectId node ) throw(IO_THROW_EXCEPTIONS); 00110 void setValue ( UniSetTypes::ObjectId id, long value); 00111 void setValue ( IOController_i::SensorInfo& si, long value, UniSetTypes::ObjectId supplier ); 00112 00114 bool saveState ( UniSetTypes::ObjectId id, bool state, UniversalIO::IOTypes type, UniSetTypes::ObjectId node ) throw(IO_THROW_EXCEPTIONS); 00115 bool saveState ( UniSetTypes::ObjectId id, bool state, UniversalIO::IOTypes type ); 00116 bool saveState ( IOController_i::SensorInfo& si, bool state, UniversalIO::IOTypes type, UniSetTypes::ObjectId supplier ); 00117 00119 bool saveValue ( UniSetTypes::ObjectId id, long value, UniversalIO::IOTypes type, UniSetTypes::ObjectId node ) throw(IO_THROW_EXCEPTIONS); 00120 bool saveValue ( UniSetTypes::ObjectId id, long value, UniversalIO::IOTypes type ); 00121 bool saveValue ( IOController_i::SensorInfo& si, long value, UniversalIO::IOTypes type, UniSetTypes::ObjectId supplier ); 00122 00123 // функции не вырабатывающие исключений... 00124 void fastSaveValue( IOController_i::SensorInfo& si, long value, UniversalIO::IOTypes type, UniSetTypes::ObjectId supplier ); 00125 void fastSaveState( IOController_i::SensorInfo& si, bool state, UniversalIO::IOTypes type, UniSetTypes::ObjectId supplier ); 00126 void fastSetState( IOController_i::SensorInfo& si, bool state, UniSetTypes::ObjectId supplier ); 00127 void fastSetValue( IOController_i::SensorInfo& si, long value, UniSetTypes::ObjectId supplier ); 00128 00129 // установка неопределённого состояния 00130 void setUndefinedState( IOController_i::SensorInfo& si, bool undefined, UniSetTypes::ObjectId supplier ); 00131 00132 00133 CORBA::Long getRawValue( const IOController_i::SensorInfo& si ); 00134 00136 void calibrate(const IOController_i::SensorInfo& si, 00137 const IOController_i::CalibrateInfo& ci, 00138 UniSetTypes::ObjectId adminId = UniSetTypes::DefaultObjectId ); 00139 00140 IOController_i::CalibrateInfo getCalibrateInfo( const IOController_i::SensorInfo& si ); 00141 00142 00144 void askRemoteState( UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, UniSetTypes::ObjectId node, 00145 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId )throw(IO_THROW_EXCEPTIONS); 00146 00147 void askState( UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, 00148 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ); 00149 00151 void askRemoteValue ( UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, UniSetTypes::ObjectId node, 00152 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ) throw(IO_THROW_EXCEPTIONS); 00153 void askValue ( UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, 00154 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ); 00155 00156 void askThreshold ( UniSetTypes::ObjectId sensorId, UniSetTypes::ThresholdId tid, 00157 UniversalIO::UIOCommand cmd, 00158 CORBA::Long lowLimit=0, CORBA::Long hiLimit=0, CORBA::Long sensibility=0, 00159 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ); 00160 00161 void askRemoteThreshold( UniSetTypes::ObjectId sensorId, UniSetTypes::ObjectId node, 00162 UniSetTypes::ThresholdId thresholdId, UniversalIO::UIOCommand cmd, 00163 CORBA::Long lowLimit=0, CORBA::Long hiLimit=0, CORBA::Long sensibility=0, 00164 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ); 00165 00167 void askSensor( UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, 00168 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ); 00169 00170 void askRemoteSensor( UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, UniSetTypes::ObjectId node, 00171 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId )throw(IO_THROW_EXCEPTIONS); 00172 00173 00174 void askOutput( UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, 00175 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ); 00176 00177 void askRemoteOutput( UniSetTypes::ObjectId id, UniversalIO::UIOCommand cmd, UniSetTypes::ObjectId node, 00178 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId )throw(IO_THROW_EXCEPTIONS); 00179 00181 void askTimer( UniSetTypes::TimerId timerid, CORBA::Long timeMS, CORBA::Short ticks=-1, 00182 UniSetTypes::Message::Priority piority=UniSetTypes::Message::High, 00183 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId) throw(IO_THROW_EXCEPTIONS); 00184 00186 void askMessage( UniSetTypes::MessageCode mid, UniversalIO::UIOCommand cmd, bool ack = true, 00187 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ) throw(IO_THROW_EXCEPTIONS); 00188 void askMessageRange( UniSetTypes::MessageCode from, UniSetTypes::MessageCode to, 00189 UniversalIO::UIOCommand cmd, bool ack = true, 00190 UniSetTypes::ObjectId backid = UniSetTypes::DefaultObjectId ) throw(IO_THROW_EXCEPTIONS); 00191 00192 00193 UniversalIO::IOTypes getIOType(UniSetTypes::ObjectId id, UniSetTypes::ObjectId node) throw(IO_THROW_EXCEPTIONS); 00194 UniversalIO::IOTypes getIOType(UniSetTypes::ObjectId id); 00195 UniSetTypes::ObjectType getType(UniSetTypes::ObjectId id, UniSetTypes::ObjectId node) throw(IO_THROW_EXCEPTIONS); 00196 UniSetTypes::ObjectType getType(UniSetTypes::ObjectId id); 00197 00198 // read from xml (only for xml!) 00199 UniversalIO::IOTypes getConfIOType( UniSetTypes::ObjectId id ); 00200 00201 IOController_i::ShortIOInfo getChangedTime( UniSetTypes::ObjectId id, UniSetTypes::ObjectId node ); 00202 IOController_i::ShortMapSeq* getSensors( UniSetTypes::ObjectId id, 00203 UniSetTypes::ObjectId node=UniSetTypes::conf->getLocalNode() ); 00204 00205 // /*! регистрация объекта в репозитории */ 00206 void registered(UniSetTypes::ObjectId id, const UniSetTypes::ObjectPtr oRef, bool force=false)throw(UniSetTypes::ORepFailed); 00207 void registered(UniSetTypes::ObjectId id, UniSetTypes::ObjectId node, const UniSetTypes::ObjectPtr oRef, bool force=false)throw(UniSetTypes::ORepFailed); 00208 00209 // /*! разрегистрация объекта */ 00210 void unregister(UniSetTypes::ObjectId id)throw(UniSetTypes::ORepFailed); 00211 void unregister(UniSetTypes::ObjectId id, UniSetTypes::ObjectId node)throw(UniSetTypes::ORepFailed); 00212 00214 inline UniSetTypes::ObjectPtr resolve(const char* name) 00215 { 00216 return rep.resolve(name); 00217 } 00218 00219 inline UniSetTypes::ObjectPtr resolve( UniSetTypes::ObjectId id ) 00220 { 00221 std::string nm = oind->getNameById(id); 00222 return rep.resolve(nm); 00223 } 00224 00225 UniSetTypes::ObjectPtr resolve( UniSetTypes::ObjectId id, UniSetTypes::ObjectId nodeName, int timeoutMS=UniversalIO::defaultTimeOut) 00226 throw(UniSetTypes::ResolveNameError, UniSetTypes::TimeOut); 00227 00228 00229 bool isExist( UniSetTypes::ObjectId id ); 00230 bool isExist( UniSetTypes::ObjectId id, UniSetTypes::ObjectId node ); 00231 00233 inline UniSetTypes::ObjectId getIdByName(const char* name) 00234 { 00235 return oind->getIdByName(name); 00236 } 00237 inline UniSetTypes::ObjectId getIdByName(const string name) 00238 { 00239 return getIdByName(name.c_str()); 00240 } 00241 00243 inline std::string getNameById( UniSetTypes::ObjectId id ) 00244 { 00245 return oind->getNameById(id); 00246 } 00247 00248 inline std::string getNameById( UniSetTypes::ObjectId id, UniSetTypes::ObjectId node ) 00249 { 00250 return oind->getNameById(id, node); 00251 } 00252 00253 inline UniSetTypes::ObjectId getNodeId(const std::string& fullname) 00254 { 00255 return oind->getNodeId(fullname); 00256 } 00257 00258 inline std::string getName(const std::string& fullname) 00259 { 00260 return oind->getName(fullname); 00261 } 00262 00263 inline std::string getTextName( UniSetTypes::ObjectId id ) 00264 { 00265 return oind->getTextName(id); 00266 } 00267 00268 static std::string timeToString(time_t tm=time(0), const std::string brk=":"); 00269 static std::string dateToString(time_t tm=time(0), const std::string brk="/"); 00272 void send( UniSetTypes::ObjectId name, UniSetTypes::TransportMessage& msg, UniSetTypes::ObjectId node) throw(IO_THROW_EXCEPTIONS); 00273 void send( UniSetTypes::ObjectId name, UniSetTypes::TransportMessage& msg); 00274 00275 bool info( std::string msg, UniSetTypes::ObjectId messenger, 00276 UniSetTypes::ObjectId fromNode = UniSetTypes::conf->getLocalNode(), 00277 UniSetTypes::InfoMessage::Character ch=UniSetTypes::InfoMessage::Normal, 00278 UniSetTypes::ObjectId from=UniSetTypes::DefaultObjectId ); 00279 00280 bool alarm( std::string msg, UniSetTypes::ObjectId messenger, 00281 UniSetTypes::ObjectId fromNode = UniSetTypes::conf->getLocalNode(), 00282 UniSetTypes::AlarmMessage::Character ch=UniSetTypes::AlarmMessage::Alarm, 00283 UniSetTypes::ObjectId from=UniSetTypes::DefaultObjectId ); 00284 00285 bool info( UniSetTypes::InfoMessage& msg, UniSetTypes::ObjectId messenger); 00286 bool alarm( UniSetTypes::AlarmMessage& msg, UniSetTypes::ObjectId messenger); 00287 00288 bool waitReady( UniSetTypes::ObjectId id, int msec, int pause=5000, 00289 UniSetTypes::ObjectId node = UniSetTypes::conf->getLocalNode() ); // used exist 00290 00291 bool waitWorking( UniSetTypes::ObjectId id, int msec, int pause=3000, 00292 UniSetTypes::ObjectId node = UniSetTypes::conf->getLocalNode() ); // used getState 00293 00294 inline void setCacheMaxSize( unsigned int newsize) 00295 { 00296 rcache.setMaxSize(newsize); 00297 } 00298 00302 class CacheOfResolve 00303 { 00304 public: 00305 CacheOfResolve(unsigned int maxsize, int cleantime): 00306 MaxSize(maxsize), CleanTime(cleantime){}; 00307 ~CacheOfResolve(){}; 00308 00309 UniSetTypes::ObjectPtr resolve( UniSetTypes::ObjectId id, UniSetTypes::ObjectId node )throw(UniSetTypes::NameNotFound); 00310 void cache(UniSetTypes::ObjectId id, UniSetTypes::ObjectId node, UniSetTypes::ObjectVar ptr); 00311 void erase(UniSetTypes::ObjectId id, UniSetTypes::ObjectId node); 00312 00313 inline void setMaxSize( unsigned int ms) 00314 { 00315 MaxSize = ms; 00316 }; 00317 00318 // void setCleanTime(); 00319 00320 protected: 00321 CacheOfResolve(){}; 00322 private: 00323 00324 bool clean(); 00325 inline void clear() 00326 { 00327 UniSetTypes::uniset_mutex_lock l(cmutex,200); 00328 mcache.clear(); 00329 }; 00330 00334 struct Info 00335 { 00336 Info( UniSetTypes::ObjectVar ptr, time_t tm=0): 00337 ptr(ptr) 00338 { 00339 if(!tm) 00340 timestamp = time(NULL); 00341 } 00342 00343 Info(): 00344 ptr(NULL), timestamp(0){}; 00345 00346 UniSetTypes::ObjectVar ptr; 00347 time_t timestamp; // время последнего обращения 00348 00349 bool operator<( const CacheOfResolve::Info& rhs ) const 00350 { 00351 return this->timestamp < rhs.timestamp; 00352 } 00353 00354 }; 00355 00356 typedef std::map<int, Info> CacheMap; 00357 CacheMap mcache; 00358 UniSetTypes::uniset_mutex cmutex; 00359 unsigned int MaxSize; 00360 unsigned int CleanTime; 00362 /* 00363 // В последствии написать функцию для использования 00364 // remove_if 00365 typedef std::pair<int, Info> CacheItem; 00366 // функция-объект для поиска устаревших(по времени) ссылок 00367 struct OldRef_eq: public unary_function<CacheItem, bool> 00368 { 00369 OldRef_eq(time_t tm):tm(tm){} 00370 bool operator()( const CacheItem& inf ) const 00371 { 00372 return inf.timestamp < tm; 00373 } 00374 time_t tm; 00375 }; 00376 */ 00377 }; 00378 00379 void initBackId( UniSetTypes::ObjectId backid ); 00380 protected: 00381 std::string set_err(const std::string& pre, UniSetTypes::ObjectId id, UniSetTypes::ObjectId node); 00382 private: 00383 void init(); 00384 00385 ObjectRepository rep; 00386 UniSetTypes::ObjectId myid; 00387 CosNaming::NamingContext_var localctx; 00388 CORBA::ORB_var orb; 00389 CacheOfResolve rcache; 00390 UniSetTypes::ObjectIndex* oind; 00391 UniSetTypes::Configuration* uconf; 00392 }; 00393 // -------------------------------------------------------------------------- 00394 #endif 00395 // --------------------------------------------------------------------------