Medical Imaging Interaction Toolkit  2022.04.99-b2814023
Medical Imaging Interaction Toolkit
mitkIGTLDevice.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef MITKIGTLDEVICE_H
14 #define MITKIGTLDEVICE_H
15 
16 #include <mutex>
17 #include <thread>
18 
19 #include "mitkCommon.h"
20 
21 //itk
22 #include "itkObject.h"
23 
24 //igtl
25 #include "igtlSocket.h"
26 #include "igtlMessageBase.h"
27 #include "igtlTransformMessage.h"
28 
29 //mitkIGTL
30 #include "MitkOpenIGTLinkExports.h"
31 #include "mitkIGTLMessageFactory.h"
32 #include "mitkIGTLMessageQueue.h"
33 #include "mitkIGTLMessage.h"
34 
35 namespace mitk {
58  class MITKOPENIGTLINK_EXPORT IGTLDevice : public itk::Object
59  {
60  public:
61  mitkClassMacroItkParent(IGTLDevice, itk::Object);
62 
63  IGTLDevice(bool ReadFully);
64 
70  enum IGTLDeviceState { Setup, Ready, Running };
71 
79  virtual bool OpenConnection() = 0;
80 
87  virtual bool CloseConnection();
88 
94  virtual bool StopCommunication();
95 
101  bool StartCommunication();
102 
112  void RunCommunication(void (IGTLDevice::*ComFunction)(void), std::mutex& mutex);
113 
114 
123  void SendMessage(mitk::IGTLMessage::Pointer msg);
124 
128  IGTLDeviceState GetState() const;
129 
134  igtl::MessageBase::Pointer GetNextCommand();
135 
140  igtl::ImageMessage::Pointer GetNextImage2dMessage();
141 
142  igtl::ImageMessage::Pointer GetNextImage3dMessage();
143 
144  igtl::TransformMessage::Pointer GetNextTransformMessage();
145 
146  igtl::TrackingDataMessage::Pointer GetNextTrackingDataMessage();
147 
148  igtl::StringMessage::Pointer GetNextStringMessage();
149 
150  igtl::MessageBase::Pointer GetNextMiscMessage();
151 
155  itkSetMacro(PortNumber, int);
156 
160  itkGetMacro(PortNumber, int);
161 
165  itkSetMacro(Hostname, std::string);
166 
170  itkGetMacro(Hostname, std::string);
171 
175  itkGetConstMacro(Name, std::string);
176 
180  itkSetMacro(Name, std::string);
181 
185  itkSetMacro(ReadFully, bool);
186 
190  itkGetConstMacro(MessageQueue, mitk::IGTLMessageQueue::Pointer);
191 
195  itkGetMacro(MessageFactory, mitk::IGTLMessageFactory::Pointer);
196 
200  void ThreadStartSending();
201 
205  void ThreadStartReceiving();
206 
210  void ThreadStartConnecting();
211 
226  virtual bool TestConnection();
227 
231  bool SendRTSMessage(const char* type);
232 
236  void EnableNoBufferingMode(mitk::IGTLMessageQueue::Pointer queue,
237  bool enable = true);
238 
239  void EnableNoBufferingMode(bool enable = true);
240 
244  virtual unsigned int GetNumberOfConnections() = 0;
245 
246  itkGetMacro(LogMessages, bool);
247  itkSetMacro(LogMessages, bool);
248 
249  protected:
264  unsigned int SendMessagePrivate(mitk::IGTLMessage::Pointer msg,
265  igtl::Socket::Pointer socket);
266 
272  virtual void Receive() = 0;
273 
288  unsigned int ReceivePrivate(igtl::Socket* device);
289 
294  virtual void Send() = 0;
295 
304  virtual void Connect();
305 
310  virtual void StopCommunicationWithSocket(igtl::Socket* socket) = 0;
311 
315  void SetState(IGTLDeviceState state);
316 
317  IGTLDevice();
318  ~IGTLDevice() override;
319 
323  std::string m_Name;
324 
336  mutable std::mutex m_StateMutex;
337 
339  std::string m_Hostname;
343  igtl::Socket::Pointer m_Socket;
344 
346  mitk::IGTLMessageQueue::Pointer m_MessageQueue;
347 
349  mitk::IGTLMessageFactory::Pointer m_MessageFactory;
350 
352 
353  private:
354 
356  std::thread m_SendThread;
358  std::thread m_ReceiveThread;
360  std::thread m_ConnectThread;
362  bool m_ReadFully;
363  };
364 
371  itkEventMacroDeclaration(MessageSentEvent, itk::AnyEvent);
372 
379  itkEventMacroDeclaration(MessageReceivedEvent, itk::AnyEvent);
380 
387  itkEventMacroDeclaration(CommandReceivedEvent, itk::AnyEvent);
388 
396  itkEventMacroDeclaration(NewClientConnectionEvent, itk::AnyEvent);
397 
405  itkEventMacroDeclaration(LostConnectionEvent, itk::AnyEvent);
406 } // namespace mitk
407 
408 #endif /* MITKIGTLDEVICE_H */
mitk::IGTLMessageFactory::Pointer m_MessageFactory
IGTLDeviceState m_State
std::string m_Name
std::mutex m_StateMutex
DataCollection - Class to facilitate loading/accessing structured data.
#define MITKOPENIGTLINK_EXPORT
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:45
itkEventMacroDeclaration(BoundingShapeInteractionEvent, itk::AnyEvent)
std::mutex m_SendingFinishedMutex
std::string m_Hostname
std::mutex m_StopCommunicationMutex
IGTLDeviceState
Type for state variable. The IGTLDevice is always in one of these states.
mitk::IGTLMessageQueue::Pointer m_MessageQueue
Interface for all OpenIGTLink Devices.
std::mutex m_ConnectingFinishedMutex
igtl::Socket::Pointer m_Socket
std::mutex m_ReceivingFinishedMutex