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