KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
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 /**
44  * This class captures Ethernet frames using tethereal.
45  * A tethereal instance is spawned through a Capture instance, for each layer-2 interface.
46  * @author Alexandre Fenyo
47  * @version $Id: CaptureManager.java,v 1.8 2007/03/08 18:21:31 fenyo Exp $
48  */

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   // deprecated donc changer de type
58
private MultiMap listeners = new MultiHashMap();
59
60   public interface HandlePacket {
61     public void document(final Document packet);
62   }
63
64   /**
65    * Constructor.
66    * main thread.
67    * @param config configuration.
68    */

69   public CaptureManager(final Config config) {
70     this.config = config;
71   }
72
73   /**
74    * Defines the GUI instance.
75    * @param GUI gui.
76    * @return void.
77    */

78   public void setGUI(final GUI gui) {
79     this.gui = gui;
80   }
81
82   /**
83    * Creates captures instances for each available interface.
84    * @param filter tethereal filter.
85    * @return void.
86    * @throws InterruptedException exception.
87    */

88   // GUI thread
89
private void startCapture(final String JavaDoc filter) throws InterruptedException JavaDoc {
90     synchronized (capture_list) {
91       final String JavaDoc [] 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 JavaDoc device : devices) {
102         final Matcher JavaDoc match = Pattern.compile("^([0-9]*)\\. ").matcher(device);
103         if (match.find() == true) {
104           final Capture capture = new Capture(config, this, new Integer JavaDoc(match.group(1)).intValue(), filter);
105           capture_list.add(capture);
106           capture.createCaptureThread();
107         }
108       }
109     }
110   }
111
112   /**
113    * Stops every capture instances.
114    * @param none.
115    * @return void.
116    * @throws InterruptedException exception.
117    */

118   // GUI thread
119
private void stopCapture() throws InterruptedException JavaDoc {
120     synchronized (capture_list) {
121       for (final Capture capture : capture_list) capture.end();
122       capture_list.clear();
123     }
124   }
125
126   /**
127    * Creates a filter that integrates every individual listener filters.
128    * @param none.
129    * @return String global filter.
130    */

131   // GUI thread
132
private String JavaDoc getGlobalFilter() {
133     String JavaDoc global_filter = "";
134     synchronized (listeners) {
135       if (!listeners.keySet().contains(""))
136         for (final String JavaDoc individual_filter : new HashSet<String JavaDoc>(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   /**
147    * Register a frame listener.
148    * @param filter filter this listener is interested in.
149    * @param callback entry point for asynchronous call.
150    * @throws InterruptedException exception.
151    */

152   // the methods registerListener, unRegisterListener and unRegisterAllListeners are synchronized
153
// to be sure only one is executed at a time
154
// GUI thread
155
public synchronized void registerListener(final String JavaDoc filter, final HandlePacket callback) throws InterruptedException JavaDoc {
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   /**
175    * Removes a frame listener.
176    * @param filter filter this listener was interested in.
177    * @param callback entry point for asynchronous call.
178    * @return void.
179    * @throws InterruptedException exception.
180    */

181   // GUI thread
182
public synchronized void unRegisterListener(final String JavaDoc filter, final HandlePacket callback) throws InterruptedException JavaDoc {
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   /**
199    * Removes every listeners.
200    * @param none.
201    * @return void.
202    * @throws InterruptedException exception.
203    */

204   // main thread
205
public synchronized void unRegisterAllListeners() throws InterruptedException JavaDoc {
206     synchronized (listeners) {
207       listeners.clear();
208     }
209
210     stopCapture();
211   }
212
213   /**
214    * Inform every listeners about the next frame.
215    * @param packet next frame.
216    * @return void.
217    */

218   // Capture thread
219
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