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