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

Quick Search    Search Deep

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


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