KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > net > AgentMonitorManager


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Uri Schneider.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.net;
47
48 import java.util.HashMap JavaDoc;
49 import java.util.HashSet JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.Set JavaDoc;
53
54 import org.mr.MantaAgent;
55 import org.mr.core.configuration.ConfigManager;
56 import org.mr.core.util.ActiveObject;
57 import org.mr.core.util.Stage;
58 import org.mr.core.util.StageHandler;
59 import org.mr.core.util.StageParams;
60 import org.mr.core.util.TimeoutTimer;
61 import org.mr.core.util.Timeoutable;
62
63 /**
64  * A manager for agent monitors.<p> This manager keeps a mapping of
65  * agent names to {@link AgentMonitor} objects, and a set of all the
66  * {@link Transport} objects for the monitored agents. A timer goes
67  * over all the transports periodically, and activates their {@link
68  * Transport#keepalive keepalive} method. As a result, transports
69  * report to their agent monitor objects regarding any change in their
70  * state, which in turn report to their listener regarding any change
71  * in the remote agent's state.
72  *
73  * Created: Tue Jun 01 16:31:36 2004
74  *
75  * @author Uri Schneider
76  * @version 1.0
77  */

78 public class AgentMonitorManager implements Timeoutable, StageHandler {
79     private Map JavaDoc monitors;
80     private Set JavaDoc transports;
81     private TimeoutTimer timer;
82     private Stage stage;
83     private int keepaliveInterval;
84
85     public AgentMonitorManager() {
86         this.monitors = new HashMap JavaDoc();
87         this.transports = new HashSet JavaDoc();
88         ConfigManager config = MantaAgent.getInstance().getSingletonRepository().getConfigManager();
89         keepaliveInterval = config.getIntProperty("network.keepalive.interval", 1) * 1000;
90         this.timer = new TimeoutTimer("Agent_monitor",100, 1); // one second
91
this.timer.setName("AgentMonitorManager Timer");
92         StageParams params = new StageParams();
93         params.setBlocking(false);
94         params.setPersistent(false);
95         params.setStageName("AgentMonitor");
96         params.setHandler(this);
97         params.setNumberOfStartThreads(1);
98         params.setMaxNumberOfThreads(10);
99         params.setStagePriority(0);
100         this.stage = new Stage(params);
101     } // AgentMonitorManager constructor
102

103      /**
104      * Adds an AgentMonitor to the agent monitors registration, if there is
105      * no such registered to the agent inputted.
106      * Adds a listener to the new/existing AgentMonitor.
107      * Adds Transports to the new/existing AgentMonitor.
108      * @param agent The name of the MantaRay Layer to be monitored.
109      * @param newTransports The set of the Transports.
110      * @param listener A listener to be registered to this Agent Monitor
111      */

112     public void addMonitor(String JavaDoc agent, Set JavaDoc newTransports,
113                            AgentStateListener listener) {
114         final String JavaDoc fagent = agent;
115         final Set JavaDoc fnewTransports = newTransports;
116         final AgentStateListener flistener = listener;
117         this.stage.enqueue(new ActiveObject() {
118                 public boolean call() {
119                     doAddMonitor(fagent, fnewTransports, flistener);
120                     return true;
121                 }
122             });
123     }
124
125     private void doAddMonitor(String JavaDoc agent, Set JavaDoc newTransports,
126                               AgentStateListener listener) {
127 // AgentMonitor monitor = new AgentMonitor(agent, newTransports,
128
// listener);
129
AgentMonitor monitor = (AgentMonitor) this.monitors.get(agent);
130         if (monitor == null) {
131             monitor = new AgentMonitor(agent, newTransports, this);
132             this.monitors.put(agent, monitor);
133         }
134         monitor.addListener(listener);
135         boolean startTimer = this.transports.isEmpty();
136         if (newTransports != null) {
137             this.transports.addAll(newTransports);
138         }
139         if (startTimer && !this.transports.isEmpty()) {
140             startTimer();
141         }
142 // this.monitors.put(agent, monitor);
143
}
144
145     /**
146      * Removes a specific listener from a registered AgentMonitor and some
147      * transports registered in the AgentMonitorManager.
148      * If after the removal the AgentMonitor has no more listeners, it will
149      * be removed itself.
150      * @param agent the name of the MantaRay layer .
151      * @param toRemoveTransports A set of the Transports to be removed.
152      * @param listener The listener to be removed.
153      */

154     public void removeMonitor(String JavaDoc agent, Set JavaDoc toRemoveTransports,
155                               AgentStateListener listener) {
156         final String JavaDoc fagent = agent;
157         final Set JavaDoc ftoRemoveTransports = toRemoveTransports;
158         final AgentStateListener flistener = listener;
159         this.stage.enqueue(new ActiveObject() {
160                 public boolean call() {
161                     doRemoveMonitor(fagent, ftoRemoveTransports, flistener);
162                     return true;
163                 }
164             });
165     }
166
167     private void doRemoveMonitor(String JavaDoc agent, Set JavaDoc toRemoveTransports,
168                                  AgentStateListener listener) {
169         AgentMonitor monitor = (AgentMonitor) this.monitors.get(agent);
170         if (monitor != null) {
171             monitor.removeListener(listener);
172             if (!monitor.hasListeners()) {
173                 monitor.shutdown();
174                 this.monitors.remove(agent);
175                 if (toRemoveTransports != null) {
176                     this.transports.removeAll(toRemoveTransports);
177                     if (this.transports.isEmpty()) {
178                         stopTimer();
179                     }
180                 }
181             }
182         }
183     }
184
185     /**
186      * Removes an AgentMonitor and some transports registered in this
187      * AgentMonitorManager. If after the removal the AgentMonitor has
188      * no more listeners, it will be removed itself.
189      * @param agent The name of the MantaRay agent its AgentMonitor
190      * will be removed.
191      * @param toRemoveTransports A set of the Transports to be
192      * removed.
193      */

194     public void removeMonitor(String JavaDoc agent, Set JavaDoc toRemoveTransports) {
195         final String JavaDoc fagent = agent;
196         final Set JavaDoc ftoRemoveTransports = toRemoveTransports;
197         this.stage.enqueue(new ActiveObject() {
198                 public boolean call() {
199                     doRemoveMonitor(fagent, ftoRemoveTransports);
200                     return true;
201                 }
202             });
203     }
204
205     private void doRemoveMonitor(String JavaDoc agent, Set JavaDoc toRemoveTransports) {
206 // AgentMonitor monitor = (AgentMonitor) this.monitors.remove(agent);
207
AgentMonitor monitor = (AgentMonitor) this.monitors.get(agent);
208         if (monitor != null) {
209             monitor.shutdown();
210             if (toRemoveTransports != null) {
211                 this.transports.removeAll(toRemoveTransports);
212                 if (this.transports.isEmpty()) {
213                     stopTimer();
214                 }
215             }
216         }
217     }
218
219     /**
220      * Adds a Transports to a registered MantaRay agent.
221      * @param agent The name of the MantaRAy agent.
222      * @param t the Transpor to be added.
223      */

224     public void addTransport(String JavaDoc agent, Transport t) {
225         final String JavaDoc fagent = agent;
226         final Transport ft = t;
227         this.stage.enqueue(new ActiveObject() {
228                 public boolean call() {
229                     doAddTransport(fagent, ft);
230                     return true;
231                 }
232             });
233     }
234
235     private void doAddTransport(String JavaDoc agent, Transport t) {
236         AgentMonitor monitor = (AgentMonitor) this.monitors.get(agent);
237         if (monitor != null) {
238             monitor.addTransport(t);
239             this.transports.add(t);
240             if (this.transports.size() == 1) {
241                 startTimer();
242             }
243         }
244     }
245
246     /**
247      * Removes a Transport registered in an AgentMonitor.
248      * @param agent The name of the agent the Transport is registered at.
249      * @param t The Transport to be removed.
250      */

251     public void removeTransport(String JavaDoc agent, Transport t) {
252         final String JavaDoc fagent = agent;
253         final Transport ft = t;
254         this.stage.enqueue(new ActiveObject() {
255                 public boolean call() {
256                     doRemoveTransport(fagent, ft);
257                     return true;
258                 }
259             });
260     }
261
262     private void doRemoveTransport(String JavaDoc agent, Transport t) {
263         AgentMonitor monitor = (AgentMonitor) this.monitors.get(agent);
264         if (monitor != null) {
265             monitor.removeTransport(t);
266             this.transports.remove(t);
267             if (this.transports.isEmpty()) {
268                 stopTimer();
269             }
270         }
271     }
272
273     /**
274      * Returns the state of the an agent that has an AgentMonitor.
275      * @param agent The name of the agent
276      * @return the state of the agent, either UP, DOWN, UNKOWN or
277      * NOT_MONITORING.
278      */

279     public int getAgentState(String JavaDoc agent) {
280         AgentMonitor monitor = (AgentMonitor) this.monitors.get(agent);
281         if (monitor != null) {
282             return monitor.getState();
283         } else {
284             return AgentStateListener.AGENT_STATE_NOT_MONITORING;
285         }
286     }//getAgentState
287

288     //Starts the timer that sends keepalives of the Transports
289
private void startTimer() {
290         this.timer.addTimeout(this, this, keepaliveInterval);
291     }
292
293     //Stops the timer that sends keepalives of the Transports
294
private void stopTimer() {
295         this.timer.removeTimeout(this);
296     }
297
298     public void sendEvent(String JavaDoc agent, int state,
299                           AgentStateListener listener)
300     {
301         final String JavaDoc fagent = agent;
302         final int fstate = state;
303         final AgentStateListener flistener = listener;
304         this.stage.enqueue(new ActiveObject() {
305                 public boolean call() {
306                     doSendEvent(fagent, fstate, flistener);
307                     return true;
308                 }
309             });
310     }
311
312     private void doSendEvent(String JavaDoc agent, int state,
313                             AgentStateListener listener)
314     {
315         listener.agentStateChanged(agent, state);
316     }
317
318     // interface Timeoutable
319
public void timeout(Object JavaDoc event) {
320         final Object JavaDoc fevent = event;
321         this.stage.enqueue(new ActiveObject() {
322                 public boolean call() {
323                     doTimeout(fevent);
324                     return true;
325                 }
326             });
327     }
328
329     private void doTimeout(Object JavaDoc event) {
330         Iterator JavaDoc i = this.transports.iterator();
331         while (i.hasNext()) {
332             Transport t = (Transport) i.next();
333             t.keepalive();
334         }
335         this.timer.addTimeout(this, this, keepaliveInterval);
336     }
337
338     public boolean handle(Object JavaDoc o) {
339         ActiveObject ao = (ActiveObject) o;
340         ao.call();
341         
342         return true;
343     }
344 } // AgentMonitorManager
345
Popular Tags