KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.Set JavaDoc;
51
52 /**
53  * An object which monitors a remote agent's state.<p> It is created
54  * with all the {@link Transport Transport}s associated with the
55  * remote agent, and registers itself as a {@link
56  * TransportStateListener TransportStateListener} with each of them.
57  * The state of the monitored agent is deducted from the state of all
58  * its transports. Any change is reported to the {@link
59  * AgentStateListener AgentStateListener} of the monitor.
60  *
61  * Created: Tue Jun 01 11:05:22 2004
62  *
63  * @author Uri Schneider
64  * @version 1.0
65  */

66 public class AgentMonitor implements TransportStateListener
67 {
68     private String JavaDoc agent;
69 // private AgentStateListener listener;
70
private Set JavaDoc listeners;
71     private Set JavaDoc alive;
72     private Set JavaDoc dead;
73     private Set JavaDoc unknown;
74     private AgentMonitorManager manager;
75
76     /**
77      * @param agent The name of the MantaRay Layer to be monitored
78      * @param transports A set of Transports
79      */

80     public AgentMonitor(String JavaDoc agent, Set JavaDoc transports,
81                         AgentMonitorManager manager)
82 // AgentStateListener listener)
83
{
84         this.agent = agent;
85         this.alive = new HashSet JavaDoc();
86         this.dead = new HashSet JavaDoc();
87         this.unknown = new HashSet JavaDoc();
88 // this.listener = listener;
89
this.listeners = new HashSet JavaDoc();
90         this.manager = manager;
91
92         if (transports != null) {
93             Iterator JavaDoc i = transports.iterator();
94             while (i.hasNext()) {
95                 Transport t = (Transport) i.next();
96                 if (t.isInitialized()) {
97                     this.alive.add(t);
98                 } else {
99                     this.unknown.add(t);
100                 }
101                 t.setStateListener(this);
102             }
103         }
104         sendEvent();
105     }
106
107     /**
108      * Adds a Transport to this Agent Monitor
109      * @param t The Transport to be added
110      */

111     public void addTransport(Transport t) {
112         synchronized (this.alive) {
113             if (t.isInitialized()) {
114                 this.alive.add(t);
115             } else {
116                 this.unknown.add(t);
117             }
118         }
119         sendEvent();
120         t.setStateListener(this);
121     }
122
123     /**
124      * Removes a Transport to this Agent Monitor
125      * @param t The Transport to be removed
126      */

127     public void removeTransport(Transport t) {
128         t.setStateListener(null);
129         synchronized (this.alive) {
130             if (this.alive.remove(t) || this.dead.remove(t) ||
131                 this.unknown.remove(t)) {
132                 sendEvent();
133             }
134         }
135     }
136
137     /**
138      * Shutdown the AgentMonitor and removes message listeners.
139      */

140     public void shutdown() {
141         Iterator JavaDoc i;
142         synchronized (this.alive) {
143             i = this.alive.iterator();
144             while (i.hasNext()) {
145                 Transport t = (Transport) i.next();
146                 t.setStateListener(null);
147             }
148             i = this.dead.iterator();
149             while (i.hasNext()) {
150                 Transport t = (Transport) i.next();
151                 t.setStateListener(null);
152             }
153             i = this.unknown.iterator();
154             while (i.hasNext()) {
155                 Transport t = (Transport) i.next();
156                 t.setStateListener(null);
157             }
158         }
159     }//shutdown
160

161     /**
162      * Gets the state of the MantaRay agent monitored by this monitor.
163      * @return The state
164      * @see AgentStateListener for exsiting states.
165      */

166     public int getState() {
167         int retval = 0;
168         synchronized (this.alive) {
169             if (!this.alive.isEmpty()) {
170                 retval = AgentStateListener.AGENT_STATE_UP;
171             } else if (!this.unknown.isEmpty()) {
172                 retval = AgentStateListener.AGENT_STATE_UNKNOWN;
173             } else if (!this.dead.isEmpty()) {
174                 retval = AgentStateListener.AGENT_STATE_DOWN;
175             } else {
176                 retval = AgentStateListener.AGENT_STATE_NOT_MONITORING;
177             }
178         }
179
180         return retval;
181     }
182
183     /**
184      * Updates that a Trasport is up.
185      */

186     // interface TransportStateListener
187
public void transportUp(Transport t) {
188         boolean send = false;
189         synchronized (this.alive) {
190             if (this.dead.remove(t) || this.unknown.remove(t)) {
191                 this.alive.add(t);
192                 send = true;
193             }
194         }
195         if (send) {
196             sendEvent();
197         }
198     }
199
200     /**
201      * Updates that a Trasport got down.
202      */

203     public void transportDown(Transport t) {
204         boolean send = false;
205         synchronized (this.alive) {
206             if (this.alive.remove(t) || this.unknown.remove(t)) {
207                 this.dead.add(t);
208                 send = true;
209             }
210         }
211         if (send) {
212             sendEvent();
213         }
214     }
215
216     /**
217      * adds an AgentStateListener to this AgentMonitor
218      * @param listener The AgentStateListener to be added
219      */

220     public void addListener(AgentStateListener listener) {
221         synchronized (this.listeners) {
222             this.listeners.add(listener);
223         }
224     }
225
226     /**
227      * removes an existing AgentStateListener to this AgentMonitor
228      * @param listener The AgentStateListener to be removed
229      */

230     public void removeListener(AgentStateListener listener) {
231         synchronized (this.listeners) {
232             this.listeners.remove(listener);
233         }
234     }
235
236     /**
237      * @return True if the AgentMonitor has any listeners, else false
238      */

239     public boolean hasListeners() {
240         return !this.listeners.isEmpty();
241     }
242     
243     
244     //Notify that the state of MantaRay Agent had changed.
245
private void sendEvent() {
246         HashSet JavaDoc copy = null;
247         synchronized (this.listeners) {
248             copy = new HashSet JavaDoc(listeners);
249         }
250         Iterator JavaDoc i = copy.iterator();
251         while (i.hasNext()) {
252             AgentStateListener listener = (AgentStateListener) i.next();
253 // listener.agentStateChanged(this.agent, getState());
254
this.manager.sendEvent(this.agent, getState(), listener);
255         }
256     }
257 } // AgentMonitor
258
Popular Tags