UniSet  2.6.0
UniSetObject.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // --------------------------------------------------------------------------
00022 //---------------------------------------------------------------------------
00023 #ifndef UniSetObject_H_
00024 #define UniSetObject_H_
00025 //--------------------------------------------------------------------------
00026 #include <condition_variable>
00027 #include <thread>
00028 #include <mutex>
00029 #include <atomic>
00030 #include <ostream>
00031 #include <memory>
00032 #include <string>
00033 #include <list>
00034 
00035 #include "UniSetTypes.h"
00036 #include "MessageType.h"
00037 #include "PassiveTimer.h"
00038 #include "Exceptions.h"
00039 #include "UInterface.h"
00040 #include "UniSetObject_i.hh"
00041 #include "ThreadCreator.h"
00042 #include "LT_Object.h"
00043 #include "MQMutex.h"
00044 #include "UHttpRequestHandler.h"
00045 
00046 //---------------------------------------------------------------------------
00047 namespace uniset
00048 {
00049 //---------------------------------------------------------------------------
00050 class UniSetActivator;
00051 class UniSetManager;
00052 
00053 //---------------------------------------------------------------------------
00054 class UniSetObject;
00055 typedef std::list< std::shared_ptr<UniSetObject> > ObjectsList;     
00056 //---------------------------------------------------------------------------
00073 class UniSetObject:
00074     public std::enable_shared_from_this<UniSetObject>,
00075     public POA_UniSetObject_i,
00076     public LT_Object
00077 #ifndef DISABLE_REST_API
00078     , public uniset::UHttp::IHttpRequest
00079 #endif
00080 {
00081     public:
00082         UniSetObject( const std::string& name, const std::string& section );
00083         UniSetObject( uniset::ObjectId id );
00084         UniSetObject();
00085         virtual ~UniSetObject();
00086 
00087         std::shared_ptr<UniSetObject> get_ptr();
00088 
00089         // Функции объявленные в IDL
00090         virtual CORBA::Boolean exist() override;
00091 
00092         virtual uniset::ObjectId getId() override;
00093 
00094         const uniset::ObjectId getId() const;
00095         std::string getName() const;
00096 
00097         virtual uniset::ObjectType getType() override
00098         {
00099             return uniset::ObjectType("UniSetObject");
00100         }
00101 
00102         const std::string getStrType();
00103 
00104         virtual uniset::SimpleInfo* getInfo( const char* userparam = "" ) override;
00105 
00106         // обёртка для вызова HTTP API через RPC
00107         virtual uniset::SimpleInfo* apiRequest( const char* query ) override;
00108 
00110         virtual void push( const uniset::TransportMessage& msg ) override;
00111 
00112 #ifndef DISABLE_REST_API
00113         // HTTP API
00114         virtual Poco::JSON::Object::Ptr httpGet( const Poco::URI::QueryParameters& p ) override;
00115         virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
00116 #endif
00117         // -------------- вспомогательные --------------
00119         uniset::ObjectPtr getRef() const;
00120 
00122         virtual timeout_t askTimer( uniset::TimerId timerid, timeout_t timeMS, clock_t ticks = -1,
00123                                     uniset::Message::Priority p = uniset::Message::High ) override;
00124 
00125         friend std::ostream& operator<<(std::ostream& os, UniSetObject& obj );
00126 
00127     protected:
00128 
00129         std::shared_ptr<UInterface> ui; 
00130         std::string myname;
00131         std::string section;
00132         std::weak_ptr<UniSetManager> mymngr;
00133 
00135         virtual void processingMessage( const uniset::VoidMessage* msg );
00136 
00137         // конкретные виды сообщений
00138         virtual void sysCommand( const uniset::SystemMessage* sm ) {}
00139         virtual void sensorInfo( const uniset::SensorMessage* sm ) {}
00140         virtual void timerInfo( const uniset::TimerMessage* tm ) {}
00141 
00143         VoidMessagePtr receiveMessage();
00144 
00146         virtual VoidMessagePtr waitMessage( timeout_t msec = UniSetTimer::WaitUpTime );
00147 
00149         void termWaiting();
00150 
00152         size_t countMessages();
00153 
00155         size_t getCountOfLostMessages() const;
00156 
00158         virtual bool activateObject();
00159 
00161         virtual bool deactivateObject();
00162 
00168         virtual void sigterm( int signo );
00169 
00170         void terminate();
00171 
00172         // управление созданием потока обработки сообщений -------
00173 
00175         void thread( bool create );
00176 
00178         void offThread();
00179 
00181         void onThread();
00182 
00184         virtual void callback();
00185 
00186         // ----- конфигурирование объекта -------
00188         void setID(uniset::ObjectId id);
00189 
00191         void setThreadPriority( Poco::Thread::Priority p );
00192 
00194         void setMaxSizeOfMessageQueue( size_t s );
00195 
00197         size_t getMaxSizeOfMessageQueue() const;
00198 
00200         bool isActive() const;
00201 
00203         void setActive( bool set );
00204 
00205 #ifndef DISABLE_REST_API
00206         // вспомогательные функции
00207         virtual Poco::JSON::Object::Ptr httpGetMyInfo( Poco::JSON::Object::Ptr root );
00208 #endif
00209 
00210     private:
00211 
00212         friend class UniSetManager;
00213         friend class UniSetActivator;
00214 
00216         void work();
00218         bool init( const std::weak_ptr<UniSetManager>& om );
00220         bool deactivate();
00222         bool activate();
00223         /* регистрация в репозитории объектов */
00224         void registered();
00225         /* удаление ссылки из репозитория объектов     */
00226         void unregister();
00227 
00228         void initObject();
00229 
00230         pid_t msgpid = { 0 }; // pid потока обработки сообщений
00231         bool regOK = { false };
00232         std::atomic_bool active;
00233 
00234         bool threadcreate;
00235         std::shared_ptr<UniSetTimer> tmr;
00236         uniset::ObjectId myid;
00237         CORBA::Object_var oref;
00238 
00240         mutable uniset::uniset_rwmutex refmutex;
00241 
00242         std::shared_ptr< ThreadCreator<UniSetObject> > thr;
00243 
00245         MQMutex mqueueLow;
00246         MQMutex mqueueMedium;
00247         MQMutex mqueueHi;
00248 
00249         std::atomic_bool a_working;
00250         std::mutex    m_working;
00251         std::condition_variable cv_working;
00252 };
00253 // -------------------------------------------------------------------------
00254 } // end of uniset namespace
00255 //---------------------------------------------------------------------------
00256 #endif
00257 //---------------------------------------------------------------------------