UniSet  2.0.0
UniSetObject.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 // --------------------------------------------------------------------------
00025 //---------------------------------------------------------------------------
00026 #ifndef UniSetObject_H_
00027 #define UniSetObject_H_
00028 //--------------------------------------------------------------------------
00029 #include <unistd.h>
00030 #include <sys/time.h>
00031 #include <queue>
00032 #include <ostream>
00033 #include <string>
00034 #include <list>
00035 
00036 #include "UniSetTypes.h"
00037 #include "MessageType.h"
00038 #include "PassiveTimer.h"
00039 #include "Exceptions.h"
00040 #include "UInterface.h"
00041 #include "UniSetObject_i.hh"
00042 #include "ThreadCreator.h"
00043 
00044 //---------------------------------------------------------------------------
00045 //#include <omnithread.h>
00046 //---------------------------------------------------------------------------
00047 class UniSetActivator;
00048 class UniSetManager;
00049 
00050 //---------------------------------------------------------------------------
00051 class UniSetObject;
00052 typedef std::list<UniSetObject *> ObjectsList;     
00053 //---------------------------------------------------------------------------
00064 class UniSetObject:
00065     public POA_UniSetObject_i
00066 {
00067     public:
00068         UniSetObject(const std::string& name, const std::string& section); 
00069         UniSetObject(UniSetTypes::ObjectId id);
00070         UniSetObject();
00071         virtual ~UniSetObject();
00072 
00073         // Функции объявленные в IDL
00074         virtual CORBA::Boolean exist() override;
00075 
00076         virtual UniSetTypes::ObjectId getId() override { return myid; }
00077         inline const UniSetTypes::ObjectId getId() const { return myid; }
00078         inline std::string getName(){ return myname; }
00079 
00080         virtual UniSetTypes::ObjectType getType() override { return UniSetTypes::ObjectType("UniSetObject"); }
00081         virtual UniSetTypes::SimpleInfo* getInfo() override;
00082         friend std::ostream& operator<<(std::ostream& os, UniSetObject& obj );
00083 
00085         virtual void push( const UniSetTypes::TransportMessage& msg ) override;
00086 
00088         inline UniSetTypes::ObjectPtr getRef() const
00089         {
00090             UniSetTypes::uniset_rwmutex_rlock lock(refmutex);
00091             return (UniSetTypes::ObjectPtr)CORBA::Object::_duplicate(oref);
00092         }
00093 
00094     protected:
00096             virtual void processingMessage( UniSetTypes::VoidMessage *msg );
00097             virtual void sysCommand( const UniSetTypes::SystemMessage* sm ){}
00098             virtual void sensorInfo( const UniSetTypes::SensorMessage* sm ){}
00099             virtual void timerInfo( const UniSetTypes::TimerMessage* tm ){}
00100 
00102             bool receiveMessage( UniSetTypes::VoidMessage& vm );
00103 
00105             unsigned int countMessages();
00106 
00108             void termWaiting();
00109 
00110             UInterface ui; 
00111             std::string myname;
00112             std::string section;
00113 
00115             virtual bool deactivateObject(){return true;}
00117             virtual bool activateObject(){return true;}
00118 
00120             inline void thread(bool create){ threadcreate = create; }
00122             inline void offThread(){ threadcreate = false; }
00124             inline void onThread(){ threadcreate = true; }
00125 
00127             virtual void callback();
00128 
00134             virtual void sigterm( int signo ){};
00135 
00136             inline void terminate(){ deactivate(); }
00137 
00139             virtual bool waitMessage(UniSetTypes::VoidMessage& msg, timeout_t timeMS=UniSetTimer::WaitUpTime);
00140 
00141             void setID(UniSetTypes::ObjectId id);
00142 
00143 
00144             void setMaxSizeOfMessageQueue( unsigned int s )
00145             {
00146                 if( s>=0 )
00147                     SizeOfMessageQueue = s;
00148             }
00149 
00150             inline unsigned int getMaxSizeOfMessageQueue()
00151             { return SizeOfMessageQueue; }
00152 
00153             void setMaxCountRemoveOfMessage( unsigned int m )
00154             {
00155                 if( m >=0 )
00156                     MaxCountRemoveOfMessage = m;
00157             }
00158 
00159             inline unsigned int getMaxCountRemoveOfMessage()
00160             { return MaxCountRemoveOfMessage; }
00161 
00162 
00163             // функция определения приоритетного сообщения для обработки
00164             struct PriorVMsgCompare: 
00165                 public std::binary_function<UniSetTypes::VoidMessage, UniSetTypes::VoidMessage, bool>
00166             {
00167                 bool operator()(const UniSetTypes::VoidMessage& lhs, 
00168                                 const UniSetTypes::VoidMessage& rhs) const;
00169             };
00170             typedef std::priority_queue<UniSetTypes::VoidMessage,std::vector<UniSetTypes::VoidMessage>,PriorVMsgCompare> MessagesQueue;
00171 
00172 
00186             virtual void cleanMsgQueue( MessagesQueue& q );
00187 
00188             inline bool isActive(){ return active; }
00189             inline void setActive( bool set ){ active = set; }
00190 
00191             UniSetTypes::VoidMessage msg;
00192             UniSetManager* mymngr; 
00193 
00194             void setThreadPriority( int p );
00195 
00196     private:
00197 
00198             friend class UniSetManager;
00199             friend class UniSetActivator;
00200             friend class ThreadCreator<UniSetObject>;
00201             inline pid_t getMsgPID()
00202             {
00203                 return msgpid;
00204             }
00205 
00207             void work();    
00209             bool init(UniSetManager* om);
00211             bool deactivate();
00213             bool activate();
00214             /* регистрация в репозитории объектов */
00215             void registered();
00216             /* удаление ссылки из репозитория объектов     */
00217             void unregister();
00218 
00219             void init_object();
00220 
00221             pid_t msgpid; // pid потока обработки сообщений
00222             bool reg;
00223             std::atomic_bool active;
00224 
00225             bool threadcreate;
00226             UniSetTimer* tmr;
00227             UniSetTypes::ObjectId myid;
00228             CORBA::Object_var oref;
00229             ThreadCreator<UniSetObject>* thr;
00230 
00232             MessagesQueue queueMsg;
00233 
00235             UniSetTypes::uniset_rwmutex qmutex;
00236 
00238             mutable UniSetTypes::uniset_rwmutex refmutex;
00239 
00241             unsigned int SizeOfMessageQueue;
00243             unsigned int MaxCountRemoveOfMessage;
00244 
00245             // статистическая информация 
00246             unsigned long stMaxQueueMessages;    /*<! Максимальное число сообщений хранившихся в очереди */
00247             unsigned long stCountOfQueueFull;     
00248 };
00249 //---------------------------------------------------------------------------
00250 #endif
00251 //---------------------------------------------------------------------------