51 #include <gtest/gtest.h> 91 EXPECT_TRUE(bytes.
init(&buffer[0], SIZE));
98 std::vector<char> bigBuffer(TOO_BIG);
99 EXPECT_FALSE(bytes.
init(&bigBuffer[0], TOO_BIG));
102 std::cout << std::string(15,
' ')
103 <<
"ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl;
115 ASSERT_TRUE(bytes.
init(&buffer[0], SIZE));
122 EXPECT_TRUE(bytes.
load(bIN));
124 EXPECT_TRUE(bytes.
unload(bOUT));
126 EXPECT_EQ(bOUT, bIN);
129 EXPECT_TRUE(bytes.
load(iIN));
131 EXPECT_TRUE(bytes.
unload(iOUT));
133 EXPECT_EQ(iOUT, iIN);
136 EXPECT_TRUE(bytes.
load(rIN));
138 EXPECT_TRUE(bytes.
unload(rOUT));
140 EXPECT_EQ(rOUT, rIN);
143 EXPECT_TRUE(empty.
load(bIN));
145 EXPECT_TRUE(empty.
unload(bOUT));
147 EXPECT_EQ(bOUT, bIN);
152 EXPECT_TRUE(empty.
load(rIN));
154 EXPECT_TRUE(empty.
load(iIN));
157 EXPECT_EQ(rOUT, rIN);
158 EXPECT_TRUE(empty.
unload(iOUT));
160 EXPECT_EQ(iOUT, iIN);
163 TEST(ByteArraySuite, byteSwapping)
165 if(ByteArray::isByteSwapEnabled())
167 ASSERT_TRUE(ByteArray::isByteSwapEnabled());
170 unsigned char buffer[] = {
171 0x00, 0x00, 0x00, 0x38,
172 0x00, 0x00, 0x00, 0x0a,
173 0x00, 0x00, 0x00, 0x01,
175 0x3e, 0x81, 0x32, 0x64,
176 0x3f, 0x30, 0x4b, 0x75,
177 0x3f, 0xa8, 0x9d, 0xd2,
178 0x3f, 0x85, 0x93, 0xdd,
179 0xbf, 0xf4, 0x8c, 0xc5,
182 const unsigned int bufferLength = 32;
186 swapped.
init((
const char*) buffer, bufferLength);
189 ASSERT_TRUE(swapped.
unload(tempReal));
190 EXPECT_FLOAT_EQ(tempReal, -1.9105459451675415);
192 ASSERT_TRUE(swapped.
unload(tempReal));
193 EXPECT_FLOAT_EQ(tempReal, 1.0435749292373657);
195 ASSERT_TRUE(swapped.
unload(tempReal));
196 EXPECT_FLOAT_EQ(tempReal, 1.3173162937164307);
198 ASSERT_TRUE(swapped.
unload(tempReal));
199 EXPECT_FLOAT_EQ(tempReal, 0.68865138292312622);
201 ASSERT_TRUE(swapped.
unload(tempReal));
202 EXPECT_FLOAT_EQ(tempReal, 0.25233757495880127);
204 ASSERT_TRUE(swapped.
unload(tempInt));
205 EXPECT_EQ(tempInt, 1);
207 ASSERT_TRUE(swapped.
unload(tempInt));
208 EXPECT_EQ(tempInt, 10);
210 ASSERT_TRUE(swapped.
unload(tempInt));
211 EXPECT_EQ(tempInt, 56);
228 EXPECT_TRUE(copyFrom.
init(&buffer[0], SIZE));
229 EXPECT_TRUE(copyTo.
load(copyFrom));
231 EXPECT_TRUE(copyTo.
load(copyFrom));
236 if (tooBig.getMaxBufferSize()-1 <= std::numeric_limits<shared_int>::max())
239 std::vector<char> bigBuffer(TOO_BIG);
241 EXPECT_TRUE(tooBig.init(&bigBuffer[0], TOO_BIG));
242 EXPECT_FALSE(copyTo.
load(tooBig));
247 std::cout << std::string(15,
' ')
248 <<
"ByteArray.MaxSize==INT_MAX. Skipping TOO_BIG tests" << std::endl;
258 return TcpClient::sendBytes(buffer);
266 return TcpServer::receiveBytes(buffer, num_bytes, -1);
271 return TcpServer::receiveBytes(buffer, num_bytes, timeout_ms);
280 return UdpClient::sendBytes(buffer);
288 return UdpServer::receiveBytes(buffer, num_bytes, -1);
293 return UdpServer::receiveBytes(buffer, num_bytes, timeout_ms);
308 const int port = TEST_PORT_BASE;
309 char ipAddr[] =
"127.0.0.1";
319 ASSERT_TRUE(server.
init(port));
322 ASSERT_TRUE(client.
init(&ipAddr[0], port));
327 serverConnectThrd.join();
329 ASSERT_TRUE(send.
load(DATA));
334 std::this_thread::sleep_for(std::chrono::seconds(2));
350 const int port = TEST_PORT_BASE;
351 char ipAddr[] =
"127.0.0.1";
362 ASSERT_TRUE(server.
init(port));
365 ASSERT_TRUE(client.
init(&ipAddr[0], port));
366 pthread_t serverConnectThrd;
370 pthread_join(serverConnectThrd, NULL);
372 ASSERT_TRUE(send.
load(DATA));
375 ASSERT_FALSE(server.
receiveBytes(recv, TWO_INTS, TIMEOUT_MS));
381 ASSERT_FALSE(server.
receiveBytes(recv, TWO_INTS, TIMEOUT_MS));
391 const int DATA = 256;
398 std::this_thread::sleep_for(std::chrono::milliseconds(100));
402 TEST(SocketSuite, splitPackets)
404 const int port = TEST_PORT_BASE + 1;
405 char ipAddr[] =
"127.0.0.1";
406 const int RECV_LENGTH = 64;
412 ASSERT_TRUE(server.
init(port));
415 ASSERT_TRUE(client.
init(&ipAddr[0], port));
419 serverConnectThrd.join();
421 bool senderRunning =
true;
422 std::thread senderThrd(
spinSender, &client, std::ref(senderRunning));
427 senderRunning =
false;
456 EXPECT_FALSE(ping.
init(msg));
462 EXPECT_TRUE(ping.init(msg));
466 TEST(PingMessageSuite, toMessage)
483 EXPECT_FALSE(ping.
toTopic(msg));
492 ASSERT_TRUE(handler.
init(&client));
495 EXPECT_FALSE(handler.
init(NULL));
499 TEST(MessageManagerSuite, init)
504 EXPECT_TRUE(manager.
init(&client));
505 EXPECT_FALSE(manager.
init(NULL));
509 TEST(MessageManagerSuite, addHandler)
517 ASSERT_TRUE(manager.
init(&client));
519 EXPECT_FALSE(manager.
add(NULL));
521 ASSERT_TRUE(handler.
init(&client));
522 EXPECT_FALSE(manager.
add(&handler));
601 int main(
int argc,
char **argv)
604 testing::InitGoogleTest(&argc, argv);
605 return RUN_ALL_TESTS();
Defines TCP server functions.
int getMessageType() const
gets message type (enumeration)
bool init(char *buff, int port_num)
initializes TCP client socket. Object can either be a client OR a server, NOT BOTH.
void * spinFunc(void *arg)
bool init(industrial::simple_message::SimpleMessage &msg)
Initializes message from a simple message.
Contains platform specific type definitions that guarantee the size of primitive data types...
void init()
Initializes or Reinitializes an empty buffer.
bool init(industrial::smpl_msg_connection::SmplMsgConnection *connection)
Class initializer.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
This class defines a simple messaging protocol for communicating with an industrial robot controller...
int getMessageType()
Gets message type(see StandardMsgType)
bool unloadFront(industrial::shared_types::shared_real &value)
unloads a double value from the beginning of the byte array. If byte swapping is enabled, then the bytes are swapped.
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
virtual bool toRequest(industrial::simple_message::SimpleMessage &msg)
creates a simple_message request
Class encapsulated ping message generation methods (either to or from a SimpleMessage type...
unsigned int getNumHandlers()
Gets number of handlers.
bool receiveBytes(ByteArray &buffer, shared_int num_bytes)
bool makeConnect()
connects to the remote host
int getMsgType()
Gets message type that callback expects.
void spin()
Perform a indefinite execution of the message manager.
void spinSender(void *arg, bool &running)
bool add(industrial::message_handler::MessageHandler *handler, bool allow_replace=false)
Adds a message handler to the manager.
Message handler that handles ping messages.
The byte array wraps a dynamic array of bytes (i.e. char).
bool sendBytes(ByteArray &buffer)
Method used by send message interface method. This should be overridden for the specific connection t...
int main(int argc, char **argv)
bool toTopic(industrial::simple_message::SimpleMessage &msg)
The ping message overrides the base method toTopic to always return false. A ping cannot be sent as a...
unsigned int getMaxBufferSize()
gets current buffer size
The message manager handles communications for a simple server.
bool init(int msgType, int commType, int replyCode, industrial::byte_array::ByteArray &data)
Initializes a fully populated simple message.
bool receiveBytes(ByteArray &buffer, shared_int num_bytes, shared_int timeout_ms)
Method used by receive message interface method. This should be overridden for the specific connectio...
virtual bool toReply(industrial::simple_message::SimpleMessage &msg, industrial::simple_message::ReplyType reply)
creates a simple_message reply
bool makeConnect()
connects to the remote host
void * connectServerFunc(void *arg)
unsigned int getBufferSize()
gets current buffer size
TEST(ByteArraySuite, init)
bool unload(industrial::shared_types::shared_bool &value)
unloads a boolean value from the byte array
bool init(int port_num)
initializes TCP server socket. The connect method must be called following initialization in order to...
bool init(industrial::smpl_msg_connection::SmplMsgConnection *connection)
Class initializer.
Defines TCP client functions.
int getReplyCode()
Gets reply code(see ReplyType)
int getCommType()
Gets message type(see CommType)