UniSet 2.32.1
MBTCPMultiMaster.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 _MBTCPMultiMaster_H_
18#define _MBTCPMultiMaster_H_
19// -----------------------------------------------------------------------------
20#include <ostream>
21#include <string>
22#include <map>
23#include <vector>
24#include "MBExchange.h"
25#include "modbus/ModbusTCPMaster.h"
26// -------------------------------------------------------------------------
27namespace uniset
28{
29 // -----------------------------------------------------------------------------
258 // -----------------------------------------------------------------------------
269 public MBExchange
270 {
271 public:
272 MBTCPMultiMaster( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
273 const std::string& prefix = "mbtcp" );
274 virtual ~MBTCPMultiMaster();
275
277 static std::shared_ptr<MBTCPMultiMaster> init_mbmaster(int argc, const char* const* argv,
278 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
279 const std::string& prefix = "mbtcp" );
280
281 static void help_print( int argc, const char* const* argv );
282
283 virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
284
285 protected:
286 virtual void sysCommand( const uniset::SystemMessage* sm ) override;
287 virtual void initIterators() override;
288 virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) override;
289 virtual bool deactivateObject() override;
290 virtual bool reconfigure( const std::shared_ptr<uniset::UniXML>& xml, const std::shared_ptr<uniset::MBConfig>& mbconf ) override;
291 void initCheckConnectionParameters();
292 void initGateList( uniset::UniXML::iterator it, const std::shared_ptr<uniset::MBConfig>& mbconf );
293
294 void poll_thread();
295 void check_thread();
296 void final_thread();
297
299 bool force_disconnect;
300 timeout_t checktime;
301 timeout_t defaultIgnoreTimeout;
302 timeout_t channelTimeout;
303
304 private:
306
307 struct MBSlaveInfo
308 {
309 MBSlaveInfo(): ip(""), port(0), mbtcp(0), priority(0),
310 respond(false), respond_id(uniset::DefaultObjectId), respond_invert(false),
311 recv_timeout(200), aftersend_pause(0), sleepPause_usec(100),
312 force_disconnect(true),
313 myname(""), use(false), initOK(false), ignore(false) {}
314
315 std::string ip;
316 int port;
317 std::shared_ptr<ModbusTCPMaster> mbtcp;
318 int priority;
319
320 // параметры для проверки соединения..
321 ModbusRTU::SlaveFunctionCode checkFunc = { ModbusRTU::fnUnknown };
322 ModbusRTU::ModbusAddr checkAddr = { 0x00 };
323 ModbusRTU::ModbusData checkReg = { 0 };
324
325 bool respond;
326 uniset::ObjectId respond_id;
327 IOController::IOStateList::iterator respond_it;
328 bool respond_invert;
329 bool respond_init = { false };
330 bool respond_force = { false }; // флаг означающий принудительно обновлять значение датчика связи на каждом цикле проверки
331 DelayTimer respondDelay;
332 timeout_t channel_timeout = { 0 };
333
334 inline bool operator < ( const MBSlaveInfo& mbs ) const noexcept
335 {
336 return priority < mbs.priority;
337 }
338
339 bool init( std::shared_ptr<DebugStream>& mblog );
340 bool check();
341 void setUse( bool st );
342
343 timeout_t recv_timeout;
344 timeout_t aftersend_pause;
345 timeout_t sleepPause_usec;
346 bool force_disconnect;
347
348 std::string myname;
349
350 bool use = { false }; // флаг используется ли в данный момент этот канал
351 bool initOK = { false };
352 bool ignore = { false }; // игнорировать данное соединение (обычно флаг выставляется на время ignoreTimeout, если узел не отвечает, хотя связь есть.
353 PassiveTimer ptIgnoreTimeout;
354
355 const std::string getShortInfo() const;
356
357 std::mutex mutInit;
358 };
359
360 typedef std::list<std::shared_ptr<MBSlaveInfo>> MBGateList;
361
362 MBGateList mblist;
363 MBGateList::reverse_iterator mbi;
364
365 // т.к. TCP может "зависнуть" на подключении к недоступному узлу
366 // делаем опрос в отдельном потоке
367 std::unique_ptr< ThreadCreator<MBTCPMultiMaster> > pollThread;
368 std::unique_ptr< ThreadCreator<MBTCPMultiMaster> > checkThread;
369 };
370 // --------------------------------------------------------------------------
371} // end of namespace uniset
372// -----------------------------------------------------------------------------
373#endif // _MBTCPMultiMaster_H_
374// -----------------------------------------------------------------------------
Definition DelayTimer.h:30
Definition MBExchange.h:58
Definition MBTCPMultiMaster.h:270
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition MBTCPMultiMaster.cc:574
static std::shared_ptr< MBTCPMultiMaster > init_mbmaster(int argc, const char *const *argv, uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=nullptr, const std::string &prefix="mbtcp")
Definition MBTCPMultiMaster.cc:698
Пассивный таймер
Definition PassiveTimer.h:94
Definition MessageType.h:171
Definition UniXML.h:44
Definition Mutex.h:32
Definition Calibration.h:27
const ObjectId DefaultObjectId
Definition UniSetTypes.h:71
long ObjectId
Definition UniSetTypes_i.idl:30
Definition UniSetTypes_i.idl:65