PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusOpenIGTLinkServer.h
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2  Program: Plus
3  Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4  See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #ifndef __VTKPLUSOPENIGTLINKSERVER_H
8 #define __VTKPLUSOPENIGTLINKSERVER_H
9 
10 // Local includes
11 #include "vtkPlusServerExport.h"
12 #include "PlusIgtlClientInfo.h"
13 #include "vtkPlusDataCollector.h"
15 #include "vtkIGSIOTransformRepository.h"
16 
17 // VTK includes
18 #include <vtkMultiThreader.h>
19 #include <vtkObject.h>
20 #include <vtkSmartPointer.h>
21 
22 // STL includes
23 #include <deque>
24 
25 // OS includes
26 #if (_MSC_VER == 1500)
27  #include <stdint.h>
28 #endif
29 
30 // IGTL includes
31 #include <igtlMessageBase.h>
32 #include <igtlServerSocket.h>
33 
34 //class igsioTrackedFrame;
37 class vtkPlusChannel;
40 class vtkIGSIORecursiveCriticalSection;
41 //class vtkIGSIOTransformRepository;
42 
43 struct ClientData
44 {
46  : ClientId(-1)
47  , ClientSocket(NULL)
48  , DataReceiverActive(std::make_pair(false, false))
50  , Server(NULL)
51  {
52  }
53 
55  int ClientId;
56 
58  igtl::ClientSocket::Pointer ClientSocket;
59 
63 
65  std::pair<bool, bool> DataReceiverActive;
67 
69 
71 };
72 
88 class vtkPlusServerExport vtkPlusOpenIGTLinkServer: public vtkObject
89 {
90  typedef std::map<int, std::vector<igtl::MessageBase::Pointer> > ClientIdToMessageListMap;
91 
92 public:
93  static vtkPlusOpenIGTLinkServer* New();
94  vtkTypeMacro(vtkPlusOpenIGTLinkServer, vtkObject);
95  virtual void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
96 
98  PlusStatus Start(vtkPlusDataCollector* dataCollector, vtkIGSIOTransformRepository* transformRepository, vtkXMLDataElement* serverElement, const std::string& configFilePath);
99 
101  PlusStatus Stop();
102 
103 
105  virtual PlusStatus ReadConfiguration(vtkXMLDataElement* serverElement, const std::string& aFilename);
106 
108  vtkSetMacro(ListeningPort, int);
110  vtkGetMacroConst(ListeningPort, int);
111 
112  vtkGetStdStringMacro(OutputChannelId);
113 
114  vtkSetMacro(MissingInputGracePeriodSec, double);
115  vtkGetMacroConst(MissingInputGracePeriodSec, double);
116 
117  vtkSetMacro(MaxTimeSpentWithProcessingMs, double);
118  vtkGetMacroConst(MaxTimeSpentWithProcessingMs, double);
119 
120  vtkSetMacro(SendValidTransformsOnly, bool);
121  vtkGetMacroConst(SendValidTransformsOnly, bool);
122 
123  vtkSetMacro(DefaultClientSendTimeoutSec, float);
124  vtkGetMacroConst(DefaultClientSendTimeoutSec, float);
125 
126  vtkSetMacro(DefaultClientReceiveTimeoutSec, float);
127  vtkGetMacroConst(DefaultClientReceiveTimeoutSec, float);
128 
130  vtkSetMacro(DataCollector, vtkPlusDataCollector*);
131  vtkGetMacroConst(DataCollector, vtkPlusDataCollector*);
132 
134  vtkSetMacro(TransformRepository, vtkIGSIOTransformRepository*);
135  vtkGetMacroConst(TransformRepository, vtkIGSIOTransformRepository*);
136 
138  virtual unsigned int GetNumberOfConnectedClients() const;
139 
143  virtual PlusStatus GetClientInfo(unsigned int clientId, PlusIgtlClientInfo& outClientInfo) const;
144 
146  PlusStatus StartOpenIGTLinkService();
147 
149  PlusStatus StopOpenIGTLinkService();
150 
151  vtkGetStdStringMacro(ConfigFilename);
152 
153  vtkGetMacro(IGTLProtocolVersion, int);
154  vtkGetMacro(IGTLHeaderVersion, int);
155 
160  int ProcessPendingCommands();
161 
162 protected:
164  virtual ~vtkPlusOpenIGTLinkServer();
165 
167  PlusStatus QueueMessageResponseForClient(int clientId, igtl::MessageBase::Pointer message);
168 
170  static void* ConnectionReceiverThread(vtkMultiThreader::ThreadInfo* data);
171 
173  static void* DataSenderThread(vtkMultiThreader::ThreadInfo* data);
174 
176  static PlusStatus SendLatestFramesToClients(vtkPlusOpenIGTLinkServer& self, double& elapsedTimeSinceLastPacketSentSec);
177 
179  static PlusStatus SendMessageResponses(vtkPlusOpenIGTLinkServer& self);
180 
182  static PlusStatus SendCommandResponses(vtkPlusOpenIGTLinkServer& self);
183 
185  static void* DataReceiverThread(vtkMultiThreader::ThreadInfo* data);
186 
188  virtual PlusStatus SendTrackedFrame(igsioTrackedFrame& trackedFrame);
189 
191  igtl::MessageBase::Pointer CreateIgtlMessageFromCommandResponse(vtkPlusCommandResponse* response);
192 
194  virtual void KeepAlive();
195 
197  void DisconnectClient(int clientId);
198 
200  vtkSetMacro(IgtlMessageCrcCheckEnabled, bool);
202  vtkGetMacroConst(IgtlMessageCrcCheckEnabled, bool);
203 
204  vtkSetMacro(LogWarningOnNoDataAvailable, bool);
205  vtkGetMacroConst(LogWarningOnNoDataAvailable, bool);
206 
207  vtkSetMacro(MaxNumberOfIgtlMessagesToSend, int);
208  vtkGetMacroConst(MaxNumberOfIgtlMessagesToSend, int);
209 
210  vtkSetMacro(NumberOfRetryAttempts, int);
211  vtkGetMacroConst(NumberOfRetryAttempts, int);
212 
213  vtkSetMacro(DelayBetweenRetryAttemptsSec, double);
214  vtkGetMacroConst(DelayBetweenRetryAttemptsSec, double);
215 
216  vtkSetMacro(KeepAliveIntervalSec, double);
217  vtkGetMacroConst(KeepAliveIntervalSec, double);
218 
219  vtkSetStdStringMacro(OutputChannelId);
220  vtkSetStdStringMacro(ConfigFilename);
221 
222  bool HasGracePeriodExpired();
223 
224 private:
226  void operator=(const vtkPlusOpenIGTLinkServer&);
227 
229  igtl::ServerSocket::Pointer ServerSocket;
230 
232  vtkSmartPointer<vtkIGSIOTransformRepository> TransformRepository;
233 
235  vtkSmartPointer<vtkPlusDataCollector> DataCollector;
236 
238  vtkSmartPointer<vtkMultiThreader> Threader;
239 
241  int IGTLProtocolVersion;
242  int IGTLHeaderVersion;
243 
245  int ListeningPort;
246 
248  int NumberOfRetryAttempts;
249 
251  double DelayBetweenRetryAttemptsSec;
252 
254  int MaxNumberOfIgtlMessagesToSend;
255 
256  // Active flag for threads (request, respond )
257  struct ThreadFlags
258  {
259  bool Request;
260  bool Respond;
261  ThreadFlags()
262  : Request(false)
263  , Respond(false)
264  {}
265  };
266  ThreadFlags ConnectionActive;
267  ThreadFlags DataSenderActive;
268 
269  // Thread IDs
270  int ConnectionReceiverThreadId;
271  int DataSenderThreadId;
272 
274  std::list<ClientData> IgtlClients;
275 
277  vtkSmartPointer<vtkPlusIgtlMessageFactory> IgtlMessageFactory;
278 
280  vtkSmartPointer<vtkIGSIORecursiveCriticalSection> IgtlClientsMutex;
281 
283  double LastSentTrackedFrameTimestamp;
284 
286  int MaxTimeSpentWithProcessingMs;
287 
289  int LastProcessingTimePerFrameMs;
290 
292  bool SendValidTransformsOnly;
293 
299  PlusIgtlClientInfo DefaultClientInfo;
300  float DefaultClientSendTimeoutSec;
301  float DefaultClientReceiveTimeoutSec;
302 
304  bool IgtlMessageCrcCheckEnabled;
305 
307  vtkSmartPointer<vtkPlusCommandProcessor> PlusCommandProcessor;
308 
310  ClientIdToMessageListMap MessageResponseQueue;
311 
313  vtkSmartPointer<vtkIGSIORecursiveCriticalSection> MessageResponseQueueMutex;
314 
316  std::string OutputChannelId;
317 
319  vtkPlusChannel* BroadcastChannel;
320 
321  bool LogWarningOnNoDataAvailable;
322 
323  double KeepAliveIntervalSec;
324 
325  std::string ConfigFilename;
326 
327  vtkPlusLogger::LogLevelType GracePeriodLogLevel;
328  double MissingInputGracePeriodSec;
329  double BroadcastStartTime;
330 
332  static int ClientIdCounter;
333 
334  static const float CLIENT_SOCKET_TIMEOUT_SEC;
335 
336  bool NewClientConnected;
337 };
338 
339 #endif
const uint32_t * data
Definition: phidget22.h:3971
uint32_t ClientSocketReceiveTimeout
igsioStatus PlusStatus
Definition: PlusCommon.h:40
PlusIgtlClientInfo ClientInfo
Creates a PlusCommand from a string. If the commands are to be executed on the main thread then call ...
vtkPlusOpenIGTLinkServer * Server
std::pair< bool, bool > DataReceiverActive
Active flag for thread (first: request, second: respond )
int ClientId
Unique client identifier. First valid value is 1.
#define VTK_OVERRIDE
Definition: PlusCommon.h:49
uint32_t ClientSocketSendTimeout
Client specific timeouts.
Manages devices that record image or positional data.
igtl::ClientSocket::Pointer ClientSocket
IGTL client socket instance.
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
const char * message
Definition: phidget22.h:2457
This class provides a network interface for data acquired by Plus as an OpenIGTLink server.
This class provides client information for vtkPlusOpenIGTLinkServer.