Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: hsr/ipm/collecting/AachenTracer.java


1   package hsr.ipm.collecting;
2   
3   import cvu.html.HTMLNode;
4   import cvu.html.HTMLTree;
5   import hsr.ipm.alerting.ErrorManager;
6   import hsr.ipm.storing.*;
7   
8   import java.io.BufferedReader;
9   import java.io.IOException;
10  import java.io.InputStreamReader;
11  import java.net.ConnectException;
12  import java.net.MalformedURLException;
13  import java.net.URL;
14  import java.util.Date;
15  import java.util.StringTokenizer;
16  
17  /*
18        This program is free software; you can redistribute it and/or modify
19        it under the terms of the GNU General Public License as published by
20        the Free Software Foundation; either version 2 of the License, or
21        (at your option) any later version.
22  
23        This program is distributed in the hope that it will be useful,
24        but WITHOUT ANY WARRANTY; without even the implied warranty of
25        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26        GNU General Public License for more details.
27  
28        You should have received a copy of the GNU General Public License
29        along with this program; if not, write to the Free Software
30        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  */
32  
33  /**
34   * These class interacts with the AachenTraceroutserver.
35   *
36   * @author Christian Niklaus
37   * @author Daniel Kamm
38   * @version 1.0
39   */
40  
41  public class AachenTracer extends Tracer implements Runnable {
42  
43      private StringBuffer response = new StringBuffer();
44      public Trace trace;
45      DatabaseHandler myHandler;
46      private ErrorManager errorhandler = ErrorManager.getInstance();
47  
48      private final String TRACESERVER_URL = new String("http://www.informatik.rwth-aachen.de/cgi-bin/trace");
49      private final String TRACESERVER_PARAM1 = new String("?");
50  
51  
52       /**
53       * Generates a object of AachenTracer
54       */
55      public AachenTracer(Trace trace) {
56          this.trace = trace;
57          this.trace.setType(Type.UDP);
58          myHandler = new DatabaseHandler();
59      }
60  
61      /**
62       * These Methode handels the connection to the Aachentracerouteserver and sends the request.
63       */
64      public void requestTS() {
65          try {
66              trace.setStartTime(new Date(System.currentTimeMillis()));
67  
68              StringBuffer buf = new StringBuffer();
69              buf.append(TRACESERVER_URL);
70              buf.append(TRACESERVER_PARAM1);
71              buf.append(trace.getDestinationHost().getIpAddress());
72  
73              URL url = new URL(buf.toString());
74  
75              BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
76              String str;
77  
78              while ((str = in.readLine()) != null) {
79                  response.append(str).append("\n");
80              }
81              in.close();
82  
83          } catch (MalformedURLException e) {
84              e.printStackTrace();
85          } catch (ConnectException  e) {
86              errorhandler.error("Server is not available! Connection refused!");
87          } catch (IOException e) {
88              e.printStackTrace();
89          }
90      }
91  
92       /**
93       * These Methode parses the response from the Tracerouteserver
94       */
95      public void parseTSResponse() {
96  
97          HTMLNode myNode = new HTMLNode("pre");
98          StringBuffer traceRoutes = new StringBuffer();
99  
100 
101         HTMLTree myTree = new HTMLTree(response.toString());
102         myNode = myTree.findInAll("pre");
103 
104         traceRoutes.append(myNode.toString());
105         traceRoutes = traceRoutes.delete(traceRoutes.indexOf("<"), traceRoutes.indexOf("\n") + 1);
106         traceRoutes = traceRoutes.delete(traceRoutes.indexOf("t"), traceRoutes.indexOf("\n"));
107         traceRoutes = traceRoutes.delete(traceRoutes.indexOf("<"), traceRoutes.indexOf(">") + 1);
108 
109 
110         StringTokenizer HopTokenizer = new StringTokenizer(traceRoutes.toString(), "\n");
111         while (HopTokenizer.hasMoreTokens()) {
112             StringTokenizer ParamTokenizer = new StringTokenizer(HopTokenizer.nextToken());
113             Hop aHop = new Hop();
114             parseHop(ParamTokenizer, aHop, trace);
115         }
116     }
117 
118     /**
119      * These Methode starts the interaction with the Aachentracerouteserver
120      */
121     public void run() {
122         StringBuffer traceFinStarted = new StringBuffer();
123         traceFinStarted.append(trace.getStartTime()).append(" Trace started. ").append("SourceHost: ").append(trace.getSourceHost()).append(" DestinationHost: ").append(trace.getDestinationHost());
124         errorhandler.info(traceFinStarted.toString());
125         requestTS();
126         parseTSResponse();
127         isTraceValid(trace);
128 
129         myHandler.openConnection();
130         try {
131             myHandler.saveTrace(trace);
132         } catch (Exception e) {
133             e.printStackTrace();
134         }
135         myHandler.closeConnection();
136 
137         StringBuffer traceFinish = new StringBuffer();
138         traceFinish.append(new Date().toString()).append(" Trace finished. ").append("SourceHost: ").append(trace.getSourceHost()).append(" DestinationHost: ").append(trace.getDestinationHost());
139         errorhandler.info(traceFinish.toString());
140     }
141 }