Commit 509bad8b authored by Alexis Girault's avatar Alexis Girault
Browse files

ENH: Implement imstkVRPNDeviceServer

parent 94d54543
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkVRPNDeviceServer.h"
#include "vrpn_3DConnexion.h"
#include "g3log/g3log.hpp"
namespace imstk {
bool
VRPNDeviceServer::addDeviceClient(const std::shared_ptr<VRPNDeviceClient> deviceClient)
{
// 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);
}
void
VRPNDeviceServer::initModule()
{
m_connection = vrpn_create_server_connection();
for (const auto& client : m_clientsList)
{
std::string name = client->getName();
switch (client->getType())
{
case DeviceType::SPACE_EXPLORER_3DCONNEXION:
{
m_devices->add(new vrpn_3DConnexion_SpaceExplorer(name.c_str(), m_connection));
} break;
case DeviceType::NAVIGATOR_3DCONNEXION:
{
m_devices->add(new vrpn_3DConnexion_Navigator(name.c_str(), m_connection));
} break;
default:
{
LOG(WARNING) << "VRPNDeviceServer::initModule error: can not connect to "
<< name<< ", device type unknown.";
} break;
}
}
}
void
VRPNDeviceServer::runModule()
{
m_connection->mainloop();
m_devices->mainloop();
}
void
VRPNDeviceServer::cleanUpModule()
{
m_devices->clear();
delete(m_devices);
m_connection->removeReference();
delete(m_connection);
}
}
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkVRPNDeviceServer_h
#define imstkVRPNDeviceServer_h
#include <vector>
#include <map>
#include <typeinfo>
#include <vrpn_Connection.h>
#include <vrpn_MainloopContainer.h>
#include "imstkModule.h"
#include "imstkVRPNDeviceClient.h"
namespace imstk {
///
/// \class VRPNDeviceServer
/// \brief Devices server using VRPN
///
class VRPNDeviceServer : public Module
{
public:
VRPNDeviceServer(std::string name = "VRPN Device Server"): Module(name) {}
virtual ~VRPNDeviceServer() {}
bool addDeviceClient(const std::shared_ptr<VRPNDeviceClient> deviceClient);
protected:
void initModule() override;
void runModule() override;
void cleanUpModule() override;
private:
vrpn_Connection * m_connection; //!< VRPN server connection
vrpn_MainloopContainer* m_devices; //!< VRPN devices connection
std::vector<std::shared_ptr<VRPNDeviceClient>> m_clientsList; //!< list of iMSTK client info
};
}
#endif // ifndef imstkVRPNDeviceServer_h
......@@ -25,6 +25,7 @@
// Devices
#include "imstkVRPNDeviceClient.h"
#include "imstkVRPNDeviceServer.h"
#include "g3log/g3log.hpp"
......@@ -63,11 +64,21 @@ void testDeviceClient()
{
auto sdk = std::make_shared<imstk::SimulationManager>();
auto client = std::make_shared<imstk::VRPNDeviceClient>("device0",
"localhost",
auto client = std::make_shared<imstk::VRPNDeviceClient>("device0", "localhost",
imstk::DeviceType::SPACE_EXPLORER_3DCONNEXION);
client->setLoopDelay(1000);
auto server = std::make_shared<imstk::VRPNDeviceServer>();
server->addDeviceClient(client);
// Start server in other thread
std::thread([server] { server->start(); });
// Start client here
client->start();
// When client quits, end server
server->end();
}
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