code cleanup and documentation

This commit is contained in:
2025-12-19 23:08:02 +01:00
parent 3fa81e9ee3
commit 1844037488
5 changed files with 59 additions and 105 deletions

View File

@@ -3,9 +3,10 @@ use bytes::Bytes;
use crate::cryptographic_signature::{CryptographicSignature, formatPubKey, sign_message};
use crate::messages_structure::{HandshakeMessage, UDPMessage};
use std::net::UdpSocket;
///
/// sends the cryptographic signature to the server using a PUT request over the HTTP API.
///
/// Registration with the server happens in two steps: first, the client
/// sends its cryptographic signature to the server using a PUT request over the HTTP API.
async fn register_with_the_server(
crypto_pair: CryptographicSignature,
) -> Result<(), reqwest::Error> {
@@ -17,7 +18,6 @@ async fn register_with_the_server(
let encoded_point = crypto_pair.pub_key.to_encoded_point(false);
let pubkey_bytes = encoded_point.as_ref().to_vec();
let pubkey_bytes_minus = pubkey_bytes[1..].to_vec();
// In order to register with the server, a peer ϕ makes a PUT request to the URL /peers/ϕ/key with its 64-byte public key in the body
let res = client.put(uri).body(pubkey_bytes_minus).send().await?;
if res.status().is_success() {
println!("Successfully registered with the server.");
@@ -34,6 +34,10 @@ async fn register_with_the_server(
Ok(())
}
///
/// sends a get request to the server to get the socket address of the given peer
///
async fn get_socket_address(username: String) -> Result<Bytes, reqwest::Error> {
let client = reqwest::Client::new();
let uri = format!("https://jch.irif.fr:8443/peers/{}/addresses", username);
@@ -50,11 +54,9 @@ async fn get_socket_address(username: String) -> Result<Bytes, reqwest::Error> {
Ok(body)
}
/// It then
/// registers each of its IP addresses by sending a Hello request to the server.
/// After the client sends a Hello request to the server, the server will verify that the client is able
/// to receive requests by sending a Hello request to the client. If the client doesnt reply to the Hello
/// request with a properly signed message, its address will not be published by the server.
///
/// registers the IP addresses by sending a Hello request to the server.
///
fn register_ip_addresses(crypto_pair: CryptographicSignature) {
let socket = UdpSocket::bind("0.0.0.0:0").expect("bind failed");
let username_size = crypto_pair.username.len();
@@ -68,7 +70,7 @@ fn register_ip_addresses(crypto_pair: CryptographicSignature) {
.expect("send failed");
let mut buf = [0u8; 1024];
socket.recv_from(&mut buf).expect("receive failed");
let hello_handshake_received = UDPMessage::parse(buf);
let hello_handshake_received = UDPMessage::parse(buf.to_vec());
hello_handshake_received.display();
}
@@ -77,6 +79,9 @@ mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
///
/// does the procedure to register with the server
///
#[tokio::test]
async fn registering_with_server() {
let username = String::from("gamixtreize");
@@ -86,6 +91,9 @@ mod tests {
}
}
///
/// retreives the socket address of a given peer
///
#[tokio::test]
async fn retreive_socket_addr() {
let username = String::from("ipjkndqfshjldfsjlbsdfjhhj");