1 23 24 package org.objectweb.clif.probe.util; 25 26 import org.objectweb.clif.server.api.BladeControl; 27 import org.objectweb.clif.server.api.BladeInsertResponse; 28 import org.objectweb.clif.datacollector.api.DataCollectorWrite; 29 import org.objectweb.clif.supervisor.api.ClifException; 30 import org.objectweb.clif.storage.api.ProbeEvent; 31 import org.objectweb.fractal.api.control.BindingController; 32 import org.objectweb.fractal.api.control.LifeCycleController; 33 import java.io.Serializable ; 34 import java.util.StringTokenizer ; 35 36 37 46 abstract public class AbstractDumbInsert 47 implements 48 BladeControl, 49 BindingController, 50 LifeCycleController, 51 Runnable 52 { 53 static private final String [] interfaceNames = new String [] { 54 DataCollectorWrite.DATA_COLLECTOR_WRITE, 55 BladeInsertResponse.BLADE_INSERT_RESPONSE }; 56 protected String probeId; 57 protected Object activity_lock = new Object (); 58 protected BladeInsertResponse bir; 59 protected Object sr_lock = new Object (); 60 protected DataCollectorWrite dcw; 61 protected Object dc_lock = new Object (); 62 protected long arg_period_ms = 0; 63 protected long arg_duration_ms = 0; 64 protected long baseTime_ms = 0; 65 protected volatile boolean started; 66 protected volatile boolean stopped; 67 protected volatile boolean suspended; 68 protected volatile boolean terminated; 69 private String fcState = LifeCycleController.STOPPED; 70 protected Thread poller; 71 72 73 77 abstract protected ProbeEvent doProbe(); 78 79 80 84 85 public void startFc() 86 { 87 fcState = LifeCycleController.STARTED; 88 } 89 90 91 public void stopFc() 92 { 93 fcState = LifeCycleController.STOPPED; 94 } 95 96 97 public String getFcState() 98 { 99 return fcState; 100 } 101 102 103 107 108 public Object lookupFc(String clientItfName) 109 { 110 if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE)) 111 { 112 return dcw; 113 } 114 else if (clientItfName.equals(BladeInsertResponse.BLADE_INSERT_RESPONSE)) 115 { 116 return bir; 117 } 118 else 119 { 120 return null; 121 } 122 } 123 124 125 public void bindFc(String clientItfName, Object serverItf) 126 { 127 if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE)) 128 { 129 synchronized (dc_lock) 130 { 131 dcw = (DataCollectorWrite) serverItf; 132 } 133 } 134 else if (clientItfName.equals(BladeInsertResponse.BLADE_INSERT_RESPONSE)) 135 { 136 synchronized (sr_lock) 137 { 138 bir = (BladeInsertResponse) serverItf; 139 } 140 } 141 } 142 143 144 public void unbindFc(String clientItfName) 145 { 146 if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE)) 147 { 148 synchronized (dc_lock) 149 { 150 dcw = null; 151 } 152 } 153 else if (clientItfName.equals(BladeInsertResponse.BLADE_INSERT_RESPONSE)) 154 { 155 synchronized (sr_lock) 156 { 157 bir = null; 158 } 159 } 160 } 161 162 163 public String [] listFc() 164 { 165 return interfaceNames; 166 } 167 168 169 173 174 177 public void init(Serializable testId) 178 { 179 started = false; 180 stopped = false; 181 suspended = false; 182 terminated = false; 183 poller = new Thread (this, "memory probe"); 184 poller.start(); 185 } 186 187 188 public void start() 189 { 190 started = true; 191 baseTime_ms = System.currentTimeMillis(); 192 } 193 194 195 public void stop() 196 { 197 stopped = true; 198 poller.interrupt(); 199 synchronized (activity_lock) 200 { 201 if (started && ! terminated) 202 { 203 try 204 { 205 activity_lock.wait(); 206 } 207 catch (InterruptedException ex) 208 { 209 } 210 } 211 } 212 } 213 214 215 public void suspend() 216 { 217 suspended = true; 218 baseTime_ms = System.currentTimeMillis() - baseTime_ms; 219 } 220 221 222 public void resume() 223 { 224 baseTime_ms = System.currentTimeMillis() - baseTime_ms; 225 suspended = false; 226 } 227 228 229 public void join() 230 { 231 synchronized (activity_lock) 232 { 233 if (!terminated) 234 { 235 try 236 { 237 activity_lock.wait(); 238 } 239 catch (InterruptedException ex) 240 { 241 } 242 } 243 } 244 } 245 246 247 252 public void setArgument(String arg) 253 throws ClifException 254 { 255 StringTokenizer parser = new StringTokenizer (arg); 256 try 257 { 258 arg_period_ms = Integer.parseInt(parser.nextToken()); 259 arg_duration_ms = Integer.parseInt(parser.nextToken()) * 1000; 260 } 261 catch (Exception ex) 262 { 263 throw new ClifException("probe expects 2 arguments: <period_ms> <duration_s>"); 264 } 265 } 266 267 268 271 public void setId(String id) 272 { 273 probeId = id; 274 } 275 276 277 280 public String getId() 281 { 282 return probeId; 283 } 284 285 286 public void run() 287 { 288 while ( 289 ! started 290 || ( 291 ! stopped 292 && 293 (suspended 294 || arg_duration_ms > System.currentTimeMillis() - baseTime_ms))) 295 { 296 try 297 { 298 dcw.add(doProbe()); 299 } 300 catch (Exception ex) 301 { 302 } 303 try 304 { 305 long time = System.currentTimeMillis(); 306 if (!started || suspended || arg_duration_ms > time - baseTime_ms + arg_period_ms) 307 { 308 Thread.currentThread().sleep(arg_period_ms); 309 } 310 else 311 { 312 Thread.currentThread().sleep( 313 arg_duration_ms 314 - time + baseTime_ms); 315 } 316 } 317 catch (InterruptedException ex) 318 { 319 } 320 } 321 synchronized (activity_lock) 322 { 323 terminated = true; 324 if (! stopped) 325 { 326 bir.completed(); 327 } 328 else 329 { 330 activity_lock.notify(); 331 } 332 } 333 } 334 } 335 | Popular Tags |