UniSet  2.8.0
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 // -------------------------------------------------------------------------
27 namespace uniset
28 {
29  // -----------------------------------------------------------------------------
276  // -----------------------------------------------------------------------------
287  public MBExchange
288  {
289  public:
290  MBTCPMultiMaster( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
291  const std::string& prefix = "mbtcp" );
292  virtual ~MBTCPMultiMaster();
293 
295  static std::shared_ptr<MBTCPMultiMaster> init_mbmaster(int argc, const char* const* argv,
296  uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
297  const std::string& prefix = "mbtcp" );
298 
299  static void help_print( int argc, const char* const* argv );
300 
301  virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
302 
303  protected:
304  virtual void sysCommand( const uniset::SystemMessage* sm ) override;
305  virtual void initIterators() override;
306  virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) override;
307  virtual bool deactivateObject() override;
308  void initCheckConnectionParameters();
309 
310  void poll_thread();
311  void check_thread();
312  void final_thread();
313 
314  uniset::uniset_rwmutex mbMutex;
315  bool force_disconnect;
316  timeout_t checktime;
317 
318  private:
320 
321  struct MBSlaveInfo
322  {
323  MBSlaveInfo(): ip(""), port(0), mbtcp(0), priority(0),
324  respond(false), respond_id(uniset::DefaultObjectId), respond_invert(false),
325  recv_timeout(200), aftersend_pause(0), sleepPause_usec(100),
326  force_disconnect(true),
327  myname(""), use(false), initOK(false), ignore(false) {}
328 
329  std::string ip;
330  int port;
331  std::shared_ptr<ModbusTCPMaster> mbtcp;
332  int priority;
333 
334  // параметры для проверки соединения..
335  ModbusRTU::SlaveFunctionCode checkFunc = { ModbusRTU::fnUnknown };
336  ModbusRTU::ModbusAddr checkAddr = { 0x00 };
337  ModbusRTU::ModbusData checkReg = { 0 };
338 
339  bool respond;
340  uniset::ObjectId respond_id;
341  IOController::IOStateList::iterator respond_it;
342  bool respond_invert;
343  bool respond_init = { false };
344  bool respond_force = { false }; // флаг означающий принудительно обновлять значение датчика связи на каждом цикле проверки
345  DelayTimer respondDelay;
346  timeout_t channel_timeout = { 0 };
347 
348  inline bool operator < ( const MBSlaveInfo& mbs ) const noexcept
349  {
350  return priority < mbs.priority;
351  }
352 
353  bool init( std::shared_ptr<DebugStream>& mblog );
354  bool check();
355  void setUse( bool st );
356 
357  timeout_t recv_timeout;
358  timeout_t aftersend_pause;
359  timeout_t sleepPause_usec;
360  bool force_disconnect;
361 
362  std::string myname;
363 
364  bool use = { false }; // флаг используется ли в данный момент этот канал
365  bool initOK = { false };
366  bool ignore = { false }; // игнорировать данное соединение (обычно флаг выставляется на время ignoreTimeout, если узел не отвечает, хотя связь есть.
367  PassiveTimer ptIgnoreTimeout;
368 
369  const std::string getShortInfo() const;
370 
371  std::mutex mutInit;
372  };
373 
374  typedef std::list<std::shared_ptr<MBSlaveInfo>> MBGateList;
375 
376  MBGateList mblist;
377  MBGateList::reverse_iterator mbi;
378 
379  // т.к. TCP может "зависнуть" на подключении к недоступному узлу
380  // делаем опрос в отдельном потоке
381  std::unique_ptr< ThreadCreator<MBTCPMultiMaster> > pollThread;
382  std::unique_ptr< ThreadCreator<MBTCPMultiMaster> > checkThread;
383  };
384  // --------------------------------------------------------------------------
385 } // end of namespace uniset
386 // -----------------------------------------------------------------------------
387 #endif // _MBTCPMultiMaster_H_
388 // -----------------------------------------------------------------------------
Пассивный таймер
Definition: PassiveTimer.h:92
Definition: MBExchange.h:55
Definition: CommonEventLoop.h:14
Definition: MessageType.h:170
Definition: MBTCPMultiMaster.h:286
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:69
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:710
Definition: DelayTimer.h:29
Definition: Mutex.h:31
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: MBTCPMultiMaster.cc:582
Definition: UniSetTypes_i.idl:64
long ObjectId
Definition: UniSetTypes_i.idl:30