Mercurial > hg > jslab
diff src/samer/core_/util/shell/AgentManager.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/samer/core_/util/shell/AgentManager.java Tue Jan 17 17:50:20 2012 +0000 @@ -0,0 +1,92 @@ +/* + * AgentManager.java + * + * Copyright (c) 2000, Samer Abdallah, King's College London. + * All rights reserved. + * + * This software is provided AS iS and WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + */ + +package samer.core.util.shell; +import samer.core.*; +import java.awt.event.*; +import java.util.*; + +public class AgentManager implements Agent, ActionListener +{ + private Vector agents=new Vector(); + //private boolean exitOnReturn=false; + + public AgentManager() { Shell.put("AgentManager",this); } + + public ActionListener getBroadcaster() { return this; } + + public void actionPerformed(ActionEvent e) { + dispatch(e.getActionCommand(),Shell.env()); + } + + public void getCommands(Agent.Registry r) { r.add("help").add("agents"); } + public void execute(String cmd, Environment env) throws Exception + { + if (cmd.equals("agents")) { + Shell.print("registered agents:"); + Enumeration i=agents.elements(); + while(i.hasMoreElements()) { + Shell.print(i.nextElement().toString()); + } + } else if (cmd.equals("help")) help(); + } + + //public void exitOnReturn() { Shell.trace("*** exit on return"); exitOnReturn=true; } + + public void registerAgent(Agent a) { + Shell.trace("registering agent: "+a); + agents.addElement(a); + } + + public void deregisterAgent(Agent a) { + Shell.trace("deregistering agent: "+a); + agents.removeElement(a); + } + + public void dispatch(String cmd, Environment env) + { + Enumeration i=agents.elements(); + while(i.hasMoreElements()) { + Agent agent=(Agent)i.nextElement(); + try { agent.execute(cmd,env); } + catch (Exception ex) { + Shell.print("*** agent error in "+agent); + Shell.print("*** exception: "+ex); + ex.printStackTrace(); + } + } + //if (exitOnReturn) System.exit(0); + } + + public void help() + { + Shell.print("____________________________________"); + Shell.print("Registered Agents and their commands"); + + Enumeration i=agents.elements(); + Agent.Registry printer = new Agent.Registry() { + public void setTarget(Agent a) {} + public Registry add( String name) { Shell.print("\t"+name); return this; } + public Registry add( String name, boolean initialValue) { + Shell.print("\t"+name+" (boolean)"); return this; + } + public void group() { Shell.print("............"); } + }; + + while(i.hasMoreElements()) { + Agent a=(Agent)(i.nextElement()); + Shell.print(""); + Shell.print("---- "+a.getClass().getName()); + a.getCommands(printer); + } + } +} +