Aglets Workbench

To Aglets Workbench Home pageThe Writer Application

Description

How to run Writer

Troubleshooting

Program Description

This program is composed of the following classes:

The Writer Class

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

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.

Source Code

You can study the Writer aglet in more detail by directly traversing the source code:


IBM home page | IBM Japan home page | Copyright | Trademark
Last modified: Fri December 27 12:00:00 JST 1996