JAVA PROGRAMMING
The Simpsons messaging system. Below is a sketch of a messaging
system SimBox for The Simpsons.
class SimBox implements Runnable { static final int MAX_SIZE =
10; class Message {
String sender;
String recipient;
String msg;
Message(String sender, String recipient,
String msg) {
this.sender = sender; this.recipient = recipient; this.msg =
msg;
} }
private final LinkedList<Message> messages;
private LinkedList<Message> myMessages;
private String myId;
private boolean stop = false;
public SimBox(String myId) {
messages = new LinkedList<Message>(); this.myId = myId;
this.myMessages = new LinkedList<Message>(); new
Thread(this).start();
}
public SimBox(String myId, SimBox s) { this.messages =
s.messages;
this.myId = myId;
this.myMessages = new LinkedList<Message>(); new
Thread(this).start();
}
public String getId() { return myId; }
public void stop() {
// make it so that this Runnable will
stop
}
public void send(String recipient, String msg) {
// add a message to the the shared message queue (messages)
// you will have to synchronize the message queue
}
public List<String> retrieve() {
// return the contents of myMessages
// and empty myMessages
// you will have to synchronize myMessages
// each message should be in the following format:
// From (the sender) to (the recipient) (actual
message)
}
public void run() {
// loop forever
// //
Approximately once every second move all messages
addressed to this mailbox from the shared message
queue to the private myMessages queue
To do so, you need to synchronize messages and myMessages.
Furthermore, you need to explicitly use the iterator() of messages
with a while loop. A for-each loop will not work. 3.Also
approximately once every second, if the message
queue has more than MAX_SIZE messages, delete oldest messages
so that size is at most MAX_SIZE. This part of code is provided in
the skeleton code
for(;;) { ...
} // endfor
} // end run()
}
A SimBox object is both a server and a client to the messaging
system. In its role as a client, it maintains the client’s id
(myId). This id is attached to every message sent to the mailbox.
It also maintains a personal message queue (myMessages). In its
role as a server, a mailbox has access to a message queue
(messages) shared by all clients.
SimBox has two constructors. The first one creates a new message
queue. The second one takes an existing SimBox as a parameter and
uses the message queue of that SimBox as the shared message queue.
Both constructors start running the SimBox object on a new thread.
The messaging system stays alive as long as there is at least one
participant; after construction, there is no difference in how the
mailbox that was created first or how the other mailboxes
operate.
The send() method adds a message to the shared message queue.
The retrieve() method returns the contents of the myMessages queue
as a list of strings (it has to compose a nicely formatted string
from the Message structure), and then clears the myMessages queue.
The run() method of SimBox periodically wakes up to iterate through
the message queue and look for messages whose recipient is the
current mailbox; if so, the messages are removed from the shared
message queue, and moved to the private myMessages queue.
Task. Complete the implementation of the SimBox class. Make sure
it is properly synchro- nized. The test program below shows how the
SimBox class can be used.
class Main2 {
static void pause(long n) {
try { Thread.sleep(n); } catch (InterruptedException e) {} }
public static void main (String[] args) {
final String homer = "Homer"; // "My doctor said don’t walk."
final String marge = "Marge"; // "That was a traffic signal!"
final String bart = "Bart"; // "There’s a 4:30 in the morning
now?"
final SimBox sHomer = new SimBox(homer);
final SimBox sMarge = new SimBox(marge, sHomer); // shares
sHomer.messages final SimBox sBart = new SimBox(bart, sHomer); //
shares sHomer.messages
// send out some messages on another
thread
new Thread( new Runnable() {
public void run() {
sHomer.send(marge, "My doctor said don’t walk."); pause(1000);
sMarge.send(homer, "That was a traffic signal!"); pause(500);
String msg = "There’s a 4:30 in the morning now?";
sBart.send(homer, msg); pause(500);
sHomer.send(bart, "D’oh!"); pause(500);
//sBart.send(homer, msg);
for (int i=0; i<20; ++i) {
sBart.send(homer, "flooding the message queue..."); }
} // end run() } ).start();
SimBox[] simpsons = { sMarge, sHomer, sBart }; long startTime =
System.currentTimeMillis();
// poll for messages in a tight loop for 5
secs
while (true) {
for (SimBox aSimpson : simpsons)
for (String m : aSimpson.retrieve()) System.out.println(m);
if (System.currentTimeMillis() - startTime > 5000) break; }
// endwhile
// stop each mailbox
for (SimBox aSimpson : simpsons) { aSimpson.stop(); } } // end
main()
} // end class Main2
The output of the above program varies from one run to another.
The following is one possible output:
From Homer to Marge: My doctor said don’t walk.
From Marge to Homer: That was a traffic signal!
From Bart to Homer: There’s a 4:30 in the morning now? From Homer
to Bart: D’oh!
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
From Bart to Homer: flooding the message queue...
----------SKELETON CODE--------
import java.util.*;
class SimBox implements Runnable {
static final int MAX_SIZE = 10;
class Message {
String sender;
String recipient;
String msg;
Message(String sender, String recipient, String msg) {
this.sender = sender;
this.recipient = recipient;
this.msg = msg;
}
}
private final LinkedList<Message> messages;
private LinkedList<Message> myMessages;
private String myId;
private boolean stop = false;
public SimBox(String myId) {
messages = new LinkedList<Message>();
this.myId = myId;
this.myMessages = new LinkedList<Message>();
new Thread(this).start();
}
public SimBox(String myId, SimBox s) {
this.messages = s.messages;
this.myId = myId;
this.myMessages = new LinkedList<Message>();
new Thread(this).start();
}
public String getId() { return myId; }
public void stop() {
// make it so that this Runnable will stop
}
public void send(String recipient, String msg) {
// add a message to the the shared message queue (messages)
// you will have to synchronize the message queue
}
public List<String> retrieve() {
// return the contents of myMessages
// and empty myMessages
// you will have to synchronize myMessages
// each message should be in the following format:
// From (the sender) to (the recipient) (actual message)
}
public void run() {
// loop forever
// 1. Approximately once every second move all messages
// addressed to this mailbox from the shared message
// queue to the private myMessages queue
// To do so, you need to synchronize messages and myMessages.
// Furthermore, you need to explicitly use the iterator() of
messages
// with a while loop. A for-each loop will not work.
// 2. Also approximately once every second, if the message
// queue has more than MAX_SIZE messages, delete oldest
messages
// so that size is at most MAX_SIZE. This part of code is provided
below.
for(;;) {
// synchronize messages and myMessages
// have the iterator of messages referred by iter of
// type Iterator<Message>
// while there is more to access on iter, access the message
// if the message's recipient is equal to myId, then remove
the
// message from messages and add the message to myMessages
// end of synchronized myMessages
while (messages.size() > MAX_SIZE) { messages.removeFirst();
}
// end of synchronized messages
if (stop) return;
try { Thread.sleep(1000); } catch (InterruptedException e) {}
} // endfor
} // end run()
}
JAVA PROGRAMMING The Simpsons messaging system. Below is a sketch of a messaging system SimBox for The Simpsons. class S
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
JAVA PROGRAMMING The Simpsons messaging system. Below is a sketch of a messaging system SimBox for The Simpsons. class S
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!