Question: Threads and Networking For this assignment, we will simulate a Internet of Things ( IOT ) broker. The 'Internet of Things' describes devices that exchange
Threads and Networking
For this assignment, we will simulate a "Internet of Things" IOT broker. The 'Internet of Things' describes devices that exchange data with other devices over the internet or a network. You'll see this term often used with 'smart' home devices. In a smart home, you have lights, appliances, TVs AC thermostats, alarms, and other devices that communicate and share data with each other and can be configured via a central hub that you control. For example, you may have sensors that measure temperature in one area of your home, and that sensor will inform your thermostat which can then make a decision on whether to adjust the temperature based on your settings.
These devices often use a publish and subscribe model to share information. Devices can publish information on a topic, and subscribes can subscribe to a topic. A sensor like the one mentioned above will 'publish' temperature changes using a topic name like 'bedroomtemperature Your thermostat will subscribe to that topic and receive updates when the temperature changes.
This is a clientserver architecture. Both Subscribers and Publishers clients connect to the broker server Subscribers inform the broker that it wants to receive any messages that were published on a specified topic. Publishers send messages on a topic. A device can both publish and subscribe. The broker will coordinate and deliver messages.
We are going to create a simple version of a broker using the techniques shown in chapter
Clients will send a message to a broker using object streams section
There will be a Message class that will contain the topic and the type, and a payload if the message is publish.
The broker will receive the message and process the client request using threads. section
Details
There will be two applications that you will write a broker and a client. Clients will send messages to the broker, so you'll have to write a Message class too. Clients will send and receive messages using Object serialization over a socket.
Broker
I've provided the skeleton class for a broker based on the server example in section When the broker starts, it will create a server socket on a specific port and then start a loop where it accepts incoming connections and creates a new thread to handle that request. You will have to write the thread to handle the incoming requests. This thread should do the following:
Use object input and output streams when readingwriting a Message
Create an appropriate data structure to hold topics and a list of clients subscribed to that topic.
if the message type is 'sub', then add this client thread to a list of subscribers for this topic.
If the message type is 'pub', then send the received message to any clients that are subscribed to that topic.
Client
Our clients will use a command line interface to publish or subscribe. The format is as follows: type either 'sub' or 'pub' topic payload
Examples:
pub bedroomtemperature
sub kitchentemperature
When a client starts, it will first connect to the broker and obtain object input and output streams. Then it will start two Threads one to process commands typed in at command line using Scanner and one thread to read any published messages from broker. Both threads should start loops that do not end.
Sender Thread
read a command from standardin using Scanner.
create a instance of Message, passing in the String read from the command line to the constructor.
send message to the broker using the socket's object output stream.
Reader thread
use object input stream to get a publish message from broker
display message topic and payload to command line systemout
Message class
This class will represent a message that is sent by clients and broker.
This class will contain three attributes: message type, topic, and a payload. All are strings.
Provide one constructor that takes a String as a argument. This string represents the line that was read and must be split into type, topic, and payload and saved into the instance variables.
provide getter methods for attributes. no need for setters since we are setting the attributes using the constructor.
The toString method should be overridden to provide a pretty print of the message type, topic, and payload if present
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
