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.
Provides three server types:
- MediaServer: Stores and serves media files (e.g., images) via UUID-indexed HashMap.
- TextServer: Manages text files with content and media references.
- ChatServer: Handles client registration and message forwarding.
All servers implement the Processor trait for packet processing, command handling, and event notifications.
- Modular servers with shared
Processorlogic for commands, packets, and fragmentation. - In-memory storage: HashMap for files (UUID keys), HashSet for chat clients.
- Request handling: Deserializes JSON requests (e.g.,
WebRequest,ChatRequest) and responds via routing. - Command system: Supports node operations (add/remove neighbors, shutdown) and server-specific actions (add/remove files, get lists).
- Error management: Handles invalid UUIDs, missing files, malformed messages.
- File conversion: Uses
common::file_conversionfor path-to-file imports. - Testing: Unit tests cover creation, operations, requests, and edge cases (e.g., large files, invalid inputs).
- Processor Trait: Interface for receivers (commands, packets), assembler, routing handler, message/command handling.
- RoutingHandler: Manages neighbors and message sending.
- FragmentAssembler: Reassembles packet fragments.
- Types: From
common::types, includes commands/events (e.g.,NodeCommand,WebEvent), requests/responses. - Common Commands (NodeCommand): Handled by all servers.
AddSender(node_id, sender): Adds a neighbor to the routing handler.RemoveSender(node_id): Removes a neighbor from the routing handler.Shutdown: Signals termination (returns true to exit processing loop).
- Storage:
HashMap<Uuid, MediaFile>(title, chunked content). - Requests (WebRequest variants):
ServerTypeQuery: Responds withWebResponse::ServerType { server_type: ServerType::MediaServer }; sends eventNodeEvent::ServerTypeQueried.MediaQuery { media_id }: Parses UUID, retrieves and serializes media file if found, responds withWebResponse::MediaFile { media_data: ... }and eventWebEvent::FileServed; orWebResponse::ErrorFileNotFoundif missing; orWebResponse::BadUuidif invalid (with eventWebEvent::BadUuid).
- Commands (WebCommand variants):
GetMediaFiles: Retrieves all media files, sends eventWebEvent::MediaFiles { files: ... }.GetMediaFile { media_id, location: _ }: Retrieves specific media file if found, sends eventWebEvent::MediaFile { file: ... }.AddMediaFile(media_file): Adds file to storage, sends eventWebEvent::MediaFileAdded { uuid: ... }.AddMediaFileFromPath(file_path): Converts path to media file, adds if successful (sendsMediaFileAdded), or sendsWebEvent::FileOperationErroron failure.RemoveMediaFile(uuid): Removes file if found (sendsWebEvent::MediaFileRemoved { uuid: ... }), or sendsFileOperationErrorif missing.
- Storage:
HashMap<Uuid, TextFile>(title, content, media refs). - Requests (WebRequest variants):
ServerTypeQuery: Responds withWebResponse::ServerType { server_type: ServerType::TextServer }; sends eventNodeEvent::ServerTypeQueried.TextFilesListQuery: Retrieves formatted list ("UUID:title"), responds withWebResponse::TextFilesList { files: ... }; sends eventWebEvent::FilesListQueried.FileQuery { file_id }: Parses UUID, retrieves and serializes text file if found, responds withWebResponse::TextFile { file_data: ... }and eventWebEvent::FileServed; orWebResponse::ErrorFileNotFoundif missing; orWebResponse::BadUuidif invalid (with eventWebEvent::BadUuid).
- Commands (WebCommand variants):
GetTextFiles: Retrieves all text files, sends eventWebEvent::TextFiles { files: ... }.GetTextFile(uuid): Retrieves specific text file if found, sends eventWebEvent::TextFile { file: ... }.AddTextFile(text_file): Adds file to storage, sends eventWebEvent::TextFileAdded { uuid: ... }.AddTextFileFromPath(file_path): Converts path to text file, adds if successful (sendsTextFileAdded), or sendsWebEvent::FileOperationErroron failure.RemoveTextFile(uuid): Removes file if found (sendsWebEvent::TextFileRemoved { uuid: ... }), or sendsFileOperationErrorif missing.
- Storage:
HashSet<NodeId>for clients. - Requests (ChatRequest variants):
ServerTypeQuery: Responds withChatResponse::ServerType { server_type: ServerType::ChatServer }; sends eventNodeEvent::ServerTypeQueried.RegistrationToChat { client_id }: Registers client, responds withChatResponse::RegistrationSuccess; sends eventChatEvent::ClientRegistered.ClientListQuery: Retrieves registered client IDs, responds withChatResponse::ClientList { list_of_client_ids: ... }; sends eventChatEvent::ClientListQueried.MessageFor { client_id, message }: If target registered, forwardsChatResponse::MessageFrom { client_id: from, message }to target; else responds withChatResponse::ErrorWrongClientId.
- Commands (ChatCommand variants):
GetRegisteredClients: Retrieves registered client IDs, sends eventChatEvent::RegisteredClients { list: ... }.