Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Token Ring Lab (Go)

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.

What is a token ring?

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

How it works

  1. You enter how many nodes (threads) to create.
  2. Nodes are wired in a ring: node i receives on channels[i] and sends to channels[i+1], except the last node, which sends back to itself (single-node ring edge case).
  3. The main goroutine injects messages into channels[0].
  4. Each node:
    • Receives a token (message payload + recipient ID + TTL).
    • If recipient matches its index, prints the message.
    • Otherwise decrements TTL and forwards to the next channel, or prints “Message expired” if TTL is 0.
  5. Type qqq as 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)

Requirements

  • Go 1.19 or newer

Run

go run .

Or build and run the binary:

go build -o token-ring .
./token-ring   # Linux/macOS
token-ring.exe # Windows

Usage example

MAIN 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 (0N-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.

Project layout

File Description
main.go Ring nodes, channel wiring, CLI loop
go.mod Go module definition

About

A small educational program that simulates a token ring network using Go goroutines

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages