Transfer is an aglet-based implementation of a utility for transfering a remote file. It consists of two aglets, one stationary (Transfer) and one mobile (TransferSlave). The latter is dispatched by Transfer to a remote location where it will make a copy of the file. Finally, TransferSlave returns to Transfer and delivers this copy.
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 Transfer aglet by specifying its class name: "ibm.aglets.samples.Transfer"(read Tahiti User's Guide for more information about aglet creation). When the Transfer 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.
Once the file is transfered, its content will be viewed in the result panel in the middile of the dialog window. In addition, a message of the form "TRANSFERED (bytes): <num> DISPLAYED (bytes):<num)" will be shown in the message panel (the bottom panel) of the dialog window. The content of large files may be partly displayed since the corresponding panel is of limited size.
Read here about troubleshooting for the sample aglets.
This program is composed of the following classes:
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 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.
You can further study the Transfer aglet in more detail by directly traversing the source code.