Week 3 - Message Brokers

Distributed Systems and Network Programming - Spring 2023

Overview

Your task for this lab is to use RabbitMQ as a message broker for a pollution monitoring system.

System Architecture

Architecture diagram

diagram

Directory structure

.
├── docker-compose.yml
├── publishers
│   ├── sensor.py
│   └── control-tower.py
└── subscribers
    ├── receiver.py
    └── reporter.py

Publishers

Subscribers

Message Broker (RabbitMQ Server)

Task

  1. Run RabbitMQ in a docker container using docker-compose by executing the following command in the same directory as docker-compose.yml file.

    docker-compose up
    
  2. The Web UI should be available at http://localhost:15672/

    • You can see default login credentials (rabbit:1234) in the compose file.
  3. Install pika, the Python library for interacting with RabbitMQ.

    • It’s recommended to create a virtual environment instead of installing pika globally.
    • You can do so by running the following commands (you may need to install python3-venv)
      python -m venv venv
      source venv/bin/activate
      pip install pika
      
  4. Implement the system components as described above. Refer to this tutorial and pika documentation for help.

    • Connect to RabbitMQ server at localhost and create a channel.
    • Use amq.topic exchange, or create your own exchange of type topic
    • Send sensor values with a routing key starting with co2 (e.g., co2.sensor)
    • Send control queries with a routing key starting with rep (e.g., rep.current and rep.average)
    • Configure the receiver to listen for co2.* and the reporter to listen for rep.*
    • Don’t forget to ack the received messages so that they do not remain in the queue.
  5. Submit a single ZIP archive named NameSurname.zip with publishers and subscribers directories inside.

Example Run

$ python publishers/sensor.py
Enter CO2 level: 499
Enter CO2 level: 500
Enter CO2 level: 501
Enter CO2 level: 500
$ python subscribers/receiver.py
[*] Waiting for CO2 data. Press CTRL+C to exit
2023-04-03 17:19:29: OK
2023-04-03 17:20:02: OK
2023-04-03 17:21:05: WARNING
2023-04-03 17:22:08: OK

$ python publishers/control-tower.py
Enter Query: current
Enter Query: average
$ python subscribers/reporter.py
[*] Waiting for queries from the control tower. Press CTRL+C to exit
2023-04-03 17:19:30: Latest CO2 level is 499
2023-04-03 17:23:02: Average CO2 level is 500.0

Checklist