UniSet  2.7.0
UniSetObject.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
22 //---------------------------------------------------------------------------
23 #ifndef UniSetObject_H_
24 #define UniSetObject_H_
25 //--------------------------------------------------------------------------
26 #include <condition_variable>
27 #include <thread>
28 #include <mutex>
29 #include <atomic>
30 #include <ostream>
31 #include <memory>
32 #include <string>
33 #include <list>
34 
35 #include "UniSetTypes.h"
36 #include "MessageType.h"
37 #include "PassiveTimer.h"
38 #include "Exceptions.h"
39 #include "UInterface.h"
40 #include "UniSetObject_i.hh"
41 #include "ThreadCreator.h"
42 #include "LT_Object.h"
43 #include "MQMutex.h"
44 #include "UHttpRequestHandler.h"
45 
46 //---------------------------------------------------------------------------
47 namespace uniset
48 {
49  //---------------------------------------------------------------------------
50  class UniSetActivator;
51  class UniSetManager;
52 
53  //---------------------------------------------------------------------------
54  class UniSetObject;
55  typedef std::list< std::shared_ptr<UniSetObject> > ObjectsList;
56  //---------------------------------------------------------------------------
73  class UniSetObject:
74  public std::enable_shared_from_this<UniSetObject>,
75  public POA_UniSetObject_i,
76  public LT_Object
77 #ifndef DISABLE_REST_API
79 #endif
80  {
81  public:
82  UniSetObject( const std::string& name, const std::string& section );
84  UniSetObject();
85  virtual ~UniSetObject();
86 
87  // Функции объявленные в IDL
88  virtual CORBA::Boolean exist() override;
89 
90  virtual uniset::ObjectId getId() override;
91 
92  const uniset::ObjectId getId() const;
93  std::string getName() const;
94 
95  virtual uniset::ObjectType getType() override
96  {
97  return uniset::ObjectType("UniSetObject");
98  }
99 
100  const std::string getStrType();
101 
102  virtual uniset::SimpleInfo* getInfo( const char* userparam = "" ) override;
103 
104  // обёртка для вызова HTTP API через RPC
105  virtual uniset::SimpleInfo* apiRequest( const char* query ) override;
106 
108  virtual void push( const uniset::TransportMessage& msg ) override;
109 
110 #ifndef DISABLE_REST_API
111  // HTTP API
112  virtual Poco::JSON::Object::Ptr httpGet( const Poco::URI::QueryParameters& p ) override;
113  virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
114 #endif
115  // -------------- вспомогательные --------------
117  uniset::ObjectPtr getRef() const;
118  std::shared_ptr<UniSetObject> get_ptr();
119 
121  virtual timeout_t askTimer( uniset::TimerId timerid, timeout_t timeMS, clock_t ticks = -1,
122  uniset::Message::Priority p = uniset::Message::High ) override;
123 
124  friend std::ostream& operator<<(std::ostream& os, UniSetObject& obj );
125 
126  protected:
127 
128  std::shared_ptr<UInterface> ui;
129  std::string myname;
130  std::weak_ptr<UniSetManager> mymngr;
131 
133  virtual void processingMessage( const uniset::VoidMessage* msg );
134 
135  // конкретные виды сообщений
136  virtual void sysCommand( const uniset::SystemMessage* sm ) {}
137  virtual void sensorInfo( const uniset::SensorMessage* sm ) {}
138  virtual void timerInfo( const uniset::TimerMessage* tm ) {}
139 
141  VoidMessagePtr receiveMessage();
142 
144  VoidMessagePtr waitMessage( timeout_t msec = UniSetTimer::WaitUpTime );
145 
147  void termWaiting();
148 
150  size_t countMessages();
151 
153  size_t getCountOfLostMessages() const;
154 
156  virtual bool activateObject();
157 
159  virtual bool deactivateObject();
160 
161  // прерывание работы всей программы (с вызовом shutdown)
162  void uterminate();
163 
164  // управление созданием потока обработки сообщений -------
165 
167  void thread( bool create );
168 
170  void offThread();
171 
173  void onThread();
174 
176  virtual void callback();
177 
178  // ----- конфигурирование объекта -------
180  void setID(uniset::ObjectId id);
181 
183  void setThreadPriority( Poco::Thread::Priority p );
184 
186  void setMaxSizeOfMessageQueue( size_t s );
187 
189  size_t getMaxSizeOfMessageQueue() const;
190 
192  bool isActive() const;
193 
195  void setActive( bool set );
196 
197 #ifndef DISABLE_REST_API
198  // вспомогательные функции
199  virtual Poco::JSON::Object::Ptr httpGetMyInfo( Poco::JSON::Object::Ptr root );
200  Poco::JSON::Object::Ptr request_conf( const std::string& req, const Poco::URI::QueryParameters& p );
201  Poco::JSON::Object::Ptr request_conf_name( const std::string& name, const std::string& props );
202 #endif
203 
204  private:
205 
206  friend class UniSetManager;
207  friend class UniSetActivator;
208 
210  void work();
212  bool init( const std::weak_ptr<UniSetManager>& om );
214  bool deactivate();
216  bool activate();
217  /* регистрация в репозитории объектов */
218  void registration();
219  /* удаление ссылки из репозитория объектов */
220  void unregistration();
221 
222  void waitFinish();
223 
224  void initObject();
225 
226  pid_t msgpid = { 0 }; // pid потока обработки сообщений
227  bool regOK = { false };
228  std::atomic_bool active;
229 
230  bool threadcreate;
231  std::unique_ptr<UniSetTimer> tmr;
232  uniset::ObjectId myid;
233  CORBA::Object_var oref;
234 
236  mutable uniset::uniset_rwmutex refmutex;
237 
238  std::unique_ptr< ThreadCreator<UniSetObject> > thr;
239 
241  MQMutex mqueueLow;
242  MQMutex mqueueMedium;
243  MQMutex mqueueHi;
244 
245  bool a_working;
246  std::mutex m_working;
247  std::condition_variable cv_working;
248  };
249  // -------------------------------------------------------------------------
250 } // end of uniset namespace
251 //---------------------------------------------------------------------------
252 #endif
253 //---------------------------------------------------------------------------
Definition: MQMutex.h:43
Definition: CallbackTimer.h:29
virtual void processingMessage(const uniset::VoidMessage *msg)
Definition: UniSetObject.cc:822
size_t getCountOfLostMessages() const
Definition: UniSetObject.cc:542
bool isActive() const
Definition: UniSetObject.cc:177
Definition: MessageType.h:168
virtual bool activateObject()
Активизация объекта (переопределяется для необходимых действий после активизации) ...
Definition: UniSetObject.cc:549
void onThread()
Definition: UniSetObject.cc:576
Definition: UHttpRequestHandler.h:81
Definition: UniSetObject.h:73
void offThread()
Definition: UniSetObject.cc:571
std::shared_ptr< UInterface > ui
Definition: UniSetObject.h:128
void setID(uniset::ObjectId id)
Definition: UniSetObject.cc:154
Definition: UniSetActivator.h:61
CORBA::Object_ptr ObjectPtr
Definition: UniSetTypes.h:96
size_t getMaxSizeOfMessageQueue() const
Definition: UniSetObject.cc:172
virtual timeout_t askTimer(uniset::TimerId timerid, timeout_t timeMS, clock_t ticks=-1, uniset::Message::Priority p=uniset::Message::High) override
Definition: UniSetObject.cc:880
void setMaxSizeOfMessageQueue(size_t s)
Definition: UniSetObject.cc:165
std::list< std::shared_ptr< UniSetObject > > ObjectsList
Definition: UniSetObject.h:54
virtual void callback()
Definition: UniSetObject.cc:798
long TimerId
Definition: UniSetTypes_i.idl:32
uniset::ObjectPtr getRef() const
Definition: UniSetObject.cc:531
void thread(bool create)
Definition: UniSetObject.cc:566
void setThreadPriority(Poco::Thread::Priority p)
Definition: UniSetObject.cc:378
Definition: MessageType.h:124
Definition: UniSetTypes_i.idl:44
void termWaiting()
Definition: UniSetObject.cc:372
Definition: MessageType.h:88
Definition: UniSetManager.h:57
virtual void push(const uniset::TransportMessage &msg) override
поместить сообщение в очередь
Definition: UniSetObject.cc:384
Definition: Mutex.h:31
size_t countMessages()
Definition: UniSetObject.cc:537
Definition: MessageType.h:211
VoidMessagePtr waitMessage(timeout_t msec=UniSetTimer::WaitUpTime)
Definition: UniSetObject.cc:202
string< SizeOfObjectType > ObjectType
Definition: UniSetTypes_i.idl:33
Definition: UniSetTypes_i.idl:64
Definition: LT_Object.h:97
virtual bool deactivateObject()
Деактивиция объекта (переопределяется для необходимых действий при завершении работы) ...
Definition: UniSetObject.cc:554
VoidMessagePtr receiveMessage()
Definition: UniSetObject.cc:191
void setActive(bool set)
Definition: UniSetObject.cc:182
long ObjectId
Definition: UniSetTypes_i.idl:30
static const timeout_t WaitUpTime
Definition: PassiveTimer.h:69