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