MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkIGTLClient.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found 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
21#include <igtlClientSocket.h>
22#include <mitkIGTLStatus.h>
23
25IGTLDevice(ReadFully)
26{
27}
28
32
34{
35 if (this->GetState() != Setup)
36 {
37 mitkThrowException(mitk::Exception) <<
38 "Can only try to open the connection if in setup mode. State was " << this->GetState();
39 return false;
40 }
41
42 std::string hostname = this->GetHostname();
43 int portNumber = this->GetPortNumber();
44
45 if (portNumber == -1 || hostname.size() <= 0)
46 {
47 //port number or hostname was not correct
48 MITK_WARN << "Port number or hostname was not correct";
49 return false;
50 }
51
52 //create a new client socket
53 m_Socket = igtl::ClientSocket::New();
54
55 //try to connect to the igtl server
56 int response = dynamic_cast<igtl::ClientSocket*>(m_Socket.GetPointer())->
57 ConnectToServer(hostname.c_str(), portNumber);
58
59 //check the response
60 if (response != 0)
61 {
62 MITK_ERROR << "The client could not connect to " << hostname << " port: " << portNumber;
63 return false;
64 }
65
66 // everything is initialized and connected so the communication can be started
67 this->SetState(Ready);
68
69 //inform observers about this new client
70 this->InvokeEvent(NewClientConnectionEvent());
71
72 return true;
73}
74
76{
77 //MITK_INFO << "Trying to receive message";
78 //try to receive a message, if the socket is not present anymore stop the
79 //communication
80 unsigned int status = this->ReceivePrivate(this->m_Socket);
81 if (status == IGTL_STATUS_NOT_PRESENT)
82 {
83 this->StopCommunicationWithSocket(this->m_Socket);
84 //inform observers about loosing the connection to this socket
85 this->InvokeEvent(LostConnectionEvent());
86 MITK_WARN("IGTLClient") << "Lost connection to server socket.";
87 }
88}
89
91{
92 mitk::IGTLMessage::Pointer mitkMessage;
93
94 //get the latest message from the queue
95 mitkMessage = this->m_MessageQueue->PullSendMessage();
96
97 // there is no message => return
98 if (mitkMessage.IsNull())
99 return;
100
101 if (!this->SendMessagePrivate(mitkMessage, this->m_Socket))
102 {
103 MITK_WARN("IGTLDevice") << "Could not send the message.";
104 }
105}
106
108{
109 m_StopCommunicationMutex.lock();
110 m_StopCommunication = true;
111 m_StopCommunicationMutex.unlock();
112}
113
115{
116 return this->m_Socket->GetConnected();
117}
void Receive() override
Call this method to receive a message.
void Send() override
Call this method to send a message.
void StopCommunicationWithSocket(igtl::Socket *) override
Stops the communication with the given socket.
bool OpenConnection() override
Establishes the connection between this client and the IGTL server.
unsigned int GetNumberOfConnections() override
Returns the number of connections of this device.
IGTLClient(bool ReadFully)
Interface for all OpenIGTLink Devices.
#define IGTL_STATUS_NOT_PRESENT