Welcome!

SOA & WOA Authors: Peter Silva, Maureen O'Gara, Tony Bishop, Mark O'Neill, Yeshim Deniz

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

In the broadcastAglets method of the MainFileAglet, FileSearchAglet is created. To create the FileSearchAglet we first have to get the context in which the MainFileAglet is launched and that's done by calling the getAgletContext method, which returns the context of the MainFileAglet:

//Getting the context in which this aglet is running.
AgletContext context = getAgletContext();

Once we have the context, we call the createAglet method on this context. The createAglet method takes three arguments. The first one is the URL where we launch the aglet. In our case we create the FileSearchAglet on the same machine on which the MainFileAglet is created, i.e., the user machine. We write the first argument as the return value of the getCodeBase method. The getCodeBase method returns the URL where MainFileAglet is running. The second argument is the name of the aglet file that contains the code of the FileSearchAglet. In the third argument we pass the proxy of the MainFileAglet using the method getProxy. The FileSearchAglet gets the proxy in its onCreation method when it's created:

//Creating the "File Search Agent".
AgletProxy proxy = context.createAglet(
     getCodeBase(),
     examples.newfilesearch.FileSearchAglet", getProxy());

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

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

Then we create an itinerary for the FileSearchAglet, which dispatches this aglet from one machine to another. The SimpleItinerary class is a built-in class that helps us create an itinerary object that can specify a destination and a message to send to the owner aglet when it arrives at its destination. This is a very flexible class that lets us change both the destination and the message easily. We pass the current aglet as an argument to the constructor of the SimpleItinerary class to create the itinerary object for this aglet:

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

After the aglet is created, the ID of each aglet that was created is stored in the vector for future use. To create the ID, we call the getAgletID method on the proxy object that returns the ID of the aglet that holds that proxy. This ID is very important if the MainFileAglet wants to communicate with the aglet that it created, i.e., the FileSearchAglet. The ID is unique and can be used to get the proxy of that aglet for communicating with that aglet:

//Adding the AgletID of the aglet created to a vector.
agletIdVector.addElement((Object)proxy.getAgletID());

The code then compiles data for file searching by instantiating the SearchData class. The data for the file search contains the file name, the folder to search, the Remote URL, and whether to search the whole system or just a particular folder. It is called the File Search Criterion:

SearchData obj=new SearchData();
//Putting the search criteria in the SearchData object.
obj.fileName=SearchGUI.txtFileName.getText();
obj.folderToSearch= (
(String)SearchGUI.folderToSearchVector.elementAt(i)).replace('\\','/');
obj.remoteUrl=(String)SearchGUI.addressVector.elementAt(i);
if (((String)SearchGUI.folderOrSystemVector.elementAt(i)).equals(
"FOLDER"))
{
obj.folder=true;
}
else
{
obj.folder=false;
}

After the File Search Criterion is compiled this way, the code sends a message called FileSearchCriterion to the created FileSearchAglet with FileSearchCriterion as an argument:

    //Send message "FileSearchCriterion" to the aglet
    //created with the SearchData object as argument.
       proxy.sendOnewayMessage(
       new Message("FileSearchCriterion", (Object)obj));

Up until now the FileSearchAglet was created on the local machine. When the FileSearchAglet gets the FileSearchCriterion message, it retrieves the File Search Criterion from the SearchData object and dispatches itself to the URL specified in the File Search Criterion. The FileSearchAglet dispatches itself to the remote machine by calling the go method on the itinerary object. The go method takes two arguments. The first argument is the remote URL to which the FileSearchAglet needs to be dispatched. The second argument is the message to be sent by FileSearchAglet to itself when it reaches the destination machine. In this case the message is called atRemote:

public boolean handleMessage(Message msg)
{
//Handling the message "FileSearchCriteria" send by
//"Main File Agent".
if(msg.sameKind("FileSearchCriteria"))
   {
   //Retrieving the SearchData object sent by
   //"Main File Agent". This SearchData object contains
     //the file search criteria for searching.
       searchData=(SearchData)msg.getArg();
//Dispatch this aglet to remote machine as specified in
//file search criteria and send message "atRemote"
//to itself when it reaches the remote machine.
//.....
   itinerary.go(""+searchData.remoteUrl, "atRemote");
   //......
   }
//......
}


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.