Two applications for generating and receiving stock quotes in real time:
- quote_server - generates quotes and broadcasts them to clients over UDP
- quote_client - connects to the server and receives quotes
quote_system/
├── Cargo.toml # Workspace
├── common/ # Shared library (StockQuote, StreamCommand, constants)
│ ├── src/lib.rs
│ └── tests/
│ └── integration_test.rs
├── server/ # quote_server binary crate
│ └── src/
│ ├── main.rs # TCP server, dispatcher, PING monitor
│ └── generator.rs # Quote generator (random walk)
├── client/ # quote_client binary crate
│ └── src/main.rs # TCP client, UDP receive, PING send
├── tickers.txt # Example tickers file
└── README.md
cargo build --releasecargo test --workspaceRUST_LOG=info cargo run --release -p quote_serverOr with parameters:
RUST_LOG=info cargo run --release -p quote_server -- \
--bind-addr 0.0.0.0:7878 \
--generation-interval-ms 100 \
--ping-timeout-secs 5RUST_LOG=info cargo run --release -p quote_client -- \
--server-addr 127.0.0.1:7878 \
--udp-port 34254 \
--tickers-file tickers.txt| Argument | Default | Description |
|---|---|---|
--bind-addr |
0.0.0.0:7878 |
TCP server address and port |
--generation-interval-ms |
100 |
Quote generation interval (ms) |
--ping-timeout-secs |
5 |
Timeout for waiting on a client PING (s) |
| Argument | Default | Description |
|---|---|---|
--server-addr |
127.0.0.1:7878 |
TCP server address |
--udp-port |
34254 |
Local port for receiving UDP |
--tickers-file |
tickers.txt |
Path to the file with the ticker list |
STREAM udp://<ip>:<port> <TICKER1>,<TICKER2>,...
Example: STREAM udp://127.0.0.1:34254 AAPL,TSLA,GOOGL
Server response:
OK- the stream has startedERR <message>- an error (invalid command, malformed address, etc.)
JSON format:
{"ticker":"AAPL","price":175.23,"volume":3456,"timestamp":1234567890}- The client sends
PINGevery 2 seconds to the server's UDP socket - If the server does not receive a PING within 5 seconds (configurable), the stream is stopped
- Generator thread - produces quotes for all 100+ tickers and sends them into a channel
- Dispatcher thread - receives quotes from the channel and distributes them to subscribed clients
- PING monitor thread - checks client timeouts and removes inactive ones
- TCP handler thread - one per incoming TCP connection
- UDP sender thread - one per active client
crossbeam-channel (mpmc) is used for inter-thread communication.
- Main thread - receives UDP data and prints quotes to the console
- PING thread - periodically sends PING to the server
One ticker per line. Empty lines and whitespace are ignored.
AAPL
GOOGL
TSLA