-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: add an example to illustrate the usage of the dhtnet API for bot…
…h server-side and client-side. Change-Id: I57c29dcf0a0fc54dcd299edd9db1a6d2b363bcf4
- Loading branch information
Showing
5 changed files
with
177 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "certstore.h" | ||
#include "connectionmanager.h" | ||
#include "fileutils.h" | ||
|
||
#include <opendht/crypto.h> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace dhtnet { | ||
void | ||
client(dht::crypto::Identity id_client, dht::InfoHash id_server) | ||
{ | ||
fmt::print("Start client\n"); | ||
fmt::print("Client identity: {}\n", id_client.second->getId()); | ||
|
||
// Create client ConnectionManager instance | ||
auto client = std::make_shared<ConnectionManager>(id_client); | ||
|
||
// Launch dht node | ||
client->onDhtConnected(id_client.first->getPublicKey()); | ||
|
||
// Connect the client to the server's device via a channel named "channelName" | ||
client->connectDevice(id_server, | ||
"channelName", | ||
[&](std::shared_ptr<ChannelSocket> socket, const dht::InfoHash&) { | ||
fmt::print("Client: Sending request\n"); | ||
if (socket) { | ||
// Send a message (example: "Hello") to the server | ||
std::error_code ec; | ||
std::string msg = "hello"; | ||
fmt::print("Client: Sending message: {}\n", msg); | ||
std::vector<unsigned char> data(msg.begin(), msg.end()); | ||
|
||
socket->write(data.data(), data.size(), ec); | ||
// For continuous data transmission, refer to the readFromPipe | ||
// function in tools/common.cpp | ||
if (ec) { | ||
fmt::print("Client: Error writing to socket: {}\n", | ||
ec.message()); | ||
} else { | ||
fmt::print("Client: Message sent\n"); | ||
} | ||
} else { | ||
fmt::print("Client: Connection failed\n"); | ||
return; | ||
} | ||
}); | ||
|
||
// keep the client running | ||
while (true) { | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
} | ||
} | ||
} // namespace dhtnet | ||
|
||
int | ||
main(int argc, char** argv) | ||
{ | ||
// Set the log level to 0 to avoids pj logs | ||
pj_log_set_level(0); | ||
|
||
// This is the root certificate that will be used to sign other certificates | ||
auto ca = dht::crypto::generateIdentity("ca_client"); | ||
|
||
auto id_client = dht::crypto::generateIdentity("client", ca); | ||
|
||
auto id_server = dht::InfoHash(argv[1]); | ||
|
||
dhtnet::client(id_client, id_server); | ||
|
||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
#include "connectionmanager.h" | ||
#include "fileutils.h" | ||
|
||
#include <opendht/log.h> | ||
#include <opendht/crypto.h> | ||
|
||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
#include <string> | ||
#include <string_view> | ||
namespace dhtnet { | ||
void | ||
server(dht::crypto::Identity id_server) | ||
{ | ||
fmt::print("Server identity: {}\n", id_server.second->getId()); | ||
// Create an instance of ConnectionManager for the server | ||
auto server = std::make_shared<ConnectionManager>(id_server); | ||
|
||
fmt::print("Start server\n"); | ||
// Start the DHT node for the server | ||
server->onDhtConnected(id_server.first->getPublicKey()); | ||
|
||
// Handle ICE connection requests from devices | ||
// This callback is triggered when a device requests an ICE connection. | ||
// The callback should decide whether to accept or decline the request. | ||
server->onICERequest([id_server](const DeviceId& device) { | ||
// Optional: Add logic to validate the device's certificate | ||
// Example: Check if the device's certificate is signed by a trusted authority | ||
// In this example, all devices are allowed to connect | ||
fmt::print("Server: ICE request received from {}\n", device.toString()); | ||
return true; | ||
}); | ||
|
||
// Handle requests for establishing a communication channel | ||
// The callback checks if the channel should be opened based on the name or device's certificate. | ||
server->onChannelRequest( | ||
[&](const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name) { | ||
// Optional: Add logic to validate the channel name or certificate | ||
// Example: Allow the connection if the channel name is "channelName" | ||
fmt::print("Server: Channel request received from {}\n", cert->getLongId()); | ||
return name == "channelName"; | ||
}); | ||
|
||
// Define a callback when the connection is established | ||
server->onConnectionReady([&](const DeviceId& device, | ||
const std::string& name, | ||
std::shared_ptr<ChannelSocket> socket) { | ||
if (socket) { | ||
fmt::print("Server: Connection succeeded\n"); | ||
// Set up a callback to handle incoming messages on this connection | ||
socket->setOnRecv([socket](const uint8_t* data, size_t size) { | ||
fmt::print("Server: Received message: {}\n", std::string((const char*) data, size)); | ||
return size; | ||
}); | ||
} else { | ||
// The connection failed | ||
fmt::print("Server: Connection failed\n"); | ||
} | ||
}); | ||
|
||
// Keep the server running indefinitely | ||
while (true) { | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
} | ||
} | ||
|
||
} // namespace dhtnet | ||
|
||
int | ||
main() | ||
{ | ||
// Set the log level to 0 to avoids pj logs | ||
pj_log_set_level(0); | ||
|
||
// This is the root certificate that will be used to sign other certificates | ||
auto ca = dht::crypto::generateIdentity("ca"); | ||
|
||
auto id_server = dht::crypto::generateIdentity("server", ca); | ||
|
||
dhtnet::server(id_server); | ||
|
||
return 0; | ||
} |