1 2 23 24 package net.fenyo.gnetwatch.activities; 25 26 import java.util.*; 27 import java.util.regex.Matcher ; 28 import java.util.regex.Pattern ; 29 30 import net.fenyo.gnetwatch.*; 31 import net.fenyo.gnetwatch.GUI.GUI; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 import org.apache.commons.collections.*; 37 38 import org.dom4j.*; 39 import org.dom4j.io.*; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.widgets.MessageBox; 42 43 49 50 public class CaptureManager { 51 private static Log log = LogFactory.getLog(CaptureManager.class); 52 53 private final Config config; 54 private GUI gui; 55 56 private final List<Capture> capture_list = new LinkedList<Capture>(); 57 private MultiMap listeners = new MultiHashMap(); 59 60 public interface HandlePacket { 61 public void document(final Document packet); 62 } 63 64 69 public CaptureManager(final Config config) { 70 this.config = config; 71 } 72 73 78 public void setGUI(final GUI gui) { 79 this.gui = gui; 80 } 81 82 88 private void startCapture(final String filter) throws InterruptedException { 90 synchronized (capture_list) { 91 final String [] devices = Capture.listDevices(); 92 93 if (devices == null) { 94 final MessageBox dialog = new MessageBox(gui.getShell(), SWT.ICON_ERROR | SWT.OK); 95 dialog.setText(config.getString("thethereal_error")); 96 dialog.setMessage(config.getString("thethereal_error_long")); 97 dialog.open(); 98 return; 99 } 100 101 for (final String device : devices) { 102 final Matcher match = Pattern.compile("^([0-9]*)\\. ").matcher(device); 103 if (match.find() == true) { 104 final Capture capture = new Capture(config, this, new Integer (match.group(1)).intValue(), filter); 105 capture_list.add(capture); 106 capture.createCaptureThread(); 107 } 108 } 109 } 110 } 111 112 118 private void stopCapture() throws InterruptedException { 120 synchronized (capture_list) { 121 for (final Capture capture : capture_list) capture.end(); 122 capture_list.clear(); 123 } 124 } 125 126 131 private String getGlobalFilter() { 133 String global_filter = ""; 134 synchronized (listeners) { 135 if (!listeners.keySet().contains("")) 136 for (final String individual_filter : new HashSet<String >(listeners.keySet())) { 137 if (!individual_filter.equals("")) { 138 if (global_filter.equals("")) global_filter = "(" + individual_filter + ")"; 139 else global_filter += " or (" + individual_filter + ")"; 140 } 141 } 142 } 143 return global_filter; 144 } 145 146 152 public synchronized void registerListener(final String filter, final HandlePacket callback) throws InterruptedException { 156 final boolean does_contain; 157 158 synchronized (listeners) { 159 does_contain = listeners.containsKey(filter); 160 if (does_contain) listeners.put(filter, callback); 161 } 162 163 if (!does_contain) { 164 stopCapture(); 165 166 synchronized (listeners) { 167 listeners.put(filter, callback); 168 } 169 170 startCapture(getGlobalFilter()); 171 } 172 } 173 174 181 public synchronized void unRegisterListener(final String filter, final HandlePacket callback) throws InterruptedException { 183 final boolean does_contain; 184 final int size; 185 186 synchronized (listeners) { 187 listeners.remove(filter, callback); 188 does_contain = listeners.containsKey(filter); 189 size = listeners.size(); 190 } 191 192 if (!does_contain) { 193 stopCapture(); 194 if (size > 0) startCapture(getGlobalFilter()); 195 } 196 } 197 198 204 public synchronized void unRegisterAllListeners() throws InterruptedException { 206 synchronized (listeners) { 207 listeners.clear(); 208 } 209 210 stopCapture(); 211 } 212 213 218 public void handlePacket(final Document packet) { 220 synchronized (listeners) { 221 for (final HandlePacket callback : new ArrayList<HandlePacket>(listeners.values())) 222 callback.document(packet); 223 } 224 } 225 } 226 | Popular Tags |