Source code: org/altara/mars/engine/ProbeWorker.java
1 /* MARS Network Monitoring Engine
2 Copyright (C) 1999 Brian H. Trammell
3 Copyright (C) 2002 Leapfrog Research & Development, LLC
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, it is available at
17 http:///www.gnu.org/copyleft/gpl.html, or by writing to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20 */
21
22 package org.altara.mars.engine;
23
24 import org.altara.util.*;
25
26 /** This worker runs Probes in a circular queue. Each Probe is checked to
27 see if it's ready to run and is placed back into the queue. ProbeWorker
28 runs at a fairly low priority and yields often, so as not to interfere
29 with interactive tasks.
30 */
31
32 public class ProbeWorker extends Worker {
33
34 private static final int PW_PRIO = 2;
35 private static final int SPIN_DELAY = 1000;
36
37 private static int nxid = 0;
38
39 private Controller controller;
40 private int spincount;
41
42 public ProbeWorker (Controller controller) {
43 super("ProbeWorker "+(nxid++));
44 setPriority(PW_PRIO);
45 this.controller = controller;
46 this.spincount = 0;
47 }
48
49 protected void work() throws InterruptedException {
50 Probe nextProbe = controller.getNextProbe();
51 if (nextProbe.canRun()) {
52 nextProbe.run();
53 spincount = 0;
54 controller.returnProbe(nextProbe);
55 } else {
56 // return the non-runnable probe immediately
57 controller.returnProbe(nextProbe);
58 // check for spin
59 if (++spincount > controller.queueSize()) {
60 Thread.sleep(SPIN_DELAY);
61 }
62 }
63 Thread.yield();
64 }
65
66 protected void workExit() {
67 // log here when we get decent logging.
68 }
69 }