Apendice 1-1 (ISS.java)

/*
 ______________________________________________________________________________________________
|  This program was designed to perform Data Seeking through TCP/IP using Agent Technology,    |
|  and the program is based upon Aglets WorkBench (AWB)                                        |
|  Program: ISS.java                                                                           |
|            										       |
|  Designers: E. Odette Castellanos Cervantes 	is085855				       |
|             Edgar Sandoval Reyes		is091892				       |
|											       |
|  Date: current work									       |
|______________________________________________________________________________________________|
*/
import java.awt.*;   
import java.net.*;
import java.io.*;
import aglet.*;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
//_____________________________________________________________________________________   

public class ISS extends Aglet {
  AgletSystem win;
 
  public void onCreation(Object init) {
    win = new AgletSystem(this, "Information Seeking System");
  }
  
  public void run() {
   win.show(); 
  } 
 
  public synchronized void onDispatching(URL URL) {
      throw new SecurityException("DBMS: Don't ever try to move me!");
  }

  public void onReverting(URL remoteURL) {
       throw new SecurityException();
  }

  public void onDisposing() {
     throw new SecurityException("DBMS: Use the main window to destroy me.");
  }

  public boolean handleMessage(Message msg) {
    if ("dispose".equals(msg.kind)) {
        String agent = (String)msg.getArg();
        win.Agents.remove(agent);
        System.out.println("the agent \'" + agent + "\' has been destroyed....");
        return true;
      }
    else if("arrival".equals(msg.kind))
      {
         String agent = (String)msg.getArg();
         System.out.println("the agent \'" + agent + "\' is arriving....");
         win.atHome.addElement(agent);
         return true;
      }
    else if("data".equals(msg.kind))
      {  String name = (String)msg.getArg();
         String data = null;
         data = name.substring(name.indexOf("&")+1, name.length());
         name = name.substring(0, name.indexOf("&"));
         AgletInfo ai = (AgletInfo)win.Agents.get(name);
         System.out.println("Data is arriving from \'" + name + "\'");
         String temp = "[Agent name] " + ai.getName() + "\n";
         temp = temp + "[Itinerary] " + ai.printNodes() + "\n";
         temp = temp + "[Date creation] " + ai.getDate() + "\n";
         temp = temp + "[Sought data] " + ai.getInfo() + "\n";
         temp = temp + "_________________________________\n";
         win.Answer.setText(temp + data);
         return true;
      }
    else if("hello".equals(msg.kind)) {
      doHello();    
      return true;  
    } else
      return false; 
  }
   
  private void doHello() {
      System.out.println("Hello World from Information Seeking System");
  }
}
//_____________________________________________________________________________________
   
class AgletSystem extends Frame   
{ 
  Aglet _aglet;     
  Button New, Properties, Delete;   
  TextArea Answer;   
  TextField Status;  
  AgletProxy Proxy = null;
  AgletIdentifier Id = null; 
  Hashtable Agents;
  String home;
  Vector atHome;
   
  AgletSystem(Aglet a, String Title) {
    super(Title);	   
    _aglet=a;  
    Agents = new Hashtable();
    atHome = new Vector();
    try {
      Id = _aglet.getIdentifier();
      Proxy = _aglet.getAgletContext().getAgletProxy(Id);
    } catch(AgletException e) { System.out.println("Error: " + e.getMessage()); }
    home = "atp://" + getMachineAddress() + ":9000";
    MenuBar mb=new MenuBar();   
    Menu ma=new Menu("Agent");   
    ma.add(new MenuItem("Create")); 
    ma.add(new MenuItem("Recreate"));  
    ma.add(new MenuItem("List"));   
    ma.add(new MenuItem("Save"));   
    ma.add(new MenuItem("-"));   
    ma.add(new MenuItem("Exit"));   
    mb.add(ma);      
    Menu mo=new Menu("Options"); 
    mo.add(new MenuItem("Agent Info"));
    mo.add(new MenuItem("Current Nodes"));   
    mb.add(mo);   
    Menu mh=new Menu("Help");   
    mh.add(new MenuItem("Contents"));   
    mh.add(new MenuItem("About AWB"));   
    mh.add(new MenuItem("-"));   
    mh.add(new MenuItem("About..."));   
    mb.setHelpMenu(mh);   
    mb.add(mh);   
    setMenuBar(mb);     
   
    Panel ButtonPanel = new Panel();   
    ButtonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));   
    New = new Button("New");    
    Properties = new Button("Properties");   
    Delete = new Button("Delete");      
    ButtonPanel.add(New);   
    ButtonPanel.add(Properties);      
    ButtonPanel.add(Delete);     
    Answer = new TextArea("");   
    Status = new TextField("");   
    Status.setEditable(false);   
    setLayout(new BorderLayout(5,2));   
    add("North", ButtonPanel);   
    add("Center", Answer);   
    add("South", Status);         
    resize(600,400);    
  }   
   
  public void setStatus(String status)   
    {  Status.setText(status); }      

  public String getMachineAddress()
    { String node = null;
      java.net.InetAddress address = null;
      try {
        address = java.net.InetAddress.getLocalHost();
        node = address.toString();
        node = node.substring(node.indexOf("/")+1, node.length());
      }
      catch(java.net.UnknownHostException e) {}
      return node;
    }


  public boolean handleEvent(Event evt)
    { if(evt.id == Event.WINDOW_DESTROY)
        { System.out.println("destroying system...");
          try { _aglet.dispose(); }
          catch(AgletException e) {System.out.println("Error:" + e.getMessage());}
          dispose();  
          return true;
        }
      return super.handleEvent(evt);
    }
   
  public boolean action(Event evt, Object obj)   
    { String msg=(String)obj;   
      if(evt.target instanceof MenuItem)   
        { if(msg.equals("Create"))    
            { CreateAgletDialog CreateDialog = new CreateAgletDialog(this, _aglet);   
              CreateDialog.show(); 
              return true;   
            }   
          else if(msg.equals("Recreate"))    
                 { if(Agents.size() > 0)
                     { NewDataDialog NewDialog = new NewDataDialog(this, _aglet);
                       NewDialog.show();
                     }  
                   return true;   
                 }
          else if(msg.equals("List"))    
                 { ListDialog AgentListDialog = new ListDialog(this, _aglet);
                   AgentListDialog.show();  
                   return true;   
                 }   
          else if(msg.equals("Save"))    
                 { openFileDialog File_Dialog = new openFileDialog(this);   
                   return true;   
                 }   
          else if(msg.equals("Current Nodes"))    
                 { NodesDialog nodesDialog = new NodesDialog(this, _aglet);
                   nodesDialog.show();   
                   return true;   
                 }   
          else if(msg.equals("Agent Info"))    
                 { AgentDialog Agent_Dialog = new AgentDialog(this, _aglet);
                   Agent_Dialog.show();   
                   return true;   
                 }
          else if(msg.equals("About..."))    
                 { AboutDialog About = new AboutDialog(this, _aglet);   
                   About.show(); 
                   return true;   
                 } 
          else if(msg.equals("Contents"))    
                 { ContentsDialog contents = new ContentsDialog(this);
                   contents.show();
                   // WarningBox m = new WarningBox(this, _aglet, "That's all folks...");
                   // m.show();
                   return true;   
                 }
          else if(msg.equals("About AWB"))    
                 { AboutAWBDialog about = new AboutAWBDialog(this, _aglet);
                   about.show();   
                   return true;
                 }
          else if(msg.equals("Exit"))    
                 { try { _aglet.dispose(); }
                   catch(AgletException e) {System.out.println("Error:" + e.getMessage());}
                   dispose();  
                   return true;   
                 }
          else return false;   
       }
     else if(evt.target instanceof Button)   
        { if(msg.equals("New"))    
            { CreateAgletDialog CreateDialog = new CreateAgletDialog(this, _aglet);   
              CreateDialog.show();   
              return true;   
            }
          if(msg.equals("Properties"))    
            { AgentDialog Agent_Dialog = new AgentDialog(this, _aglet);
              Agent_Dialog.show();   
              return true;   
            }
          if(msg.equals("Delete"))    
            { Answer.setText("");   
              return true;   
            }
          else return false;
        }   
     else return false;   
    }    
  }   
//_____________________________________________________________________________________  
 
class CreateAgletDialog extends Dialog   
{ Aglet _aglet;
  AgletSystem Parent;
  TextField AgletName;   
  TextField AgletInfo;   
  TextField NodesNumber;   
  Checkbox SomeNodes;   
  Checkbox AllNodes;   
  Button CancelButton, OkButton, ClearButton; 
  List Nodes;  
     
  CreateAgletDialog(Frame Parent, Aglet a)   
    { super(Parent,"Information Seeking System",true);
      _aglet = a;   
      this.Parent = (AgletSystem)Parent;   
      AgletName = new TextField("",15);   
      AgletInfo = new TextField("",25);   
      NodesNumber = new TextField("",5); 
      NodesNumber.setFont(new Font("TimesRoman", Font.BOLD, 14));
      NodesNumber.enable(false);
      Nodes = new List(5,true);  
      Nodes.setForeground(Color.white);
      Nodes.setFont(new Font("TimesRoman", Font.PLAIN, 16));
   
      GridBagLayout PrimaryLayout = new GridBagLayout();   
      GridBagLayout Layout = new GridBagLayout();   
      setLayout(PrimaryLayout);   
      GridBagLayout InfoPanelLayout = new GridBagLayout();   
      Panel InfoPanel = new Panel();   
      GridBagConstraints InfoPanelConstraints = new GridBagConstraints();   
      InfoPanelConstraints.gridwidth = GridBagConstraints.REMAINDER;   
      Layout.setConstraints(InfoPanel,InfoPanelConstraints);   
      InfoPanel.setLayout(InfoPanelLayout);   
   
      GridBagConstraints InInfoConstraints = new GridBagConstraints();   
      InInfoConstraints.gridwidth = GridBagConstraints.REMAINDER;   
      InInfoConstraints.anchor = GridBagConstraints.NORTH; 
      
      Panel titlePanel = new Panel();
      titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
      URL remote = null;
      try { remote = new URL("http://rico/~is091892/Network.gif");}
      catch(MalformedURLException e) {System.out.println("URL:" + e.getMessage());}
      Image img1 = null;
      try { img1 = _aglet.getAgletContext().getImage(remote);}
      catch(AgletException e) {System.out.println("Error:" + e.getMessage());} 
      Label InfoLabel = new Label("Agent Creation Information");  
      InfoLabel.setFont(new Font("Times", Font.BOLD+Font.ITALIC, 16)); 
      InfoLabel.setForeground(Color.red);  
      titlePanel.add(InfoLabel);
      titlePanel.add(new LogoCanvas(img1,50,60));
      InfoPanelLayout.setConstraints(titlePanel, InInfoConstraints);   
      InfoPanel.add(titlePanel);   
   
      Panel AgletNamePanel = new Panel();   
      AgletNamePanel.setLayout(new BorderLayout());   
      AgletNamePanel.add("West",new Label("Agent Name:"));   
      AgletNamePanel.add("East", AgletName);   
      InInfoConstraints.anchor = GridBagConstraints.WEST;   
      InfoPanelLayout.setConstraints(AgletNamePanel, InInfoConstraints);   
      InfoPanel.add(AgletNamePanel);   
   
      Panel AgletInfoPanel = new Panel();   
      AgletInfoPanel.setLayout(new BorderLayout());   
      AgletInfoPanel.add("West",new Label("Seek Info:   "));   
      AgletInfoPanel.add("East", AgletInfo);   
      InInfoConstraints.anchor = GridBagConstraints.WEST;   
      InfoPanelLayout.setConstraints(AgletInfoPanel, InInfoConstraints);   
      InfoPanel.add(AgletInfoPanel);   
      PrimaryLayout.setConstraints(InfoPanel, InfoPanelConstraints);   
      add(InfoPanel);   
   
      Panel TravelPanel = new Panel();   
      TravelPanel.setLayout(new BorderLayout());  
      Label msg = new Label("How do you want the agent travel?");  
      msg.setFont(new Font("Times", Font.BOLD, 12));
      msg.setForeground(Color.darkGray);
      TravelPanel.add("North", msg);   
      CheckboxGroup TravelGroup = new CheckboxGroup();   
      SomeNodes = new Checkbox("Travel to some nodes?",TravelGroup,true);   
      AllNodes = new Checkbox("Travel to all nodes?",TravelGroup,false);   
      TravelPanel.add("Center", SomeNodes);   
      TravelPanel.add("South", AllNodes);   
   
      GridBagConstraints TravelConstraints = new GridBagConstraints();   
      TravelConstraints.gridwidth= 2;   
      TravelConstraints.gridy = GridBagConstraints.RELATIVE;   
      PrimaryLayout.setConstraints(TravelPanel, TravelConstraints);   
      add(TravelPanel);   
      GridBagConstraints NumberFieldConstraints = new GridBagConstraints();         
      NumberFieldConstraints.gridwidth = GridBagConstraints.REMAINDER;   
      PrimaryLayout.setConstraints(NodesNumber, NumberFieldConstraints);   
      add(NodesNumber); 

      GridBagConstraints NodesConstraints = new GridBagConstraints();   
      NodesConstraints.gridwidth= GridBagConstraints.REMAINDER;   
      // NodesConstraints.gridy = GridBagConstraints.RELATIVE;  
      NodesConstraints.fill = GridBagConstraints.HORIZONTAL;
      PrimaryLayout.setConstraints(Nodes, NodesConstraints);   
      add(Nodes);     
   
      // Buttons creation   
      GridBagConstraints ButtonConstraints = new GridBagConstraints();   
      Panel ButtonPanel = new Panel();   
      ButtonPanel.setLayout(new FlowLayout());   
      ButtonConstraints.gridwidth = GridBagConstraints.REMAINDER;   
      ClearButton = new Button("Clear");   
      OkButton = new Button("OK");   
      CancelButton = new Button("Cancel");      
      ButtonPanel.add(OkButton);   
      ButtonPanel.add(CancelButton); 
      ButtonPanel.add(ClearButton);  
      PrimaryLayout.setConstraints(ButtonPanel, ButtonConstraints);   
      add(ButtonPanel);   
      getNodes();
      // dialog size  
      resize(330,370);   
    }   

  public void getNodes()   
    { String node = null; 
      String temp = null;   
      try {  
        DataInputStream is = new DataInputStream(new FileInputStream("/home/u/raid/vol07/is091892//Aglets/nodes.dat"));  
        node = is.readLine(); 
        java.net.InetAddress address = null;
        while((node.length())!=0)   
          { try {
              temp = node.substring(node.lastIndexOf("/")+1, node.lastIndexOf(":"));
              address = java.net.InetAddress.getByName(temp);
              temp = address.toString();
              temp = temp.substring(temp.indexOf("/")+1, temp.length());
              temp = "atp://" + temp + node.substring(node.lastIndexOf(":"), node.length());
              temp = temp.trim();
            }
            catch(java.net.UnknownHostException e) {}
            if(!temp.equals(Parent.home)) Nodes.addItem(node); 
            node = is.readLine();   
          }  
      }   
      catch(Exception e) { System.out.println(e.getMessage()); }   
    }
   
  public boolean handleEvent(Event evt)   
    {  if(evt.target == AllNodes)   
         { NodesNumber.setText("");
           for(int i=0; i