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