MCPcopy Create free account
hub / github.com/attermann/microReticulum / client

Function client

examples/link_native/src/main.cpp:233–292  ·  view source on GitHub ↗

This initialisation is executed when the users chooses to run as a client

Source from the content-addressed store, hash-verified

231// This initialisation is executed when the users chooses
232// to run as a client
233void client(const char* destination_hexhash) {
234 // We need a binary representation of the destination
235 // hash that was entered on the command line
236 RNS::Bytes destination_hash;
237 try {
238 int dest_len = (RNS::Type::Reticulum::TRUNCATED_HASHLENGTH/8)*2;
239 if (strlen(destination_hexhash) != dest_len) {
240 throw std::invalid_argument("Destination length is invalid, must be "+std::to_string(dest_len)+" hexadecimal characters ("+std::to_string(dest_len/2)+" bytes).");
241 }
242
243 destination_hash.assignHex(destination_hexhash);
244 }
245 catch (const std::exception& e) {
246 RNS::log("Invalid destination entered. Check your input!", RNS::LOG_ERROR);
247 return;
248 }
249
250 // Check if we know a path to the destination
251 if (!RNS::Transport::has_path(destination_hash)) {
252 RNS::log("Destination is not yet known. Requesting path and waiting for announce to arrive...");
253 RNS::Transport::request_path(destination_hash);
254 while (!RNS::Transport::has_path(destination_hash)) {
255 reticulum.loop();
256 RNS::Utilities::OS::sleep(0.1);
257 }
258 }
259
260 // Recall the server identity
261 RNS::Identity server_identity = RNS::Identity::recall(destination_hash);
262
263 // Inform the user that we'll begin connecting
264 RNS::log("Establishing link with server...");
265
266 // When the server identity is known, we set
267 // up a destination
268 RNS::Destination server_destination = RNS::Destination(
269 server_identity,
270 RNS::Type::Destination::OUT,
271 RNS::Type::Destination::SINGLE,
272 APP_NAME,
273 "linkexample"
274 );
275
276 // And create a link
277 RNS::Link link = RNS::Link(server_destination);
278
279 // We set a callback that will get executed
280 // every time a packet is received over the
281 // link
282 link.set_packet_callback(client_packet_received);
283
284 // We'll also set up functions to inform the
285 // user when the link is established or closed
286 link.set_link_established_callback(link_established);
287 link.set_link_closed_callback(link_closed);
288
289 // Everything is set up, so let's enter a loop
290 // for the user to interact with the example

Callers 1

mainFunction · 0.70

Calls 9

logFunction · 0.85
client_loopFunction · 0.85
DestinationClass · 0.50
LinkClass · 0.50
assignHexMethod · 0.45
loopMethod · 0.45
set_packet_callbackMethod · 0.45

Tested by

no test coverage detected