RoveComm C++ 3.0.0
Welcome to the RoveComm C++ repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of RoveComm C++. The RoveComm C++ project is the RoveComm interface for high level systems and should not be used for microcontrollers.
Loading...
Searching...
No Matches
tcp.cc File Reference

Unit test for TCP functionality in RoveComm. More...

Include dependency graph for tcp.cc:

Functions

 TEST (RoveCommTCP, InitSocket)
 Test Initialization of a TCP Socket.
 TEST (RoveCommTCP, SendTCPPacket)
 Test Sending a TCP Packet.
 TEST (RoveCommTCP, CallbackInvoked)
 Test Adding and Removing a TCP Callback.
 TEST (RoveCommTCP, CallbacksBelongToTheirNodeAndClearRemovesOne)

Detailed Description

Unit test for TCP functionality in RoveComm.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-02-07

Function Documentation

◆ TEST() [1/4]

TEST ( RoveCommTCP ,
CallbackInvoked  )

Test Adding and Removing a TCP Callback.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-02-08
115{
116 // Run the test via the RunTimedTest function to allow for retries and timeouts.
118 []()
119 {
120 // Create RoveComm Nodes
121 rovecomm::RoveCommTCP RoveCommTCPNode;
122
123 // Give the node three chances to initialize the socket
124 // Since the socket is bound to a specific port, it may take a few tries to find an available port
125 for (int i = 0; i < 3; ++i)
126 {
127 if (RoveCommTCPNode.Init("127.0.0.1", 12002))
128 {
129 break;
130 }
131 else
132 {
133 std::this_thread::sleep_for(std::chrono::seconds(1));
134 }
135 }
136
137 // Flag to check if the callback was invoked
138 bool bCallbackInvoked = false;
139
140 std::vector<int32_t> vExpectedData = {-100, -1, 5555};
141
142 // Add a callback function for data ID 1100
143 RoveCommTCPNode.On<int32_t>(1100,
144 [&](const rovecomm::RoveCommPacket<int32_t>& packet)
145 {
146 // Set the flag to true to indicate that the callback was invoked
147 bCallbackInvoked = true;
148
149 // Assertions to verify the behavior of the callback function
150 EXPECT_EQ(packet.unDataId, 1100); // Check the data ID
151 EXPECT_EQ(packet.GetDataCount(), 3); // Check the data count
152 EXPECT_EQ(packet.eDataType, manifest::DataTypes::INT32_T); // Check the data type
153
154 for (size_t i = 0; i < packet.vData.size(); i++)
155 {
156 EXPECT_EQ(packet.vData[i], vExpectedData[i]); // Check the data
157 }
158 });
159
160 // Simulate receiving a packet with data ID 1100
161 // Create RoveCommPacket
162 rovecomm::RoveCommPacket<int32_t> stPacket{1100, {}};
163
164 for (int32_t nData : vExpectedData)
165 {
166 stPacket.vData.push_back(nData); // Sample data
167 }
168
169 // Pack the packet
170 std::vector<uint8_t> vData = rovecomm::PackPacket(stPacket);
171
172 // Process the received packet (simulate callback invocation)
173 RoveCommTCPNode.ProcessPacket<int32_t>(vData);
174
175 // Check if the callback was invoked
176 EXPECT_TRUE(bCallbackInvoked);
177
178 // Close the socket
179 RoveCommTCPNode.Close();
180 },
181 3, // 3 total attempts
182 30000); // 30 second timeout (30,000 ms)
183}
The RoveCommTCP class is used to send and receive data over a TCP connection.
Definition RoveCommTCP.h:52
void Close()
Closes the TCP socket.
Definition RoveCommTCP.cpp:459
void ProcessPacket(std::span< const uint8_t > stData)
Processes a received packet and invokes the appropriate callback function from the vector of TCP call...
Definition RoveCommTCP.cpp:329
CallbackHandle On(const uint16_t unDataId, std::function< void(const RoveCommPacket< T > &)> fnCallback)
Register a callback for TCP packets with the given data ID. The callback belongs to this node: packet...
Definition RoveCommTCP.cpp:227
bool Init(const manifest::AddressEntry &stIPAddress={0, 0, 0, 0}, int nPort=manifest::General::ETHERNET_TCP_PORT)
Initializes a TCP socket and binds it to the specified IP address and port. And then starts the threa...
Definition RoveCommTCP.cpp:76
@ INT32_T
Definition RoveCommManifest.h:39
std::vector< uint8_t > PackPacket(const RoveCommPacket< T > &stPacket)
Create a RoveCommData structure from a RoveCommPacket structure.
Definition RoveCommPacket.cpp:44
void RunTimedTest(const std::function< void()> &fnTestCode, int nMaxRetries, int nTimeoutMS)
Run a test with a timeout and retries.
Definition TestUtils.cc:31
The RoveCommPacket struct is used to store data in a packet format for transmission over a network....
Definition RoveCommPacket.h:75
Here is the call graph for this function:

◆ TEST() [2/4]

TEST ( RoveCommTCP ,
CallbacksBelongToTheirNodeAndClearRemovesOne  )
186{
187 // Run the test via the RunTimedTest function to allow for retries and timeouts.
189 []()
190 {
191 // Two nodes in one process. Neither needs a socket: packets are fed straight to ProcessPacket.
192 rovecomm::RoveCommTCP RoveCommTCPNodeA;
193 rovecomm::RoveCommTCP RoveCommTCPNodeB;
194
195 const uint16_t unTestDataId = 1402;
196 int nCallsFirst = 0;
197 int nCallsSecond = 0;
198 std::vector<uint8_t> vData = rovecomm::PackPacket(rovecomm::RoveCommPacket<int16_t>{.unDataId = unTestDataId, .vData = {-7, 7}});
199
200 // Two callbacks for the same data ID, both on node A.
201 rovecomm::CallbackHandle stFirst = RoveCommTCPNodeA.On<int16_t>(unTestDataId, [&](const rovecomm::RoveCommPacket<int16_t>&) { ++nCallsFirst; });
202 rovecomm::CallbackHandle stSecond = RoveCommTCPNodeA.On<int16_t>(unTestDataId, [&](const rovecomm::RoveCommPacket<int16_t>&) { ++nCallsSecond; });
203
204 // A packet received by node B must not reach node A's callbacks.
205 RoveCommTCPNodeB.ProcessPacket<int16_t>(vData);
206 EXPECT_EQ(nCallsFirst + nCallsSecond, 0) << "Node B invoked callbacks registered on node A";
207
208 // Node A invokes both, then only the second once Clear() removes the first.
209 RoveCommTCPNodeA.ProcessPacket<int16_t>(vData);
210 RoveCommTCPNodeA.Clear(stFirst);
211 RoveCommTCPNodeA.ProcessPacket<int16_t>(vData);
212 EXPECT_EQ(nCallsFirst, 1) << "Clear() left its callback registered";
213 EXPECT_EQ(nCallsSecond, 2) << "Clear() removed a different callback";
214
215 // Clear() removes the second callback.
216 RoveCommTCPNodeA.Clear(stSecond);
217 RoveCommTCPNodeA.ProcessPacket<int16_t>(vData);
218 EXPECT_EQ(nCallsSecond, 2) << "Clear() left a callback registered";
219 },
220 3, // 3 total attempts
221 30000); // 30 second timeout (30,000 ms)
222}
void Clear(const CallbackHandle &stHandle)
Remove the one callback identified by a handle from On(). Other callbacks for the same data ID stay r...
Definition RoveCommTCP.cpp:251
Identifies one callback registered with a node's On(), so Clear() can remove that callback without to...
Definition RoveCommPacket.h:104
Here is the call graph for this function:

◆ TEST() [3/4]

TEST ( RoveCommTCP ,
InitSocket  )

Test Initialization of a TCP Socket.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-02-08
28{
29 // Run the test via the RunTimedTest function to allow for retries and timeouts.
31 []()
32 {
33 // Create RoveComm Node
34 rovecomm::RoveCommTCP RoveCommTCPNode;
35
36 bool bInitSuccess = false;
37
38 // Give the node three chances to initialize the socket
39 // Since the socket is bound to a specific port, it may take a few tries to find an available port
40 for (int i = 0; i < 3; ++i)
41 {
42 if (RoveCommTCPNode.Init("127.0.0.1", 12000))
43 {
44 bInitSuccess = true;
45 break;
46 }
47 else
48 {
49 std::this_thread::sleep_for(std::chrono::seconds(1));
50 }
51 }
52
53 // Initialize the TCP node
54 EXPECT_TRUE(bInitSuccess);
55
56 // Close the socket
57 RoveCommTCPNode.Close();
58 },
59 3, // 3 total attempts
60 30000); // 30 second timeout (30,000 ms)
61}
Here is the call graph for this function:

◆ TEST() [4/4]

TEST ( RoveCommTCP ,
SendTCPPacket  )

Test Sending a TCP Packet.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-02-08
70{
71 // Run the test via the RunTimedTest function to allow for retries and timeouts.
73 []()
74 {
75 // Create RoveComm Nodes
76 rovecomm::RoveCommTCP RoveCommTCPNode;
77
78 // Give the node three chances to initialize the socket
79 // Since the socket is bound to a specific port, it may take a few tries to find an available port
80 for (int i = 0; i < 3; ++i)
81 {
82 if (RoveCommTCPNode.Init("127.0.0.1", 12001))
83 {
84 break;
85 }
86 else
87 {
88 std::this_thread::sleep_for(std::chrono::seconds(1));
89 }
90 }
91
92 // Create RoveCommPacket
94
95 // Send the packet to the localhost
96 ssize_t siBytesSent = RoveCommTCPNode.WriteTo(stPacket, "127.0.0.1", 12001);
97
98 // Check if the packet successfully sent
99 EXPECT_EQ(siBytesSent, 7);
100
101 // Close the socket
102 RoveCommTCPNode.Close();
103 },
104 3, // 3 total attempts
105 30000); // 30 second timeout (30,000 ms)
106}
ssize_t WriteTo(const RoveCommPacket< T > &stPacket, const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Sends a TCP packet to the specified client IP address and port. The data is packed into a RoveCommDat...
Definition RoveCommTCP.cpp:162
constexpr ManifestEntry STARTAUTONOMY
Definition RoveCommManifest.h:533
Here is the call graph for this function: