Question
Simulate a sensor system monitoring a house. Sensor and Dispatcher are Threads. Sensor produces a SensorEvent at random times and adds the event to the
Simulate a sensor system monitoring a house. Sensor and Dispatcher are Threads. Sensor produces a SensorEvent at random times and adds the event to the EventQueue which is implemented as a queue. The type of event depends on the type of the sensor that produced it. Dispatcher consumes events by removing them from the EventQueue. Consuming an event takes 50 ms and involves outputting on the screen the event information: source (id of the sensor), time and type. 1) Implement the above classes and in Main instantiate 4 sensors, 1 dispatcher and start all of them. Use ReentrantLock for synchronization. Use Executors for implementing the producers and consumers. Implement a second version of the program in which you use a BlockingQueue for sychronization instead of using locks.
These are the rest of the classes:
class EventData {
private String eventID;
private String eventName;
private String eventDate;
private String eventType;
private String eventLocation;
public String getEventID() {
return eventID;
}
public void setEventID(String eventID) {
this.eventID = eventID;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getEventDate() {
return eventDate;
}
public void setEventDate(String eventDate) {
this.eventDate = eventDate;
}
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public String getEventLocation() {
return eventLocation;
}
public void setEventLocation(String eventLocation) {
this.eventLocation = eventLocation;
}
}
*******************************************************
import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class QueueService{ private static QueueService instance = null; private static BlockingQueue
//define your map collection
private QueueService() {} public static QueueService getInstance() { if (instance == null) { instance = new QueueService(); } return instance; } private void initialize() { if (eventQueue== null) { eventQueue= new LinkedBlockingQueue
//check if the destination MAC exists in the map collection if(true){ // CHANGE TRUE WITH YOUR CONDITION! System.out.println("Message Data : Content : " + eventData.getContent() + " / Source : " + eventData.getSourceC().getIP() + " / Destination : " + eventData.getDestinationC().getIP()); } else{ // store the MACs inside a map collection //send the message from source to all the computers which exist inside the map collection } } catch (InterruptedException ex) { ex.printStackTrace(); } } } } }
*****************************************************************************************************************************
public class EventService{ public static void main(String arg[]) { try { EventData event = null; for (int i= 0; i< 100; i++) { event = new EventData(); event.setEventType("EventType" + i); event.setEventName("EventName:AirQuality" + i); QueueService.getInstance().putEventInQueue(event); Thread.sleep(100); } } catch (InterruptedException e) { e.printStackTrace(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started