UniSet  2.8.0
UNetExchange.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 // -----------------------------------------------------------------------------
17 #ifndef UNetExchange_H_
18 #define UNetExchange_H_
19 // -----------------------------------------------------------------------------
20 #include <ostream>
21 #include <string>
22 #include <queue>
23 #include <deque>
24 #include "UniSetObject.h"
25 #include "Trigger.h"
26 #include "Mutex.h"
27 #include "SMInterface.h"
28 #include "SharedMemory.h"
29 #include "ThreadCreator.h"
30 #include "UNetReceiver.h"
31 #include "UNetSender.h"
32 #include "LogServer.h"
33 #include "DebugStream.h"
34 #include "UNetLogSugar.h"
35 #include "LogAgregator.h"
36 #include "VMonitor.h"
37 // -----------------------------------------------------------------------------
38 #ifndef vmonit
39 #define vmonit( var ) vmon.add( #var, var )
40 #endif
41 // --------------------------------------------------------------------------
42 namespace uniset
43 {
44  // -----------------------------------------------------------------------------
146  // -----------------------------------------------------------------------------
148  public UniSetObject
149  {
150  public:
151  UNetExchange( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, const std::string& prefix = "unet" );
152  virtual ~UNetExchange();
153 
155  static std::shared_ptr<UNetExchange> init_unetexchange( int argc, const char* const argv[],
156  uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = 0, const std::string& prefix = "unet" );
157 
159  static void help_print( int argc, const char* argv[] ) noexcept;
160 
161  bool checkExistUNetHost( const std::string& host, int port ) noexcept;
162 
163  inline std::shared_ptr<LogAgregator> getLogAggregator() noexcept
164  {
165  return loga;
166  }
167  inline std::shared_ptr<DebugStream> log() noexcept
168  {
169  return unetlog;
170  }
171 
172  virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
173 
174  protected:
175 
176  xmlNode* cnode;
177  std::string s_field;
178  std::string s_fvalue;
179 
180  std::shared_ptr<SMInterface> shm;
181  void step() noexcept;
182 
183  void sysCommand( const uniset::SystemMessage* msg ) override;
184  void sensorInfo( const uniset::SensorMessage* sm ) override;
185  void timerInfo( const uniset::TimerMessage* tm ) override;
186  void askSensors( UniversalIO::UIOCommand cmd );
187  bool waitSMReady();
188  void receiverEvent( const std::shared_ptr<UNetReceiver>& r, UNetReceiver::Event ev ) noexcept;
189 
190  virtual bool activateObject() override;
191  virtual bool deactivateObject() override;
192 
193  // действия при завершении работы
194  void termSenders();
195  void termReceivers();
196 
197  void initIterators() noexcept;
198  void startReceivers();
199 
200  enum Timer
201  {
202  tmStep
203  };
204 
205  private:
206  UNetExchange();
207  timeout_t initPause = { 0 };
208  uniset::uniset_rwmutex mutex_start;
209 
210  PassiveTimer ptHeartBeat;
211  uniset::ObjectId sidHeartBeat = { uniset::DefaultObjectId };
212  timeout_t maxHeartBeat = { 10 };
213  IOController::IOStateList::iterator itHeartBeat;
215 
216  timeout_t steptime = { 1000 };
218  std::atomic_bool activated = { false };
219  std::atomic_bool cancelled = { false };
220  timeout_t activateTimeout = { 20000 }; // msec
221 
222  struct ReceiverInfo
223  {
224  ReceiverInfo() noexcept: r1(nullptr), r2(nullptr),
225  sidRespond(uniset::DefaultObjectId),
226  respondInvert(false),
227  sidLostPackets(uniset::DefaultObjectId),
228  sidChannelNum(uniset::DefaultObjectId)
229  {}
230 
231  ReceiverInfo( const std::shared_ptr<UNetReceiver>& _r1, const std::shared_ptr<UNetReceiver>& _r2 ) noexcept:
232  r1(_r1), r2(_r2),
233  sidRespond(uniset::DefaultObjectId),
234  respondInvert(false),
235  sidLostPackets(uniset::DefaultObjectId),
236  sidChannelNum(uniset::DefaultObjectId)
237  {}
238 
239  std::shared_ptr<UNetReceiver> r1;
240  std::shared_ptr<UNetReceiver> r2;
242  void step(const std::shared_ptr<SMInterface>& shm, const std::string& myname, std::shared_ptr<DebugStream>& log ) noexcept;
243 
244  inline void setRespondID( uniset::ObjectId id, bool invert = false ) noexcept
245  {
246  sidRespond = id;
247  respondInvert = invert;
248  }
249  inline void setLostPacketsID( uniset::ObjectId id ) noexcept
250  {
251  sidLostPackets = id;
252  }
253  inline void setChannelNumID( uniset::ObjectId id ) noexcept
254  {
255  sidChannelNum = id;
256  }
257 
258  inline void setChannelSwitchCountID( uniset::ObjectId id ) noexcept
259  {
260  sidChannelSwitchCount = id;
261  }
262 
263  inline void initIterators( const std::shared_ptr<SMInterface>& shm ) noexcept
264  {
265  shm->initIterator(itLostPackets);
266  shm->initIterator(itRespond);
267  shm->initIterator(itChannelNum);
268  shm->initIterator(itChannelSwitchCount);
269  }
270 
271  // Сводная информация по двум каналам
272  // сумма потерянных пакетов и наличие связи
273  // хотя бы по одному каналу, номер рабочего канала
274  // количество переключений с канала на канал
275  // ( реализацию см. ReceiverInfo::step() )
276  uniset::ObjectId sidRespond;
277  IOController::IOStateList::iterator itRespond;
278  bool respondInvert = { false };
279  uniset::ObjectId sidLostPackets;
280  IOController::IOStateList::iterator itLostPackets;
281  uniset::ObjectId sidChannelNum;
282  IOController::IOStateList::iterator itChannelNum;
283 
284  long channelSwitchCount = { 0 };
285  uniset::ObjectId sidChannelSwitchCount = { uniset::DefaultObjectId };
286  IOController::IOStateList::iterator itChannelSwitchCount;
287  };
288 
289  typedef std::deque<ReceiverInfo> ReceiverList;
290  ReceiverList recvlist;
291 
292  bool no_sender = { false };
293  std::shared_ptr<UNetSender> sender;
294  std::shared_ptr<UNetSender> sender2;
295 
296  std::shared_ptr<LogAgregator> loga;
297  std::shared_ptr<DebugStream> unetlog;
298  std::shared_ptr<LogServer> logserv;
299  std::string logserv_host = {""};
300  int logserv_port = {0};
301 
302  VMonitor vmon;
303  };
304  // --------------------------------------------------------------------------
305 } // end of namespace uniset
306 // -----------------------------------------------------------------------------
307 #endif // UNetExchange_H_
308 // -----------------------------------------------------------------------------
Пассивный таймер
Definition: PassiveTimer.h:92
static std::shared_ptr< UNetExchange > init_unetexchange(int argc, const char *const argv[], uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=0, const std::string &prefix="unet")
Definition: unetexchange.cc:891
Definition: CommonEventLoop.h:14
Definition: UNetExchange.h:147
Definition: MessageType.h:170
Definition: UniSetObject.h:73
static void help_print(int argc, const char *argv[]) noexcept
Definition: unetexchange.cc:849
Definition: VMonitor.h:116
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:69
Event
Definition: UNetReceiver.h:154
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации)
Definition: unetexchange.cc:767
Definition: MessageType.h:126
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: unetexchange.cc:783
Definition: Mutex.h:31
Definition: MessageType.h:213
Definition: UniSetTypes_i.idl:64
long ObjectId
Definition: UniSetTypes_i.idl:30