Skip to content

Implement async chat server and CLI client#35

Open
SonicDev111 wants to merge 1 commit intowhamcloud:mainfrom
SonicDev111:implementation
Open

Implement async chat server and CLI client#35
SonicDev111 wants to merge 1 commit intowhamcloud:mainfrom
SonicDev111:implementation

Conversation

@SonicDev111
Copy link
Copy Markdown

Summary

Full implementation of the simple-chat assignment: an async TCP chat server and interactive CLI client, built with Tokio.

Author

Shivendra

Architecture

The project is a Cargo workspace with three crates:

  • protocol — shared message types (ClientMessage, ServerMessage) serialized as newline-delimited JSON via serde_json
  • server — async TCP server binary backed by a server_lib library crate
  • client — async CLI client binary

Server

  • Built on Tokio — fully non-blocking, each connection is handled in its own spawned task
  • Room manages connected users in a HashMap<String, broadcast::Sender> protected by a std::sync::Mutex (held only for bookkeeping, never across await points)
  • Each user gets their own tokio::sync::broadcast channel — messages are fanned out to all other users with zero copying of the sender's own messages
  • Usernames are enforced to be unique — duplicate join attempts receive an Error message and are dropped
  • Clean disconnect handling: both explicit Leave messages and TCP disconnects trigger leave() cleanup and notify remaining users

Client

  • Accepts --host, --port, --username as CLI flags or CHAT_HOST, CHAT_PORT, CHAT_USERNAME environment variables (via clap)
  • Automatically connects and sends a Join on startup
  • Two concurrent async tasks: one reads from the server and prints messages, one reads stdin and sends commands
  • Supported commands:
    • send <MSG> — broadcasts a message to the room
    • leave — sends a Leave message and exits cleanly

Tests

19 tests total, all passing:

Suite Count
protocol unit tests 7
server_lib::room unit tests 5
Integration tests (real TCP) 7

Integration tests cover: single user join, duplicate username rejection, message broadcast, leave notification, join notification, sender does not receive own message, and multi-user chat.

Bonus

  • Pre-commit hook at hooks/pre-commit — runs cargo fmt --check, cargo clippy -- -D warnings, and cargo build
  • GitHub Actions CI (.github/workflows/ci.yml) — runs on every push and PR:
    • check job: fmt, clippy, build, and full test suite
    • integration job: starts the server, connects via nc, sends a message, and verifies clean exit

Checklist

  • Async server with Tokio
  • Unique usernames enforced
  • Broadcast to all users except sender
  • Clean leave / disconnect handling
  • Async CLI client with env var + CLI arg support
  • send <MSG> and leave commands
  • Unit tests
  • Integration tests
  • cargo fmt — no issues
  • cargo clippy -- -D warnings — zero warnings
  • Pre-commit hook
  • GitHub Actions CI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant