Commit 35c630d3 authored by Pavel Vainerman's avatar Pavel Vainerman

(ModbusMaster): добавил отдельный поток проверяющий не "зависло ли соединение"

parent 82739c72
......@@ -8,7 +8,7 @@
Name: libuniset
Version: 1.7
Release: alt5
Release: alt6
Summary: UniSet - library for building distributed industrial control systems
......@@ -335,6 +335,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
%exclude %_pkgconfigdir/libUniSet.pc
%changelog
* Wed Feb 12 2014 Pavel Vainerman <pv@altlinux.ru> 1.7-alt6
- ModbusMaster: add thread for check connection
* Mon Feb 10 2014 Pavel Vainerman <pv@altlinux.ru> 1.7-alt5
- ModbusMaster: minor fixes
......
......@@ -15,7 +15,8 @@ MBTCPMaster::MBTCPMaster( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shm
MBExchange(objId,shmId,ic,prefix),
force_disconnect(true),
mbtcp(0),
pollThread(0)
pollThread(0),
checkThread(0)
{
if( objId == DefaultObjectId )
throw UniSetTypes::SystemError("(MBTCPMaster): objId=-1?!! Use --" + prefix + "-name" );
......@@ -54,6 +55,20 @@ pollThread(0)
throw UniSetTypes::SystemError(myname+"(MBMaster): Unknown inet port...(Use: " + tmp +")" );
int t_check = conf->getArgInt("--" + prefix + "-exchange-timeout",it.getProp("exchange_timeout"));
if( t_check == 0 )
t_check = 5000;
checkTime = t_check < 0 ? 0 : t_check;
if( checkTime > 0 )
{
int etout = conf->getArgInt("--" + prefix + "-exchange-timeout",it.getProp("exchange_timeout"));
if( etout <= 0 )
etout = 5000;
ptExchangeTimeout.setTiming(etout);
}
force_disconnect = conf->getArgInt("--" + prefix + "-persistent-connection",it.getProp("persistent_connection")) ? false : true;
dlog[Debug::INFO] << myname << "(init): persisten-connection=" << (!force_disconnect) << endl;
......@@ -67,6 +82,7 @@ pollThread(0)
ic->addReadItem( sigc::mem_fun(this,&MBTCPMaster::readItem) );
pollThread = new ThreadCreator<MBTCPMaster>(this, &MBTCPMaster::poll_thread);
checkThread = new ThreadCreator<MBTCPMaster>(this, &MBTCPMaster::check_thread);
if( dlog.debugging(Debug::INFO) )
printMap(rmap);
......@@ -75,6 +91,7 @@ pollThread(0)
MBTCPMaster::~MBTCPMaster()
{
delete pollThread;
delete checkThread;
delete mbtcp;
}
// -----------------------------------------------------------------------------
......@@ -131,7 +148,15 @@ void MBTCPMaster::sysCommand( UniSetTypes::SystemMessage *sm )
{
MBExchange::sysCommand(sm);
if( sm->command == SystemMessage::StartUp )
{
pollThread->start();
{
uniset_mutex_lock l(checkMutex,300);
ptExchangeTimeout.reset();
}
if( checkTime > 0 )
checkThread->start();
}
}
// -----------------------------------------------------------------------------
void MBTCPMaster::poll_thread()
......@@ -141,6 +166,11 @@ void MBTCPMaster::poll_thread()
ptTimeout.reset();
}
{
uniset_mutex_lock l(checkMutex,300);
ptExchangeTimeout.reset();
}
while( checkProcActive() )
{
try
......@@ -151,7 +181,16 @@ void MBTCPMaster::poll_thread()
catch(...){}
try
{
{
uniset_mutex_lock l(checkMutex,300);
ptExchangeTimeout.reset();
}
poll();
{
uniset_mutex_lock l(checkMutex,300);
ptExchangeTimeout.reset();
}
}
catch(...){}
......@@ -197,3 +236,45 @@ MBTCPMaster* MBTCPMaster::init_mbmaster( int argc, const char* const* argv,
return new MBTCPMaster(ID,icID,ic,prefix);
}
// -----------------------------------------------------------------------------
void MBTCPMaster::check_thread()
{
while( checkProcActive() )
{
try
{
bool fail = false;
{
uniset_mutex_lock l(checkMutex,100);
fail = ptExchangeTimeout.checkTime();
}
if( dlog.debugging(Debug::LEVEL4) )
dlog[Debug::LEVEL4] << myname << ": check connection... " << ( fail ? "FAIL":"OK" ) << endl;
if( fail )
{
try
{
if( dlog.debugging(Debug::CRIT) )
dlog[Debug::CRIT] << myname << ": CONNECTION FAIL... terminate.." << endl;
mbtcp->disconnect();
}
catch( std::exception& ex )
{
cerr << "exception: " << ex.what() << endl;
}
catch( ... )
{
cerr << "exception: ...." << endl;
}
}
}
catch(...){}
if( !checkProcActive() )
break;
msleep(checkTime);
}
}
// -----------------------------------------------------------------------------
......@@ -217,6 +217,7 @@ class MBTCPMaster:
virtual ModbusClient* initMB( bool reopen=false );
void poll_thread();
void check_thread();
bool force_disconnect;
private:
......@@ -228,6 +229,11 @@ class MBTCPMaster:
// делаем опрос в отдельном потоке
ThreadCreator<MBTCPMaster>* pollThread; /*!< поток опроса */
UniSetTypes::uniset_mutex tcpMutex;
ThreadCreator<MBTCPMaster>* checkThread; /*!< поток проверки "зависания связи" */
UniSetTypes::uniset_mutex checkMutex;
PassiveTimer ptExchangeTimeout;
timeout_t checkTime;
};
// -----------------------------------------------------------------------------
#endif // _MBTCPMaster_H_
......
......@@ -21,7 +21,7 @@ class ModbusTCPMaster:
void disconnect();
bool isConnection();
static bool checkConnection( const std::string ip, int port, int timeout_msec=100 );
static bool checkConnection( const std::string& ip, int port, int timeout_msec=100 );
inline void setForceDisconnect( bool s )
{
......@@ -47,6 +47,7 @@ class ModbusTCPMaster:
std::queue<unsigned char> qrecv;
PassiveTimer ptTimeout;
std::string iaddr;
std::string addr;
bool force_disconnect;
};
// -------------------------------------------------------------------------
......
......@@ -272,7 +272,7 @@ void ModbusTCPMaster::cleanInputStream()
while( ret > 0);
}
// -------------------------------------------------------------------------
bool ModbusTCPMaster::checkConnection( const std::string ip, int port, int timeout_msec )
bool ModbusTCPMaster::checkConnection( const std::string& ip, int port, int timeout_msec )
{
try
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment