KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > activities > Background


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.activities;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.GUI.VisualElement;
28 import net.fenyo.gnetwatch.actions.Action;
29 import net.fenyo.gnetwatch.targets.Target;
30
31 import java.util.*;
32 import java.io.*;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 // every queues must be created at startup, no addition after.
38

39 /**
40  * This class creates queues at startup and manages them through a background thread:
41  * long active actions in a queue are interrupted after a timeout.
42  * @author Alexandre Fenyo
43  * @version $Id: Background.java,v 1.24 2007/03/12 05:04:15 fenyo Exp $
44  */

45
46 public class Background implements Runnable JavaDoc {
47   private static Log log = LogFactory.getLog(Background.class);
48
49   private final Config config;
50
51   private Thread JavaDoc interrupt_thread = null;
52
53   // gérer des queues (loops) qui sont chacune associée à un thread particulier,
54
// le nombre de loops est dynamique
55
// many threads access to queues
56
private final Map<String JavaDoc, Queue> queues =
57     Collections.synchronizedMap(new HashMap<String JavaDoc, Queue>());
58
59   /**
60    * Constructor.
61    * main thread.
62    * @param config configuration.
63    */

64   public Background(final Config config) {
65     this.config = config;
66
67     // create default queues
68
// queues.put("standard", new Queue("standard", config));
69
// queues.put("debug", new DebugQueue("debug", config));
70
for (int cnt = 1; cnt <= new Integer JavaDoc(config.getProperty("queues.count.icmp")); cnt++)
71       queues.put("icmp-" + cnt, new PingQueue("icmp-" + cnt, config));
72     for (int cnt = 1; cnt <= new Integer JavaDoc(config.getProperty("queues.count.snmp")); cnt++)
73       queues.put("snmp-" + cnt, new SNMPQueue("snmp-" + cnt, config));
74     for (int cnt = 1; cnt <= new Integer JavaDoc(config.getProperty("queues.count.flood")); cnt++)
75       queues.put("flood-" + cnt, new FloodQueue("flood-" + cnt, config));
76     for (int cnt = 1; cnt <= new Integer JavaDoc(config.getProperty("queues.count.http")); cnt++)
77       queues.put("http-" + cnt, new HTTPQueue("http-" + cnt, config));
78     for (int cnt = 1; cnt <= new Integer JavaDoc(config.getProperty("queues.count.nmap")); cnt++)
79       queues.put("nmap-" + cnt, new NmapQueue("nmap-" + cnt, config));
80     // never add nor remove queues after this constructor returns,
81
// since we may iterate on queues in run()
82
}
83
84   /**
85    * Terminates background threads.
86    * @param none.
87    * @return void.
88    * @throws InterruptedException exception.
89    */

90   // main thread
91
public void end() throws InterruptedException JavaDoc {
92     // terminate each background queue thread
93
for (final Queue queue : queues.values()) queue.end();
94
95     // terminate the background interrupt thread
96
interrupt_thread.interrupt();
97     interrupt_thread.join();
98   }
99
100   /**
101    * Returns the list of background queues.
102    * @param none.
103    * @return Map<String, Queue> list of queues.
104    */

105   // main & GUI thread
106
public Map<String JavaDoc, Queue> getQueues() {
107     return queues;
108   }
109
110   /**
111    * Adds an action to a running queue.
112    * @param action action to add.
113    * @return void.
114    * @throws GeneralException queue does not exist.
115    */

116   // GUI thread
117
public void addActionQueue(final Action action) throws GeneralException {
118     if (!queues.containsKey(action.getQueueName() + "-1"))
119       throw new GeneralException("queue does not exist");
120
121     int smallest_queue = 1;
122     int smallest_size = queues.get(action.getQueueName() + "-1").size();
123     for (int cnt = 1; cnt <= new Integer JavaDoc(config.getProperty("queues.count." + action.getQueueName())); cnt++) {
124       if (queues.get(action.getQueueName() + "-" + cnt).size() < smallest_size)
125         smallest_queue = cnt;
126     }
127     queues.get(action.getQueueName() + "-" + smallest_queue).addAction(action);
128   }
129
130   /**
131    * Removes an action from a running queue.
132    * @param action action to remove.
133    * @return void.
134    * @throws GeneralException queue does not exist.
135    */

136   // GUI thread
137
public void removeActionQueue(final Action action) throws GeneralException {
138     if (!queues.containsKey(action.getQueueName() + "-1"))
139       throw new GeneralException("queue does not exist");
140
141     for (int cnt = 1; cnt <= new Integer JavaDoc(config.getProperty("queues.count." + action.getQueueName())); cnt++)
142       queues.get(action.getQueueName() + "-" + cnt).removeAction(action);
143   }
144
145   /**
146    * Starts the background thread.
147    * @param none.
148    * @return void.
149    */

150   // main thread
151
public void createBackgroundThread() {
152     interrupt_thread = new Thread JavaDoc(this, "Interrupt Thread");
153     interrupt_thread.start();
154   }
155
156   /**
157    * Interrupts long actions.
158    * @param none.
159    * @return void.
160    */

161   // Background thread
162
public void run() {
163     while (!config.isEnd())
164       try {
165         Thread.sleep(1000);
166         for (final Queue queue : queues.values())
167           if (queue.isExhausted()) {
168             queue.interrupt(Action.InterruptCause.timeout);
169           }
170       } catch (final InterruptedException JavaDoc ex) {
171         // this thread is interrupted when the application is terminated
172
} catch (final IOException ex) {
173         log.warn("Exception", ex);
174       }
175   }
176 }
177
Popular Tags