Writer is an aglet-based implementation of the "write" application in Unix. It consists of two aglets, one of them is stationary (Writer) and the other is mobile (WriterSlave). The latter is dispatched by Writer to a remote location, where it will pops up a window and displays a message from the Writer aglet. Finally, the WriterSlave returns to Writer and delivers an acknowledgment from the reader of the message.
To be written.
Run at least two aglet servers in your network (read here how to run an aglet server).
Use the "new aglet" panel of Tahiti to create your Writer aglet by specifying its class name: "ibm.aglets.samples.Writer"(read Tahiti User's Guide for more information about aglet creation). When the Writer aglet is created, its dialog window is displayed:
atp://aglets.trl.ibm.com
or, if your aglet server is running on a port other than the default one (434):
atp://aglets.trl.ibm.com:500
In order to update the AddressBook, click once to display its panel. Then, either select an address and remove it (via the delete button) or add the current address in the "Address" field (via the add button). Click on the close button to hide the AddressBook panel.
When the WriterSlave aglet arrives at the target host, it pops up a new window that looks like this:
The window includes the sender's name, the host name and the original message text. The window is displayed for at most 10 seconds unless the receiver presses either the Quit or Thank buttons, causing the window to be closed.
When the WriterSlave aglet returns to its master, it delivers as an acknowledgment the "Returned" String, which is displayed on the message panel (the bottom panel) of the Writer's interaction window. When the Thank button of the message window is pressed, the acknowledgment is the "Returned: THANKS!!" string.
Read here about troubleshooting for the sample aglets.
This program is composed of the following classes:
The Writer class inherits the SampleAglet abstract class for a stationary aglet (see a description of the SampleAglet abstract class here). Its go method creates the slave aglet.
protected void go(URL url, String text) { ... AgletContext ac = null; String username = "Unknown"; try { ac=getAgletContext(); username = ac.getProperty("aglets.tahiti.user.name", "Unknown"); } catch ... Arguments args = new Arguments(); args.putArgument("msg",text); args.putArgument("user", username); try { Slave.create(null, "ibm.aglets.samples.WriterSlave", getAgletContext(), this, new SeqItinerary(url), args); } catch ... }
The slave is given an itinerary of a single destination (the url parameter), the message text (the text parameter), and an Argument object (the args) that includes the message text and the sender's username. The Writer aglet is considered the slave's master (specified by the this argument of the Slave.create method).
The Writer aglet, as a master, receives the slave's result (handled by the callback method) and error messages sent by the slave whenever its fails to perform its task (handled by the inError method).
protected synchronized void callback(Object obj) { if (obj != null) { try { setTheMessage((String)obj); // print on the message panel } catch (Exception e) { // not yet implemented } } }
The callback method is overwritten to receive the result (a String object) and to display it in the message panel of the interaction window (via the setTheMessage method).
The WriterSlave class implements the abstract methods of the Slave class. These are initializeJob() and doJob().
private final int SHOW_TIME = 10
protected synchronized void doJob() throws AgletException { WriterSlaveWindow win = null; try { String from = new String ((String)(((Arguments)ARGUMENT).getArgument("user")) + "@" + getOrigin().getHost()); win = new WriterSlaveWindow(this, (String)(((Arguments)ARGUMENT).getArgument("msg")), // the message from); // the sender } catch (Exception e) { ... } try { wait(SHOW_TIME*1000); } catch (Exception e) { e.printStackTrace(); } win.dispose(); setResult("returned" + ((RESULT != null) ? ":" + (String)RESULT : ".")); // acknowledgment }
In doJob(), the slave creates and pops up a new WriterSlaveWindow window (this class is found in the same source file of the WriterSlave class) in which the message and its source (user name and host name) are displayed. Unless either the Quit or Thank buttons are pressed earlier, the window is closed after 10 seconds and then slave returns to its master.
You can study the Writer aglet in more detail by directly traversing the source code: