Medical Imaging Interaction Toolkit  2018.4.99-c4b6bb11
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 (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 #include "mitkIGTLClient.h"
14 //#include "mitkIGTTimeStamp.h"
15 //#include "mitkIGTHardwareException.h"
16 #include "igtlTrackingDataMessage.h"
17 #include <cstdio>
18 
19 #include <itksys/SystemTools.hxx>
20 #include <itkMutexLockHolder.h>
21 
22 #include <igtlClientSocket.h>
23 #include <igtl_status.h>
24 
26 
28 IGTLDevice(ReadFully)
29 {
30 }
31 
33 {
34 }
35 
37 {
38  if (this->GetState() != Setup)
39  {
41  "Can only try to open the connection if in setup mode. State was " << this->GetState();
42  return false;
43  }
44 
45  std::string hostname = this->GetHostname();
46  int portNumber = this->GetPortNumber();
47 
48  if (portNumber == -1 || hostname.size() <= 0)
49  {
50  //port number or hostname was not correct
51  MITK_WARN << "Port number or hostname was not correct";
52  return false;
53  }
54 
55  //create a new client socket
56  m_Socket = igtl::ClientSocket::New();
57 
58  //try to connect to the igtl server
59  int response = dynamic_cast<igtl::ClientSocket*>(m_Socket.GetPointer())->
60  ConnectToServer(hostname.c_str(), portNumber);
61 
62  //check the response
63  if (response != 0)
64  {
65  MITK_ERROR << "The client could not connect to " << hostname << " port: " << portNumber;
66  return false;
67  }
68 
69  // everything is initialized and connected so the communication can be started
70  this->SetState(Ready);
71 
72  //inform observers about this new client
73  this->InvokeEvent(NewClientConnectionEvent());
74 
75  return true;
76 }
77 
79 {
80  //MITK_INFO << "Trying to receive message";
81  //try to receive a message, if the socket is not present anymore stop the
82  //communication
83  unsigned int status = this->ReceivePrivate(this->m_Socket);
84  if (status == IGTL_STATUS_NOT_PRESENT)
85  {
87  //inform observers about loosing the connection to this socket
88  this->InvokeEvent(LostConnectionEvent());
89  MITK_WARN("IGTLClient") << "Lost connection to server socket.";
90  }
91 }
92 
94 {
95  mitk::IGTLMessage::Pointer mitkMessage;
96 
97  //get the latest message from the queue
98  mitkMessage = this->m_MessageQueue->PullSendMessage();
99 
100  // there is no message => return
101  if (mitkMessage.IsNull())
102  return;
103 
104  if (!this->SendMessagePrivate(mitkMessage, this->m_Socket))
105  {
106  MITK_WARN("IGTLDevice") << "Could not send the message.";
107  }
108 }
109 
110 void mitk::IGTLClient::StopCommunicationWithSocket(igtl::Socket* /*socket*/)
111 {
112  m_StopCommunicationMutex->Lock();
113  m_StopCommunication = true;
114  m_StopCommunicationMutex->Unlock();
115 }
116 
118 {
119  return this->m_Socket->GetConnected();
120 }
IGTLDeviceState GetState() const
Returns current object state (Setup, Ready or Running)
itk::FastMutexLock::Pointer m_StopCommunicationMutex
virtual int GetPortNumber()
Returns the port number of the device.
void SetState(IGTLDeviceState state)
change object state
#define MITK_ERROR
Definition: mitkLogMacros.h:20
void Send() override
Call this method to send a message.
unsigned int GetNumberOfConnections() override
Returns the number of connections of this device.
void Receive() override
Call this method to receive a message.
itk::MutexLockHolder< itk::FastMutexLock > MutexLockHolder
unsigned int ReceivePrivate(igtl::Socket *device)
Call this method to receive a message from the given device.
unsigned int SendMessagePrivate(mitk::IGTLMessage::Pointer msg, igtl::Socket::Pointer socket)
Sends a message.
#define MITK_WARN
Definition: mitkLogMacros.h:19
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
void StopCommunicationWithSocket(igtl::Socket *) override
Stops the communication with the given socket.
~IGTLClient() override
mitk::IGTLMessageQueue::Pointer m_MessageQueue
Interface for all OpenIGTLink Devices.
#define mitkThrowException(classname)
igtl::Socket::Pointer m_Socket
IGTLClient(bool ReadFully)
virtual std::string GetHostname()
Returns the ip/hostname of the device.
bool OpenConnection() override
Establishes the connection between this client and the IGTL server.