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