Commit 9a33b689 authored by Alexis Girault's avatar Alexis Girault
Browse files

ENH: Improve VRPN Server Client architecture

1) Get rid of type in deviceClient
2) Rename URL to IP in deviceClient
3) Add machine (name or ip) and port number as parameters
in device server to be able to create external servers
4) Refactor VRPNDeviceServer::addDeviceClient into addDevice
to use only a deviceName and a deviceType
parent 509bad8b
......@@ -24,28 +24,17 @@
#include "g3log/g3log.hpp"
namespace imstk {
const DeviceType&
DeviceClient::getType()
{
return m_type;
}
void
DeviceClient::setType(const DeviceType& type)
{
m_type = type;
}
const std::string&
DeviceClient::getUrl()
DeviceClient::getIp()
{
return m_url;
return m_ip;
}
void
DeviceClient::setUrl(const std::string& url)
DeviceClient::setIp(const std::string& ip)
{
m_url = url;
m_ip = ip;
}
const bool&
......
......@@ -29,12 +29,6 @@
namespace imstk {
enum class DeviceType
{
SPACE_EXPLORER_3DCONNEXION,
NAVIGATOR_3DCONNEXION
};
///
/// \class DeviceClient
/// \brief Base class for any device client
......@@ -47,16 +41,10 @@ public:
// Accessors
///
/// \brief Get/Set the type of the device
///
const DeviceType& getType();
void setType(const DeviceType& type);
///
/// \brief Get/Set the device URL
/// \brief Get/Set the device IP
///
const std::string& getUrl();
void setUrl(const std::string& url);
const std::string& getIp();
void setIp(const std::string& ip);
///
/// \brief Get/Set what listeners to enable on the device: tracking, analogic, force, buttons.
......@@ -107,14 +95,12 @@ public:
protected:
DeviceClient(std::string name, std::string url, DeviceType type):
DeviceClient(std::string name, std::string ip):
Module(name),
m_url(url),
m_type(type)
m_ip(ip)
{}
DeviceType m_type; //!< Device type
std::string m_url; //!< Connection device URL
std::string m_ip; //!< Connection device IP
bool m_trackingEnabled = true; //!< Tracking enabled if true
bool m_analogicEnabled = true; //!< Analogic enabled if true
......
......@@ -28,12 +28,12 @@ namespace imstk {
void
VRPNDeviceClient::initModule()
{
std::string fullDeviceUrl = this->getName() + "@" + this->getUrl();
std::string fullDeviceIp = this->getName() + "@" + this->getIp();
m_vrpnTracker = std::make_shared<vrpn_Tracker_Remote>(fullDeviceUrl.c_str());
m_vrpnAnalog = std::make_shared<vrpn_Analog_Remote>(fullDeviceUrl.c_str());
m_vrpnButton = std::make_shared<vrpn_Button_Remote>(fullDeviceUrl.c_str());
m_vrpnForceDevice = std::make_shared<vrpn_ForceDevice_Remote>(fullDeviceUrl.c_str());
m_vrpnTracker = std::make_shared<vrpn_Tracker_Remote>(fullDeviceIp.c_str());
m_vrpnAnalog = std::make_shared<vrpn_Analog_Remote>(fullDeviceIp.c_str());
m_vrpnButton = std::make_shared<vrpn_Button_Remote>(fullDeviceIp.c_str());
m_vrpnForceDevice = std::make_shared<vrpn_ForceDevice_Remote>(fullDeviceIp.c_str());
m_vrpnTracker->register_change_handler(this, trackerChangeHandler);
m_vrpnTracker->register_change_handler(this, velocityChangeHandler);
......
......@@ -42,8 +42,8 @@ class VRPNDeviceClient : public DeviceClient
{
public:
VRPNDeviceClient(std::string name, std::string url, DeviceType type):
DeviceClient(name, url, type)
VRPNDeviceClient(std::string name, std::string ip):
DeviceClient(name, ip)
{}
virtual ~VRPNDeviceClient() {}
......
......@@ -27,38 +27,33 @@
namespace imstk {
bool
VRPNDeviceServer::addDeviceClient(const std::shared_ptr<VRPNDeviceClient> deviceClient)
VRPNDeviceServer::addDevice(std::string deviceName, DeviceType deviceType)
{
// Check that url is localhost
if (deviceClient->getUrl() != "localhost")
{
LOG(WARNING) << "VRPNDeviceServer::addDeviceClient error: can not connect to "
<< deviceClient->getUrl() << "\n"
<< "Can only communicate with devices locally (url = localhost).";
return EXIT_FAILURE;
}
m_clientsList.push_back(deviceClient);
m_deviceInfoMap[deviceName] = deviceType;
}
void
VRPNDeviceServer::initModule()
{
m_connection = vrpn_create_server_connection();
std::string ip = m_machine + ":" + std::to_string(m_port);
m_serverConnection = vrpn_create_server_connection(ip.c_str());
m_deviceConnections = new vrpn_MainloopContainer();
for (const auto& client : m_clientsList)
for (const auto& device : m_deviceInfoMap)
{
std::string name = client->getName();
std::string name = device.first;
DeviceType type = device.second;
switch (client->getType())
switch (type)
{
case DeviceType::SPACE_EXPLORER_3DCONNEXION:
{
m_devices->add(new vrpn_3DConnexion_SpaceExplorer(name.c_str(), m_connection));
m_deviceConnections->add(new vrpn_3DConnexion_SpaceExplorer(name.c_str(), m_serverConnection));
} break;
case DeviceType::NAVIGATOR_3DCONNEXION:
{
m_devices->add(new vrpn_3DConnexion_Navigator(name.c_str(), m_connection));
m_deviceConnections->add(new vrpn_3DConnexion_Navigator(name.c_str(), m_serverConnection));
} break;
default:
{
......@@ -72,17 +67,17 @@ VRPNDeviceServer::initModule()
void
VRPNDeviceServer::runModule()
{
m_connection->mainloop();
m_devices->mainloop();
m_serverConnection->mainloop();
m_deviceConnections->mainloop();
}
void
VRPNDeviceServer::cleanUpModule()
{
m_devices->clear();
delete(m_devices);
m_deviceConnections->clear();
delete(m_deviceConnections);
m_connection->removeReference();
delete(m_connection);
m_serverConnection->removeReference();
//delete(m_connection);
}
}
......@@ -34,6 +34,12 @@
namespace imstk {
enum class DeviceType
{
SPACE_EXPLORER_3DCONNEXION,
NAVIGATOR_3DCONNEXION
};
///
/// \class VRPNDeviceServer
/// \brief Devices server using VRPN
......@@ -42,11 +48,15 @@ class VRPNDeviceServer : public Module
{
public:
VRPNDeviceServer(std::string name = "VRPN Device Server"): Module(name) {}
VRPNDeviceServer(std::string machine = "localhost", int port = vrpn_DEFAULT_LISTEN_PORT_NO):
m_machine(machine),
m_port(port),
Module(machine + ":" + std::to_string(port))
{}
virtual ~VRPNDeviceServer() {}
bool addDeviceClient(const std::shared_ptr<VRPNDeviceClient> deviceClient);
bool addDevice(std::string deviceName, DeviceType deviceType);
protected:
......@@ -56,10 +66,13 @@ protected:
private:
vrpn_Connection * m_connection; //!< VRPN server connection
vrpn_MainloopContainer* m_devices; //!< VRPN devices connection
const std::string m_machine; //!< machine name or IP
const int m_port; //!< connection port
std::map<std::string, DeviceType> m_deviceInfoMap; //!< list of iMSTK client info
vrpn_Connection * m_serverConnection; //!< VRPN server connection
vrpn_MainloopContainer* m_deviceConnections; //!< VRPN device connections
std::vector<std::shared_ptr<VRPNDeviceClient>> m_clientsList; //!< list of iMSTK client info
};
}
......
......@@ -29,7 +29,7 @@
#include "g3log/g3log.hpp"
void testDeviceClient();
void testDevices();
void testReadMesh();
void testViewer();
void testAnalyticalGeometry();
......@@ -46,7 +46,7 @@ int main()
<< "Starting Sandbox\n"
<< "****************\n";
testDeviceClient();
testDevices();
//testViewer();
//testReadMesh();
//testAnalyticalGeometry();
......@@ -60,25 +60,24 @@ int main()
return 0;
}
void testDeviceClient()
void testDevices()
{
auto sdk = std::make_shared<imstk::SimulationManager>();
auto client = std::make_shared<imstk::VRPNDeviceClient>("device0", "localhost",
imstk::DeviceType::SPACE_EXPLORER_3DCONNEXION);
auto server = std::make_shared<imstk::VRPNDeviceServer>("127.0.0.1");
server->addDevice("device0", imstk::DeviceType::SPACE_EXPLORER_3DCONNEXION);
auto server = std::make_shared<imstk::VRPNDeviceServer>();
server->addDeviceClient(client);
auto client = std::make_shared<imstk::VRPNDeviceClient>("device0", "localhost"); // localhost = 127.0.0.1
// Start server in other thread
std::thread([server] { server->start(); });
auto t = std::thread([server] { server->start(); });
// Start client here
client->start();
// When client quits, end server
server->end();
t.join();
}
void testReadMesh()
......
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