A small educational program that simulates a token ring network using Go goroutines and channels. Each “node” is a worker that forwards messages around a logical ring until they reach the intended recipient or expire.
In a token ring LAN, stations are connected in a loop. A special frame (the token) circulates; only the station holding the token may transmit. This project does not implement a full IEEE 802.5 stack—it models the ring topology and hop-by-hop forwarding with a simple message struct and TTL (time-to-live).
- You enter how many nodes (threads) to create.
- Nodes are wired in a ring: node
ireceives onchannels[i]and sends tochannels[i+1], except the last node, which sends back to itself (single-node ring edge case). - The main goroutine injects messages into
channels[0]. - Each node:
- Receives a
token(message payload + recipient ID + TTL). - If
recipientmatches its index, prints the message. - Otherwise decrements TTL and forwards to the next channel, or prints “Message expired” if TTL is 0.
- Receives a
- Type
qqqas message data to shut down node 0 and exit the input loop; remaining nodes are signaled to stop.
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Node 0 │────▶│ Node 1 │────▶│ Node 2 │
└────▲────┘ └─────────┘ └────┬────┘
│ │
└────────────────────────────────┘
(ring)
- Go 1.19 or newer
go run .Or build and run the binary:
go build -o token-ring .
./token-ring # Linux/macOS
token-ring.exe # WindowsMAIN THREAD| Enter number of threads
3
MAIN THREAD| Enter message data or write qqq to exit
hello
MAIN THREAD| Enter recipient id (starting from 0)
2
MAIN THREAD| Enter message timeout
5
- Message data — arbitrary string shown when the target node receives it.
- Recipient id — zero-based index of the node that should print the payload (
0…N-1). - TTL — maximum hops before the message is dropped (each non-recipient node consumes one TTL).
To exit, enter qqq when prompted for message data.
| File | Description |
|---|---|
main.go |
Ring nodes, channel wiring, CLI loop |
go.mod |
Go module definition |