UniSet @VERSION@
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 // -----------------------------------------------------------------------------
51 public UniSetObject
52 {
53 public:
54 UNetExchange( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, const std::string& prefix = "unet" );
55 virtual ~UNetExchange();
56
58 static std::shared_ptr<UNetExchange> init_unetexchange( int argc, const char* const argv[],
59 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = 0, const std::string& prefix = "unet" );
60
62 static void help_print( int argc, const char* argv[] ) noexcept;
63
64 bool checkExistTransport( const std::string& transportID ) noexcept;
65
66 inline std::shared_ptr<LogAgregator> getLogAggregator() noexcept
67 {
68 return loga;
69 }
70 inline std::shared_ptr<DebugStream> log() noexcept
71 {
72 return unetlog;
73 }
74
75 virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
76
77#ifndef DISABLE_REST_API
78 // HTTP API
79 virtual Poco::JSON::Object::Ptr httpHelp( const Poco::URI::QueryParameters& p ) override;
80 virtual Poco::JSON::Object::Ptr httpRequest( const UHttp::HttpRequestContext& ctx ) override;
81 virtual Poco::JSON::Object::Ptr httpGetMyInfo( Poco::JSON::Object::Ptr root ) override;
82
83 Poco::JSON::Object::Ptr httpStatus();
84 Poco::JSON::Object::Ptr httpReceivers( const Poco::URI::QueryParameters& p );
85 Poco::JSON::Object::Ptr httpSenders( const Poco::URI::QueryParameters& p );
86 Poco::JSON::Object::Ptr httpGetParam( const Poco::URI::QueryParameters& p );
87 Poco::JSON::Object::Ptr httpSetParam( const Poco::URI::QueryParameters& p );
88
89 bool httpEnabledSetParams = { false };
90#endif
91
92 protected:
93
94 xmlNode* cnode;
95 std::string s_field;
96 std::string s_fvalue;
97
98 std::shared_ptr<SMInterface> shm;
99 void step() noexcept;
100
101 void sysCommand( const uniset::SystemMessage* msg ) override;
102 void sensorInfo( const uniset::SensorMessage* sm ) override;
103 void timerInfo( const uniset::TimerMessage* tm ) override;
104 void askSensors( UniversalIO::UIOCommand cmd );
105 bool waitSMReady();
106 void receiverEvent( const std::shared_ptr<UNetReceiver>& r, UNetReceiver::Event ev ) noexcept;
107
108 virtual bool activateObject() override;
109 virtual bool deactivateObject() override;
110
111 // действия при завершении работы
112 void termSenders();
113 void termReceivers();
114
115 void initMulticastTransport( UniXML::iterator nodes, const std::string& n_field, const std::string& n_fvalue, const std::string& prefix );
116 void initMulticastReceiverForNode( UniXML::iterator root, UniXML::iterator n_it, const std::string& prefix );
117
118 void initUDPTransport(UniXML::iterator nodes, const std::string& n_field, const std::string& n_fvalue, const std::string& prefix);
119 void initIterators() noexcept;
120 void startReceivers();
121
122 enum Timer
123 {
124 tmStep
125 };
126
127 private:
128 UNetExchange();
129 timeout_t initPause = { 0 };
130 uniset::uniset_rwmutex mutex_start;
131
132 PassiveTimer ptHeartBeat;
133 uniset::ObjectId sidHeartBeat = { uniset::DefaultObjectId };
134 timeout_t maxHeartBeat = { 10 };
135 IOController::IOStateList::iterator itHeartBeat;
137
138 timeout_t steptime = { 1000 };
140 std::atomic_bool activated = { false };
141 std::atomic_bool cancelled = { false };
142 timeout_t activateTimeout = { 20000 }; // msec
143
144 struct ReceiverInfo
145 {
146 ReceiverInfo() noexcept: r1(nullptr), r2(nullptr),
147 sidRespond(uniset::DefaultObjectId),
148 respondInvert(false),
149 sidLostPackets(uniset::DefaultObjectId),
150 sidChannelNum(uniset::DefaultObjectId)
151 {}
152
153 ReceiverInfo( const std::shared_ptr<UNetReceiver>& _r1, const std::shared_ptr<UNetReceiver>& _r2 ) noexcept:
154 r1(_r1), r2(_r2),
155 sidRespond(uniset::DefaultObjectId),
156 respondInvert(false),
157 sidLostPackets(uniset::DefaultObjectId),
158 sidChannelNum(uniset::DefaultObjectId)
159 {}
160
161 std::shared_ptr<UNetReceiver> r1;
162 std::shared_ptr<UNetReceiver> r2;
164 void step(const std::shared_ptr<SMInterface>& shm, const std::string& myname, std::shared_ptr<DebugStream>& log ) noexcept;
165
166 inline void setRespondID( uniset::ObjectId id, bool invert = false ) noexcept
167 {
168 sidRespond = id;
169 respondInvert = invert;
170 }
171 inline void setLostPacketsID( uniset::ObjectId id ) noexcept
172 {
173 sidLostPackets = id;
174 }
175 inline void setChannelNumID( uniset::ObjectId id ) noexcept
176 {
177 sidChannelNum = id;
178 }
179
180 inline void setChannelSwitchCountID( uniset::ObjectId id ) noexcept
181 {
182 sidChannelSwitchCount = id;
183 }
184
185 inline void initIterators( const std::shared_ptr<SMInterface>& shm ) noexcept
186 {
187 shm->initIterator(itLostPackets);
188 shm->initIterator(itRespond);
189 shm->initIterator(itChannelNum);
190 shm->initIterator(itChannelSwitchCount);
191 }
192
193 // Сводная информация по двум каналам
194 // сумма потерянных пакетов и наличие связи
195 // хотя бы по одному каналу, номер рабочего канала
196 // количество переключений с канала на канал
197 // ( реализацию см. ReceiverInfo::step() )
198 uniset::ObjectId sidRespond;
199 IOController::IOStateList::iterator itRespond;
200 bool respondInvert = { false };
201 uniset::ObjectId sidLostPackets;
202 IOController::IOStateList::iterator itLostPackets;
203 uniset::ObjectId sidChannelNum;
204 IOController::IOStateList::iterator itChannelNum;
205
206 long channelSwitchCount = { 0 };
207 uniset::ObjectId sidChannelSwitchCount = { uniset::DefaultObjectId };
208 IOController::IOStateList::iterator itChannelSwitchCount;
209 };
210
211 typedef std::deque<ReceiverInfo> ReceiverList;
212 ReceiverList recvlist;
213
214 bool no_sender = { false };
215 std::shared_ptr<UNetSender> sender;
216 std::shared_ptr<UNetSender> sender2;
217
218 std::shared_ptr<LogAgregator> loga;
219 std::shared_ptr<DebugStream> unetlog;
220 std::shared_ptr<LogServer> logserv;
221 std::string logserv_host = {""};
222 int logserv_port = {0};
223
224 VMonitor vmon;
225 };
226 // --------------------------------------------------------------------------
227} // end of namespace uniset
228// -----------------------------------------------------------------------------
229#endif // UNetExchange_H_
230// -----------------------------------------------------------------------------
Пассивный таймер
Definition PassiveTimer.h:94
Definition MessageType.h:127
Definition MessageType.h:171
Definition MessageType.h:214
Definition UNetExchange.h:52
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition UNetUDP/unetexchange.cc:625
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации)
Definition UNetUDP/unetexchange.cc:590
static void help_print(int argc, const char *argv[]) noexcept
Definition UNetUDP/unetexchange.cc:691
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:730
Event
Definition UNetReceiver.h:158
Definition UniSetObject.h:80
Definition UniXML.h:44
Definition VMonitor.h:117
Definition Mutex.h:32
Definition AccessConfig.h:30
const ObjectId DefaultObjectId
Definition UniSetTypes.h:71
long ObjectId
Definition UniSetTypes_i.idl:30
Definition UniSetTypes_i.idl:65
Definition UHttpRequestHandler.h:87