|
1 | | -# Common |
2 | | -This library contains all the code that is common between each component (clients and servers). |
| 1 | +# Overview of the `common` library |
3 | 2 |
|
4 | | -## How to Instantiate a generic Node (client or server) |
5 | | -It follows an example implementation of a generic Node (in this case a Server): |
| 3 | +The `common` library provides foundational components for a drone-based network simulation in Rust, supporting packet routing, fragmentation, reassembly, and node management in an unreliable network environment. It integrates with `wg_internal` for core packet and node primitives. Key design principles include idempotent operations, flood-based discovery, source routing, and resilience to packet drops or node crashes via acknowledgments and retries. |
6 | 4 |
|
7 | | -```rust |
8 | | -pub struct Server { |
9 | | - routing_handler: RoutingHandler, |
10 | | - received_messages: Vec<(String, String)>, |
11 | | - communication_server: Vec<NodeId>, |
12 | | - controller_recv: Receiver<Box<dyn Any>>, |
13 | | - packet_recv: Receiver<Packet>, |
14 | | - assembler: FragmentAssembler, |
15 | | - ... |
16 | | -} |
| 5 | +## Modules and Key Components |
17 | 6 |
|
18 | | -impl Server { |
19 | | - #[must_use] |
20 | | - pub fn new( |
21 | | - id: NodeId, |
22 | | - neighbors: HashMap<NodeId, Sender<Packet>>, |
23 | | - packet_recv: Receiver<Packet>, |
24 | | - controller_recv: Receiver<Box<dyn Any>>, |
25 | | - controller_send: Sender<Box<dyn Any>>, |
26 | | - ... |
27 | | - ) -> Self { |
28 | | - let routing_handler = RoutingHandler::new(id, NodeType::Client, neighbors, controller_send); |
| 7 | +### `types` |
| 8 | +Defines core data structures and enums for network entities, files, requests/responses, commands, and events. |
29 | 9 |
|
30 | | - Self { |
31 | | - routing_handler, |
32 | | - received_messages: vec![], |
33 | | - communication_server: vec![], |
34 | | - controller_recv, |
35 | | - packet_recv, |
36 | | - assembler: FragmentAssembler::default() |
37 | | - ... |
38 | | - } |
39 | | - } |
40 | | -} |
| 10 | +- **MediaReference**: Represents a reference to media stored at a specific node (NodeId) with a UUID. |
| 11 | +- **TextFile**: Encapsulates a text file with title, content, and embedded media references. |
| 12 | +- **MediaFile**: Handles binary media files, chunked into 1024-byte segments for transmission. |
| 13 | +- **File**: Composite of a TextFile and associated MediaFiles. |
| 14 | +- **WebRequest/WebResponse**: Enums for web-like queries (e.g., server type, file lists, media retrieval) and responses (e.g., data delivery, errors like not found or UUID parsing failures). |
| 15 | +- **ChatRequest/ChatResponse**: Enums for chat operations (e.g., registration, client lists, messaging) and responses (e.g., message delivery, client lists). |
| 16 | +- **Event/Command**: Traits and enums for node-specific events (e.g., NodeEvent for packet sent/flood started) and commands (e.g., NodeCommand for adding/removing senders, shutdown). |
| 17 | +- **ChatEvent/WebEvent/NodeEvent**: Specific event variants for chat (e.g., message received, registration), web (e.g., file added/removed, queries), and general node operations. |
| 18 | +- **ClientType/ServerType/NodeType**: Enums classifying nodes (e.g., ChatClient, TextServer, Drone). |
41 | 19 |
|
| 20 | +### `assembler` |
| 21 | +Manages packet fragmentation and reassembly. |
42 | 22 |
|
43 | | -impl Processor for Server { |
44 | | - fn controller_recv(&self) -> &Receiver<Box<dyn Any>> { |
45 | | - &self.controller_recv |
46 | | - } |
| 23 | +- **FragmentAssembler**: Tracks fragments by session ID and sender NodeId. Adds fragments, checks completeness via expected/received counts, and reassembles data into a complete message when all fragments arrive. |
47 | 24 |
|
48 | | - fn packet_recv(&self) -> &Receiver<Packet> { |
49 | | - &self.packet_recv |
50 | | - } |
| 25 | +### `file_conversion` |
| 26 | +Utilities for converting local files to library types. |
51 | 27 |
|
52 | | - fn handle_command(&mut self, cmd: Box<dyn Any>) { |
53 | | - if let Ok(cmd) = cmd.downcast::<ServerCommand>() { |
54 | | - match *cmd { |
55 | | - // match on server command |
56 | | - } |
57 | | - } |
| 28 | +- **file_to_media_file**: Reads binary file content, chunks it, and creates a MediaFile. |
| 29 | +- **file_to_text_file**: Reads text file content and creates a TextFile (without media refs by default). |
58 | 30 |
|
59 | | - } |
| 31 | +### `network` |
| 32 | +Models the network topology and operations. |
60 | 33 |
|
61 | | - fn assembler(&mut self) -> &mut FragmentAssembler { |
62 | | - &mut self.assembler |
63 | | - } |
| 34 | +- **NetworkError**: Enum for errors like path not found, node removal, or send failures. |
| 35 | +- **Node**: Represents a network node with ID, type (NodeType), and adjacent nodes. |
| 36 | +- **Network**: Maintains a list of nodes; supports adding/removing/updating nodes, changing types, finding shortest paths via BFS, and filtering by type (e.g., get_servers, get_clients). |
64 | 37 |
|
65 | | - fn routing_header(&mut self) -> &mut RoutingHandler { |
66 | | - &mut self.routing_handler |
67 | | - } |
| 38 | +### `routing_handler` |
| 39 | +Handles routing logic, including discovery and packet transmission. |
68 | 40 |
|
69 | | - fn handle_msg(&mut self, msg: Vec<u8>) { |
| 41 | +- **RoutingHandler**: Core struct managing node ID, network view (Network), neighbors (senders by NodeId), flood tracking, and buffers for packets/fragments. |
| 42 | + - Initiates floods for discovery (start_flood). |
| 43 | + - Handles flood requests/responses to update topology. |
| 44 | + - Sends messages with fragmentation if >128 bytes (send_message). |
| 45 | + - Processes acks (mark fragments received), nacks (retry or remove faulty nodes), and retries (retry_send). |
| 46 | + - Manages neighbor addition/removal and buffering for pending packets. |
70 | 47 |
|
71 | | - if let Ok(msg) = serde_json::from_slice::<ClientRequest>(&msg) { |
72 | | - match msg { |
73 | | - // match on ClientRequest |
74 | | - } |
75 | | - } |
76 | | - } |
77 | | -} |
78 | | -``` |
| 48 | +### `packet_processor` |
| 49 | +Defines processing loop for packets and commands. |
| 50 | + |
| 51 | +- **Processor**: Trait for entities that handle incoming packets and commands. |
| 52 | + - Integrates FragmentAssembler and RoutingHandler. |
| 53 | + - Processes packets (e.g., fragments to reassemble messages, acks/nacks/floods via routing handler). |
| 54 | + - Runs an event loop selecting between controller commands (handle_command) and packets (handle_packet), with flood initiation on start. |
| 55 | + - Subtypes must implement message handling (handle_msg) and command processing. |
0 commit comments