RoveComm C++ main function.
35{
36 using namespace std::literals::chrono_literals;
37
38 std::ifstream fHeaderText("../data/ASCII/v3.txt");
39 std::string szHeaderText;
40 if (fHeaderText)
41 {
42 std::ostringstream pHeaderText;
43 pHeaderText << fHeaderText.rdbuf();
44 szHeaderText = pHeaderText.str();
45 }
46
47 std::cout << szHeaderText << std::endl;
48 std::cout << "Copyright \u00A9 2024 - Mars Rover Design Team\n" << std::endl;
49
50
52 {
53 std::cout << "Received TCP packet with:" << std::endl;
54 std::cout << "\tData ID: " << stPacket.unDataId << std::endl;
55 std::cout << "\tData Count: " << stPacket.GetDataCount() << std::endl;
56 std::cout << "\tData Type: " << stPacket.eDataType << std::endl;
57 std::cout << "\tData: " << std::endl;
58
59 for (auto data : stPacket.vData)
60 {
61 std::cout << "\t\t>>" << (int) data << std::endl;
62 }
63 };
64
65
67 {
68 std::cout << "Received UDP packet with:" << std::endl;
69 std::cout << "\tData ID: " << stPacket.unDataId << std::endl;
70 std::cout << "\tData Count: " << stPacket.GetDataCount() << std::endl;
71 std::cout << "\tData Type: " << stPacket.eDataType << std::endl;
72 std::cout << "\tData: " << std::endl;
73
74 for (auto data : stPacket.vData)
75 {
76 std::cout << "\t\t>>" << (int) data << std::endl;
77 }
78 };
79
80
83
84
85 if (pRoveCommUDP_Node.
Init(11000))
86 {
87 std::cout << "UDP Node Initialized." << std::endl;
88 }
89 else
90 {
91 std::cout << "UDP Node Failed to Initialize." << std::endl;
92 }
93
94 if (pRoveCommTCP_Node.
Init(
"127.0.0.1", 12000))
95 {
96 std::cout << "TCP Node Initialized." << std::endl;
97 }
98 else
99 {
100 std::cout << "TCP Node Failed to Initialize." << std::endl;
101 }
102
105
106
108
110
111
112
113
114
117
118
119 std::this_thread::sleep_for(500ms);
120
121
122 pRoveCommUDP_Node.
Clear(stUDPHandle);
123 pRoveCommTCP_Node.
Clear(stTCPHandle);
124
126
127
128 pRoveCommUDP_Node.
WriteTo(stPacket,
"127.0.0.1", 11000);
129 pRoveCommTCP_Node.
WriteTo(stPacket,
"127.0.0.1", 12000);
130
131
132 std::this_thread::sleep_for(500ms);
133
134
136 if (pRemoteRoveCommUDP_Node.
Init(11003))
137 {
138 std::cout << "Remote UDP Node Initialized." << std::endl;
139 }
140
141 pRemoteRoveCommUDP_Node.
Subscribe(
"127.0.0.1", 11000);
142
143
144 std::this_thread::sleep_for(500ms);
145
147 [](const auto& stPacket)
148 {
149 (void) stPacket;
150 std::cout << "Remote UDP Callback" << std::endl;
151 });
152
153
155
156
157 std::this_thread::sleep_for(500ms);
158
159 pRemoteRoveCommUDP_Node.
Unsubscribe(
"127.0.0.1", 11000);
160
161
162 std::this_thread::sleep_for(500ms);
163
164
166
167
168 std::this_thread::sleep_for(500ms);
169
170
171 pRoveCommUDP_Node.
Close();
172 pRoveCommTCP_Node.
Close();
173 pRemoteRoveCommUDP_Node.
Close();
174
175 std::cout << "All nodes closed." << std::endl;
176
177 exit(0);
178}
The RoveCommTCP class is used to send and receive data over a TCP connection.
Definition RoveCommTCP.h:52
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
void Close()
Closes the TCP socket.
Definition RoveCommTCP.cpp:459
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
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
The RoveCommUDP class is used to send and receive data over a UDP connection.
Definition RoveCommUDP.h:52
ssize_t WriteTo(const RoveCommPacket< T > &stPacket, const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Sends a packet to a specific destination. This is often used to send a command to a specific device.
Definition RoveCommUDP.h:143
void Close()
Close the UDP socket. This method is called when the RoveCommUDP object is destroyed....
Definition RoveCommUDP.cpp:696
bool Init(int nPort=manifest::General::ETHERNET_UDP_PORT)
Initialize the UDP socket and bind it to the specified port. This method also starts the thread that ...
Definition RoveCommUDP.cpp:110
CallbackHandle On(const uint16_t unDataId, std::function< void(const RoveCommPacket< T > &)> fnCallback)
Register a callback for UDP packets with the given data ID. The callback belongs to this node: packet...
Definition RoveCommUDP.cpp:333
ssize_t Write(const RoveCommPacket< T > &stPacket)
Sends a packet to all subscribers. This is often used to send errors or telemetry data to all devices...
Definition RoveCommUDP.h:160
ssize_t Subscribe(const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Definition RoveCommUDP.h:220
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 RoveCommUDP.cpp:357
ssize_t Unsubscribe(const manifest::AddressEntry &stIPAddress, int nPort=manifest::General::ETHERNET_UDP_PORT)
Definition RoveCommUDP.h:225
constexpr ManifestEntry STARTAUTONOMY
Definition RoveCommManifest.h:533
constexpr ManifestEntry CURRENTSTATE
Definition RoveCommManifest.h:562
RoveCommPacket< EntryType< Entry > > CreatePacket()
Definition RoveCommPacket.h:117
Identifies one callback registered with a node's On(), so Clear() can remove that callback without to...
Definition RoveCommPacket.h:104
The RoveCommPacket struct is used to store data in a packet format for transmission over a network....
Definition RoveCommPacket.h:75