blob: 39073b65ed09012ad9e9cde226495ba4035ffb99 [file] [log] [blame]
Amna125f5b32024-09-17 17:32:31 -04001#include "certstore.h"
2#include "connectionmanager.h"
3#include "fileutils.h"
4
5#include <opendht/crypto.h>
6
7#include <string>
8#include <vector>
9
10namespace dhtnet {
11void
12client(dht::crypto::Identity id_client, dht::InfoHash id_server)
13{
14 fmt::print("Start client\n");
15 fmt::print("Client identity: {}\n", id_client.second->getId());
16
17 // Create client ConnectionManager instance
18 auto client = std::make_shared<ConnectionManager>(id_client);
19
20 // Launch dht node
21 client->onDhtConnected(id_client.first->getPublicKey());
22
23 // Connect the client to the server's device via a channel named "channelName"
24 client->connectDevice(id_server,
25 "channelName",
26 [&](std::shared_ptr<ChannelSocket> socket, const dht::InfoHash&) {
27 fmt::print("Client: Sending request\n");
28 if (socket) {
29 // Send a message (example: "Hello") to the server
30 std::error_code ec;
31 std::string msg = "hello";
32 fmt::print("Client: Sending message: {}\n", msg);
33 std::vector<unsigned char> data(msg.begin(), msg.end());
34
35 socket->write(data.data(), data.size(), ec);
36 // For continuous data transmission, refer to the readFromPipe
37 // function in tools/common.cpp
38 if (ec) {
39 fmt::print("Client: Error writing to socket: {}\n",
40 ec.message());
41 } else {
42 fmt::print("Client: Message sent\n");
43 }
44 } else {
45 fmt::print("Client: Connection failed\n");
46 return;
47 }
48 });
49
50 // keep the client running
51 while (true) {
52 std::this_thread::sleep_for(std::chrono::seconds(1));
53 }
54}
55} // namespace dhtnet
56
57int
58main(int argc, char** argv)
59{
60 // Set the log level to 0 to avoids pj logs
61 pj_log_set_level(0);
62
63 // This is the root certificate that will be used to sign other certificates
64 auto ca = dht::crypto::generateIdentity("ca_client");
65
66 auto id_client = dht::crypto::generateIdentity("client", ca);
67
68 auto id_server = dht::InfoHash(argv[1]);
69
70 dhtnet::client(id_client, id_server);
71
72
73 return 0;
74}