Medical Imaging Interaction Toolkit  2023.12.00
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 #if !defined(MITK_WINDOWS_NO_UNDEF) && defined(SendMessage)
36  #undef SendMessage
37 #endif
38 
39 namespace mitk {
62  class MITKOPENIGTLINK_EXPORT IGTLDevice : public itk::Object
63  {
64  public:
65  mitkClassMacroItkParent(IGTLDevice, itk::Object);
66 
67  IGTLDevice(bool ReadFully);
68 
74  enum IGTLDeviceState { Setup, Ready, Running };
75 
83  virtual bool OpenConnection() = 0;
84 
91  virtual bool CloseConnection();
92 
98  virtual bool StopCommunication();
99 
105  bool StartCommunication();
106 
116  void RunCommunication(void (IGTLDevice::*ComFunction)(void), std::mutex& mutex);
117 
118 
127  void SendMessage(mitk::IGTLMessage::Pointer msg);
128 
132  IGTLDeviceState GetState() const;
133 
138  igtl::MessageBase::Pointer GetNextCommand();
139 
144  igtl::ImageMessage::Pointer GetNextImage2dMessage();
145 
146  igtl::ImageMessage::Pointer GetNextImage3dMessage();
147 
148  igtl::TransformMessage::Pointer GetNextTransformMessage();
149 
150  igtl::TrackingDataMessage::Pointer GetNextTrackingDataMessage();
151 
152  igtl::StringMessage::Pointer GetNextStringMessage();
153 
154  igtl::MessageBase::Pointer GetNextMiscMessage();
155 
159  itkSetMacro(PortNumber, int);
160 
164  itkGetMacro(PortNumber, int);
165 
169  itkSetMacro(Hostname, std::string);
170 
174  itkGetMacro(Hostname, std::string);
175 
179  itkGetConstMacro(Name, std::string);
180 
184  itkSetMacro(Name, std::string);
185 
189  itkSetMacro(ReadFully, bool);
190 
194  itkGetConstMacro(MessageQueue, mitk::IGTLMessageQueue::Pointer);
195 
199  itkGetMacro(MessageFactory, mitk::IGTLMessageFactory::Pointer);
200 
204  void ThreadStartSending();
205 
209  void ThreadStartReceiving();
210 
214  void ThreadStartConnecting();
215 
230  virtual bool TestConnection();
231 
235  bool SendRTSMessage(const char* type);
236 
240  void EnableNoBufferingMode(mitk::IGTLMessageQueue::Pointer queue,
241  bool enable = true);
242 
243  void EnableNoBufferingMode(bool enable = true);
244 
248  virtual unsigned int GetNumberOfConnections() = 0;
249 
250  itkGetMacro(LogMessages, bool);
251  itkSetMacro(LogMessages, bool);
252 
253  protected:
268  unsigned int SendMessagePrivate(mitk::IGTLMessage::Pointer msg,
269  igtl::Socket::Pointer socket);
270 
276  virtual void Receive() = 0;
277 
292  unsigned int ReceivePrivate(igtl::Socket* device);
293 
298  virtual void Send() = 0;
299 
308  virtual void Connect();
309 
314  virtual void StopCommunicationWithSocket(igtl::Socket* socket) = 0;
315 
319  void SetState(IGTLDeviceState state);
320 
321  IGTLDevice();
322  ~IGTLDevice() override;
323 
327  std::string m_Name;
328 
340  mutable std::mutex m_StateMutex;
341 
343  std::string m_Hostname;
347  igtl::Socket::Pointer m_Socket;
348 
350  mitk::IGTLMessageQueue::Pointer m_MessageQueue;
351 
353  mitk::IGTLMessageFactory::Pointer m_MessageFactory;
354 
356 
357  private:
358 
360  std::thread m_SendThread;
362  std::thread m_ReceiveThread;
364  std::thread m_ConnectThread;
366  bool m_ReadFully;
367  };
368 
375  itkEventMacroDeclaration(MessageSentEvent, itk::AnyEvent);
376 
383  itkEventMacroDeclaration(MessageReceivedEvent, itk::AnyEvent);
384 
391  itkEventMacroDeclaration(CommandReceivedEvent, itk::AnyEvent);
392 
400  itkEventMacroDeclaration(NewClientConnectionEvent, itk::AnyEvent);
401 
409  itkEventMacroDeclaration(LostConnectionEvent, itk::AnyEvent);
410 } // namespace mitk
411 
412 #endif
mitk::IGTLDevice::m_StopCommunicationMutex
std::mutex m_StopCommunicationMutex
Definition: mitkIGTLDevice.h:332
mitkIGTLMessageFactory.h
mitk::IGTLDevice::Setup
@ Setup
Definition: mitkIGTLDevice.h:74
mitk::IGTLDevice::m_ReceivingFinishedMutex
std::mutex m_ReceivingFinishedMutex
Definition: mitkIGTLDevice.h:336
mitk::IGTLDevice::m_StopCommunication
bool m_StopCommunication
Definition: mitkIGTLDevice.h:330
mitkIGTLMessageQueue.h
mitk::IGTLDevice::m_MessageFactory
mitk::IGTLMessageFactory::Pointer m_MessageFactory
Definition: mitkIGTLDevice.h:353
mitkClassMacroItkParent
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:45
mitk::IGTLDevice
Interface for all OpenIGTLink Devices.
Definition: mitkIGTLDevice.h:62
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::IGTLDevice::m_State
IGTLDeviceState m_State
Definition: mitkIGTLDevice.h:325
mitk::IGTLDevice::m_StateMutex
std::mutex m_StateMutex
Definition: mitkIGTLDevice.h:340
mitk::IGTLDevice::m_PortNumber
int m_PortNumber
Definition: mitkIGTLDevice.h:345
mitk::IGTLDevice::m_Socket
igtl::Socket::Pointer m_Socket
Definition: mitkIGTLDevice.h:347
mitk::IGTLDevice::m_SendingFinishedMutex
std::mutex m_SendingFinishedMutex
Definition: mitkIGTLDevice.h:334
mitk::IGTLDevice::m_Hostname
std::string m_Hostname
Definition: mitkIGTLDevice.h:343
mitkCommon.h
mitk::IGTLDevice::m_ConnectingFinishedMutex
std::mutex m_ConnectingFinishedMutex
Definition: mitkIGTLDevice.h:338
mitk::IGTLDevice::m_LogMessages
bool m_LogMessages
Definition: mitkIGTLDevice.h:355
mitk::IGTLDevice::IGTLDeviceState
IGTLDeviceState
Type for state variable. The IGTLDevice is always in one of these states.
Definition: mitkIGTLDevice.h:74
mitk::IGTLDevice::m_MessageQueue
mitk::IGTLMessageQueue::Pointer m_MessageQueue
Definition: mitkIGTLDevice.h:350
mitkIGTLMessage.h
mitk::IGTLDevice::m_Name
std::string m_Name
Definition: mitkIGTLDevice.h:327
mitk::itkEventMacroDeclaration
itkEventMacroDeclaration(BoundingShapeInteractionEvent, itk::AnyEvent)