|
1 | | -# chat-server |
| 1 | +# Overview of the `server` library |
| 2 | + |
| 3 | +The `server` library implements specialized Rust server nodes for media, text, and chat handling in a packet-based networked system. It integrates with `wg_internal` for networking and `common` for shared utilities, using channels for inter-thread communication and a routing handler for message routing. |
| 4 | + |
| 5 | +Provides three server types: |
| 6 | +- **MediaServer**: Stores and serves media files (e.g., images) via UUID-indexed HashMap. |
| 7 | +- **TextServer**: Manages text files with content and media references. |
| 8 | +- **ChatServer**: Handles client registration and message forwarding. |
| 9 | + |
| 10 | +All servers implement the `Processor` trait for packet processing, command handling, and event notifications. |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- Modular servers with shared `Processor` logic for commands, packets, and fragmentation. |
| 15 | +- In-memory storage: HashMap for files (UUID keys), HashSet for chat clients. |
| 16 | +- Request handling: Deserializes JSON requests (e.g., `WebRequest`, `ChatRequest`) and responds via routing. |
| 17 | +- Command system: Supports node operations (add/remove neighbors, shutdown) and server-specific actions (add/remove files, get lists). |
| 18 | +- Error management: Handles invalid UUIDs, missing files, malformed messages. |
| 19 | +- File conversion: Uses `common::file_conversion` for path-to-file imports. |
| 20 | +- Testing: Unit tests cover creation, operations, requests, and edge cases (e.g., large files, invalid inputs). |
| 21 | + |
| 22 | +## Architecture |
| 23 | + |
| 24 | +### Core Components |
| 25 | + |
| 26 | +- **Processor Trait**: Interface for receivers (commands, packets), assembler, routing handler, message/command handling. |
| 27 | +- **RoutingHandler**: Manages neighbors and message sending. |
| 28 | +- **FragmentAssembler**: Reassembles packet fragments. |
| 29 | +- **Types**: From `common::types`, includes commands/events (e.g., `NodeCommand`, `WebEvent`), requests/responses. |
| 30 | +- **Common Commands (NodeCommand)**: Handled by all servers. |
| 31 | + - `AddSender(node_id, sender)`: Adds a neighbor to the routing handler. |
| 32 | + - `RemoveSender(node_id)`: Removes a neighbor from the routing handler. |
| 33 | + - `Shutdown`: Signals termination (returns true to exit processing loop). |
| 34 | + |
| 35 | +### Server Details |
| 36 | + |
| 37 | +#### MediaServer |
| 38 | +- **Storage**: `HashMap<Uuid, MediaFile>` (title, chunked content). |
| 39 | +- **Requests** (WebRequest variants): |
| 40 | + - `ServerTypeQuery`: Responds with `WebResponse::ServerType { server_type: ServerType::MediaServer }`; sends event `NodeEvent::ServerTypeQueried`. |
| 41 | + - `MediaQuery { media_id }`: Parses UUID, retrieves and serializes media file if found, responds with `WebResponse::MediaFile { media_data: ... }` and event `WebEvent::FileServed`; or `WebResponse::ErrorFileNotFound` if missing; or `WebResponse::BadUuid` if invalid (with event `WebEvent::BadUuid`). |
| 42 | +- **Commands** (WebCommand variants): |
| 43 | + - `GetMediaFiles`: Retrieves all media files, sends event `WebEvent::MediaFiles { files: ... }`. |
| 44 | + - `GetMediaFile { media_id, location: _ }`: Retrieves specific media file if found, sends event `WebEvent::MediaFile { file: ... }`. |
| 45 | + - `AddMediaFile(media_file)`: Adds file to storage, sends event `WebEvent::MediaFileAdded { uuid: ... }`. |
| 46 | + - `AddMediaFileFromPath(file_path)`: Converts path to media file, adds if successful (sends `MediaFileAdded`), or sends `WebEvent::FileOperationError` on failure. |
| 47 | + - `RemoveMediaFile(uuid)`: Removes file if found (sends `WebEvent::MediaFileRemoved { uuid: ... }`), or sends `FileOperationError` if missing. |
| 48 | + |
| 49 | +#### TextServer |
| 50 | +- **Storage**: `HashMap<Uuid, TextFile>` (title, content, media refs). |
| 51 | +- **Requests** (WebRequest variants): |
| 52 | + - `ServerTypeQuery`: Responds with `WebResponse::ServerType { server_type: ServerType::TextServer }`; sends event `NodeEvent::ServerTypeQueried`. |
| 53 | + - `TextFilesListQuery`: Retrieves formatted list ("UUID:title"), responds with `WebResponse::TextFilesList { files: ... }`; sends event `WebEvent::FilesListQueried`. |
| 54 | + - `FileQuery { file_id }`: Parses UUID, retrieves and serializes text file if found, responds with `WebResponse::TextFile { file_data: ... }` and event `WebEvent::FileServed`; or `WebResponse::ErrorFileNotFound` if missing; or `WebResponse::BadUuid` if invalid (with event `WebEvent::BadUuid`). |
| 55 | +- **Commands** (WebCommand variants): |
| 56 | + - `GetTextFiles`: Retrieves all text files, sends event `WebEvent::TextFiles { files: ... }`. |
| 57 | + - `GetTextFile(uuid)`: Retrieves specific text file if found, sends event `WebEvent::TextFile { file: ... }`. |
| 58 | + - `AddTextFile(text_file)`: Adds file to storage, sends event `WebEvent::TextFileAdded { uuid: ... }`. |
| 59 | + - `AddTextFileFromPath(file_path)`: Converts path to text file, adds if successful (sends `TextFileAdded`), or sends `WebEvent::FileOperationError` on failure. |
| 60 | + - `RemoveTextFile(uuid)`: Removes file if found (sends `WebEvent::TextFileRemoved { uuid: ... }`), or sends `FileOperationError` if missing. |
| 61 | + |
| 62 | +#### ChatServer |
| 63 | +- **Storage**: `HashSet<NodeId>` for clients. |
| 64 | +- **Requests** (ChatRequest variants): |
| 65 | + - `ServerTypeQuery`: Responds with `ChatResponse::ServerType { server_type: ServerType::ChatServer }`; sends event `NodeEvent::ServerTypeQueried`. |
| 66 | + - `RegistrationToChat { client_id }`: Registers client, responds with `ChatResponse::RegistrationSuccess`; sends event `ChatEvent::ClientRegistered`. |
| 67 | + - `ClientListQuery`: Retrieves registered client IDs, responds with `ChatResponse::ClientList { list_of_client_ids: ... }`; sends event `ChatEvent::ClientListQueried`. |
| 68 | + - `MessageFor { client_id, message }`: If target registered, forwards `ChatResponse::MessageFrom { client_id: from, message }` to target; else responds with `ChatResponse::ErrorWrongClientId`. |
| 69 | +- **Commands** (ChatCommand variants): |
| 70 | + - `GetRegisteredClients`: Retrieves registered client IDs, sends event `ChatEvent::RegisteredClients { list: ... }`. |
0 commit comments