This crate exposes the gRPC API for the macro traffic simulation via 4-step demand model. It can be used as a Rust library (crates.io), run as a server binary, and distributed via Docker. Go and Python client stubs can be generated from the same protos.
- Prerequisites for building from source
- Build and run (binary)
- Docker
- Pre-built binaries from GitHub releases page
- Usage
- Client code generation
- Rust 1.91.0 which is tested with 2024 edition in my case
protocavailable on PATH- Optional:
dockerfor container builds
- Debug build (library mode):
make build
- Run the gRPC server:
make run-server
- Release binary:
make build-release- Binary path:
target/release/macro_traffic_sim
Notes:
- The server is behind a Cargo feature flag
server. Commands above enable it when needed. Those are basically:
cargo build --release --features server- Default listen address is
0.0.0.0:50052. Override withMACRO_SIM_ADDRenvironment variable (e.g.,MACRO_SIM_ADDR=0.0.0.0:25250). - Session TTL is controlled by
MACRO_SIM_SESSION_TTLenvironment variable (default:600seconds).
There are two supported paths: build locally with Dockerfile, or pull from registry.
- Build
make docker-build IMAGE=macro-traffic-sim/server TAG=latest
- Run
make docker-run IMAGE=macro-traffic-sim/server TAG=latest- This maps host port 50052 -> container port 50052.
The Docker image is built with a multi-stage process (Rust builder + slim runtime). It compiles with the server feature enabled.
The server image is available from both Docker Hub and GitHub Container Registry.
Docker Hub:
docker pull dimahkiin/macro-traffic-sim-server:latest
docker run --rm -it -p 50052:50052 -e MACRO_SIM_ADDR=0.0.0.0:50052 dimahkiin/macro-traffic-sim-server:latestGitHub Container Registry:
docker pull ghcr.io/lddl/macro-traffic-sim-server:latest
docker run --rm -it -p 50052:50052 -e MACRO_SIM_ADDR=0.0.0.0:50052 ghcr.io/lddl/macro-traffic-sim-server:latestReplace latest with a specific version tag (e.g., 0.2.1) for reproducible deployments.
Download pre-built binaries from the GitHub Releases page.
Available builds:
- Linux (amd64):
macro-traffic-sim-server-{version}-linux-amd64.tar.gz - Windows (amd64):
macro-traffic-sim-server-{version}-windows-amd64.zip
Linux example:
# Download and extract
wget https://github.com/LdDl/macro_traffic_sim_grpc/releases/download/v0.1.2/macro-traffic-sim-server-0.2.1-linux-amd64.tar.gz
tar -xzf macro-traffic-sim-server-0.2.1-linux-amd64.tar.gz
# Run the server
./macro_traffic_simWindows example:
# Extract the zip file, then run:
.\macro_traffic_sim.exeThe server listens on 0.0.0.0:50052 by default. Override with MACRO_SIM_ADDR environment variable (e.g., MACRO_SIM_ADDR=0.0.0.0:25250). Session TTL: MACRO_SIM_SESSION_TTL (default 600 seconds).
E.g. we can run the server in debug mode with:
cargo run --features server --bin macro_traffic_simTo use a custom address and session TTL:
MACRO_SIM_ADDR=0.0.0.0:25250 MACRO_SIM_SESSION_TTL=1200 cargo run --features server --bin macro_traffic_simAdd the crate to your project: cargo add macro_traffic_sim
export MACRO_SIM_ADDR=127.0.0.1:50052
cargo run --example rust_clientHere more details: clients/go/README.md
export MACRO_SIM_ADDR=127.0.0.1:50052
# from repository root
cd ./clients/go
go run ./cmd/example/main.goHere more details: clients/python/README.md
export MACRO_SIM_ADDR=127.0.0.1:50052
# from repository root
cd ./clients/python
source .venv/bin/activate
python examples/main.pyThis section describes how I've used to generate client code for different languages from the proto files.
Client code generation for Golang is done via scripts/gen_go.sh. It requires protoc and protoc-gen-go to be installed and available on PATH.
chmod +x ./scripts/gen_go.sh
./scripts/gen_go.sh clients/go
cd ./clients/go
go mod init github.com/LdDl/macro_traffic_sim_grpc/clients/go
go mod tidy
cd -Client code generation for Python is done via scripts/gen_python.sh. The script automatically creates a virtual environment and installs all dependencies.
chmod +x ./scripts/gen_python.sh
./scripts/gen_python.shThe script:
- Creates
.venvinclients/python/(if not exists) - Installs dependencies from
requirements.txt - Generates
*_pb2.py,*_pb2.pyi(type stubs), and*_pb2_grpc.py - Installs the
macro-traffic-simpackage in editable mode