|
UniSet
2.6.0
|
00001 /* 00002 * Copyright (c) 2015 Pavel Vainerman. 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as 00006 * published by the Free Software Foundation, version 2.1. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Lesser General Lesser Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 */ 00016 // ----------------------------------------------------------------------------- 00017 #ifndef _MBTCPMultiMaster_H_ 00018 #define _MBTCPMultiMaster_H_ 00019 // ----------------------------------------------------------------------------- 00020 #include <ostream> 00021 #include <string> 00022 #include <map> 00023 #include <vector> 00024 #include "MBExchange.h" 00025 #include "modbus/ModbusTCPMaster.h" 00026 // ------------------------------------------------------------------------- 00027 namespace uniset 00028 { 00029 // ----------------------------------------------------------------------------- 00275 // ----------------------------------------------------------------------------- 00285 class MBTCPMultiMaster: 00286 public MBExchange 00287 { 00288 public: 00289 MBTCPMultiMaster( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, 00290 const std::string& prefix = "mbtcp" ); 00291 virtual ~MBTCPMultiMaster(); 00292 00294 static std::shared_ptr<MBTCPMultiMaster> init_mbmaster(int argc, const char* const* argv, 00295 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, 00296 const std::string& prefix = "mbtcp" ); 00297 00299 static void help_print( int argc, const char* const* argv ); 00300 00301 virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override; 00302 00303 protected: 00304 virtual void sysCommand( const uniset::SystemMessage* sm ) override; 00305 virtual void initIterators() override; 00306 virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) override; 00307 virtual void sigterm( int signo ) override; 00308 virtual bool deactivateObject() override; 00309 void initCheckConnectionParameters(); 00310 00311 void poll_thread(); 00312 void check_thread(); 00313 void final_thread(); 00314 00315 uniset::uniset_rwmutex mbMutex; 00316 bool force_disconnect; 00317 timeout_t checktime; 00318 00319 private: 00320 MBTCPMultiMaster(); 00321 00322 struct MBSlaveInfo 00323 { 00324 MBSlaveInfo(): ip(""), port(0), mbtcp(0), priority(0), 00325 respond(false), respond_id(uniset::DefaultObjectId), respond_invert(false), 00326 recv_timeout(200), aftersend_pause(0), sleepPause_usec(100), 00327 force_disconnect(true), 00328 myname(""), use(false), initOK(false), ignore(false) {} 00329 00330 std::string ip; 00331 int port; 00332 std::shared_ptr<ModbusTCPMaster> mbtcp; 00333 int priority; 00334 00335 // параметры для проверки соединения.. 00336 ModbusRTU::SlaveFunctionCode checkFunc = { ModbusRTU::fnUnknown }; 00337 ModbusRTU::ModbusAddr checkAddr = { 0x00 }; 00338 ModbusRTU::ModbusData checkReg = { 0 }; 00339 00340 bool respond; 00341 uniset::ObjectId respond_id; 00342 IOController::IOStateList::iterator respond_it; 00343 bool respond_invert; 00344 bool respond_init = { false }; 00345 bool respond_force = { false }; // флаг означающий принудительно обновлять значение датчика связи на каждом цикле проверки 00346 DelayTimer respondDelay; 00347 timeout_t channel_timeout = { 0 }; 00348 00349 inline bool operator < ( const MBSlaveInfo& mbs ) const noexcept 00350 { 00351 return priority < mbs.priority; 00352 } 00353 00354 bool init( std::shared_ptr<DebugStream>& mblog ); 00355 bool check(); 00356 void setUse( bool st ); 00357 00358 timeout_t recv_timeout; 00359 timeout_t aftersend_pause; 00360 timeout_t sleepPause_usec; 00361 bool force_disconnect; 00362 00363 std::string myname; 00364 00365 bool use = { false }; // флаг используется ли в данный момент этот канал 00366 bool initOK = { false }; 00367 bool ignore = { false }; // игнорировать данное соединение (обычно флаг выставляется на время ignoreTimeout, если узел не отвечает, хотя связь есть. 00368 PassiveTimer ptIgnoreTimeout; 00369 00370 const std::string getShortInfo() const; 00371 00372 std::mutex mutInit; 00373 }; 00374 00375 typedef std::list<std::shared_ptr<MBSlaveInfo>> MBGateList; 00376 00377 MBGateList mblist; 00378 MBGateList::reverse_iterator mbi; 00379 00380 // т.к. TCP может "зависнуть" на подключении к недоступному узлу 00381 // делаем опрос в отдельном потоке 00382 std::shared_ptr< ThreadCreator<MBTCPMultiMaster> > pollThread; 00383 std::shared_ptr< ThreadCreator<MBTCPMultiMaster> > checkThread; 00384 }; 00385 // -------------------------------------------------------------------------- 00386 } // end of namespace uniset 00387 // ----------------------------------------------------------------------------- 00388 #endif // _MBTCPMultiMaster_H_ 00389 // -----------------------------------------------------------------------------
1.7.6.1