Apendice 1-5 (Traveller.java)

/* _________________________________________________________________________________________________ | This program was designed to perform data manipulation through TCP/IP using Agent Technology. | | Program: Traveller.java | | | | Designers: E. Odette Castellanos Cervantes is085855 | | Edgar Sandoval Reyes is091892 | | | |_________________________________________________________________________________________________| */ import aglet.*; import java.io.DataInputStream; import java.awt.*; import java.net.*; import java.util.Vector; import TravelInfo; //_____________________________________________________________________________________ public class Traveller extends Aglet { TravelWindow win; Vector Machines_to_Visit = new Vector(); TravelInfo Travel_Info; StringBuffer DataBuffer = new StringBuffer(); String CurrentNode; boolean atHome; boolean workDone; public void onCreation(Object init) { try { Travel_Info = (TravelInfo)init; System.out.println(Travel_Info.getName() + " is initilizing..."); workDone = false; atHome = false; Machines_to_Visit = (Vector)Travel_Info.getNodes(); travelNextNode(); } catch(Exception e) { System.out.println("Error:" + e.getMessage()); } } public void onDispatching(URL url) { if(win!=null) win.dispose(); } private void travelNextNode() { String node = null; boolean goAhead = true; while(goAhead) { try { if(!Machines_to_Visit.isEmpty()) node = (String)Machines_to_Visit.elementAt(0); else node = Travel_Info.getHome(); URL remote = new URL(node); CurrentNode = node; dispatch(remote); goAhead = false; } catch(Exception e) { System.out.println("Error:" + e.getMessage()); } if(goAhead) { if(!Machines_to_Visit.isEmpty()) Machines_to_Visit.removeElementAt(0); } } } public void onArrival() { if(!Machines_to_Visit.isEmpty()) Machines_to_Visit.removeElementAt(0); if(Travel_Info.getHome().equals(getMachineAddress())) { AgletProxy proxy = null; Message msg = new Message("arrival", Travel_Info.getName()); try { proxy = getAgletContext().getAgletProxy(Travel_Info.getId()); FutureReply reply = proxy.sendAsyncMessage(msg); atHome = true; } catch(Exception e) { System.out.println("Error:" + e.getMessage()); } } else workDone = false; } public void run() { try { if(atHome) { win = new TravelWindow(this, Travel_Info, DataBuffer.toString()); win.show(); } else { if(!workDone) { Execute e = new Execute(Travel_Info.getInfo()); Thread t = new Thread(e); t.start(); while(!e.done) { } DataBuffer.append(CurrentNode + "\n" + e.getData()); workDone = true; t.stop(); t = null; travelNextNode(); } } } catch(Exception e) { System.out.println("Error:" + e.getMessage()); } } public void onDisposing() { URL remote = null; AgletProxy proxy = null; Message msg = new Message("dispose", Travel_Info.getName()); try { remote = new URL(Travel_Info.getHome()); proxy = getAgletContext().getAgletProxy(remote, Travel_Info.getId()); FutureReply reply = proxy.sendAsyncMessage(msg); if(win!=null) win.dispose(); } catch(Exception e) { System.out.println("Error:" + e.getMessage()); } } public void onDeactivating(long duration) { System.out.println(Travel_Info.getName() + " is going to sleep during " + duration/1000 + " seconds"); if(win!=null) win.dispose(); } public void onActivation() { System.out.println(Travel_Info.getName() + " is awaking up"); if(atHome) { if(win==null) { win = new TravelWindow(this, Travel_Info, DataBuffer.toString()); win.show(); } } else if(!workDone) { Execute e = new Execute(Travel_Info.getInfo()); Thread t = new Thread(e); t.start(); while(!e.done) { } DataBuffer.append(CurrentNode + "\n" + e.getData()); workDone = true; t.stop(); t = null; travelNextNode(); } } public boolean handleMessage(Message msg) { System.out.println(Travel_Info.getName() +" is handling a message..."); if("your name".equals(msg.kind)) { System.out.println((String)msg.getArg()); return true; } else if("desactivate".equals(msg.kind)) { try { int time = Integer.parseInt((String)msg.getArg()); deactivate(1000 * time); } catch(AgletException e) { System.out.println("Error:" + e.getMessage()); } return true; } else if("dispose".equals(msg.kind)) { System.out.println(Travel_Info.getName() + " is being destroyed..."); if(win != null) { win.dispose(); } try { this.dispose(); } catch(AgletException e) { System.out.println("Error:" + e.getMessage()); } return true; } else if("come back".equals(msg.kind)) { URL remote = null; System.out.println(Travel_Info.getName() + " is going home..."); try { if(win!=null) win.dispose(); try { remote = new URL(Travel_Info.getHome()); } catch(MalformedURLException e) { System.out.println("URL:" + e.getMessage()); } dispatch(remote); } catch(Exception e) { System.out.println("error:" + e.getMessage()); } return true; } else if("info".equals(msg.kind)) { System.out.println(Travel_Info.getName() + " is receiving new data to seek..."); Travel_Info = (TravelInfo)msg.getArg(); workDone = false; atHome = false; Machines_to_Visit = (Vector)Travel_Info.getNodes(); travelNextNode(); return true; } return false; } private String getMachineAddress() { String tempNode = null; String Node = null; java.net.InetAddress address = null; try { address = java.net.InetAddress.getLocalHost(); tempNode = address.toString(); tempNode = tempNode.substring(tempNode.indexOf("/")+1, tempNode.length()); Node = CurrentNode.substring(CurrentNode.lastIndexOf(":"), CurrentNode.length()); } catch(java.net.UnknownHostException e) { System.out.println("Error:" + e.getMessage()); } Node = "atp://" + tempNode + Node; return Node.trim(); } } //_____________________________________________________________________________________ class TravelWindow extends Frame { Aglet _aglet; Button Destroy, Send; TextArea ta; Label la; TravelInfo ti; String data = null; TravelWindow(Aglet a, TravelInfo ti, String data) { super("Traveller Agent"); _aglet = a; this.ti = ti; this.data = data; setTitle("Traveller Agent \'" + this.ti.getName() + "\'"); setLayout(new BorderLayout()); add("North", la = new Label("Information", Label.CENTER)); la.setForeground(Color.red); la.setFont(new Font("Times", Font.BOLD, 14)); add("Center", ta = new TextArea(10,10)); Panel ButtonsPanel = new Panel(); ButtonsPanel.setLayout(new GridLayout(4,1)); Destroy = new Button("Destroy"); Send = new Button("Send"); ButtonsPanel.add(Destroy); ButtonsPanel.add(Send); add("East", ButtonsPanel); ta.setText("Sought Data [" + this.ti.getInfo() + "]\n\n"); ta.appendText(this.data); resize(400,350); } public void sendData() { AgletProxy proxy = null; Message msg = new Message("data", ti.getName() + "&" + data); try { proxy = _aglet.getAgletContext().getAgletProxy(ti.getId()); FutureReply reply = proxy.sendAsyncMessage(msg); } catch(AgletException e) { System.out.println("Error:" + e.getMessage()); } } public boolean action(Event evt, Object obj) { String msg=(String)obj; if(evt.target instanceof Button) { if(msg.equals("Destroy")) { System.out.println(ti.getName() + " is dying..."); try { _aglet.dispose(); this.dispose(); } catch(AgletException e) { System.out.println("Error: " + e.getMessage()); } return true; } else if(msg.equals("Send")) { System.out.println(ti.getName() + " is sending the data to parent..."); sendData(); return true; } else return false; } else return false; } } //_____________________________________________________________________________________