UniSet  2.2.1
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 <condition_variable>
00030 #include <thread>
00031 #include <mutex>
00032 #include <atomic>
00033 #include <unistd.h>
00034 #include <sys/time.h>
00035 #include <queue>
00036 #include <ostream>
00037 #include <memory>
00038 #include <string>
00039 #include <list>
00040 
00041 #include "UniSetTypes.h"
00042 #include "MessageType.h"
00043 #include "PassiveTimer.h"
00044 #include "Exceptions.h"
00045 #include "UInterface.h"
00046 #include "UniSetObject_i.hh"
00047 #include "ThreadCreator.h"
00048 #include "LT_Object.h"
00049 
00050 //---------------------------------------------------------------------------
00051 //#include <omnithread.h>
00052 //---------------------------------------------------------------------------
00053 class UniSetActivator;
00054 class UniSetManager;
00055 
00056 //---------------------------------------------------------------------------
00057 class UniSetObject;
00058 typedef std::list< std::shared_ptr<UniSetObject> > ObjectsList;     
00059 //---------------------------------------------------------------------------
00070 class UniSetObject:
00071     public std::enable_shared_from_this<UniSetObject>,
00072     public POA_UniSetObject_i,
00073     public LT_Object
00074 {
00075     public:
00076         UniSetObject(const std::string& name, const std::string& section);
00077         UniSetObject(UniSetTypes::ObjectId id);
00078         UniSetObject();
00079         virtual ~UniSetObject();
00080 
00081         std::shared_ptr<UniSetObject> get_ptr()
00082         {
00083             return shared_from_this();
00084         }
00085 
00086         // Функции объявленные в IDL
00087         virtual CORBA::Boolean exist() override;
00088 
00089         virtual UniSetTypes::ObjectId getId() override
00090         {
00091             return myid;
00092         }
00093         inline const UniSetTypes::ObjectId getId() const
00094         {
00095             return myid;
00096         }
00097         inline std::string getName()
00098         {
00099             return myname;
00100         }
00101 
00102         virtual UniSetTypes::ObjectType getType() override
00103         {
00104             return UniSetTypes::ObjectType("UniSetObject");
00105         }
00106         virtual UniSetTypes::SimpleInfo* getInfo( ::CORBA::Long userparam = 0 ) override;
00107         friend std::ostream& operator<<(std::ostream& os, UniSetObject& obj );
00108 
00110         virtual void push( const UniSetTypes::TransportMessage& msg ) override;
00111 
00113         inline UniSetTypes::ObjectPtr getRef() const
00114         {
00115             UniSetTypes::uniset_rwmutex_rlock lock(refmutex);
00116             return (UniSetTypes::ObjectPtr)CORBA::Object::_duplicate(oref);
00117         }
00118 
00119         virtual timeout_t askTimer( UniSetTypes::TimerId timerid, timeout_t timeMS, clock_t ticks = -1,
00120                                     UniSetTypes::Message::Priority p = UniSetTypes::Message::High ) override;
00121 
00122     protected:
00124         virtual void processingMessage( UniSetTypes::VoidMessage* msg );
00125         virtual void sysCommand( const UniSetTypes::SystemMessage* sm ) {}
00126         virtual void sensorInfo( const UniSetTypes::SensorMessage* sm ) {}
00127         virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) {}
00128 
00130         bool receiveMessage( UniSetTypes::VoidMessage& vm );
00131 
00133         unsigned int countMessages();
00134 
00136         void termWaiting();
00137 
00138         std::shared_ptr<UInterface> ui; 
00139         std::string myname;
00140         std::string section;
00141 
00143         virtual bool deactivateObject()
00144         {
00145             return true;
00146         }
00148         virtual bool activateObject()
00149         {
00150             return true;
00151         }
00152 
00154         inline void thread(bool create)
00155         {
00156             threadcreate = create;
00157         }
00159         inline void offThread()
00160         {
00161             threadcreate = false;
00162         }
00164         inline void onThread()
00165         {
00166             threadcreate = true;
00167         }
00168 
00170         virtual void callback();
00171 
00177         virtual void sigterm( int signo );
00178 
00179         inline void terminate()
00180         {
00181             deactivate();
00182         }
00183 
00185         virtual bool waitMessage(UniSetTypes::VoidMessage& msg, timeout_t timeMS = UniSetTimer::WaitUpTime);
00186 
00187         void setID(UniSetTypes::ObjectId id);
00188 
00189         void setMaxSizeOfMessageQueue( size_t s )
00190         {
00191             SizeOfMessageQueue = s;
00192         }
00193 
00194         inline unsigned int getMaxSizeOfMessageQueue()
00195         {
00196             return SizeOfMessageQueue;
00197         }
00198 
00199         void setMaxCountRemoveOfMessage( size_t m )
00200         {
00201             MaxCountRemoveOfMessage = m;
00202         }
00203 
00204         inline unsigned int getMaxCountRemoveOfMessage()
00205         {
00206             return MaxCountRemoveOfMessage;
00207         }
00208 
00209 
00210         // функция определения приоритетного сообщения для обработки
00211         struct PriorVMsgCompare:
00212             public std::binary_function<UniSetTypes::VoidMessage, UniSetTypes::VoidMessage, bool>
00213         {
00214             bool operator()(const UniSetTypes::VoidMessage& lhs,
00215                             const UniSetTypes::VoidMessage& rhs) const;
00216         };
00217         typedef std::priority_queue<UniSetTypes::VoidMessage, std::vector<UniSetTypes::VoidMessage>, PriorVMsgCompare> MessagesQueue;
00218 
00219 
00233         virtual void cleanMsgQueue( MessagesQueue& q );
00234 
00235         inline bool isActive()
00236         {
00237             return active;
00238         }
00239         inline void setActive( bool set )
00240         {
00241             active = set;
00242         }
00243 
00244         UniSetTypes::VoidMessage msg;
00245         std::weak_ptr<UniSetManager> mymngr;
00246 
00247         void setThreadPriority( int p );
00248 
00249     private:
00250 
00251         friend class UniSetManager;
00252         friend class UniSetActivator;
00253 
00254         inline pid_t getMsgPID()
00255         {
00256             return msgpid;
00257         }
00258 
00260         void work();
00262         bool init( const std::weak_ptr<UniSetManager>& om );
00264         bool deactivate();
00266         bool activate();
00267         /* регистрация в репозитории объектов */
00268         void registered();
00269         /* удаление ссылки из репозитория объектов     */
00270         void unregister();
00271 
00272         void init_object();
00273 
00274         pid_t msgpid; // pid потока обработки сообщений
00275         bool regOK = { false };
00276         std::atomic_bool active;
00277 
00278         bool threadcreate;
00279         std::shared_ptr<UniSetTimer> tmr;
00280         UniSetTypes::ObjectId myid;
00281         CORBA::Object_var oref;
00282 
00283         std::shared_ptr< ThreadCreator<UniSetObject> > thr;
00284 
00286         MessagesQueue queueMsg;
00287 
00289         UniSetTypes::uniset_rwmutex qmutex;
00290 
00292         mutable UniSetTypes::uniset_rwmutex refmutex;
00293 
00295         size_t SizeOfMessageQueue;
00297         size_t MaxCountRemoveOfMessage;
00298 
00299         // статистическая информация
00300         size_t stMaxQueueMessages;    /*<! Максимальное число сообщений хранившихся в очереди */
00301         size_t stCountOfQueueFull;    
00303         std::atomic_bool a_working;
00304         std::mutex    m_working;
00305         std::condition_variable cv_working;
00306         //            timeout_t workingTerminateTimeout; /*!< время ожидания завершения потока */
00307 };
00308 //---------------------------------------------------------------------------
00309 #endif
00310 //---------------------------------------------------------------------------