Skip to content
Snippets Groups Projects
SilecsConnection.cpp 14.24 KiB
// Copyright 2016 CERN and GSI
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <silecs-communication/interface/equipment/SilecsCluster.h>
#include <silecs-communication/interface/utility/SilecsLog.h>
#include <silecs-communication/interface/communication/SilecsConnection.h>
#include <silecs-communication/interface/equipment/SilecsPLC.h>
#include <silecs-communication/protocol/core/silecs.h>

namespace Silecs
{

	// static definition
	bool Connection::isAlive_ = false;
	const unsigned int Connection::numberConn_ = 3;

	//------------------------------------------------------------------------------------------------------------------------------------------------
	Connection::Connection(PLC* thePLC)
	{
		// Start the PLC Thread
		readMux_ 	= new Mutex("readMux");
		writeMux_ 	= new Mutex("writeMux");
		connMux_ 	= new Mutex("connMux");

		//Not yet allowed, not yet connected!
		isEnabled_    = false;
		isConnected_  = false;

		//Reset the Reconnection mechanism
		timeConn_ = NULL;
		delayConn_ = UrgentConnection;   //initial reconnection delay
		remainConn_ = numberConn_; 		 //initial number of attempt
	}


	//------------------------------------------------------------------------------------------------------------------------------------------------
	Connection::~Connection()
	{
	    //Connection has been closed before from the concrete connection object
		if (timeConn_ != NULL) delete timeConn_;

		delete connMux_;
		delete writeMux_;
		delete readMux_;
	}

	//------------------------------------------------------------------------------------------------------------------------------------------------
	bool Connection::enable(PLC* thePLC, bool connectNow)
	{
		{
			Lock lock(connMux_);
			isEnabled_ = true;
		}
		if (connectNow) doOpen(thePLC);
		return isConnected_;
	}

	//------------------------------------------------------------------------------------------------------------------------------------------------