Welcome!

SOA & WOA Authors: Salvatore Genovese, Yeshim Deniz, 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

Once the FilesearchAglet reaches the remote machine, it sends itself the atRemote message. The message handler for this calls the atRemote method. The code inside the atRemote method creates a separate thread for searching the files on the remote system. The search is done in a thread so it can be interrupted when desired. If we don't create a separate thread for searching the FileSearchAglet won't be able to listen to the messages sent by the MainFileAglet.

Below is the code snippet for the atRemote method of FileSearchAglet:

private void atRemote()
{
     //.....
     //Creating a thread for searching the file.
     thread = new SearchThread();
     //Start the thread.
     thread.start();
     //.....
}

When the search thread is started it calls the run method of the SearchThread class, which is the inner class of the FileSearchAglet. The run method of the SearchThread Class contains the code that controls the search operation. The code first checks the search criteria to see if the user wants to search the entire system or just a particular folder. If it's a particular folder then the code checks to see if the folder exists on the remote machine. If it does then the search method is called with the folder to search and the file name as an argument. If it doesn't then a message is sent to the MainFileAglet saying that the folder doesn't exist on the remote machine:

if(searchData.folder) //Specified folder.
{
    File dir = new File(""+searchData.folder
    ToSearch);
    if (dir.exists())
    {
       //call the serach method.
       search(dir,""+searchData.fileName);
    }
    else
    {
       //Send the file name and the remote url
       //where the file if found to the user.
       MainAgletProxy.sendOnewayMessage(
         new Message("Results",
         ""+dir.getAbsolutePath()+" DOES NOT EXIST ON "
         +""+searchData.remoteUrl));
    }
}

If the user wants to search the entire system the code finds the list of drives on the remote machine and searches for the required file one by one on each drive by calling the search method with the appropriate arguments:

File[] roots=File.listRoots();
//Searching the file in all drives one-by-one of the remote system.
for(int x=0;x<roots.length;x++)
{
     //call the serach method.
     search(roots[x],""+searchData.fileName);
}

The search method contains the actual logic of the file search. This method is a recursive method. It checks whether the file in the argument is a file or directory.

If it's a file it checks whether the name matches the required file name and then sends a Results message to the MainFileAglet with the entire path of the file. If the argument file is a directory then it lists the files and folders in the directory and recursively checks this list of files and folders one by one by calling itself.

Below is the code snippet of the search method of the FileSearchAglet:

private void search(File dir, String name)
{
//Check whether the current file name matches
//the specified file name or not.
   if(dir.getName().toLowerCase().indexOf(name.toLowerCase())!=-1)
     {
     //......
     MainAgletProxy.sendOnewayMessage(
     new Message(
     "Results", ""+dir.getAbsolutePath()+" | "+searchData.remoteUrl));
     //......
     }
     else if(dir.isDirectory()) //if the file is a folder.
     {
     //Get the list of files in this folder.
     String[] children=dir.list();
     for(int i=0;i<children.length;i++)
     {
       //......
       search(new File(dir,children[i]),name);
       //......
     }
   }
}


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.