MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkOpenIGTLinkClientServerTest.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//TEST
14#include <mitkTestingMacros.h>
15#include <mitkTestFixture.h>
16
17//STD
18#include <thread>
19#include <chrono>
20
21//MITK
22#include "mitkIGTLServer.h"
23#include "mitkIGTLClient.h"
25
26//IGTL
27#include "igtlStatusMessage.h"
28#include "igtlClientSocket.h"
29#include "igtlServerSocket.h"
30
31static int PORT = 35352;
32static const std::string HOSTNAME = "localhost";
33static const std::string SERVER_DEVICE_NAME = "Test Server";
34static const std::string CLIENT_ONE_DEVICE_NAME = "Test Client 1";
35static const std::string CLIENT_TWO_DEVICE_NAME = "Test Client 2";
36
37class mitkOpenIGTLinkClientServerTestSuite : public mitk::TestFixture
38{
39 CPPUNIT_TEST_SUITE(mitkOpenIGTLinkClientServerTestSuite);
40 //MITK_TEST(Test_JustIGTLImpl_OpenAndCloseAndThenReopenAndCloseServer_Successful);
41 //MITK_TEST(Test_ConnectingOneClientAndOneServer_Successful);
42 //MITK_TEST(Test_ConnectingMultipleClientsToOneServer_Successful);
43#ifdef _WIN32 //does only work under windows, see bug 19633
45#endif
46 //MITK_TEST(Test_SendingMessageFromServerToOneClient_Successful);
47 //MITK_TEST(Test_SendingMessageFromServerToMultipleClients_Successful);
48 CPPUNIT_TEST_SUITE_END();
49
50private:
51 std::string m_Message;
52 mitk::IGTLServer::Pointer m_Server;
53 mitk::IGTLClient::Pointer m_Client_One;
54 mitk::IGTLClient::Pointer m_Client_Two;
55 mitk::IGTLMessageFactory::Pointer m_MessageFactory;
56
57public:
58
59 void setUp() override
60 {
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);
67
68 m_Server->SetObjectName(SERVER_DEVICE_NAME);
69 m_Server->SetHostname(HOSTNAME);
70 m_Server->SetName(SERVER_DEVICE_NAME);
71 m_Server->SetPortNumber(PORT);
72
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);
77
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);
82 }
83
84 void tearDown() override
85 {
86 std::this_thread::sleep_for(std::chrono::milliseconds(20));
87 m_Message.clear();
88 m_Server = nullptr;
89 m_Client_One = nullptr;
90 m_Client_Two = nullptr;
91 m_MessageFactory = nullptr;
92 }
93
94 void testMessagesEqual(igtl::MessageBase::Pointer sentMessage, igtl::MessageBase::Pointer receivedMessage)
95 {
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);
103
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);
109 };
110
112 {
113 igtl::ServerSocket::Pointer server = igtl::ServerSocket::New();
114 igtl::ClientSocket::Pointer client = igtl::ClientSocket::New();
115
116 CPPUNIT_ASSERT(server->CreateServer(PORT) == 0);
117 CPPUNIT_ASSERT(client->ConnectToServer("localhost", PORT) == 0);
118
119 client->CloseSocket();
120 server->CloseSocket();
121
122 CPPUNIT_ASSERT(server->CreateServer(PORT) == 0);
123 CPPUNIT_ASSERT(client->ConnectToServer("localhost", PORT) == 0);
124
125 client->CloseSocket();
126 server->CloseSocket();
127
128 server = nullptr;
129 client = nullptr;
130 }
131
133 {
134 CPPUNIT_ASSERT_MESSAGE("Could not open Connection with Server", m_Server->OpenConnection());
135 CPPUNIT_ASSERT_MESSAGE("Could not connect to Server with first client", m_Client_One->OpenConnection());
136
137 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
138 CPPUNIT_ASSERT(m_Server->CloseConnection());
139 }
140
142 {
143 CPPUNIT_ASSERT_MESSAGE("Could not open Connection with Server", m_Server->OpenConnection());
144 m_Server->StartCommunication();
145
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());
148
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());
151
152 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
153 CPPUNIT_ASSERT(m_Client_Two->CloseConnection());
154 CPPUNIT_ASSERT(m_Server->CloseConnection());
155 }
156
158 {
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());
165
166 std::this_thread::sleep_for(std::chrono::milliseconds(200));
167
168 CPPUNIT_ASSERT(m_Server->CloseConnection());
169 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
170 CPPUNIT_ASSERT(m_Client_Two->CloseConnection());
171 }
172
174 {
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();
179
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;
184 int steps = 0;
185 while ((receivedMessage = m_Client_One->GetMessageQueue()->PullMiscMessage()) == nullptr)
186 {
187 std::this_thread::sleep_for(std::chrono::milliseconds(5));
188 if (++steps > 20)
189 break;
190 }
191 CPPUNIT_ASSERT(receivedMessage != nullptr);
192
193 CPPUNIT_ASSERT(m_Client_One->StopCommunication());
194 CPPUNIT_ASSERT(m_Server->StopCommunication());
195
196 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
197 CPPUNIT_ASSERT(m_Server->CloseConnection());
198
199 testMessagesEqual(sentMessage, receivedMessage);
200 }
201
203 {
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();
210
211 std::this_thread::sleep_for(std::chrono::milliseconds(100));
212
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";
217
218 igtl::MessageBase::Pointer receivedMessage1;
219 igtl::MessageBase::Pointer receivedMessage2;
220 int steps = 0;
221 while (receivedMessage1 == nullptr || receivedMessage2 == nullptr)
222 {
223 std::this_thread::sleep_for(std::chrono::milliseconds(20));
224
225 igtl::MessageBase::Pointer tmpMessage1 = m_Client_One->GetMessageQueue()->PullMiscMessage();
226 if (tmpMessage1.IsNotNull())
227 receivedMessage1 = tmpMessage1;
228
229 igtl::MessageBase::Pointer tmpMessage2 = m_Client_Two->GetMessageQueue()->PullMiscMessage();
230 if (tmpMessage2.IsNotNull())
231 receivedMessage2 = tmpMessage2;
232
233 if (++steps > 50)
234 break;
235 }
236
237 CPPUNIT_ASSERT(m_Client_Two->StopCommunication());
238 CPPUNIT_ASSERT(m_Client_One->StopCommunication());
239 CPPUNIT_ASSERT(m_Server->StopCommunication());
240
241 CPPUNIT_ASSERT(m_Client_Two->CloseConnection());
242 CPPUNIT_ASSERT(m_Client_One->CloseConnection());
243 CPPUNIT_ASSERT(m_Server->CloseConnection());
244
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());
249
250 testMessagesEqual(sentMessage, receivedMessage1);
251 testMessagesEqual(sentMessage, receivedMessage2);
252 testMessagesEqual(receivedMessage2, receivedMessage1);
253 }
254};
255
256MITK_TEST_SUITE_REGISTRATION(mitkOpenIGTLinkClientServer)
void testMessagesEqual(igtl::MessageBase::Pointer sentMessage, igtl::MessageBase::Pointer receivedMessage)