001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch.activities;
024:
025: import net.fenyo.gnetwatch.*;
026: import net.fenyo.gnetwatch.GUI.VisualElement;
027: import net.fenyo.gnetwatch.actions.Action;
028: import net.fenyo.gnetwatch.targets.Target;
029:
030: import java.util.*;
031: import java.io.*;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: // every queues must be created at startup, no addition after.
037:
038: /**
039: * This class creates queues at startup and manages them through a background thread:
040: * long active actions in a queue are interrupted after a timeout.
041: * @author Alexandre Fenyo
042: * @version $Id: Background.java,v 1.24 2007/03/12 05:04:15 fenyo Exp $
043: */
044:
045: public class Background implements Runnable {
046: private static Log log = LogFactory.getLog(Background.class);
047:
048: private final Config config;
049:
050: private Thread interrupt_thread = null;
051:
052: // gérer des queues (loops) qui sont chacune associée à un thread particulier,
053: // le nombre de loops est dynamique
054: // many threads access to queues
055: private final Map<String, Queue> queues = Collections
056: .synchronizedMap(new HashMap<String, Queue>());
057:
058: /**
059: * Constructor.
060: * main thread.
061: * @param config configuration.
062: */
063: public Background(final Config config) {
064: this .config = config;
065:
066: // create default queues
067: // queues.put("standard", new Queue("standard", config));
068: // queues.put("debug", new DebugQueue("debug", config));
069: for (int cnt = 1; cnt <= new Integer(config
070: .getProperty("queues.count.icmp")); cnt++)
071: queues.put("icmp-" + cnt, new PingQueue("icmp-" + cnt,
072: config));
073: for (int cnt = 1; cnt <= new Integer(config
074: .getProperty("queues.count.snmp")); cnt++)
075: queues.put("snmp-" + cnt, new SNMPQueue("snmp-" + cnt,
076: config));
077: for (int cnt = 1; cnt <= new Integer(config
078: .getProperty("queues.count.flood")); cnt++)
079: queues.put("flood-" + cnt, new FloodQueue("flood-" + cnt,
080: config));
081: for (int cnt = 1; cnt <= new Integer(config
082: .getProperty("queues.count.http")); cnt++)
083: queues.put("http-" + cnt, new HTTPQueue("http-" + cnt,
084: config));
085: for (int cnt = 1; cnt <= new Integer(config
086: .getProperty("queues.count.nmap")); cnt++)
087: queues.put("nmap-" + cnt, new NmapQueue("nmap-" + cnt,
088: config));
089: // never add nor remove queues after this constructor returns,
090: // since we may iterate on queues in run()
091: }
092:
093: /**
094: * Terminates background threads.
095: * @param none.
096: * @return void.
097: * @throws InterruptedException exception.
098: */
099: // main thread
100: public void end() throws InterruptedException {
101: // terminate each background queue thread
102: for (final Queue queue : queues.values())
103: queue.end();
104:
105: // terminate the background interrupt thread
106: interrupt_thread.interrupt();
107: interrupt_thread.join();
108: }
109:
110: /**
111: * Returns the list of background queues.
112: * @param none.
113: * @return Map<String, Queue> list of queues.
114: */
115: // main & GUI thread
116: public Map<String, Queue> getQueues() {
117: return queues;
118: }
119:
120: /**
121: * Adds an action to a running queue.
122: * @param action action to add.
123: * @return void.
124: * @throws GeneralException queue does not exist.
125: */
126: // GUI thread
127: public void addActionQueue(final Action action)
128: throws GeneralException {
129: if (!queues.containsKey(action.getQueueName() + "-1"))
130: throw new GeneralException("queue does not exist");
131:
132: int smallest_queue = 1;
133: int smallest_size = queues.get(action.getQueueName() + "-1")
134: .size();
135: for (int cnt = 1; cnt <= new Integer(config
136: .getProperty("queues.count." + action.getQueueName())); cnt++) {
137: if (queues.get(action.getQueueName() + "-" + cnt).size() < smallest_size)
138: smallest_queue = cnt;
139: }
140: queues.get(action.getQueueName() + "-" + smallest_queue)
141: .addAction(action);
142: }
143:
144: /**
145: * Removes an action from a running queue.
146: * @param action action to remove.
147: * @return void.
148: * @throws GeneralException queue does not exist.
149: */
150: // GUI thread
151: public void removeActionQueue(final Action action)
152: throws GeneralException {
153: if (!queues.containsKey(action.getQueueName() + "-1"))
154: throw new GeneralException("queue does not exist");
155:
156: for (int cnt = 1; cnt <= new Integer(config
157: .getProperty("queues.count." + action.getQueueName())); cnt++)
158: queues.get(action.getQueueName() + "-" + cnt).removeAction(
159: action);
160: }
161:
162: /**
163: * Starts the background thread.
164: * @param none.
165: * @return void.
166: */
167: // main thread
168: public void createBackgroundThread() {
169: interrupt_thread = new Thread(this , "Interrupt Thread");
170: interrupt_thread.start();
171: }
172:
173: /**
174: * Interrupts long actions.
175: * @param none.
176: * @return void.
177: */
178: // Background thread
179: public void run() {
180: while (!config.isEnd())
181: try {
182: Thread.sleep(1000);
183: for (final Queue queue : queues.values())
184: if (queue.isExhausted()) {
185: queue.interrupt(Action.InterruptCause.timeout);
186: }
187: } catch (final InterruptedException ex) {
188: // this thread is interrupted when the application is terminated
189: } catch (final IOException ex) {
190: log.warn("Exception", ex);
191: }
192: }
193: }
|