KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > Main


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;
25
26 import net.fenyo.gnetwatch.activities.*;
27 import net.fenyo.gnetwatch.targets.*;
28 import net.fenyo.gnetwatch.GUI.*;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.dom4j.Document;
33 import org.dom4j.Node;
34
35 import java.util.*;
36
37 /**
38  * This class is dedicated to maintain main operations.
39  * @author Alexandre Fenyo
40  * @version $Id: Main.java,v 1.16 2007/03/03 00:38:19 fenyo Exp $
41  */

42
43 public class Main {
44   private static Log log = LogFactory.getLog(Main.class);
45   private final Config config;
46   private GUI gui = null;
47   private final CaptureManager capture_mgr;
48   private CaptureManager.HandlePacket handler = null;
49
50   /**
51    * Constructor.
52    * main thread
53    * @param config configuration.
54    */

55   public Main(final Config config, final CaptureManager capture_mgr) {
56     this.config = config;
57     this.capture_mgr = capture_mgr;
58   }
59
60   /**
61    * Defines the GUI instance.
62    * @param GUI gui.
63    * @return void.
64    */

65   public void setGUI(final GUI gui) {
66     this.gui = gui;
67   }
68
69   /**
70    * Ask to spawn tethereal instances and add targets for new discovered IP targets.
71    * @param none.
72    * @return void.
73    */

74   // GUI thread
75
public void startDiscover() {
76     handler =
77       new CaptureManager.HandlePacket() {
78         public void document(final Document packet) {
79           final List<Node> nodes = packet.selectNodes("/packet/proto[@name='ip']/field[@name='ip.addr']");
80           if (nodes.size() == 2) {
81             final String JavaDoc srcaddr = nodes.get(0).valueOf("@show");
82             final String JavaDoc dstaddr = nodes.get(1).valueOf("@show");
83             // shorter but slower way :
84
// final String srcaddr = (String) packet.selectObject("string(//field[@name='ip.addr'][1]/@show)");
85

86 // log.debug("ipv4 packet captured: " + srcaddr + " -> " + dstaddr);
87
TargetIPv4.addTargetIPv4(gui, srcaddr);
88             TargetIPv4.addTargetIPv4(gui, dstaddr);
89           }
90
91           final List<Node> nodes_ipv6 = packet.selectNodes("/packet/proto[@name='ipv6']/field[@name='ipv6.addr']");
92           if (nodes_ipv6.size() == 2) {
93             final String JavaDoc srcaddr = nodes_ipv6.get(0).valueOf("@show");
94             final String JavaDoc dstaddr = nodes_ipv6.get(1).valueOf("@show");
95
96 // log.debug("ipv6 packet captured: " + srcaddr + " -> " + dstaddr);
97
TargetIPv6.addTargetIPv6(gui, srcaddr);
98             TargetIPv6.addTargetIPv6(gui, dstaddr);
99           }
100         }
101       };
102     try {
103       capture_mgr.registerListener("ip", handler);
104     } catch (final InterruptedException JavaDoc ex) {
105       log.error("Exception", ex);
106     }
107   }
108
109   /**
110    * Ask to kill tethereal instances if this is the last listener.
111    * @param none.
112    * @return void.
113    */

114   // GUI thread
115
public void stopDiscover() {
116     try {
117       capture_mgr.unRegisterListener("ip", handler);
118     } catch (final InterruptedException JavaDoc ex) {
119       log.error("Exception", ex);
120     }
121   }
122 }
123
Popular Tags