UniSet  1.4.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 "UniversalInterface.h"
00041 #include "UniSetObject_i.hh"
00042 #include "ThreadCreator.h"
00043 
00044 //---------------------------------------------------------------------------
00045 //#include <omnithread.h>
00046 //---------------------------------------------------------------------------
00047 class ObjectsActivator;
00048 class ObjectsManager;
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();
00075         virtual char* getName(){return (char*)myname.c_str();}
00076         virtual UniSetTypes::ObjectId getId(){ return myid; }
00077         virtual UniSetTypes::ObjectType getType() { return UniSetTypes::getObjectType("UniSetObject"); }
00078         virtual UniSetTypes::SimpleInfo* getInfo();
00079         friend std::ostream& operator<<(std::ostream& os, UniSetObject& obj );
00080 
00082         virtual void push(const UniSetTypes::TransportMessage& msg);
00083 
00085         inline UniSetTypes::ObjectPtr getRef()
00086         {
00087             UniSetTypes::uniset_mutex_lock lock(refmutex, 300);
00088             return (UniSetTypes::ObjectPtr)CORBA::Object::_duplicate(oref);
00089         }
00090 
00091     protected:
00093             virtual void processingMessage(UniSetTypes::VoidMessage *msg);
00094 
00096             bool receiveMessage(UniSetTypes::VoidMessage& vm);
00097     
00099             unsigned int countMessages();
00100                 
00102             void termWaiting();
00103 
00104             UniversalInterface ui; 
00105             std::string myname;
00106             std::string section;
00107 
00109             virtual bool disactivateObject(){return true;}
00111             virtual bool activateObject(){return true;}
00112 
00114             inline void thread(bool create){ threadcreate = create; }
00116             inline void offThread(){ threadcreate = false; }
00118             inline void onThread(){ threadcreate = true; }
00119 
00121             virtual void callback();
00122 
00128             virtual void sigterm( int signo ){};
00129 
00130             inline void terminate(){ disactivate(); }
00131 
00133             virtual bool waitMessage(UniSetTypes::VoidMessage& msg, timeout_t timeMS=UniSetTimer::WaitUpTime);
00134 
00135             void setID(UniSetTypes::ObjectId id);
00136 
00137 
00138             void setMaxSizeOfMessageQueue( unsigned int s )
00139             {
00140                 if( s>=0 )
00141                     SizeOfMessageQueue = s;
00142             }
00143 
00144             inline unsigned int getMaxSizeOfMessageQueue()
00145             { return SizeOfMessageQueue; }
00146             
00147             void setMaxCountRemoveOfMessage( unsigned int m )
00148             {
00149                 if( m >=0 )
00150                     MaxCountRemoveOfMessage = m;
00151             }
00152 
00153             inline unsigned int getMaxCountRemoveOfMessage()
00154             { return MaxCountRemoveOfMessage; }
00155 
00156 
00157             // функция определения приоритетного сообщения для обработки
00158             struct PriorVMsgCompare: 
00159                 public std::binary_function<UniSetTypes::VoidMessage, UniSetTypes::VoidMessage, bool>
00160             {
00161                 bool operator()(const UniSetTypes::VoidMessage& lhs, 
00162                                 const UniSetTypes::VoidMessage& rhs) const;
00163             };
00164             typedef std::priority_queue<UniSetTypes::VoidMessage,std::vector<UniSetTypes::VoidMessage>,PriorVMsgCompare> MessagesQueue;
00165 
00166 
00180             virtual void cleanMsgQueue( MessagesQueue& q );
00181 
00182 
00183             void setRecvMutexTimeout( unsigned long msec );
00184             inline unsigned long getRecvMutexTimeout(){ return recvMutexTimeout; }
00185             
00186             void setPushMutexTimeout( unsigned long msec );
00187             unsigned long getPushMutexTimeout(){ return pushMutexTimeout; }
00188 
00189             bool isActive();
00190             void setActive( bool set );
00191 
00192             UniSetTypes::VoidMessage msg;   
00193             ObjectsManager* mymngr; 
00194 
00195     private:
00196 
00197             friend class ObjectsManager;
00198             friend class ObjectsActivator;
00199             friend class ThreadCreator<UniSetObject>;
00200             inline pid_t getMsgPID()
00201             {
00202                 return msgpid;
00203             }
00204 
00206             void work();    
00208             bool init(ObjectsManager* om);
00210             bool disactivate();
00212             bool activate();
00213             /* регистрация в репозитории объектов */
00214             void registered();
00215             /* удаление ссылки из репозитория объектов   */
00216             void unregister();
00217 
00218             void init_object();
00219 
00220             pid_t msgpid; // pid потока обработки сообщений
00221             bool reg;
00222             bool active;
00223             UniSetTypes::uniset_mutex act_mutex;
00224             bool threadcreate;
00225             UniSetTimer* tmr;
00226             UniSetTypes::ObjectId myid;
00227             CORBA::Object_var oref;
00228             ThreadCreator<UniSetObject>* thr;
00229 
00231             MessagesQueue queueMsg;
00232 
00234             UniSetTypes::uniset_mutex qmutex;
00235 
00237             UniSetTypes::uniset_mutex refmutex;
00238 
00240             unsigned int SizeOfMessageQueue;
00242             unsigned int MaxCountRemoveOfMessage;
00243             unsigned long recvMutexTimeout; 
00244             unsigned long pushMutexTimeout; 
00246             // статистическая информация 
00247             unsigned long stMaxQueueMessages;   /*<! Максимальное число сообщений хранившихся в очереди */
00248             unsigned long stCountOfQueueFull;   
00249 };
00250 
00251 #endif