1 2 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 39 45 46 public class Background implements Runnable { 47 private static Log log = LogFactory.getLog(Background.class); 48 49 private final Config config; 50 51 private Thread interrupt_thread = null; 52 53 private final Map<String , Queue> queues = 57 Collections.synchronizedMap(new HashMap<String , Queue>()); 58 59 64 public Background(final Config config) { 65 this.config = config; 66 67 for (int cnt = 1; cnt <= new Integer (config.getProperty("queues.count.icmp")); cnt++) 71 queues.put("icmp-" + cnt, new PingQueue("icmp-" + cnt, config)); 72 for (int cnt = 1; cnt <= new Integer (config.getProperty("queues.count.snmp")); cnt++) 73 queues.put("snmp-" + cnt, new SNMPQueue("snmp-" + cnt, config)); 74 for (int cnt = 1; cnt <= new Integer (config.getProperty("queues.count.flood")); cnt++) 75 queues.put("flood-" + cnt, new FloodQueue("flood-" + cnt, config)); 76 for (int cnt = 1; cnt <= new Integer (config.getProperty("queues.count.http")); cnt++) 77 queues.put("http-" + cnt, new HTTPQueue("http-" + cnt, config)); 78 for (int cnt = 1; cnt <= new Integer (config.getProperty("queues.count.nmap")); cnt++) 79 queues.put("nmap-" + cnt, new NmapQueue("nmap-" + cnt, config)); 80 } 83 84 90 public void end() throws InterruptedException { 92 for (final Queue queue : queues.values()) queue.end(); 94 95 interrupt_thread.interrupt(); 97 interrupt_thread.join(); 98 } 99 100 105 public Map<String , Queue> getQueues() { 107 return queues; 108 } 109 110 116 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 (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 136 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 (config.getProperty("queues.count." + action.getQueueName())); cnt++) 142 queues.get(action.getQueueName() + "-" + cnt).removeAction(action); 143 } 144 145 150 public void createBackgroundThread() { 152 interrupt_thread = new Thread (this, "Interrupt Thread"); 153 interrupt_thread.start(); 154 } 155 156 161 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 ex) { 171 } catch (final IOException ex) { 173 log.warn("Exception", ex); 174 } 175 } 176 } 177 | Popular Tags |