Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 eventQueue= null;

//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 (); EventProcessor eventProcessor= new EventProcessor(); eventProcessor.start(); } } public void putEventInQueue(Message eventData) { try { initialize(); eventQueue.put(eventData); } catch (InterruptedException ex) { ex.printStackTrace(); } } class EventProcessor extends Thread { @Override public void run() { for (;;) { Message eventData= null; try { eventData= eventQueue.take();

//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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions

Question

Are my points each supported by at least two subpoints?

Answered: 1 week ago