Source code: hsr/ipm/collecting/TelstraTracer.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 TelstraTracerouteserver.
35 *
36 * @author Christian Niklaus
37 * @author Daniel Kamm
38 * @version 1.0
39 */
40 public class TelstraTracer extends Tracer implements Runnable {
41
42 private StringBuffer response = new StringBuffer();
43 public Trace trace;
44 DatabaseHandler myHandler;
45 private ErrorManager errorhandler = ErrorManager.getInstance();
46
47 private final String TRACESERVER_URL = new String("http://tcruskit.telstra.net/cgi-bin/trace");
48 private final String TRACESERVER_PARAM1 = new String("?");
49
50 /**
51 * Generates a object of TelstraTracer
52 */
53 public TelstraTracer(Trace trace) {
54 this.trace = trace;
55 this.trace.setType(Type.UDP);
56 myHandler = new DatabaseHandler();
57
58 }
59
60 /**
61 * These Methode handels the connection to the TelstraTracerouteserver and sends the request.
62 */
63 public void requestTS() {
64 try {
65 trace.setStartTime(new Date(System.currentTimeMillis()));
66
67 StringBuffer buf = new StringBuffer();
68 buf.append(TRACESERVER_URL);
69 buf.append(TRACESERVER_PARAM1);
70 buf.append(trace.getDestinationHost().getIpAddress());
71
72 URL url = new URL(buf.toString());
73
74 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
75 String str;
76
77 while ((str = in.readLine()) != null) {
78 response.append(str).append("\n");
79 }
80 in.close();
81
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 TelstraTracerouteserver
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 TelstraTracerouteserver
120 */
121 public void run() {
122 StringBuffer traceStarted = new StringBuffer();
123 traceStarted.append(trace.getStartTime()).append(" Trace started. ").append("SourceHost: ").append(trace.getSourceHost()).append(" DestinationHost: ").append(trace.getDestinationHost());
124 errorhandler.info(traceStarted.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 StringBuffer traceFinish = new StringBuffer();
137 traceFinish.append(new Date().toString()).append(" Trace finished. ").append("SourceHost: ").append(trace.getSourceHost()).append(" DestinationHost: ").append(trace.getDestinationHost());
138 errorhandler.info(traceFinish.toString());
139 }
140 }