20 #include <itksys/SystemTools.hxx>
21 #include <itkMutexLockHolder.h>
23 #include <igtlServerSocket.h>
24 #include <igtlTrackingDataMessage.h>
25 #include <igtlImageMessage.h>
26 #include <igtl_status.h>
37 m_ReceiveListMutex =
nullptr;
38 m_SentListMutex =
nullptr;
43 if (this->GetState() !=
Setup)
46 "Can only try to create a server if in setup mode";
50 int portNumber = this->GetPortNumber();
62 int response =
dynamic_cast<igtl::ServerSocket*
>(m_Socket.GetPointer())->
63 CreateServer(portNumber);
69 "The server could not be created. Port: " << portNumber;
74 this->SetState(Ready);
82 m_SentListMutex->Lock();
83 m_ReceiveListMutex->Lock();
85 m_SentListMutex->Unlock();
86 m_ReceiveListMutex->Unlock();
87 this->StopCommunicationWithSocket(allRegisteredSockets);
97 ((igtl::ServerSocket*)(this->m_Socket.GetPointer()))->WaitForConnection(1);
99 if (socket.IsNotNull())
102 m_SentListMutex->Lock();
103 m_ReceiveListMutex->Lock();
104 this->m_RegisteredClients.push_back(socket);
105 m_SentListMutex->Unlock();
106 m_ReceiveListMutex->Unlock();
108 this->InvokeEvent(NewClientConnectionEvent());
109 MITK_INFO(
"IGTLServer") <<
"Connected to a new client: " << socket;
115 unsigned int status = IGTL_STATUS_OK;
121 m_ReceiveListMutex->Lock();
122 auto it_end = this->m_RegisteredClients.end();
123 for (it = this->m_RegisteredClients.begin(); it != it_end; ++it)
128 status = this->ReceivePrivate(*it);
129 if (status == IGTL_STATUS_NOT_PRESENT)
133 socketsToBeRemoved.push_back(*it);
134 MITK_WARN(
"IGTLServer") <<
"Lost connection to a client socket. ";
136 else if (status != 1)
138 MITK_WARN(
"IGTLServer") <<
"IGTL Message with status: " << status;
141 m_ReceiveListMutex->Unlock();
142 if (socketsToBeRemoved.size() > 0)
145 this->StopCommunicationWithSocket(socketsToBeRemoved);
147 this->InvokeEvent(LostConnectionEvent());
156 curMessage = this->m_MessageQueue->PullSendMessage();
159 if (curMessage.IsNull())
169 m_SentListMutex->Lock();
172 this->m_RegisteredClients.end();
173 for (it = this->m_RegisteredClients.begin(); it != it_end; ++it)
176 this->SendMessagePrivate(curMessage.GetPointer(), *it);
177 MITK_DEBUG(
"IGTLServer") <<
"Sent IGTL Message";
179 m_SentListMutex->Unlock();
185 for (
auto i = toBeRemovedSockets.begin(); i != toBeRemovedSockets.end(); i++)
186 this->StopCommunicationWithSocket(*i);
191 m_SentListMutex->Lock();
192 m_ReceiveListMutex->Lock();
193 auto i = m_RegisteredClients.begin();
194 auto end = m_RegisteredClients.end();
202 i = this->m_RegisteredClients.erase(i);
203 MITK_INFO(
"IGTLServer") <<
"Removed client socket from server client list.";
211 m_SentListMutex->Unlock();
212 m_ReceiveListMutex->Unlock();
217 return this->m_RegisteredClients.size();
virtual void Send() override
Call this method to send a message. The message will be read from the queue. So far the message is se...
std::list< igtl::Socket::Pointer > SocketListType
itk::SmartPointer< Self > Pointer
virtual void Connect() override
Call this method to check for other devices that want to connect to this one.
virtual bool OpenConnection() override
Initialize the connection for the IGTLServer.
itk::FastMutexLock::Pointer m_ReceiveListMutex
IGTLServer(bool ReadFully)
SocketListType::iterator SocketListIteratorType
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
virtual bool CloseConnection()
Closes the connection to the device.
virtual bool CloseConnection() override
Closes the connection to the device.
virtual void Receive() override
Call this method to receive a message.
itk::FastMutexLock::Pointer m_SentListMutex
Interface for all OpenIGTLink Devices.
#define mitkThrowException(classname)
virtual void StopCommunicationWithSocket(SocketListType &toBeRemovedSockets)
Stops the communication with the given sockets.
virtual unsigned int GetNumberOfConnections() override
Returns the number of client connections of this device.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.