Skip to content

Commit 6fb3baa

Browse files
authored
[src] Update code to not include external header libs inside the main header for third party tool integration (#16)
1 parent 882be6e commit 6fb3baa

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE "-DSOFA_BUILD_SOFAHAPLYROBOTI
3434

3535
# Link the plugin library to its dependencies (other libraries).
3636
target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Component.Controller Sofa.Component.Haptics)
37-
target_link_libraries(${PROJECT_NAME} PUBLIC hv_static nlohmann_json::nlohmann_json)
37+
target_link_libraries(${PROJECT_NAME} PRIVATE hv_static nlohmann_json::nlohmann_json)
3838

3939
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>")
4040
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/external>")

src/SofaHaplyRobotics/Haply_Inverse3Controller.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,23 @@ void Haply_Inverse3Controller::initDevice()
129129
reconn.min_delay = 1000;
130130
reconn.max_delay = 5000;
131131
reconn.delay_policy = 2;
132-
m_ws.setReconnect(&reconn);
132+
133+
m_ws = std::make_unique<WebSocketClient>();
134+
m_ws->setReconnect(&reconn);
133135

134136
m_initDevice = true;
135137
}
136138

137139

138140
void Haply_Inverse3Controller::disconnect()
139141
{
140-
if (m_ws.isConnected()) {
141-
m_ws.close();
142+
if (m_ws->isConnected()) {
143+
m_ws->close();
144+
}
145+
146+
if (m_ws != nullptr)
147+
{
148+
m_ws.release();
142149
}
143150
}
144151

@@ -150,15 +157,15 @@ bool Haply_Inverse3Controller::createHapticThreads()
150157
m_terminateCopy = false;
151158
copy_thread = std::thread(&Haply_Inverse3Controller::CopyData, this, std::ref(this->m_terminateCopy), this);
152159

153-
m_ws.onopen = [&]() {
160+
m_ws->onopen = [&]() {
154161
std::cout << "[MyDeviceDriver] WebSocket opened." << std::endl;
155162
};
156163

157-
m_ws.onmessage = [&](const std::string& msg) {
164+
m_ws->onmessage = [&](const std::string& msg) {
158165
HapticsHandling(msg);
159166
};
160167

161-
m_ws.onclose = [&]() {
168+
m_ws->onclose = [&]() {
162169
std::cout << "[MyDeviceDriver] WebSocket closed." << std::endl;
163170
};
164171

@@ -167,7 +174,7 @@ bool Haply_Inverse3Controller::createHapticThreads()
167174

168175

169176
void Haply_Inverse3Controller::connect() {
170-
m_ws.open("ws://localhost:10001");
177+
m_ws->open("ws://localhost:10001");
171178
}
172179

173180

@@ -199,7 +206,7 @@ void Haply_Inverse3Controller::HapticsHandling(const std::string& msg)
199206

200207
std::cout << "[MyDeviceDriver] No devices found. Waiting for connection..." << std::endl;
201208
std::this_thread::sleep_for(1000ms);
202-
m_ws.send(update_request.dump());
209+
m_ws->send(update_request.dump());
203210

204211
return;
205212
}
@@ -320,7 +327,7 @@ void Haply_Inverse3Controller::HapticsHandling(const std::string& msg)
320327
}
321328
}
322329

323-
m_ws.send(request.dump());
330+
m_ws->send(request.dump());
324331

325332
cptLoop++;
326333
ctime_t endTime = CTime::getRefTime();

src/SofaHaplyRobotics/Haply_Inverse3Controller.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
#include <sofa/type/Vec.h>
1111
#include <sofa/component/controller/Controller.h>
1212
#include <mutex>
13-
#include <libhv.h>
14-
1513

1614
//force feedback
1715
#include <sofa/component/haptics/ForceFeedback.h>
1816

17+
namespace hv
18+
{
19+
class WebSocketClient;
20+
}
21+
1922
namespace sofa::HaplyRobotics
2023
{
2124
using namespace sofa::defaulttype;
@@ -137,7 +140,7 @@ class SOFA_HAPLYROBOTICS_API Haply_Inverse3Controller : public Controller
137140
std::thread copy_thread;
138141

139142
/// WebSocket client
140-
hv::WebSocketClient m_ws;
143+
std::unique_ptr<hv::WebSocketClient> m_ws;
141144

142145
// Todo : protect data with mutex
143146
//std::mutex dataMutex_;

0 commit comments

Comments
 (0)