1 46 package org.mr.core.net; 47 48 import java.util.HashSet ; 49 import java.util.Iterator ; 50 import java.util.Set ; 51 52 66 public class AgentMonitor implements TransportStateListener 67 { 68 private String agent; 69 private Set listeners; 71 private Set alive; 72 private Set dead; 73 private Set unknown; 74 private AgentMonitorManager manager; 75 76 80 public AgentMonitor(String agent, Set transports, 81 AgentMonitorManager manager) 82 { 84 this.agent = agent; 85 this.alive = new HashSet (); 86 this.dead = new HashSet (); 87 this.unknown = new HashSet (); 88 this.listeners = new HashSet (); 90 this.manager = manager; 91 92 if (transports != null) { 93 Iterator 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 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 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 140 public void shutdown() { 141 Iterator 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 } 161 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 186 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 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 220 public void addListener(AgentStateListener listener) { 221 synchronized (this.listeners) { 222 this.listeners.add(listener); 223 } 224 } 225 226 230 public void removeListener(AgentStateListener listener) { 231 synchronized (this.listeners) { 232 this.listeners.remove(listener); 233 } 234 } 235 236 239 public boolean hasListeners() { 240 return !this.listeners.isEmpty(); 241 } 242 243 244 private void sendEvent() { 246 HashSet copy = null; 247 synchronized (this.listeners) { 248 copy = new HashSet (listeners); 249 } 250 Iterator i = copy.iterator(); 251 while (i.hasNext()) { 252 AgentStateListener listener = (AgentStateListener) i.next(); 253 this.manager.sendEvent(this.agent, getState(), listener); 255 } 256 } 257 } | Popular Tags |