UniSet  2.6.0
MBTCPMaster.h
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 _MBTCPMaster_H_
00018 #define _MBTCPMaster_H_
00019 // -----------------------------------------------------------------------------
00020 #include <ostream>
00021 #include <string>
00022 #include <map>
00023 #include <vector>
00024 #include <memory>
00025 #include "MBExchange.h"
00026 #include "modbus/ModbusTCPMaster.h"
00027 // -------------------------------------------------------------------------
00028 namespace uniset
00029 {
00030 // -----------------------------------------------------------------------------
00211 // -----------------------------------------------------------------------------
00221 class MBTCPMaster:
00222     public MBExchange
00223 {
00224     public:
00225         MBTCPMaster( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr,
00226                      const std::string& prefix = "mbtcp" );
00227         virtual ~MBTCPMaster();
00228 
00230         static std::shared_ptr<MBTCPMaster> init_mbmaster( int argc, const char* const* argv,
00231                 uniset::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 uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
00238 
00239     protected:
00240         virtual void sysCommand( const uniset::SystemMessage* sm ) override;
00241         virtual std::shared_ptr<ModbusClient> initMB( bool reopen = false ) override;
00242         virtual void sigterm( int signo ) override;
00243         virtual bool deactivateObject() override;
00244 
00245         std::string iaddr;
00246         int port;
00247 
00248         void poll_thread();
00249         void final_thread();
00250         bool force_disconnect;
00251 
00252     private:
00253         MBTCPMaster();
00254 
00255         std::shared_ptr<ModbusTCPMaster> mbtcp;
00256 
00257         // т.к. TCP может "зависнуть" на подключении к недоступному узлу
00258         // делаем опрос в отдельном потоке
00259         std::shared_ptr<ThreadCreator<MBTCPMaster>> pollThread; 
00260 };
00261 // --------------------------------------------------------------------------
00262 } // end of namespace uniset
00263 // -----------------------------------------------------------------------------
00264 #endif // _MBTCPMaster_H_
00265 // -----------------------------------------------------------------------------