cisst-saw
Loading...
Searching...
No Matches
mtsIGTLBridge.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3/*
4
5 Author(s): Ali Uneri, Anton Deguet, Youri Tan
6 Created on: 2009-08-10
7
8 (C) Copyright 2009-2020 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
24
25#ifndef _mtsIGTLBridge_h
26#define _mtsIGTLBridge_h
27
28#include <map>
29
32
33// Always include last!
35
36class mtsIGTLBridge;
37class mtsIGTLBridgeData;
38
39namespace igtl {
40 class MessageBase;
41 class Socket;
42}
43
45{
46public:
47 inline mtsIGTLSenderBase(const std::string & name, mtsIGTLBridge * bridge):
48 mName(name), mBridge(bridge) {}
49
52
53 virtual ~mtsIGTLSenderBase() {};
54
55 virtual bool Execute(void) = 0;
56
57protected:
58 std::string mName;
60};
61
62template <typename _cisstType, typename _igtlType>
64{
65public:
66 inline mtsIGTLSender(const std::string & name, mtsIGTLBridge * bridge):
67 mtsIGTLSenderBase(name, bridge) {
68 }
69 inline virtual ~mtsIGTLSender() {}
70 bool Execute(void) override;
71
72protected:
73 _cisstType mCISSTData;
74 typedef typename _igtlType::Pointer IGTLPointer;
76};
77
78template <typename _cisstType, typename _igtlType>
80{
81public:
82 inline mtsIGTLEventWriteSender(const std::string & name, mtsIGTLBridge * bridge):
83 mtsIGTLSenderBase(name, bridge) {
84 mIGTLData = _igtlType::New();
85 mIGTLData->SetDeviceName(name);
86 }
87 inline virtual ~mtsIGTLEventWriteSender() {}
88 bool Execute(void) override {
89 return true;
90 }
91 void EventHandler(const _cisstType & cisstData);
92protected:
93 typedef typename _igtlType::Pointer IGTLPointer;
95};
96
97
99{
100public:
101 inline mtsIGTLReceiverBase(const std::string & name, mtsIGTLBridge * bridge):
102 mName(name), mBridge(bridge) {}
103
106
108
109 virtual bool Execute(igtl::Socket * socket, igtl::MessageBase * header) = 0;
110
111protected:
112 std::string mName;
114};
115
116template <typename _igtlType, typename _cisstType>
118{
119public:
120 inline mtsIGTLReceiver(const std::string & name, mtsIGTLBridge * bridge):
121 mtsIGTLReceiverBase(name, bridge) {
122 }
123 inline virtual ~mtsIGTLReceiver() {}
124 bool Execute(igtl::Socket * socket, igtl::MessageBase * header) override;
125
126protected:
127 typedef typename _igtlType::Pointer IGTLPointer;
128 _cisstType mCISSTData;
129};
130
131
133{
135
136 public:
138 inline mtsIGTLBridge(const std::string & componentName, const double & period):
139 mtsTaskPeriodic(componentName, period, false, 500) {
140 Init();
141 }
142
144 mtsTaskPeriodic(arg) {
145 Init();
146 }
147
149 inline ~mtsIGTLBridge(void) {}
150
151 protected:
153 void Init(void);
154
156 void InitServer(void);
157
158 public:
159 inline void SetPort(const int port) {
160 mPort = port;
161 InitServer();
162 }
163
164 void Configure(const std::string & jsonFile) override;
165 virtual void ConfigureJSON(const Json::Value & jsonConfig);
166
167 void Startup(void) override;
168 void Run(void) override;
169 void Cleanup(void) override;
170
171 template <typename _cisstType, typename _igtlType>
172 bool AddSenderFromCommandRead(const std::string & interfaceRequiredName,
173 const std::string & functionName,
174 const std::string & igtlDeviceName);
175
176 template <typename _cisstType, typename _igtlType>
177 bool AddSenderFromEventWrite(const std::string & interfaceRequiredName,
178 const std::string & eventName,
179 const std::string & igtlDeviceName);
180
181 template <typename _igtlType, typename _cisstType>
182 bool AddReceiverToCommandWrite(const std::string & interfaceRequiredName,
183 const std::string & commandName,
184 const std::string & igtlDeviceName);
185
186 void SendAll(void);
187
188 template <typename _igtlMessagePointer>
189 void Send(_igtlMessagePointer message);
190
191 void ReceiveAll(void);
192
193 protected:
194 // igtl networking
195 int mPort = 0; // default
196 mtsIGTLBridgeData * mData = nullptr;
197
198 // cisst interfaces
199 typedef std::list<mtsIGTLSenderBase *> SendersType;
201
202 typedef std::map<std::string, mtsIGTLReceiverBase *> ReceiversType;
204};
205
206
207template <typename _cisstType, typename _igtlType>
209{
211 if (result) {
212 mIGTLData = _igtlType::New();
213 mIGTLData->SetDeviceName(mName);
215 mIGTLData->Pack();
216 mBridge->Send(mIGTLData);
217 return true;
218 }
219 } else {
220 CMN_LOG_RUN_ERROR << "mtsIGTLSender::Execute: " << result
221 << " for " << mName << std::endl;
222 }
223 return false;
224}
225
226template <typename _cisstType, typename _igtlType>
228{
229 mIGTLData = _igtlType::New();
230 mIGTLData->SetDeviceName(mName);
231 if (mtsCISSTToIGTL(cisstData, mIGTLData)) {
232 mIGTLData->Pack();
233 mBridge->Send(mIGTLData);
234 }
235}
236
237template <typename _cisstType, typename _igtlType>
238bool mtsIGTLBridge::AddSenderFromCommandRead(const std::string & interfaceRequiredName,
239 const std::string & functionName,
240 const std::string & igtlDeviceName)
241{
242 // check if the interface exists of try to create one
243 mtsInterfaceRequired * interfaceRequired
244 = this->GetInterfaceRequired(interfaceRequiredName);
245 if (!interfaceRequired) {
246 interfaceRequired = this->AddInterfaceRequired(interfaceRequiredName);
247 }
248 if (!interfaceRequired) {
249 CMN_LOG_CLASS_INIT_ERROR << "AddSenderFromCommandRead: failed to create required interface \""
250 << interfaceRequiredName << "\"" << std::endl;
251 return false;
252 }
253 mtsIGTLSenderBase * newSender =
254 new mtsIGTLSender<_cisstType, _igtlType>(igtlDeviceName, this);
255 if (!interfaceRequired->AddFunction(functionName, newSender->Function)) {
256 CMN_LOG_CLASS_INIT_ERROR << "AddSenderFromCommandRead: failed to add function \""
257 << functionName << "\" to interface required \""
258 << interfaceRequiredName << "\"" << std::endl;
259 delete newSender;
260 return false;
261 }
262 mSenders.push_back(newSender);
263 return true;
264}
265
266
267template <typename _cisstType, typename _igtlType>
268bool mtsIGTLBridge::AddSenderFromEventWrite(const std::string & interfaceRequiredName,
269 const std::string & eventName,
270 const std::string & igtlDeviceName)
271{
272 // check if the interface exists of try to create one
273 mtsInterfaceRequired * interfaceRequired
274 = this->GetInterfaceRequired(interfaceRequiredName);
275 if (!interfaceRequired) {
276 interfaceRequired = this->AddInterfaceRequired(interfaceRequiredName);
277 }
278 if (!interfaceRequired) {
279 CMN_LOG_CLASS_INIT_ERROR << "AddSenderFromEventWrite: failed to create required interface \""
280 << interfaceRequiredName << "\"" << std::endl;
281 return false;
282 }
284 = new mtsIGTLEventWriteSender<_cisstType, _igtlType>(igtlDeviceName, this);
286 newSender, eventName)) {
287 CMN_LOG_CLASS_INIT_ERROR << "AddSenderFromEventWrite: failed to add event \""
288 << eventName << "\" to required interface \""
289 << interfaceRequiredName << "\"" << std::endl;
290 delete newSender;
291 return false;
292 }
293 mSenders.push_back(newSender);
294 return true;
295}
296
297template <typename _igtlType, typename _cisstType>
298bool mtsIGTLBridge::AddReceiverToCommandWrite(const std::string & interfaceRequiredName,
299 const std::string & commandName,
300 const std::string & igtlDeviceName)
301{
302 // check if the interface exists of try to create one
303 mtsInterfaceRequired * interfaceRequired = this->GetInterfaceRequired(interfaceRequiredName);
304 if (!interfaceRequired) {
305 interfaceRequired = this->AddInterfaceRequired(interfaceRequiredName);
306 }
307 if (!interfaceRequired) {
308 CMN_LOG_CLASS_INIT_ERROR << "AddReceiverToCommandWrite: failed to create required interface \""
309 << interfaceRequiredName << "\"" << std::endl;
310 return false;
311 }
313 = new mtsIGTLReceiver<_igtlType, _cisstType>(igtlDeviceName, this);
314 if (!interfaceRequired->AddFunction(commandName, newReceiver->Function)) {
315 CMN_LOG_CLASS_INIT_ERROR << "AddReceiverToCommandWrite: failed to add function \""
316 << commandName << "\" to interface required \""
317 << interfaceRequiredName << "\"" << std::endl;
318 delete newReceiver;
319 return false;
320 }
321 mReceivers.insert({igtlDeviceName, newReceiver});
322 return true;
323}
324
326
327#endif // _mtsIGTLBridge_h
mtsInterfaceRequired * GetInterfaceRequired(const std::string &interfaceRequiredName)
mtsInterfaceRequired * AddInterfaceRequired(const std::string &interfaceRequiredName, mtsRequiredType isRequired=MTS_REQUIRED)
Definition mtsExecutionResult.h:34
Definition mtsFunctionRead.h:37
Definition mtsFunctionWrite.h:37
Definition mtsIGTLBridge.h:133
mtsIGTLBridge(const mtsTaskPeriodicConstructorArg &arg)
Definition mtsIGTLBridge.h:143
virtual void ConfigureJSON(const Json::Value &jsonConfig)
void Init(void)
bool AddSenderFromEventWrite(const std::string &interfaceRequiredName, const std::string &eventName, const std::string &igtlDeviceName)
Definition mtsIGTLBridge.h:268
void Cleanup(void) override
void InitServer(void)
void SendAll(void)
ReceiversType mReceivers
Definition mtsIGTLBridge.h:203
mtsIGTLBridge(const std::string &componentName, const double &period)
Definition mtsIGTLBridge.h:138
void ReceiveAll(void)
void Run(void) override
mtsIGTLBridgeData * mData
Definition mtsIGTLBridge.h:196
std::map< std::string, mtsIGTLReceiverBase * > ReceiversType
Definition mtsIGTLBridge.h:202
bool AddSenderFromCommandRead(const std::string &interfaceRequiredName, const std::string &functionName, const std::string &igtlDeviceName)
Definition mtsIGTLBridge.h:238
void Configure(const std::string &jsonFile) override
~mtsIGTLBridge(void)
Definition mtsIGTLBridge.h:149
void Send(_igtlMessagePointer message)
void SetPort(const int port)
Definition mtsIGTLBridge.h:159
SendersType mSenders
Definition mtsIGTLBridge.h:200
int mPort
Definition mtsIGTLBridge.h:195
void Startup(void) override
std::list< mtsIGTLSenderBase * > SendersType
Definition mtsIGTLBridge.h:199
bool AddReceiverToCommandWrite(const std::string &interfaceRequiredName, const std::string &commandName, const std::string &igtlDeviceName)
Definition mtsIGTLBridge.h:298
Definition mtsIGTLBridge.h:80
void EventHandler(const _cisstType &cisstData)
Definition mtsIGTLBridge.h:227
_igtlType::Pointer IGTLPointer
Definition mtsIGTLBridge.h:93
virtual ~mtsIGTLEventWriteSender()
Definition mtsIGTLBridge.h:87
IGTLPointer mIGTLData
Definition mtsIGTLBridge.h:94
mtsIGTLEventWriteSender(const std::string &name, mtsIGTLBridge *bridge)
Definition mtsIGTLBridge.h:82
bool Execute(void) override
Definition mtsIGTLBridge.h:88
virtual bool Execute(igtl::Socket *socket, igtl::MessageBase *header)=0
virtual ~mtsIGTLReceiverBase()
Definition mtsIGTLBridge.h:107
mtsIGTLReceiverBase(const std::string &name, mtsIGTLBridge *bridge)
Definition mtsIGTLBridge.h:101
mtsIGTLBridge * mBridge
Definition mtsIGTLBridge.h:113
std::string mName
Definition mtsIGTLBridge.h:112
mtsFunctionWrite Function
Function used to pull data from the cisst component.
Definition mtsIGTLBridge.h:105
Definition mtsIGTLBridge.h:118
virtual ~mtsIGTLReceiver()
Definition mtsIGTLBridge.h:123
bool Execute(igtl::Socket *socket, igtl::MessageBase *header) override
_igtlType::Pointer IGTLPointer
Definition mtsIGTLBridge.h:127
mtsIGTLReceiver(const std::string &name, mtsIGTLBridge *bridge)
Definition mtsIGTLBridge.h:120
_cisstType mCISSTData
Definition mtsIGTLBridge.h:128
Definition mtsIGTLBridge.h:45
mtsIGTLBridge * mBridge
Definition mtsIGTLBridge.h:59
std::string mName
Definition mtsIGTLBridge.h:58
virtual bool Execute(void)=0
mtsFunctionRead Function
Function used to pull data from the cisst component.
Definition mtsIGTLBridge.h:51
virtual ~mtsIGTLSenderBase()
Definition mtsIGTLBridge.h:53
mtsIGTLSenderBase(const std::string &name, mtsIGTLBridge *bridge)
Definition mtsIGTLBridge.h:47
_cisstType mCISSTData
Definition mtsIGTLBridge.h:73
_igtlType::Pointer IGTLPointer
Definition mtsIGTLBridge.h:74
virtual ~mtsIGTLSender()
Definition mtsIGTLBridge.h:69
bool Execute(void) override
Definition mtsIGTLBridge.h:208
mtsIGTLSender(const std::string &name, mtsIGTLBridge *bridge)
Definition mtsIGTLBridge.h:66
IGTLPointer mIGTLData
Definition mtsIGTLBridge.h:75
Definition mtsInterfaceRequired.h:83
mtsCommandWriteBase * AddEventHandlerWrite(void(__classType::*method)(const __argumentType &), __classType *classInstantiation, const std::string &eventName, mtsEventQueueingPolicy queueingPolicy=MTS_INTERFACE_EVENT_POLICY)
Definition mtsInterfaceRequired.h:411
bool AddFunction(const std::string &functionName, mtsFunctionVoid &function, mtsRequiredType required=MTS_REQUIRED)
Definition mtsParameterTypes.h:1622
mtsTaskPeriodic(const std::string &name, double periodicityInSeconds, bool isHardRealTime=false, unsigned int sizeStateTable=256, bool newThread=true)
const int CMN_DYNAMIC_CREATION_ONEARG
Definition cmnClassRegisterMacros.h:330
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition cmnClassRegisterMacros.h:199
#define CISST_EXPORT
Definition cmnExportMacros.h:50
#define CMN_LOG_ALLOW_DEFAULT
Definition cmnLogLoD.h:76
#define CMN_LOG_RUN_ERROR
Definition cmnLogger.h:166
#define CMN_LOG_CLASS_INIT_ERROR
Definition cmnLogger.h:113
bool mtsCISSTToIGTL(const std::string &cisstData, igtl::StringMessage::Pointer igtlData)
Declaration of mtsInterfaceRequired.
Defines a periodic task.
Definition mtsIGTLBridge.h:39