Attention une mise à jour du service Gitlab va être effectuée le mardi 14 décembre entre 13h30 et 14h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes.

Commit 4b31284b authored by Jussi Lindgren's avatar Jussi Lindgren
Browse files

Plugins: Refactored the TCP Tagging client class to its own file

parent bd52d1bc
......@@ -7,13 +7,7 @@
#include <string>
#include <algorithm>
// For stimulussender
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <sys/timeb.h>
using boost::asio::ip::tcp;
#include "../ovpCStimulusSender.h"
using namespace OpenViBE;
using namespace OpenViBE::Kernel;
......@@ -22,98 +16,6 @@ using namespace OpenViBE::Plugins;
using namespace OpenViBEPlugins;
using namespace OpenViBEPlugins::SimpleVisualisation;
/*
* \class StimulusSender
* \brief Simple client to send stimuli to Acquisition Server TCP Tagging
* \todo Refactor to its own file
*/
class StimulusSender {
public:
~StimulusSender()
{
if(m_oStimulusSocket.is_open())
{
m_oStimulusSocket.close();
}
}
StimulusSender(void)
: m_oStimulusSocket(m_ioService), m_bConnectedOnce(false)
{
}
boolean connect(const char* sAddress, const char* sStimulusPort)
{
tcp::resolver resolver(m_ioService);
// Stimulus port
std::cout << "Connecting to stimulus port [" << sAddress << " : " << sStimulusPort << "]\n";
try
{
boost::system::error_code error;
tcp::resolver::query query = tcp::resolver::query(tcp::v4(), sAddress, sStimulusPort, boost::asio::ip::resolver_query_base::numeric_service);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
m_oStimulusSocket.connect(*endpoint_iterator, error);
if(error)
{
std::cout << "Connection error: " << error << "\n";
return false;
}
}
catch (boost::system::system_error l_oError)
{
std::cout << "Issue '" << l_oError.code().message().c_str() << "' with opening connection to server\n";
return false;
}
m_bConnectedOnce = true;
return true;
}
boolean sendStimuli(uint64 ui64Stimuli)
{
if(!m_bConnectedOnce) {
return false;
}
timeb time_buffer;
ftime(&time_buffer);
const uint64 posixTime = time_buffer.time*1000ULL + time_buffer.millitm;
if(!m_oStimulusSocket.is_open())
{
std::cout << "Error: Cannot send stimulation, socket is not open\n";
return false;
}
uint64 l_ui64tmp = 0;
try
{
boost::asio::write(m_oStimulusSocket, boost::asio::buffer((void *)&l_ui64tmp, sizeof(uint64)));
boost::asio::write(m_oStimulusSocket, boost::asio::buffer((void *)&ui64Stimuli, sizeof(uint64)));
//boost::asio::write(m_oStimulusSocket, boost::asio::buffer((void *)&posixTime, sizeof(uint64)));
boost::asio::write(m_oStimulusSocket, boost::asio::buffer((void *)&l_ui64tmp, sizeof(uint64)));
}
catch (boost::system::system_error l_oError)
{
std::cout << "Issue '" << l_oError.code().message().c_str() << "' with writing stimulus to server\n";
}
return true;
}
private:
boost::asio::io_service m_ioService;
tcp::socket m_oStimulusSocket;
OpenViBE::boolean m_bConnectedOnce;
};
namespace
{
class _AutoCast_
......@@ -961,7 +863,7 @@ void CBoxAlgorithmP300SpellerVisualisation::flushQueue(void)
for(size_t i=0;i<m_vStimuliQueue.size();i++)
{
m_pStimulusSender->sendStimuli(m_vStimuliQueue[i]);
m_pStimulusSender->sendStimulation(m_vStimuliQueue[i]);
}
m_vStimuliQueue.clear();
......
......@@ -20,12 +20,12 @@
#define OVP_ClassId_BoxAlgorithm_P300SpellerVisualisation OpenViBE::CIdentifier(0x195E41D6, 0x6E684D47)
#define OVP_ClassId_BoxAlgorithm_P300SpellerVisualisationDesc OpenViBE::CIdentifier(0x31DE2B0D, 0x028202E7)
class StimulusSender; // fwd declare
namespace OpenViBEPlugins
{
namespace SimpleVisualisation
{
class StimulusSender; // fwd declare
class CBoxAlgorithmP300SpellerVisualisation : public OpenViBEToolkit::TBoxAlgorithm < OpenViBE::Plugins::IBoxAlgorithm >
{
public:
......@@ -131,7 +131,7 @@ namespace OpenViBEPlugins
guint m_uiIdleFuncTag;
boost::mutex m_oIdleFuncMutex;
StimulusSender* m_pStimulusSender;
OpenViBEPlugins::SimpleVisualisation::StimulusSender* m_pStimulusSender;
};
class CBoxAlgorithmP300SpellerVisualisationDesc : public OpenViBE::Plugins::IBoxAlgorithmDesc
......
#include "ovpCStimulusSender.h"
#include <iostream>
#include <boost/array.hpp>
#include <sys/timeb.h>
using namespace OpenViBE;
using namespace OpenViBEPlugins::SimpleVisualisation;
using boost::asio::ip::tcp;
// @fixme we're using std::cout here and there as this component may be run out of a box process() scope where the LogManager is available...
StimulusSender::~StimulusSender()
{
if(m_oStimulusSocket.is_open())
{
m_oStimulusSocket.close();
}
}
boolean StimulusSender::connect(const char* sAddress, const char* sStimulusPort)
{
tcp::resolver resolver(m_ioService);
// Stimulus port
std::cout << "Connecting to stimulus port [" << sAddress << " : " << sStimulusPort << "]\n";
try
{
boost::system::error_code error;
tcp::resolver::query query = tcp::resolver::query(tcp::v4(), sAddress, sStimulusPort, boost::asio::ip::resolver_query_base::numeric_service);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
m_oStimulusSocket.connect(*endpoint_iterator, error);
if(error)
{
std::cout << "Connection error: " << error << "\n";
return false;
}
}
catch (boost::system::system_error l_oError)
{
std::cout << "Issue '" << l_oError.code().message().c_str() << "' with opening connection to server\n";
return false;
}
m_bConnectedOnce = true;
return true;
}
boolean StimulusSender::sendStimulation(uint64 ui64Stimulation)
{
if(!m_bConnectedOnce) {
return false;
}
timeb time_buffer;
ftime(&time_buffer);
// const uint64 posixTime = time_buffer.time*1000ULL + time_buffer.millitm;
if(!m_oStimulusSocket.is_open())
{
std::cout << "Error: Cannot send stimulation, socket is not open\n";
return false;
}
uint64 l_ui64tmp = 0;
try
{
boost::asio::write(m_oStimulusSocket, boost::asio::buffer((void *)&l_ui64tmp, sizeof(uint64)));
boost::asio::write(m_oStimulusSocket, boost::asio::buffer((void *)&ui64Stimulation, sizeof(uint64)));
//boost::asio::write(m_oStimulusSocket, boost::asio::buffer((void *)&posixTime, sizeof(uint64)));
boost::asio::write(m_oStimulusSocket, boost::asio::buffer((void *)&l_ui64tmp, sizeof(uint64)));
}
catch (boost::system::system_error l_oError)
{
std::cout << "Issue '" << l_oError.code().message().c_str() << "' with writing stimulus to server\n";
}
return true;
}
#include <openvibe/ov_types.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
using boost::asio::ip::tcp;
namespace OpenViBEPlugins
{
namespace SimpleVisualisation
{
/*
* \class StimulusSender
* \author Jussi T. Lindgren / Inria
* \brief Simple client to send stimuli to Acquisition Server's TCP Tagging
*/
class StimulusSender {
public:
StimulusSender(void)
: m_oStimulusSocket(m_ioService), m_bConnectedOnce(false) { }
~StimulusSender();
// Connect to the TCP Tagging plugin of the Acquisition Server
OpenViBE::boolean connect(const char* sAddress, const char* sStimulusPort);
// Send a stimulation
OpenViBE::boolean sendStimulation(OpenViBE::uint64 ui64Stimulation) ;
protected:
boost::asio::io_service m_ioService;
tcp::socket m_oStimulusSocket;
OpenViBE::boolean m_bConnectedOnce;
};
}
}
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