61 std::this_thread::sleep_for(std::chrono::milliseconds(20));
62 m_Message =
"This is a test status message";
63 m_MessageFactory = mitk::IGTLMessageFactory::New();
64 m_Server = mitk::IGTLServer::New(
true);
65 m_Client_One = mitk::IGTLClient::New(
true);
66 m_Client_Two = mitk::IGTLClient::New(
true);
68 m_Server->SetObjectName(SERVER_DEVICE_NAME);
69 m_Server->SetHostname(HOSTNAME);
70 m_Server->SetName(SERVER_DEVICE_NAME);
71 m_Server->SetPortNumber(PORT);
73 m_Client_One->SetObjectName(CLIENT_ONE_DEVICE_NAME);
74 m_Client_One->SetHostname(HOSTNAME);
75 m_Client_One->SetName(CLIENT_ONE_DEVICE_NAME);
76 m_Client_One->SetPortNumber(PORT);
78 m_Client_Two->SetObjectName(CLIENT_TWO_DEVICE_NAME);
79 m_Client_Two->SetHostname(HOSTNAME);
80 m_Client_Two->SetName(CLIENT_TWO_DEVICE_NAME);
81 m_Client_Two->SetPortNumber(PORT);
94 void testMessagesEqual(igtl::MessageBase::Pointer sentMessage, igtl::MessageBase::Pointer receivedMessage)
96 std::string lhs(sentMessage->GetDeviceName());
97 std::string rhs(receivedMessage->GetDeviceName());
98 CPPUNIT_ASSERT_MESSAGE(
"The device names were not the same", lhs == rhs);
99 igtl::StatusMessage::Pointer receivedStatusMessage =
dynamic_cast<igtl::StatusMessage*
>(receivedMessage.GetPointer());
100 igtl::StatusMessage::Pointer sentStatusMessage =
dynamic_cast<igtl::StatusMessage*
>(sentMessage.GetPointer());
101 CPPUNIT_ASSERT_MESSAGE(
"The received message was not of the appropriate type.", receivedStatusMessage !=
nullptr);
102 CPPUNIT_ASSERT_MESSAGE(
"The sent message was not of the appropriate type.", sentStatusMessage !=
nullptr);
104 lhs = receivedStatusMessage->GetStatusString();
105 rhs = sentStatusMessage->GetStatusString();
106 CPPUNIT_ASSERT_MESSAGE(
"The sent and received message did not contain the same status message.", lhs == rhs);
107 CPPUNIT_ASSERT_MESSAGE(
"The sent message did not contain the correct status message.", lhs == m_Message);
108 CPPUNIT_ASSERT_MESSAGE(
"The received message did not contain the correct status message.", m_Message == rhs);
113 igtl::ServerSocket::Pointer server = igtl::ServerSocket::New();
114 igtl::ClientSocket::Pointer client = igtl::ClientSocket::New();
116 CPPUNIT_ASSERT(server->CreateServer(PORT) == 0);
117 CPPUNIT_ASSERT(client->ConnectToServer(
"localhost", PORT) == 0);
119 client->CloseSocket();
120 server->CloseSocket();
122 CPPUNIT_ASSERT(server->CreateServer(PORT) == 0);
123 CPPUNIT_ASSERT(client->ConnectToServer(
"localhost", PORT) == 0);
125 client->CloseSocket();
126 server->CloseSocket();
143 CPPUNIT_ASSERT_MESSAGE(
"Could not open Connection with Server", m_Server->OpenConnection());
144 m_Server->StartCommunication();
146 CPPUNIT_ASSERT_MESSAGE(
"Could not connect to Server with first client", m_Client_One->OpenConnection());
147 CPPUNIT_ASSERT_MESSAGE(
"Could not start communication with first client", m_Client_One->StartCommunication());
149 CPPUNIT_ASSERT_MESSAGE(
"Could not connect to Server with second client", m_Client_Two->OpenConnection());
150 CPPUNIT_ASSERT_MESSAGE(
"Could not start communication with second client", m_Client_Two->StartCommunication());
152 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
153 CPPUNIT_ASSERT(m_Client_Two->CloseConnection());
154 CPPUNIT_ASSERT(m_Server->CloseConnection());
159 CPPUNIT_ASSERT_MESSAGE(
"Could not open Connection with Server", m_Server->OpenConnection());
160 m_Server->StartCommunication();
161 CPPUNIT_ASSERT_MESSAGE(
"Could not connect to Server with first client", m_Client_One->OpenConnection());
162 CPPUNIT_ASSERT_MESSAGE(
"Could not start communication with first client", m_Client_One->StartCommunication());
163 CPPUNIT_ASSERT_MESSAGE(
"Could not connect to Server with second client", m_Client_Two->OpenConnection());
164 CPPUNIT_ASSERT_MESSAGE(
"Could not start communication with second client", m_Client_Two->StartCommunication());
166 std::this_thread::sleep_for(std::chrono::milliseconds(200));
168 CPPUNIT_ASSERT(m_Server->CloseConnection());
169 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
170 CPPUNIT_ASSERT(m_Client_Two->CloseConnection());
175 CPPUNIT_ASSERT_MESSAGE(
"Server not connected to Client.", m_Server->OpenConnection());
176 CPPUNIT_ASSERT_MESSAGE(
"Client 1 not connected to Server.", m_Client_One->OpenConnection());
177 m_Server->StartCommunication();
178 m_Client_One->StartCommunication();
180 igtl::MessageBase::Pointer sentMessage = m_MessageFactory->CreateInstance(
"STATUS");
181 dynamic_cast<igtl::StatusMessage*
>(sentMessage.GetPointer())->SetStatusString(m_Message.c_str());
182 m_Server->SendMessage(mitk::IGTLMessage::New(sentMessage));
183 igtl::MessageBase::Pointer receivedMessage;
185 while ((receivedMessage = m_Client_One->GetMessageQueue()->PullMiscMessage()) ==
nullptr)
187 std::this_thread::sleep_for(std::chrono::milliseconds(5));
191 CPPUNIT_ASSERT(receivedMessage !=
nullptr);
193 CPPUNIT_ASSERT(m_Client_One->StopCommunication());
194 CPPUNIT_ASSERT(m_Server->StopCommunication());
196 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
197 CPPUNIT_ASSERT(m_Server->CloseConnection());
204 CPPUNIT_ASSERT_MESSAGE(
"Server not connected to Client.", m_Server->OpenConnection());
205 m_Server->StartCommunication();
206 CPPUNIT_ASSERT_MESSAGE(
"Client 1 not connected to Server.", m_Client_One->OpenConnection());
207 m_Client_One->StartCommunication();
208 CPPUNIT_ASSERT_MESSAGE(
"Client 2 not connected to Server.", m_Client_Two->OpenConnection());
209 m_Client_Two->StartCommunication();
211 std::this_thread::sleep_for(std::chrono::milliseconds(100));
213 igtl::MessageBase::Pointer sentMessage = m_MessageFactory->CreateInstance(
"STATUS");
214 dynamic_cast<igtl::StatusMessage*
>(sentMessage.GetPointer())->SetStatusString(m_Message.c_str());
215 m_Server->SendMessage(mitk::IGTLMessage::New(sentMessage));
216 MITK_INFO <<
"SENT MESSAGE";
218 igtl::MessageBase::Pointer receivedMessage1;
219 igtl::MessageBase::Pointer receivedMessage2;
221 while (receivedMessage1 ==
nullptr || receivedMessage2 ==
nullptr)
223 std::this_thread::sleep_for(std::chrono::milliseconds(20));
225 igtl::MessageBase::Pointer tmpMessage1 = m_Client_One->GetMessageQueue()->PullMiscMessage();
226 if (tmpMessage1.IsNotNull())
227 receivedMessage1 = tmpMessage1;
229 igtl::MessageBase::Pointer tmpMessage2 = m_Client_Two->GetMessageQueue()->PullMiscMessage();
230 if (tmpMessage2.IsNotNull())
231 receivedMessage2 = tmpMessage2;
237 CPPUNIT_ASSERT(m_Client_Two->StopCommunication());
238 CPPUNIT_ASSERT(m_Client_One->StopCommunication());
239 CPPUNIT_ASSERT(m_Server->StopCommunication());
241 CPPUNIT_ASSERT(m_Client_Two->CloseConnection());
242 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
243 CPPUNIT_ASSERT(m_Server->CloseConnection());
245 CPPUNIT_ASSERT_MESSAGE(
"Message from first client was null..", receivedMessage1 !=
nullptr);
246 CPPUNIT_ASSERT_MESSAGE(
"Message from first client was null..", receivedMessage1.IsNotNull());
247 CPPUNIT_ASSERT_MESSAGE(
"Message from second client was null..", receivedMessage2 !=
nullptr);
248 CPPUNIT_ASSERT_MESSAGE(
"Message from second client was null..", receivedMessage2.IsNotNull());