Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkIGTLClient.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkIGTLClient.h"
18 //#include "mitkIGTTimeStamp.h"
19 //#include "mitkIGTHardwareException.h"
20 #include "igtlTrackingDataMessage.h"
21 #include <stdio.h>
22 
23 #include <itksys/SystemTools.hxx>
24 #include <itkMutexLockHolder.h>
25 
26 #include <igtlClientSocket.h>
27 #include <igtl_status.h>
28 
30 
32 IGTLDevice(ReadFully)
33 {
34 }
35 
37 {
38 }
39 
41 {
42  if (this->GetState() != Setup)
43  {
45  "Can only try to open the connection if in setup mode. State was " << this->GetState();
46  return false;
47  }
48 
49  std::string hostname = this->GetHostname();
50  int portNumber = this->GetPortNumber();
51 
52  if (portNumber == -1 || hostname.size() <= 0)
53  {
54  //port number or hostname was not correct
55  MITK_WARN << "Port number or hostname was not correct";
56  return false;
57  }
58 
59  //create a new client socket
60  m_Socket = igtl::ClientSocket::New();
61 
62  //try to connect to the igtl server
63  int response = dynamic_cast<igtl::ClientSocket*>(m_Socket.GetPointer())->
64  ConnectToServer(hostname.c_str(), portNumber);
65 
66  //check the response
67  if (response != 0)
68  {
69  MITK_ERROR << "The client could not connect to " << hostname << " port: " << portNumber;
70  return false;
71  }
72 
73  // everything is initialized and connected so the communication can be started
74  this->SetState(Ready);
75 
76  //inform observers about this new client
77  this->InvokeEvent(NewClientConnectionEvent());
78 
79  return true;
80 }
81 
83 {
84  MITK_INFO << "Trying to receive message";
85  //try to receive a message, if the socket is not present anymore stop the
86  //communication
87  unsigned int status = this->ReceivePrivate(this->m_Socket);
88  if (status == IGTL_STATUS_NOT_PRESENT)
89  {
90  this->StopCommunicationWithSocket(this->m_Socket);
91  //inform observers about loosing the connection to this socket
92  this->InvokeEvent(LostConnectionEvent());
93  MITK_WARN("IGTLClient") << "Lost connection to server socket.";
94  }
95 }
96 
98 {
99  igtl::MessageBase::Pointer curMessage;
100 
101  //get the latest message from the queue
102  curMessage = this->m_MessageQueue->PullSendMessage();
103 
104  // there is no message => return
105  if (curMessage.IsNull())
106  return;
107 
108  if (!this->SendMessagePrivate(curMessage.GetPointer(), this->m_Socket))
109  {
110  MITK_WARN("IGTLDevice") << "Could not send the message.";
111  }
112 }
113 
114 void mitk::IGTLClient::StopCommunicationWithSocket(igtl::Socket* /*socket*/)
115 {
116  m_StopCommunicationMutex->Lock();
117  m_StopCommunication = true;
118  m_StopCommunicationMutex->Unlock();
119 }
120 
122 {
123  return this->m_Socket->GetConnected();
124 }
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
virtual void Send() override
Call this method to send a message.
virtual unsigned int GetNumberOfConnections() override
Returns the number of connections of this device.
virtual void Receive() override
Call this method to receive a message.
itk::MutexLockHolder< itk::FastMutexLock > MutexLockHolder
#define MITK_WARN
Definition: mitkLogMacros.h:23
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
virtual void StopCommunicationWithSocket(igtl::Socket *) override
Stops the communication with the given socket.
Interface for all OpenIGTLink Devices.
#define mitkThrowException(classname)
IGTLClient(bool ReadFully)
static void Setup()
virtual bool OpenConnection() override
Establishes the connection between this client and the IGTL server.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.