Aglets Workbench

To Aglets Workbench Home pageThe Transfer Application

Description

How to run Transfer

Troubleshooting

Program Description

This program is composed of the following classes:

The Transfer Class

The Transfer 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 static final String SlaveClassName
                                 = "ibm.aglets.samples.TransferSlave";

   protected void go(URL url, String filename) {
      super.go(url);      
      try {
          Slave.create(null, SlaveClassName, getAgletContext(), 
                       this, new SeqItinerary(url), filename);
      } catch (IOException ae) {
          inError(ae.getMessage());
      } catch (AgletException ae) {
          inError(ae.getMessage());
      }
   }

The slave is given an itinerary of a single destination (the url parameter)and the name of the file to be transfered (specified by the filename parameter). The Transfer stationary aglet is considered the slave's master (specified by the this parameter for the Slave.create method).

The Transfer aglet, as a master, receives the slave's result (handled by the callback method) and error messages which are sent by the slave whenever its fails to perform its task (handled by the inError method).

  protected synchronized void callback(Object arg) {
     try {
        if (arg != null) {
           byte[] data = ((TransferInfo)arg).getByteArray();
           byte[] chunk = new byte[10000];
           .....

           //-- display the file by chunks of 10000 bytes.
           while (not end of file) {
             chunk = 
             _msw.appendResult(new String(chunk,255));
           }
           setTheMessage("TRANSFERED(bytes): " + data.length + 
                         "DISPLAYED(bytes): " + _msw.getResultSize());
       }
     } catch (Exception e) {
           ...
     }
     setTheMessage("Finished!!!");
  }

The callback method is overwritten to receive the content of the file (a TransferInfo object), divide it into chunks of bytes which are sequentially displayed on the result panel of the interaction window (via the appendResult method). A division into chunks is done from practical reasons since the file can be of any size, while the temporary string which is created to save its content (see the parameter to the appendResult method invocation) is of limited size. The current size of the chucks was chosen arbitrary in order to make the program work for any file size.

Finally, a message is displayed, indicating both the size of the transfered file and the number of bytes which are displayed, in practice (these numbers might differ since the capacity of the result panel is itself limited).

The Transfer Class

The TransferSlave class implements the abstract methods of the Slave class. These are initializeJob() and doJob().

   protected synchronized void doJob() throws Exception {
      String  _filePath = (String)ARGUMENT;
      File file;

      //-- check file existence
      if ((file = new File(_filePath)) == null) {
         throw new AgletException("AgletException:" + "Null File object error");
      }
      
      if (!file.exists()) 
         throw new AgletException("Try to access non-existing file");

      RESULT = new TransferInfo(readfile(file));
   }

In doJob, the slave tries to locate the local file, read it and convert its content to a new TransferInfo object. This object is assigned to the result instance variable of the slave which is finally transfered to the master.

Source Code

You can further study the Transfer 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