[Aglets-commits] aglets2.1/src/examples/finder HostTravellor.class,NONE,1.1 Test.java,NONE,1.1 HostT
Status: Beta
Brought to you by:
cat4hire
From: Luca F. <cat...@us...> - 2005-11-07 11:38:28
|
Update of /cvsroot/aglets/aglets2.1/src/examples/finder In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7551/src/examples/finder Added Files: HostTravellor.class Test.java HostTravellor.java Finder.class Traveller.java HostList.java Register.class Test.class Finder.java HostCollector.class Traveller.class HostCollector.java HostList.class Register.java Log Message: --- NEW FILE: Test.class --- (This appears to be a binary file; contents omitted.) --- NEW FILE: HostTravellor.class --- (This appears to be a binary file; contents omitted.) --- NEW FILE: HostCollector.java --- package examples.finder; /* * @(#)HostCollector.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.aglet.*; import com.ibm.aglet.event.*; import com.ibm.aglet.message.Message; import java.net.URL; import java.util.Hashtable; import java.util.Enumeration; /** * The HostCollector carries a list of aglet server names. * When it moves to another server, it adds current server address * to the list. If there is no HostList aglet is running, it creates * an instance of HostList on that server. If a HostList aglet is running, * it appends a list of server name to list in the HostList and * then get the list belongs to the HostList. * * @version 1.00 $Date: 2005/11/07 11:38:10 $ * @author Yoshiaki Mima * @see examples.finder.HostList */ public class HostCollector extends Aglet implements MobilityListener { Hashtable hostList; public void appendList(Hashtable list) { for (Enumeration e = list.keys(); e.hasMoreElements(); ) { Object key = e.nextElement(); if (hostList.get(key) == null) { System.out.println("new: " + key); hostList.put(key, "new"); } else { System.out.println("key: " + key + " value: " + hostList.get(key)); } } } public boolean handleMessage(Message msg) { if (msg.sameKind("dialog")) { try { System.out.println(hostList); } catch (Exception ex) { ex.printStackTrace(); } return true; } else if (msg.sameKind("shutdown")) { try { deactivate(0); } catch (Exception e) {} return true; } else {} return true; } public void onArrival(MobilityEvent event) { AgletProxy ap = (AgletProxy)getAgletContext().getProperty("hostlist"); hostList.put(getAgletContext().getHostingURL().toString(), "running"); try { if ((ap == null) ||!ap.isValid()) { ap = getAgletContext().createAglet(getCodeBase(), "examples.finder.HostList", hostList); } ap.sendMessage(new Message("append", hostList)); Hashtable list = (Hashtable)ap.sendMessage(new Message("getlist")); if (list != null) { appendList(list); } } catch (Exception e) { System.out.println(e); } } public void onCreation(Object init) { hostList = new Hashtable(); AgletProxy ap = (AgletProxy)getAgletContext().getProperty("hostlist"); hostList.put(getAgletContext().getHostingURL().toString(), "running"); try { if ((ap == null) ||!ap.isValid()) { ap = getAgletContext().createAglet(getCodeBase(), "examples.finder.HostList", hostList); } } catch (Exception e) { System.out.println(e); } addMobilityListener(this); } public void onDispatching(MobilityEvent event) {} public void onRetraction(MobilityEvent event) {} public void onReverting(MobilityEvent event) {} } --- NEW FILE: HostCollector.class --- (This appears to be a binary file; contents omitted.) --- NEW FILE: Traveller.java --- package examples.finder; /* * @(#)Traveller.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.aglet.*; public class Traveller extends Aglet { Register register = null; public void onCreation(Object o) { // get the default finder.. AgletProxy finder = (AgletProxy)getAgletContext().getProperty("finder"); register = new Register(this, finder, "Traveller"); } public void onDisposing() { register.unregister(); } } --- NEW FILE: Traveller.class --- (This appears to be a binary file; contents omitted.) --- NEW FILE: Register.java --- package examples.finder; /* * @(#)Register.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.aglet.*; import com.ibm.aglet.event.*; import com.ibm.aglet.message.Message; // now this is reusable! public class Register extends MobilityAdapter { AgletProxy _finder; Message _msg = new Message("Register"); Register(Aglet a, AgletProxy finder, String name) { a.addMobilityListener(this); _msg.setArg("NAME", name); _finder = finder; AgletProxy proxy = a.getAgletContext().getAgletProxy(a.getAgletID()); register(proxy); } public void onArrival(MobilityEvent me) { System.out.println(me.getAgletProxy()); register(me.getAgletProxy()); } public void register(AgletProxy proxy) { _msg.setArg("PROXY", proxy); try { _finder.sendOnewayMessage(_msg); } catch (Exception ex) { ex.printStackTrace(); } } public void unregister() { Message unreg = new Message("Unregister"); unreg.setArg("NAME", _msg.getArg("NAME")); try { _finder.sendOnewayMessage(unreg); } catch (Exception ex) { ex.printStackTrace(); } } } --- NEW FILE: Register.class --- (This appears to be a binary file; contents omitted.) --- NEW FILE: Finder.java --- package examples.finder; /* * @(#)Finder.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.aglet.*; import com.ibm.aglet.message.Message; import java.util.Hashtable; public class Finder extends Aglet { // add this. Hashtable _database = new Hashtable(); public boolean handleMessage(Message msg) { if (msg.sameKind("Lookup")) { msg.sendReply(_database.get(msg.getArg())); } else if (msg.sameKind("Register")) { System.out.println("Registering .. " + msg.getArg("NAME")); System.out.println(msg.getArg("PROXY")); _database.put(msg.getArg("NAME"), msg.getArg("PROXY")); // same } else if (msg.sameKind("Unregister")) { _database.remove(msg.getArg("NAME")); } else { return false; } return true; } public void onCreation(Object init) { // register it as an default finder.. AgletProxy proxy = getAgletContext().getAgletProxy(getAgletID()); getAgletContext().setProperty("finder", proxy); } } --- NEW FILE: HostList.java --- package examples.finder; /* * @(#)HostList.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.aglet.*; import com.ibm.aglet.event.*; import com.ibm.aglet.message.Message; import java.net.URL; import java.util.Hashtable; import java.util.Enumeration; /** * The HostList keeps a list of aglet server names. * When it moves to another server, it automatically disappears. * * @version 1.00 $Date: 2005/11/07 11:38:10 $ * @author Yoshiaki Mima * @see examples.finder.HostCollector */ public class HostList extends Aglet implements PersistencyListener, MobilityListener { Hashtable hostList; public void appendList(Hashtable list) { for (Enumeration e = list.keys(); e.hasMoreElements(); ) { Object key = e.nextElement(); if (list.get(key) instanceof String) { if (hostList.get(key) == null) { System.out.println("new: " + key); hostList.put(key, "new"); } else { // System.out.println("key: " + key + " value: " + hostList.get(key)); } } } } public boolean handleMessage(Message msg) { if (msg.sameKind("dialog")) { try { int i = 0; System.out.println("HostList -- begin"); for (Enumeration e = hostList.keys(); e.hasMoreElements(); ) { System.out.println(i++ + ": " + e.nextElement()); } System.out.println("HostList -- end"); } catch (Exception ex) { ex.printStackTrace(); } return true; } else if (msg.sameKind("register")) { if (msg.getArg() instanceof String) { hostList.put(msg.getArg(), "running"); } return true; } else if (msg.sameKind("append")) { if (msg.getArg() instanceof Hashtable) { appendList((Hashtable)msg.getArg()); } return true; } else if (msg.sameKind("getlist")) { msg.sendReply(hostList); return true; } else if (msg.sameKind("shutdown")) { try { deactivate(0); } catch (Exception e) {} return true; } else if (msg.sameKind("dispose")) { dispose(); return true; } else {} return true; } public void onActivation(PersistencyEvent event) { AgletContext ac = getAgletContext(); AgletProxy proxy = getProxy(); AgletProxy ap; // check if another HostList registration ap = (AgletProxy)ac.getProperty("hostlist"); if ((ap != null) && (ap.isValid())) { try { ap.sendMessage(new Message("append", hostList)); } catch (Exception e) { System.out.println(e); } dispose(); } else { ac.setProperty("hostlist", proxy); } } public void onArrival(MobilityEvent event) { dispose(); } public void onCreation(Object init) { AgletContext ac = getAgletContext(); AgletProxy proxy = getProxy(); AgletProxy ap; addMobilityListener(this); addPersistencyListener(this); // check if another HostList registration ap = (AgletProxy)ac.getProperty("hostlist"); if ((ap != null) && (ap.isValid())) { if (init instanceof Hashtable) { try { ap.sendMessage(new Message("append", init)); } catch (Exception e) { System.out.println(e); } } dispose(); } else { ac.setProperty("hostlist", proxy); if (init instanceof Hashtable) { hostList = (Hashtable)((Hashtable)init).clone(); } else { hostList = new Hashtable(); } hostList.put(ac.getHostingURL().toString(), "running"); } } public void onDeactivating(PersistencyEvent event) { // unregistration getAgletContext().setProperty("hostlist", null); } public void onDispatching(MobilityEvent event) {} public void onRetraction(MobilityEvent event) {} public void onReverting(MobilityEvent event) {} } --- NEW FILE: HostTravellor.java --- package examples.finder; /* * @(#)HostTravellor.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.aglet.*; import com.ibm.aglet.event.*; import com.ibm.aglet.message.Message; import java.net.URL; import java.util.Hashtable; import java.util.Vector; import java.util.Enumeration; /** * The HostTravellor carries a list of aglet server names. * When it moves to another server, it adds current server address * to the list. If there is no HostList aglet is running, it creates * an instance of HostList on that server. If a HostList aglet is running, * it appends a list of server name to list in the HostList and * then get the list belongs to the HostList. * * @version 1.00 $Date: 2005/11/07 11:38:10 $ * @author Yoshiaki Mima * @see examples.finder.HostList */ public class HostTravellor extends Aglet implements MobilityListener { Hashtable hostList; Vector visitOrder = new Vector(); int nextVisit = 0; int count = 0; public void appendList(Hashtable list) { for (Enumeration e = list.keys(); e.hasMoreElements(); ) { Object key = e.nextElement(); if (hostList.get(key) == null) { hostList.put(key, "new"); } } } public void goNext() { String next = ""; try { while (true) { try { if (nextVisit >= visitOrder.size()) { nextVisit = 0; count++; setText("reset"); Thread.sleep(5000); } next = (String)visitOrder.elementAt(nextVisit); setText(count + "> goto: " + next); Thread.sleep(2000); dispatch(new URL((String)visitOrder .elementAt(nextVisit++))); } catch (Exception e) { System.out.println("dispatch to " + next + " failed"); } } } catch (Exception e) { System.out.println(e); } } public boolean handleMessage(Message msg) { if (msg.sameKind("dialog")) { try { int i = 0; System.out.println("HostList -- begin"); for (Enumeration e = hostList.keys(); e.hasMoreElements(); ) { System.out.println(i++ + ": " + e.nextElement()); } System.out.println("HostList -- end"); } catch (Exception ex) { ex.printStackTrace(); } return true; } else if (msg.sameKind("shutdown")) { try { deactivate(0); } catch (Exception e) {} return true; } else {} return true; } public void onArrival(MobilityEvent event) { AgletProxy ap = (AgletProxy)getAgletContext().getProperty("hostlist"); hostList.put(getAgletContext().getHostingURL().toString(), "running"); try { if ((ap == null) ||!ap.isValid()) { ap = getAgletContext().createAglet(getCodeBase(), "examples.finder.HostList", hostList); } else { ap.sendMessage(new Message("append", hostList)); Hashtable list = (Hashtable)ap.sendMessage(new Message("getlist")); if (list != null) { appendList(list); setVisitOrder(); } } // go next goNext(); } catch (Exception e) { System.out.println(e); } } public void onCreation(Object init) { hostList = new Hashtable(); AgletProxy ap = (AgletProxy)getAgletContext().getProperty("hostlist"); hostList.put(getAgletContext().getHostingURL().toString(), "running"); addMobilityListener(this); try { if ((ap == null) ||!ap.isValid()) { ap = getAgletContext().createAglet(getCodeBase(), "examples.finder.HostList", hostList); } Hashtable list = (Hashtable)ap.sendMessage(new Message("getlist")); if (list != null) { appendList(list); } // go next setVisitOrder(); goNext(); } catch (Exception e) { System.out.println(e); } } public void onDispatching(MobilityEvent event) {} public void onRetraction(MobilityEvent event) {} public void onReverting(MobilityEvent event) {} public void setVisitOrder() { for (Enumeration e = hostList.keys(); e.hasMoreElements(); ) { Object key = e.nextElement(); if (key instanceof String) { if (visitOrder.indexOf(key) == -1) { visitOrder.addElement(key); System.out.println("add: " + key); } } } } } --- NEW FILE: HostList.class --- (This appears to be a binary file; contents omitted.) --- NEW FILE: Test.java --- package examples.finder; /* * @(#)Test.java * * 03L7246 (c) Copyright IBM Corp. 1996, 1998 * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.aglet.*; import com.ibm.aglet.message.Message; public class Test extends Aglet { AgletProxy _finder; Message lookup = new Message("Lookup", "Traveller"); public boolean handleMessage(Message msg) { try { AgletProxy proxy = (AgletProxy)_finder.sendMessage(lookup); System.out.println(proxy.getAgletInfo()); } catch (Exception ex) { ex.printStackTrace(); } return true; } public void onCreation(Object init) { _finder = (AgletProxy)getAgletContext().getProperty("finder"); try { AgletProxy proxy = (AgletProxy)_finder.sendMessage(lookup); System.out.println(proxy.getAgletInfo()); } catch (Exception ex) { ex.printStackTrace(); } } } --- NEW FILE: Finder.class --- (This appears to be a binary file; contents omitted.) |