Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quote System - a stock-quote streaming system

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

Project structure

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

Build

cargo build --release

Running the tests

cargo test --workspace

Running

1. Start the server

RUST_LOG=info cargo run --release -p quote_server

Or 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 5

2. Start the client

RUST_LOG=info cargo run --release -p quote_client -- \
    --server-addr 127.0.0.1:7878 \
    --udp-port 34254 \
    --tickers-file tickers.txt

Server arguments

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)

Client arguments

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

Protocol

The STREAM command (TCP)

STREAM udp://<ip>:<port> <TICKER1>,<TICKER2>,...

Example: STREAM udp://127.0.0.1:34254 AAPL,TSLA,GOOGL

Server response:

  • OK - the stream has started
  • ERR <message> - an error (invalid command, malformed address, etc.)

Quote data (UDP)

JSON format:

{"ticker":"AAPL","price":175.23,"volume":3456,"timestamp":1234567890}

Keep-Alive (UDP)

  • The client sends PING every 2 seconds to the server's UDP socket
  • If the server does not receive a PING within 5 seconds (configurable), the stream is stopped

Architecture

Server (threads)

  • 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.

Client (threads)

  • Main thread - receives UDP data and prints quotes to the console
  • PING thread - periodically sends PING to the server

Ticker file format

One ticker per line. Empty lines and whitespace are ignored.

AAPL
GOOGL
TSLA

About

Real-time market quote streaming in Rust: TCP control channel + UDP data plane, keep-alive protocol, crossbeam channels. Course assignment.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages