This initialisation is executed when the users chooses to run as a client
| 231 | // This initialisation is executed when the users chooses |
| 232 | // to run as a client |
| 233 | void 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 |
no test coverage detected