Mercurial > hg > jslab
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bf79fb79ee13 |
---|---|
1 /* | |
2 * AgentManager.java | |
3 * | |
4 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
5 * All rights reserved. | |
6 * | |
7 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
8 * without even the implied warranty of MERCHANTABILITY or | |
9 * FITNESS FOR A PARTICULAR PURPOSE. | |
10 */ | |
11 | |
12 package samer.core.util.shell; | |
13 import samer.core.*; | |
14 import java.awt.event.*; | |
15 import java.util.*; | |
16 | |
17 public class AgentManager implements Agent, ActionListener | |
18 { | |
19 private Vector agents=new Vector(); | |
20 //private boolean exitOnReturn=false; | |
21 | |
22 public AgentManager() { Shell.put("AgentManager",this); } | |
23 | |
24 public ActionListener getBroadcaster() { return this; } | |
25 | |
26 public void actionPerformed(ActionEvent e) { | |
27 dispatch(e.getActionCommand(),Shell.env()); | |
28 } | |
29 | |
30 public void getCommands(Agent.Registry r) { r.add("help").add("agents"); } | |
31 public void execute(String cmd, Environment env) throws Exception | |
32 { | |
33 if (cmd.equals("agents")) { | |
34 Shell.print("registered agents:"); | |
35 Enumeration i=agents.elements(); | |
36 while(i.hasMoreElements()) { | |
37 Shell.print(i.nextElement().toString()); | |
38 } | |
39 } else if (cmd.equals("help")) help(); | |
40 } | |
41 | |
42 //public void exitOnReturn() { Shell.trace("*** exit on return"); exitOnReturn=true; } | |
43 | |
44 public void registerAgent(Agent a) { | |
45 Shell.trace("registering agent: "+a); | |
46 agents.addElement(a); | |
47 } | |
48 | |
49 public void deregisterAgent(Agent a) { | |
50 Shell.trace("deregistering agent: "+a); | |
51 agents.removeElement(a); | |
52 } | |
53 | |
54 public void dispatch(String cmd, Environment env) | |
55 { | |
56 Enumeration i=agents.elements(); | |
57 while(i.hasMoreElements()) { | |
58 Agent agent=(Agent)i.nextElement(); | |
59 try { agent.execute(cmd,env); } | |
60 catch (Exception ex) { | |
61 Shell.print("*** agent error in "+agent); | |
62 Shell.print("*** exception: "+ex); | |
63 ex.printStackTrace(); | |
64 } | |
65 } | |
66 //if (exitOnReturn) System.exit(0); | |
67 } | |
68 | |
69 public void help() | |
70 { | |
71 Shell.print("____________________________________"); | |
72 Shell.print("Registered Agents and their commands"); | |
73 | |
74 Enumeration i=agents.elements(); | |
75 Agent.Registry printer = new Agent.Registry() { | |
76 public void setTarget(Agent a) {} | |
77 public Registry add( String name) { Shell.print("\t"+name); return this; } | |
78 public Registry add( String name, boolean initialValue) { | |
79 Shell.print("\t"+name+" (boolean)"); return this; | |
80 } | |
81 public void group() { Shell.print("............"); } | |
82 }; | |
83 | |
84 while(i.hasMoreElements()) { | |
85 Agent a=(Agent)(i.nextElement()); | |
86 Shell.print(""); | |
87 Shell.print("---- "+a.getClass().getName()); | |
88 a.getCommands(printer); | |
89 } | |
90 } | |
91 } | |
92 |