Welcome!

SOA & WOA Authors: Yeshim Deniz, Salvatore Genovese, Mark O'Neill, Irfan Khan, Vikas Aggarwal

Related Topics: PowerBuilder

PowerBuilder: Article

Mobile Agent-Based File Search Utility

Creating a utility that can search for any desired document on multiple remote machines simultaneously

Starting the File Transfer
The search results are displayed on the MainFileAglet GUI in a Search Results list box. The user can select an element containing the path to the located file from this list box and click the FETCH & OPEN button to call the createFileTransferAglet method on the MainFileAglet with the file path and Remote URL as an argument:

    MainAglet.createFileTransferAglet(
       filePath.replace('\\','/'),remoteUrl.trim());

In the createFileTransferAglet the code actually creates the FileTransferAglet on the local machine. FileTransferAglet is responsible for bringing the sought file from the remote machine to the local machine. As with the FileSearchAglet, the FileTransferAglet is first created on the local machine and the proxy of the MainFileAglet is passed as an argument:

    AgletProxy proxy=context.createAglet(homeUrl,
       "examples.newfilesearch.FileTransferAglet",getProxy());

When the FileTransferAglet is created, its onCreation method is executed. The method receives a reference to the MailFileAglet as its argument. This proxy is used by the FileTransferAglet to send messages to the MainFileAglet.

Below is the code snippet of the onCreation method of FileTransferAglet:

public void onCreation(Object proxy)
{
    //Getting the proxy of the aglet who created this aglet.
    MainAgletProxy=(AgletProxy)proxy;
    //....

We also create an itinerary for the FileTransferAglet that dispatches this aglet from one machine to another:

     //Creating an itinerary object to control
     //the travel path of this aglet.
     itinerary = new SimpleItinerary(this);

The code then compiles data for the file transfer by instantiating the FileTransferData class. The file transfer data contains the remote file path and the Remote URL that contains the file sought:

//Creating an object of the "FileTransferData" class.
     FileTransferData fileTransferObj=
     new FileTransferData(filePath,remoteUrl);

It then sends a message called Go to the created FileTransferAglet with the FileTransferData object as an argument:

//Sending message "Go" to the "File Transfer Agent" //with an argument with it.
    proxy.sendOnewayMessage(new Message("Go",(Object)fileTransferObj));

The Go message is processed by the handleMessage method of the FileTransferAglet. The message handler retrieves the file transfer data from the message and then calls the startTrip method:

public boolean handleMessage(Message msg)
{
//.....
    else if(msg.sameKind("Go"))
    {
    //Getting the argument send with this message.
       fileTransObj=(FileTransferData)msg.getArg();
       remoteUrl=fileTransObj.remoteUrl;
       filePath=fileTransObj.filePath;
startTrip();
    }
//....
}

The startTrip method dispatches the aglet to the appropriate machine:

private void startTrip()
{
//.....
//Dispatch this aglet to remote machine as specified
//in file search criteria and sends message "atRemote"
//to itself when it reaches the remote machine.
    itinerary.go(""+remoteUrl, "atRemote");
    //.....
}

Once the FileTransferAglet reaches the remote machine, it sends itself the atRemote message. The message handler for this aglet calls the atRemote method. The code inside the atRemote method creates an inputstream to read from the required file and then reads the bytes into a byte buffer. Once it's read the bytes from the file it dispatches itself back to the home location, i.e., the user machine:

private void atRemote()
{
//....
//Creating byte array to hold the contents of the file.
    fileTransObj.bytes = new byte[(int)len];
    int offset = 0;
    int numRead = 0;
    //Putting the contents of the file into the bytes array.
    while(offset< fileTransObj.bytes.length
    && (numRead=is.read(fileTransObj.bytes, offset,
    fileTransObj.bytes.length - offset )) >= 0)
    {
    offset += numRead;
    }
    // Dispatch this agent to user machine.
    itinerary.go(homeUrl, "atHome");
    //....
}


More Stories By P.G. Sarang

Dr. Sarang in his long tenure of 20+ years has worked in various capacities in the IT industry. Dr. Sarang currently holds the position of Director (Architecture) with Kynetia, Spain and has been a Consultant to Sun Microsystems for last several years. He has previously worked as a Visiting Professor of Computer Engineering at University of Notre Dame, USA and is currently an adjunct faculty in the Univ. Dept. of Computer Science at University of Mumbai. Dr. Sarang has spoken in number of prestigious international conferences on Java/CORBA/XML/.NET and has authored several articles, research papers, courseware and books.

More Stories By Ninad Kamerkar

Ninad Kamerkar is currently pursuing a master's degree in computer science at the University of Mumbai in India. His research interests involve mobile agents, networking, and distributed systems. Besides his studies, he is interested in painting and listening to music.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.