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