Apendice 1-6 (FutureWork.java)

Este codigo esta basado en el codigo del archivo Traveller.java pero a excepcion del anterior este codigo muestra un agente movil que para interactuar en los nodos utiliza la tecnologia JDBC de Java para accesar bases de datos. 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.yellow); 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; } } //_____________________________________________________________________________________ class DBInterface { java.sql.Connection conn; public DBInterface() throws java.sql.SQLException { conn = java.sql.DriverManager.getConnection("jdbc:sybase://rico/data", "admin", "friend"); } public String getData(String info) { try { java.sql.Statement statement = conn.createStatement(); String sql_statement = "select url from urls where key in (select key from words where word='" + info + "')"; java.sql.ResultSet result = statement.executeQuery(sql_statement); StringBuffer data = ""; while(result.next()) data.append(result.getString(1) + "\n"); statement.close(); conn.close(); } catch(java.sql.SQLException e) { e.printStackTrace(); } return data.toString(); } } //_____________________________________________________________________________________ import java.io.DataInputStream; public class Execute extends java.lang.Object implements Runnable { String info; String data; boolean done; public Execute (String info) { this.info = info; this.data = null; done = false; } public void run() { doTask(info); pause(20); done = true; } private void doTask(String data) { StringBuffer buffer = new StringBuffer(); java.util.StringTokenizer st = new java.util.StringTokenizer(data, ";"); while(st.hasMoreElements()) { String item = (String)st.nextElement(); try { Class.forName("").newInstance(); DBInterface db = new DBInterface(); buffer.append(db.getData(item)); } catch(java.sql.SQLException e) { System.out.println("Error: " + e.printStackTrace()); } } this.data = buffer.toString(); } private void pause(long seconds) { java.util.Date time = new java.util.Date(); System.out.println("started at:" + time.toString()); try { Thread.sleep(1000 * seconds); } catch(java.lang.InterruptedException e) {} time = new java.util.Date(); System.out.println("ending at:" + time.toString()); } public String getData() { return (String)data; } } //_____________________________________________________________________________________