Source code: org/altara/mars/engine/Notifier.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 /** Delays the delivery of StatusChangeEvents to notification plugins. Used to
23 smooth out notifications on rough networks.
24 */
25
26 package org.altara.mars.engine;
27
28 import java.util.*;
29 import org.altara.mars.*;
30
31 public class Notifier implements StatusChangeListener, ProbeListener {
32
33 Map pendingTable;
34 Controller controller;
35
36 public Notifier(Controller controller) {
37 this.pendingTable = Collections.synchronizedMap(new HashMap());
38 this.controller = controller;
39 controller.addStatusChangeListener(this);
40 controller.addProbeListener(this);
41 }
42
43 public void statusChanged(StatusChangeEvent sce) {
44 Service svc = sce.getService();
45 int notac = svc.getNotac();
46 if (notac > 1) {
47 pendingTable.put(svc,new NotificationEntry(sce,notac));
48 } else if (notac == 1) {
49 controller.notifyStatusChanged(sce);
50 }
51 }
52
53 public void probeRun(ProbeEvent pe) {
54 Service svc = pe.getService();
55 NotificationEntry entry = (NotificationEntry)pendingTable.get(svc);
56 if (entry != null && --entry.notac <= 1) {
57 pendingTable.remove(svc);
58 controller.notifyStatusChanged(entry.sce);
59 }
60 }
61
62 private static class NotificationEntry {
63 private StatusChangeEvent sce;
64 private int notac;
65
66 private NotificationEntry(StatusChangeEvent sce, int notac) {
67 this.sce = sce;
68 this.notac = notac;
69 }
70 }
71 }