UniSet  2.24.2
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  // -----------------------------------------------------------------------------
213  // -----------------------------------------------------------------------------
216  public UniSetObject
217  {
218  public:
219  UNetExchange( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, const std::string& prefix = "unet" );
220  virtual ~UNetExchange();
221 
223  static std::shared_ptr<UNetExchange> init_unetexchange( int argc, const char* const argv[],
224  uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = 0, const std::string& prefix = "unet" );
225 
227  static void help_print( int argc, const char* argv[] ) noexcept;
228 
229  bool checkExistTransport( const std::string& transportID ) noexcept;
230 
231  inline std::shared_ptr<LogAgregator> getLogAggregator() noexcept
232  {
233  return loga;
234  }
235  inline std::shared_ptr<DebugStream> log() noexcept
236  {
237  return unetlog;
238  }
239 
240  virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
241 
242  protected:
243 
244  xmlNode* cnode;
245  std::string s_field;
246  std::string s_fvalue;
247 
248  std::shared_ptr<SMInterface> shm;
249  void step() noexcept;
250 
251  void sysCommand( const uniset::SystemMessage* msg ) override;
252  void sensorInfo( const uniset::SensorMessage* sm ) override;
253  void timerInfo( const uniset::TimerMessage* tm ) override;
254  void askSensors( UniversalIO::UIOCommand cmd );
255  bool waitSMReady();
256  void receiverEvent( const std::shared_ptr<UNetReceiver>& r, UNetReceiver::Event ev ) noexcept;
257 
258  virtual bool activateObject() override;
259  virtual bool deactivateObject() override;
260 
261  // действия при завершении работы
262  void termSenders();
263  void termReceivers();
264 
265  void initMulticastTransport( UniXML::iterator nodes, const std::string& n_field, const std::string& n_fvalue, const std::string& prefix );
266  void initMulticastReceiverForNode( UniXML::iterator root, UniXML::iterator n_it, const std::string& prefix );
267 
268  void initUDPTransport(UniXML::iterator nodes, const std::string& n_field, const std::string& n_fvalue, const std::string& prefix);
269  void initIterators() noexcept;
270  void startReceivers();
271 
272  enum Timer
273  {
274  tmStep
275  };
276 
277  private:
278  UNetExchange();
279  timeout_t initPause = { 0 };
280  uniset::uniset_rwmutex mutex_start;
281 
282  PassiveTimer ptHeartBeat;
283  uniset::ObjectId sidHeartBeat = { uniset::DefaultObjectId };
284  timeout_t maxHeartBeat = { 10 };
285  IOController::IOStateList::iterator itHeartBeat;
287 
288  timeout_t steptime = { 1000 };
290  std::atomic_bool activated = { false };
291  std::atomic_bool cancelled = { false };
292  timeout_t activateTimeout = { 20000 }; // msec
293 
294  struct ReceiverInfo
295  {
296  ReceiverInfo() noexcept: r1(nullptr), r2(nullptr),
297  sidRespond(uniset::DefaultObjectId),
298  respondInvert(false),
299  sidLostPackets(uniset::DefaultObjectId),
300  sidChannelNum(uniset::DefaultObjectId)
301  {}
302 
303  ReceiverInfo( const std::shared_ptr<UNetReceiver>& _r1, const std::shared_ptr<UNetReceiver>& _r2 ) noexcept:
304  r1(_r1), r2(_r2),
305  sidRespond(uniset::DefaultObjectId),
306  respondInvert(false),
307  sidLostPackets(uniset::DefaultObjectId),
308  sidChannelNum(uniset::DefaultObjectId)
309  {}
310 
311  std::shared_ptr<UNetReceiver> r1;
312  std::shared_ptr<UNetReceiver> r2;
314  void step(const std::shared_ptr<SMInterface>& shm, const std::string& myname, std::shared_ptr<DebugStream>& log ) noexcept;
315 
316  inline void setRespondID( uniset::ObjectId id, bool invert = false ) noexcept
317  {
318  sidRespond = id;
319  respondInvert = invert;
320  }
321  inline void setLostPacketsID( uniset::ObjectId id ) noexcept
322  {
323  sidLostPackets = id;
324  }
325  inline void setChannelNumID( uniset::ObjectId id ) noexcept
326  {
327  sidChannelNum = id;
328  }
329 
330  inline void setChannelSwitchCountID( uniset::ObjectId id ) noexcept
331  {
332  sidChannelSwitchCount = id;
333  }
334 
335  inline void initIterators( const std::shared_ptr<SMInterface>& shm ) noexcept
336  {
337  shm->initIterator(itLostPackets);
338  shm->initIterator(itRespond);
339  shm->initIterator(itChannelNum);
340  shm->initIterator(itChannelSwitchCount);
341  }
342 
343  // Сводная информация по двум каналам
344  // сумма потерянных пакетов и наличие связи
345  // хотя бы по одному каналу, номер рабочего канала
346  // количество переключений с канала на канал
347  // ( реализацию см. ReceiverInfo::step() )
348  uniset::ObjectId sidRespond;
349  IOController::IOStateList::iterator itRespond;
350  bool respondInvert = { false };
351  uniset::ObjectId sidLostPackets;
352  IOController::IOStateList::iterator itLostPackets;
353  uniset::ObjectId sidChannelNum;
354  IOController::IOStateList::iterator itChannelNum;
355 
356  long channelSwitchCount = { 0 };
357  uniset::ObjectId sidChannelSwitchCount = { uniset::DefaultObjectId };
358  IOController::IOStateList::iterator itChannelSwitchCount;
359  };
360 
361  typedef std::deque<ReceiverInfo> ReceiverList;
362  ReceiverList recvlist;
363 
364  bool no_sender = { false };
365  std::shared_ptr<UNetSender> sender;
366  std::shared_ptr<UNetSender> sender2;
367 
368  std::shared_ptr<LogAgregator> loga;
369  std::shared_ptr<DebugStream> unetlog;
370  std::shared_ptr<LogServer> logserv;
371  std::string logserv_host = {""};
372  int logserv_port = {0};
373 
374  VMonitor vmon;
375  };
376  // --------------------------------------------------------------------------
377 } // end of namespace uniset
378 // -----------------------------------------------------------------------------
379 #endif // UNetExchange_H_
380 // -----------------------------------------------------------------------------
Пассивный таймер
Definition: PassiveTimer.h:94
Definition: MessageType.h:127
Definition: MessageType.h:171
Definition: MessageType.h:214
Definition: UNetExchange.h:217
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: UNetUDP/UNetExchange.cc:535
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации)
Definition: UNetUDP/UNetExchange.cc:519
static void help_print(int argc, const char *argv[]) noexcept
Definition: UNetUDP/UNetExchange.cc:601
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: UNetUDP/UNetExchange.cc:639
Event
Definition: UNetReceiver.h:154
Definition: UniSetObject.h:80
Definition: UniXML.h:44
Definition: VMonitor.h:117
Definition: Mutex.h:32
Definition: CommonEventLoop.h:15
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:70
long ObjectId
Definition: UniSetTypes_i.idl:30
Definition: UniSetTypes_i.idl:65