Commit 0a35969d authored by Jussi Lindgren's avatar Jussi Lindgren

Plugins: Port can now be specified for VRPN server boxes

Note that all the VRPN server boxes in the scenarios require an update after this.
parent 0ba33790
......@@ -6,10 +6,13 @@
*/
#include <iostream>
#include <sstream>
#include <vrpn_Button.h>
#include <vrpn_Analog.h>
#define DEFAULT_PORT 3883
void VRPN_CALLBACK vrpn_button_callback(void* user_data, vrpn_BUTTONCB button)
{
std::cout << "Button ID : " << button.button << " / Button State : " << button.state << std::endl;
......@@ -37,14 +40,20 @@ int main(int argc, char** argv)
vrpn_Button_Remote* VRPNButton;
/* Binding of the VRPN Button to a callback */
VRPNButton = new vrpn_Button_Remote( "openvibe_vrpn_button@localhost" );
std::stringstream buttonUrl;
buttonUrl << std::string("openvibe_vrpn_button@localhost:") << (argc>1 ? atoi(argv[1]) : DEFAULT_PORT);
std::cout << "Server button URL = " << buttonUrl.str().c_str() << "\n";
VRPNButton = new vrpn_Button_Remote(buttonUrl.str().c_str());
VRPNButton->register_change_handler( &running, vrpn_button_callback );
/* VRPN Analog object */
vrpn_Analog_Remote* VRPNAnalog;
/* Binding of the VRPN Analog to a callback */
VRPNAnalog = new vrpn_Analog_Remote( "openvibe_vrpn_analog@localhost" );
std::stringstream analogUrl;
analogUrl << std::string("openvibe_vrpn_analog@localhost:") << (argc>1 ? atoi(argv[1]) : DEFAULT_PORT);
std::cout << "Server analog URL = " << analogUrl.str().c_str() << "\n";
VRPNAnalog = new vrpn_Analog_Remote(analogUrl.str().c_str());
VRPNAnalog->register_change_handler( NULL, vrpn_analog_callback );
/* The main loop of the program, each VRPN object must be called in order to process data */
......
......@@ -15,6 +15,7 @@ CVRPNAnalogServer::CVRPNAnalogServer()
{
}
boolean CVRPNAnalogServer::initialize()
{
IBox& l_rStaticBoxContext=this->getStaticBoxContext();
......@@ -23,6 +24,8 @@ boolean CVRPNAnalogServer::initialize()
CString l_oServerName;
l_rStaticBoxContext.getSettingValue(0, l_oServerName);
uint64 l_ui64Port=FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
// Creates the stream decoders
for(uint32 i=0; i<l_rStaticBoxContext.getInputCount(); i++)
{
......@@ -31,7 +34,11 @@ boolean CVRPNAnalogServer::initialize()
}
// Creates the peripheral
IVRPNServerManager::getInstance().initialize();
if(!IVRPNServerManager::getInstance().initialize(static_cast<uint32>(l_ui64Port)))
{
this->getLogManager() << LogLevel_Error << "Server init failed\n";
return false;
}
IVRPNServerManager::getInstance().addServer(l_oServerName, m_oServerIdentifier);
return true;
......
......@@ -91,6 +91,7 @@ namespace OpenViBEPlugins
{
rPrototype.addInput ("Input 1", OV_TypeId_StreamedMatrix);
rPrototype.addSetting("Peripheral name", OV_TypeId_String, "openvibe-vrpn");
rPrototype.addSetting("Server port", OV_TypeId_Integer, "3883");
rPrototype.addFlag (OpenViBE::Kernel::BoxFlag_CanAddInput);
return true;
}
......
......@@ -30,7 +30,14 @@ boolean CVRPNButtonServer::initialize()
CString l_oServerName;
l_pBox->getSettingValue(0, l_oServerName);
IVRPNServerManager::getInstance().initialize();
uint64 l_ui64Port=FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
if(!IVRPNServerManager::getInstance().initialize(static_cast<uint32>(l_ui64Port)))
{
this->getLogManager() << LogLevel_Error << "Server init failed\n";
return false;
}
IVRPNServerManager::getInstance().addServer(l_oServerName, m_oServerIdentifier);
IVRPNServerManager::getInstance().setButtonCount(m_oServerIdentifier, l_pBox->getInputCount());
......@@ -39,8 +46,8 @@ boolean CVRPNButtonServer::initialize()
{
CString l_sOnStimulationIdentifier;
CString l_sOffStimulationIdentifier;
l_pBox->getSettingValue(1+i*2, l_sOnStimulationIdentifier);
l_pBox->getSettingValue(2+i*2, l_sOffStimulationIdentifier);
l_pBox->getSettingValue(2+i*2, l_sOnStimulationIdentifier);
l_pBox->getSettingValue(3+i*2, l_sOffStimulationIdentifier);
m_vStimulationPair[i]=
pair<uint64, uint64>(
getBoxAlgorithmContext()->getPlayerContext()->getTypeManager().getEnumerationEntryValueFromName(OVTK_TypeId_Stimulation, l_sOnStimulationIdentifier),
......
......@@ -49,6 +49,7 @@ namespace OpenViBEPlugins
//Pairs of start/stop stimulations id
std::map<OpenViBE::uint32, std::pair<OpenViBE::uint64, OpenViBE::uint64> > m_vStimulationPair;
};
class CVRPNButtonServerListener : public OpenViBEToolkit::TBoxListener < OpenViBE::Plugins::IBoxListener >
......@@ -70,12 +71,12 @@ namespace OpenViBEPlugins
for(i=0; i<rBox.getInputCount(); i++)
{
sprintf(l_sName, "Button %u ON", i+1);
rBox.setSettingName(i*2+1, l_sName);
rBox.setSettingType(i*2+1, OV_TypeId_Stimulation);
rBox.setSettingName(i*2+2+0, l_sName);
rBox.setSettingType(i*2+2+0, OV_TypeId_Stimulation);
sprintf(l_sName, "Button %u OFF", i+1);
rBox.setSettingName(i*2+2, l_sName);
rBox.setSettingType(i*2+2, OV_TypeId_Stimulation);
rBox.setSettingName(i*2+2+1, l_sName);
rBox.setSettingType(i*2+2+1, OV_TypeId_Stimulation);
}
return true;
......@@ -83,8 +84,9 @@ namespace OpenViBEPlugins
virtual OpenViBE::boolean onInputRemoved(OpenViBE::Kernel::IBox& rBox, const OpenViBE::uint32 ui32Index)
{
rBox.removeSetting(ui32Index*2+1);
rBox.removeSetting(ui32Index*2+1);
// Note that we have the same index in both removes because the first remove changes the indexing
rBox.removeSetting(ui32Index*2+2+0);
rBox.removeSetting(ui32Index*2+2+0);
return this->check(rBox);
};
......@@ -122,6 +124,7 @@ namespace OpenViBEPlugins
{
rPrototype.addInput ("Input 1", OVTK_TypeId_Stimulations);
rPrototype.addSetting("Peripheral name", OV_TypeId_String, "openvibe-vrpn");
rPrototype.addSetting("Server port", OV_TypeId_Integer, "3883");
rPrototype.addSetting("Button 1 ON", OV_TypeId_Stimulation, "OVTK_GDF_Feedback_Continuous");
rPrototype.addSetting("Button 1 OFF", OV_TypeId_Stimulation, "OVTK_GDF_End_Of_Trial");
rPrototype.addFlag (OpenViBE::Kernel::BoxFlag_CanAddInput);
......
......@@ -34,6 +34,7 @@ namespace OpenViBEPlugins
virtual ~CVRPNServerManager(void);
virtual boolean initialize(void);
virtual boolean initialize(uint32 ui32port);
virtual boolean uninitialize(void);
virtual boolean process(void);
......@@ -90,6 +91,7 @@ namespace OpenViBEPlugins
map<CIdentifier, vector<boolean> > m_vButtonCache;
uint32 m_ui32InitializeCount;
uint32 m_ui32Port;
};
};
......@@ -104,6 +106,7 @@ namespace OpenViBEPlugins
CVRPNServerManager::CVRPNServerManager(void)
:m_pConnection(NULL)
,m_ui32InitializeCount(0)
,m_ui32Port(0)
{
}
......@@ -112,18 +115,43 @@ CVRPNServerManager::~CVRPNServerManager(void)
}
boolean CVRPNServerManager::initialize(void)
{
return initialize(vrpn_DEFAULT_LISTEN_PORT_NO);
}
boolean CVRPNServerManager::initialize(uint32 ui32port)
{
if(!m_ui32InitializeCount)
{
//m_pConnection=new vrpn_Connection;
m_pConnection=vrpn_create_server_connection();
}
m_pConnection=vrpn_create_server_connection(static_cast<int>(ui32port));
if(!m_pConnection) {
cout << "Error: Unable to create server at port " << ui32port << "\n";
return false;
}
m_ui32Port = ui32port;
}
m_ui32InitializeCount++;
if(ui32port != m_ui32Port)
{
cout << "Error: All servers running under a single openvibe Designer instance must have the same port.\n";
cout << "Requested port: " << ui32port << " Allocated port: " << m_ui32Port << "\n";
cout << "Consider using different server names instead to differentiate.\n";
return false;
}
return true;
}
boolean CVRPNServerManager::uninitialize(void)
{
if(m_ui32InitializeCount==0) {
cout << "Error: uninitialize() called without corresp. init\n";
return false;
}
m_ui32InitializeCount--;
if(!m_ui32InitializeCount)
{
......
......@@ -20,6 +20,7 @@ namespace OpenViBEPlugins
virtual ~IVRPNServerManager(void) { }
virtual OpenViBE::boolean initialize(void)=0;
virtual OpenViBE::boolean initialize(OpenViBE::uint32 ui32port)=0;
virtual OpenViBE::boolean uninitialize(void)=0;
virtual OpenViBE::boolean process(void)=0;
......
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