Medical Imaging Interaction Toolkit  2018.4.99-6ca56567
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 "mitkCommon.h"
17 
18 //itk
19 #include "itkObject.h"
20 #include "itkFastMutexLock.h"
21 #include "itkMultiThreader.h"
22 
23 //igtl
24 #include "igtlSocket.h"
25 #include "igtlMessageBase.h"
26 #include "igtlTransformMessage.h"
27 
28 //mitkIGTL
29 #include "MitkOpenIGTLinkExports.h"
30 #include "mitkIGTLMessageFactory.h"
31 #include "mitkIGTLMessageQueue.h"
32 #include "mitkIGTLMessage.h"
33 
34 namespace mitk {
57  class MITKOPENIGTLINK_EXPORT IGTLDevice : public itk::Object
58  {
59  public:
60  mitkClassMacroItkParent(IGTLDevice, itk::Object);
61 
62  IGTLDevice(bool ReadFully);
63 
69  enum IGTLDeviceState { Setup, Ready, Running };
70 
78  virtual bool OpenConnection() = 0;
79 
86  virtual bool CloseConnection();
87 
93  virtual bool StopCommunication();
94 
100  bool StartCommunication();
101 
111  void RunCommunication(void (IGTLDevice::*ComFunction)(void), itk::FastMutexLock* mutex);
112 
113 
122  void SendMessage(mitk::IGTLMessage::Pointer msg);
123 
127  IGTLDeviceState GetState() const;
128 
133  igtl::MessageBase::Pointer GetNextCommand();
134 
139  igtl::ImageMessage::Pointer GetNextImage2dMessage();
140 
141  igtl::ImageMessage::Pointer GetNextImage3dMessage();
142 
143  igtl::TransformMessage::Pointer GetNextTransformMessage();
144 
145  igtl::TrackingDataMessage::Pointer GetNextTrackingDataMessage();
146 
147  igtl::StringMessage::Pointer GetNextStringMessage();
148 
149  igtl::MessageBase::Pointer GetNextMiscMessage();
150 
154  itkSetMacro(PortNumber, int);
155 
159  itkGetMacro(PortNumber, int);
160 
164  itkSetMacro(Hostname, std::string);
165 
169  itkGetMacro(Hostname, std::string);
170 
174  itkGetConstMacro(Name, std::string);
175 
179  itkSetMacro(Name, std::string);
180 
184  itkSetMacro(ReadFully, bool);
185 
189  itkGetConstMacro(MessageQueue, mitk::IGTLMessageQueue::Pointer);
190 
194  itkGetMacro(MessageFactory, mitk::IGTLMessageFactory::Pointer);
195 
200  static ITK_THREAD_RETURN_TYPE ThreadStartSending(void* data);
201 
206  static ITK_THREAD_RETURN_TYPE ThreadStartReceiving(void* data);
207 
212  static ITK_THREAD_RETURN_TYPE ThreadStartConnecting(void* data);
213 
228  virtual bool TestConnection();
229 
233  bool SendRTSMessage(const char* type);
234 
238  void EnableNoBufferingMode(mitk::IGTLMessageQueue::Pointer queue,
239  bool enable = true);
240 
241  void EnableNoBufferingMode(bool enable = true);
242 
246  virtual unsigned int GetNumberOfConnections() = 0;
247 
248  itkGetMacro(LogMessages, bool);
249  itkSetMacro(LogMessages, bool);
250 
251  protected:
266  unsigned int SendMessagePrivate(mitk::IGTLMessage::Pointer msg,
267  igtl::Socket::Pointer socket);
268 
274  virtual void Receive() = 0;
275 
290  unsigned int ReceivePrivate(igtl::Socket* device);
291 
296  virtual void Send() = 0;
297 
306  virtual void Connect();
307 
312  virtual void StopCommunicationWithSocket(igtl::Socket* socket) = 0;
313 
317  void SetState(IGTLDeviceState state);
318 
319  IGTLDevice();
320  ~IGTLDevice() override;
321 
325  std::string m_Name;
326 
330  itk::FastMutexLock::Pointer m_StopCommunicationMutex;
332  itk::FastMutexLock::Pointer m_SendingFinishedMutex;
334  itk::FastMutexLock::Pointer m_ReceivingFinishedMutex;
336  itk::FastMutexLock::Pointer m_ConnectingFinishedMutex;
338  itk::FastMutexLock::Pointer m_StateMutex;
339 
341  std::string m_Hostname;
345  igtl::Socket::Pointer m_Socket;
346 
348  mitk::IGTLMessageQueue::Pointer m_MessageQueue;
349 
351  mitk::IGTLMessageFactory::Pointer m_MessageFactory;
352 
354 
355  private:
356 
359  itk::MultiThreader::Pointer m_MultiThreader;
361  int m_SendThreadID;
363  int m_ReceiveThreadID;
365  int m_ConnectThreadID;
367  bool m_ReadFully;
368  };
369 
376  itkEventMacro(MessageSentEvent, itk::AnyEvent);
377 
384  itkEventMacro(MessageReceivedEvent, itk::AnyEvent);
385 
392  itkEventMacro(CommandReceivedEvent, itk::AnyEvent);
393 
401  itkEventMacro(NewClientConnectionEvent, itk::AnyEvent);
402 
410  itkEventMacro(LostConnectionEvent, itk::AnyEvent);
411 } // namespace mitk
412 
413 #endif /* MITKIGTLDEVICE_H */
itk::FastMutexLock::Pointer m_ReceivingFinishedMutex
itk::FastMutexLock::Pointer m_StopCommunicationMutex
mitk::IGTLMessageFactory::Pointer m_MessageFactory
IGTLDeviceState m_State
itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent)
std::string m_Name
itk::FastMutexLock::Pointer m_StateMutex
void LogMessages(unsigned int threadID, unsigned int numberOfTimes)
itk::FastMutexLock::Pointer m_ConnectingFinishedMutex
DataCollection - Class to facilitate loading/accessing structured data.
#define MITKOPENIGTLINK_EXPORT
#define mitkClassMacroItkParent(className, SuperClassName)
Definition: mitkCommon.h:49
std::string m_Hostname
IGTLDeviceState
Type for state variable. The IGTLDevice is always in one of these states.
mitk::IGTLMessageQueue::Pointer m_MessageQueue
itk::FastMutexLock::Pointer m_SendingFinishedMutex
Interface for all OpenIGTLink Devices.
igtl::Socket::Pointer m_Socket