16#include <itksys/SystemTools.hxx>
18#include <igtlServerSocket.h>
19#include <igtlTrackingDataMessage.h>
20#include <igtlImageMessage.h>
34 if (this->GetState() != Setup)
36 mitkThrowException(mitk::Exception) <<
37 "Can only try to create a server if in setup mode";
41 int portNumber = this->GetPortNumber();
50 m_Socket = igtl::ServerSocket::New();
53 int response =
dynamic_cast<igtl::ServerSocket*
>(m_Socket.GetPointer())->
54 CreateServer(portNumber);
59 mitkThrowException(mitk::Exception) <<
60 "The server could not be created. Port: " << portNumber;
65 this->SetState(Ready);
73 m_SentListMutex.lock();
74 m_ReceiveListMutex.lock();
76 m_SentListMutex.unlock();
77 m_ReceiveListMutex.unlock();
78 this->StopCommunicationWithSocket(allRegisteredSockets);
85 igtl::Socket::Pointer socket;
88 ((igtl::ServerSocket*)(this->m_Socket.GetPointer()))->WaitForConnection(1);
90 if (socket.IsNotNull())
93 m_SentListMutex.lock();
94 m_ReceiveListMutex.lock();
95 this->m_RegisteredClients.push_back(socket);
96 m_SentListMutex.unlock();
97 m_ReceiveListMutex.unlock();
99 this->InvokeEvent(NewClientConnectionEvent());
100 MITK_INFO(
"IGTLServer") <<
"Connected to a new client: " << socket;
112 m_ReceiveListMutex.lock();
113 auto it_end = this->m_RegisteredClients.end();
114 for (it = this->m_RegisteredClients.begin(); it != it_end; ++it)
119 status = this->ReceivePrivate(*it);
124 socketsToBeRemoved.push_back(*it);
125 MITK_WARN(
"IGTLServer") <<
"Lost connection to a client socket. ";
127 else if (status != 1)
129 MITK_DEBUG(
"IGTLServer") <<
"IGTL Message with status: " << status;
132 m_ReceiveListMutex.unlock();
133 if (socketsToBeRemoved.size() > 0)
136 this->StopCommunicationWithSocket(socketsToBeRemoved);
138 this->InvokeEvent(LostConnectionEvent());
145 mitk::IGTLMessage::Pointer curMessage = this->m_MessageQueue->PullSendMessage();
148 if (curMessage.IsNull())
158 m_SentListMutex.lock();
161 this->m_RegisteredClients.end();
162 for (it = this->m_RegisteredClients.begin(); it != it_end; ++it)
165 this->SendMessagePrivate(curMessage, *it);
166 MITK_DEBUG(
"IGTLServer") <<
"Sent IGTL Message";
168 m_SentListMutex.unlock();
174 for (
auto i = toBeRemovedSockets.begin(); i != toBeRemovedSockets.end(); i++)
175 this->StopCommunicationWithSocket(*i);
180 m_SentListMutex.lock();
181 m_ReceiveListMutex.lock();
182 auto i = m_RegisteredClients.begin();
183 auto end = m_RegisteredClients.end();
191 i = this->m_RegisteredClients.erase(i);
192 MITK_INFO(
"IGTLServer") <<
"Removed client socket from server client list.";
200 m_SentListMutex.unlock();
201 m_ReceiveListMutex.unlock();
206 return this->m_RegisteredClients.size();
Interface for all OpenIGTLink Devices.
virtual bool CloseConnection()
Closes the connection to the device.
virtual void StopCommunicationWithSocket(SocketListType &toBeRemovedSockets)
Stops the communication with the given sockets.
unsigned int GetNumberOfConnections() override
Returns the number of client connections of this device.
bool CloseConnection() override
Closes the connection to the device.
bool OpenConnection() override
Initialize the connection for the IGTLServer.
void Send() override
Call this method to send a message. The message will be read from the queue. So far the message is se...
void Receive() override
Call this method to receive a message.
std::list< igtl::Socket::Pointer > SocketListType
IGTLServer(bool ReadFully)
SocketListType::iterator SocketListIteratorType
void Connect() override
Call this method to check for other devices that want to connect to this one.
#define IGTL_STATUS_NOT_PRESENT