UniSet  2.2.1
MBTCPMultiMaster.h
00001 #ifndef _MBTCPMultiMaster_H_
00002 #define _MBTCPMultiMaster_H_
00003 // -----------------------------------------------------------------------------
00004 #include <ostream>
00005 #include <string>
00006 #include <map>
00007 #include <vector>
00008 #include "MBExchange.h"
00009 #include "modbus/ModbusTCPMaster.h"
00010 // -----------------------------------------------------------------------------
00211 // -----------------------------------------------------------------------------
00221 class MBTCPMultiMaster:
00222     public MBExchange
00223 {
00224     public:
00225         MBTCPMultiMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
00226                           const std::string& prefix = "mbtcp" );
00227         virtual ~MBTCPMultiMaster();
00228 
00230         static std::shared_ptr<MBTCPMultiMaster> init_mbmaster(int argc, const char* const* argv,
00231                 UniSetTypes::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
00232                 const std::string& prefix = "mbtcp" );
00233 
00235         static void help_print( int argc, const char* const* argv );
00236 
00237         virtual UniSetTypes::SimpleInfo* getInfo( CORBA::Long userparam = 0 ) override;
00238 
00239     protected:
00240         virtual void sysCommand( const UniSetTypes::SystemMessage* sm ) override;
00241         virtual void initIterators() override;
00242         virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) override;
00243         virtual void sigterm( int signo ) override;
00244 
00245         void poll_thread();
00246         void check_thread();
00247         void final_thread();
00248 
00249 
00250         UniSetTypes::uniset_rwmutex mbMutex;
00251         bool force_disconnect;
00252         int checktime;
00253 
00254     private:
00255         MBTCPMultiMaster();
00256 
00257         struct MBSlaveInfo
00258         {
00259             MBSlaveInfo(): ip(""), port(0), mbtcp(0), priority(0),
00260                 respond(false), respond_id(UniSetTypes::DefaultObjectId), respond_invert(false),
00261                 recv_timeout(200), aftersend_pause(0), sleepPause_usec(100),
00262                 force_disconnect(true),
00263                 myname(""), use(false), initOK(false), ignore(false) {}
00264 
00265             std::string ip;
00266             int port;
00267             std::shared_ptr<ModbusTCPMaster> mbtcp;
00268             int priority;
00269 
00270             bool respond;
00271             UniSetTypes::ObjectId respond_id;
00272             IOController::IOStateList::iterator respond_it;
00273             bool respond_invert;
00274             bool respond_init = { false };
00275             bool respond_force = { false }; // флаг означающий принудительно обновлять значение датчика связи на каждом цикле проверки
00276 
00277             inline bool operator < ( const MBSlaveInfo& mbs ) const
00278             {
00279                 return priority < mbs.priority;
00280             }
00281 
00282             bool init( std::shared_ptr<DebugStream>& mblog );
00283             bool check();
00284             inline void setUse( bool st )
00285             {
00286                 respond_init = !( st && !use );
00287                 use = st;
00288             }
00289 
00290             int recv_timeout;
00291             int aftersend_pause;
00292             int sleepPause_usec;
00293             bool force_disconnect;
00294 
00295             std::string myname;
00296 
00297             bool use = { false }; // флаг используется ли в данный момент этот канал
00298             bool initOK = { false };
00299             bool ignore = { false }; // игнорировать данное соединение (обычно флаг выставляется на время ignoreTimeout, если узел не отвечает, хотя связь есть.
00300             PassiveTimer ptIgnoreTimeout;
00301 
00302             const std::string getShortInfo() const;
00303         };
00304 
00305         typedef std::list<MBSlaveInfo> MBGateList;
00306 
00307         MBGateList mblist;
00308         MBGateList::reverse_iterator mbi;
00309 
00310         // т.к. TCP может "зависнуть" на подключении к недоступному узлу
00311         // делаем опрос в отдельном потоке
00312         std::shared_ptr< ThreadCreator<MBTCPMultiMaster> > pollThread; 
00313         UniSetTypes::uniset_rwmutex tcpMutex;
00314 
00315         std::shared_ptr< ThreadCreator<MBTCPMultiMaster> > checkThread; 
00316 };
00317 // -----------------------------------------------------------------------------
00318 #endif // _MBTCPMultiMaster_H_
00319 // -----------------------------------------------------------------------------