1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package zeus.agents;
29
30 import java.util.*;
31 import zeus.util.*;
32 import zeus.concepts.*;
33 import zeus.actors.*;
34 import zeus.actors.event.*;
35 import zeus.agents.*;
36 import zeus.actors.outtrays.*;
37 import zeus.actors.intrays.*;
38
39 /***
40 The DFAgent is run to provide an IIOP ACC service for Zeus that uses FIPA addressing
41 @see zeus.concepts.FIPA_AID_Address
42 @see zeus.actors.fipa.FIPA_Postman
43 @see zeus.actors.fipa.FIPA_Server
44 @see zeus.actors.fipa.Zeus_ACC_Server
45 @see zeus.actors.fipa.Zeus_ACC_Mailbox
46 @author Simon Thompson
47 */
48 public class ACCAgent extends BasicAgent
49 {
50 private HSet[] eventMonitor = new HSet[4];
51
52 public static final int CREATE = 0;
53 public static final int DEATH = 1;
54 public static final int SUSPEND = 2;
55 public static final int RESUME = 3;
56
57 protected AgentContext context = null;
58
59 public ACCAgent(String type, String name, Vector nameservers) {
60 this(type, name, nameservers, null);
61 }
62
63 public ACCAgent(String type, String name,
64 Vector nameservers, Clock clock) {
65
66 context = new ZeusAgentContext(name,type);
67 context.set(this);
68 context.setNameservers(nameservers);
69 new MsgHandler(context);
70 Zeus_ACC_MailBox mbox = new Zeus_ACC_MailBox(context);
71
72 context.set(mbox);
73
74
75 if ( clock != null )
76 context.set(clock);
77
78 for(int i = 0; i < eventMonitor.length; i++ )
79 eventMonitor[i] = new HSet();
80
81 long now, start = System.currentTimeMillis();
82
83 while( context.Clock() == null ) {
84 now = System.currentTimeMillis();
85 if ( now - start > (long)(context.getRegistrationTimeout()*60000) ) {
86 System.err.println("Cannot initialize " + name + " -- exiting");
87 notifyMonitors(DEATH);
88 System.exit(0);
89 }
90 try {
91 Thread.currentThread().sleep(1000);
92 }
93 catch(InterruptedException e) {
94 }
95 }
96 notifyMonitors(CREATE);
97 }
98
99 public AgentContext getAgentContext() { return context; }
100
101
102 /*** Shortcut to add a MessageMonitor, used if your code needs to react to
103 changes in the state of the mailbox */
104 public void addMessageMonitor(MessageMonitor monitor, long event_type) {
105 context.MailBox().addMessageMonitor(monitor,event_type);
106 }
107
108 public void removeMessageMonitor(MessageMonitor monitor, long event_type) {
109 context.MailBox().removeMessageMonitor(monitor,event_type);
110 }
111
112 /*** Add an AgentMonitor if your code needs to react to Agent-level changes in state */
113 public void addAgentMonitor(AgentMonitor monitor, long event_type) {
114 Assert.notNull(monitor);
115 if ( (event_type & AgentEvent.CREATE_MASK) != 0 )
116 eventMonitor[CREATE].add(monitor);
117 if ( (event_type & AgentEvent.DEATH_MASK) != 0 )
118 eventMonitor[DEATH].add(monitor);
119 if ( (event_type & AgentEvent.SUSPEND_MASK) != 0 )
120 eventMonitor[SUSPEND].add(monitor);
121 if ( (event_type & AgentEvent.RESUME_MASK) != 0 )
122 eventMonitor[RESUME].add(monitor);
123 }
124 public void removeAgentMonitor(AgentMonitor monitor, long event_type) {
125 Assert.notNull(monitor);
126 if ( (event_type & AgentEvent.CREATE_MASK) != 0 )
127 eventMonitor[CREATE].remove(monitor);
128 if ( (event_type & AgentEvent.DEATH_MASK) != 0 )
129 eventMonitor[DEATH].remove(monitor);
130 if ( (event_type & AgentEvent.SUSPEND_MASK) != 0 )
131 eventMonitor[SUSPEND].remove(monitor);
132 if ( (event_type & AgentEvent.RESUME_MASK) != 0 )
133 eventMonitor[RESUME].remove(monitor);
134 }
135 public void notifyMonitors(int type) {
136 if ( eventMonitor[type].isEmpty() ) return;
137
138 AgentMonitor monitor;
139 AgentEvent event;
140 Enumeration enum = eventMonitor[type].elements();
141
142 switch(type) {
143 case CREATE:
144 event = new AgentEvent(this,this,AgentEvent.CREATE_MASK);
145 while( enum.hasMoreElements() ) {
146 monitor = (AgentMonitor)enum.nextElement();
147 monitor.agentCreatedEvent(event);
148 }
149 break;
150 case DEATH:
151 event = new AgentEvent(this,this,AgentEvent.DEATH_MASK);
152 while( enum.hasMoreElements() ) {
153 monitor = (AgentMonitor)enum.nextElement();
154 monitor.agentDeathEvent(event);
155 }
156 break;
157 case SUSPEND:
158 event = new AgentEvent(this,this,AgentEvent.SUSPEND_MASK);
159 while( enum.hasMoreElements() ) {
160 monitor = (AgentMonitor)enum.nextElement();
161 monitor.agentSuspendedEvent(event);
162 }
163 break;
164 case RESUME:
165 event = new AgentEvent(this,this,AgentEvent.RESUME_MASK);
166 while( enum.hasMoreElements() ) {
167 monitor = (AgentMonitor)enum.nextElement();
168 monitor.agentResumedEvent(event);
169 }
170 break;
171 }
172 }
173 }