Files
i2p.itoopie/apps/tests/echotester/EchoTester.java

167 lines
5.5 KiB
Java
Raw Normal View History

2004-04-08 04:41:54 +00:00
/*
* Test for an echo server. This test is intended to be used via an
* I2PTunnel, but should work as well on other networks that provide
* TCP tunneling and an echo server.
*
* Copyright (c) 2004 Michael Schierl
*
* Licensed unter GNU General Public License.
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ConnectException;
import java.net.Socket;
import java.net.SocketException;
2004-04-08 04:41:54 +00:00
/**
* The main engine for the EchoTester.
*/
public class EchoTester extends Thread {
/**
* How long to wait between packets. Default is 6 seconds.
*/
2004-04-10 11:50:11 +00:00
private static long PACKET_DELAY = 6000;
2004-04-08 04:41:54 +00:00
/**
* How many packets may be on the way before the connection is
* seen as "broken" and disconnected.
*/
2004-04-10 11:50:11 +00:00
private static final long MAX_PACKETS_QUEUED = 50; // unused
2004-04-08 04:41:54 +00:00
private EchoTestAnalyzer eta;
private String host;
private int port;
// the following vars are synchronized via the lock.
private Object lock = new Object();
2004-04-10 11:50:11 +00:00
private long nextPacket = 0;
private long nextUnreceived = 0;
private boolean readerRunning = false;
2004-04-08 04:41:54 +00:00
public static void main(String[] args) {
2004-04-10 11:50:11 +00:00
if (args.length == 3) PACKET_DELAY = Long.parseLong(args[2]);
new EchoTester(args[0], Integer.parseInt(args[1]), new BasicEchoTestAnalyzer());
2004-04-08 04:41:54 +00:00
}
2004-04-10 11:50:11 +00:00
2004-04-08 04:41:54 +00:00
public EchoTester(String host, int port, EchoTestAnalyzer eta) {
2004-04-10 11:50:11 +00:00
this.eta = eta;
this.host = host;
this.port = port;
start();
2004-04-08 04:41:54 +00:00
}
public void run() {
2004-04-10 11:50:11 +00:00
try {
while (true) {
Socket s;
try {
s = new Socket(host, port);
} catch (ConnectException ex) {
eta.disconnected(true);
Thread.sleep(PACKET_DELAY);
continue;
}
System.out.println("41: Connected to " + host + ":" + port);
synchronized (lock) {
nextUnreceived = nextPacket;
}
Thread t = new ResponseReaderThread(s);
Writer w = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
while (true) {
long no;
synchronized (lock) {
no = nextPacket++;
}
try {
w.write(no + " " + System.currentTimeMillis() + "\n");
w.flush();
} catch (SocketException ex) {
break;
}
Thread.sleep(PACKET_DELAY);
}
s.close();
t.join();
synchronized (lock) {
if (readerRunning) {
System.out.println("*** WHY IS THIS THREAD STILL" + " RUNNING?");
}
while (nextUnreceived < nextPacket) {
nextUnreceived++;
eta.packetLossOccurred(true);
}
if (nextUnreceived > nextPacket) {
System.out.println("*** WTF? " + nextUnreceived + " > " + nextPacket);
}
}
eta.disconnected(false);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
2004-04-14 23:26:59 +00:00
System.exit(1); // treat these errors as fatal
2004-04-10 11:50:11 +00:00
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1); // treat these errors as fatal
}
2004-04-08 04:41:54 +00:00
}
private class ResponseReaderThread extends Thread {
2004-04-10 11:50:11 +00:00
private Socket s;
2004-04-08 04:41:54 +00:00
2004-04-10 11:50:11 +00:00
public ResponseReaderThread(Socket s) {
this.s = s;
synchronized (lock) {
readerRunning = true;
}
start();
}
2004-04-08 04:41:54 +00:00
2004-04-10 11:50:11 +00:00
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
int index;
while ((line = br.readLine()) != null) {
if ((index = line.indexOf(" ")) == -1) continue;
long now, packetNumber, packetTime;
now = System.currentTimeMillis();
try {
packetNumber = Long.parseLong(line.substring(0, index));
packetTime = Long.parseLong(line.substring(index + 1));
} catch (NumberFormatException ex) {
System.out.println(ex.toString());
continue;
}
synchronized (lock) {
while (packetNumber > nextUnreceived) {
nextUnreceived++;
eta.packetLossOccurred(false);
}
if (nextUnreceived > packetNumber) {
System.out.println("*** DOUBLE PACKET!");
} else {
nextUnreceived++;
}
}
eta.successOccurred(now - packetTime);
}
} catch (SocketException ex) {
// ignore
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
synchronized (lock) {
readerRunning = false;
}
}
2004-04-08 04:41:54 +00:00
}
2004-04-10 11:50:11 +00:00
}