I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit baa70299 authored by zzz's avatar zzz
Browse files

* I2Ping:

       - Add -n count option
       - Add rtt output
       - Enhance help
       - Fix option handling
parent 47856f31
No related branches found
No related tags found
No related merge requests found
...@@ -284,6 +284,7 @@ public class I2PTunnel implements Logging, EventDispatcher { ...@@ -284,6 +284,7 @@ public class I2PTunnel implements Logging, EventDispatcher {
l.log("Command list:"); l.log("Command list:");
l.log("config <i2phost> <i2pport>"); l.log("config <i2phost> <i2pport>");
l.log("listen_on <ip>"); l.log("listen_on <ip>");
l.log("clientoptions[ key=value]*");
l.log("read_timeout <msecs>"); l.log("read_timeout <msecs>");
l.log("owndest yes|no"); l.log("owndest yes|no");
l.log("ping <args>"); l.log("ping <args>");
...@@ -1000,9 +1001,14 @@ public class I2PTunnel implements Logging, EventDispatcher { ...@@ -1000,9 +1001,14 @@ public class I2PTunnel implements Logging, EventDispatcher {
notifyEvent("pingTaskId", Integer.valueOf(task.getId())); notifyEvent("pingTaskId", Integer.valueOf(task.getId()));
} else { } else {
l.log("ping <opts> <dest>"); l.log("ping <opts> <dest>");
l.log("ping <opts> -h"); l.log("ping <opts> -h (pings all hosts in hosts.txt)");
l.log("ping <opts> -l <destlistfile>"); l.log("ping <opts> -l <destlistfile> (pings a list of hosts in a file)");
l.log(" Tests communication with peers.\n" + " opts can be -ns (nosync) or not."); l.log(" Options:\n" +
" -c (require 5 consecutive pings to report success)\n" +
" -m maxSimultaneousPings (default 10)\n" +
" -n numberOfPings (default 3)\n" +
" -t timeout (ms, default 5000)\n");
l.log(" Tests communication with peers.\n");
notifyEvent("pingTaskId", Integer.valueOf(-1)); notifyEvent("pingTaskId", Integer.valueOf(-1));
} }
} }
......
...@@ -20,7 +20,7 @@ import net.i2p.util.Log; ...@@ -20,7 +20,7 @@ import net.i2p.util.Log;
public class I2Ping extends I2PTunnelTask implements Runnable { public class I2Ping extends I2PTunnelTask implements Runnable {
private final static Log _log = new Log(I2Ping.class); private final static Log _log = new Log(I2Ping.class);
private static final int PING_COUNT = 3; private int PING_COUNT = 3;
private static final int CPING_COUNT = 5; private static final int CPING_COUNT = 5;
private static final int PING_TIMEOUT = 5000; private static final int PING_TIMEOUT = 5000;
...@@ -29,6 +29,7 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -29,6 +29,7 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
private int MAX_SIMUL_PINGS = 10; // not really final... private int MAX_SIMUL_PINGS = 10; // not really final...
private boolean countPing = false; private boolean countPing = false;
private boolean reportTimes = true;
private I2PSocketManager sockMgr; private I2PSocketManager sockMgr;
private Logging l; private Logging l;
...@@ -82,6 +83,7 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -82,6 +83,7 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
} }
public void runCommand(String cmd) throws InterruptedException, IOException { public void runCommand(String cmd) throws InterruptedException, IOException {
while (true) {
if (cmd.startsWith("-t ")) { // timeout if (cmd.startsWith("-t ")) { // timeout
cmd = cmd.substring(3); cmd = cmd.substring(3);
int pos = cmd.indexOf(" "); int pos = cmd.indexOf(" ");
...@@ -92,8 +94,7 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -92,8 +94,7 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
timeout = Long.parseLong(cmd.substring(0, pos)); timeout = Long.parseLong(cmd.substring(0, pos));
cmd = cmd.substring(pos + 1); cmd = cmd.substring(pos + 1);
} }
} } else if (cmd.startsWith("-m ")) { // max simultaneous pings
if (cmd.startsWith("-m ")) { // max simultaneous pings
cmd = cmd.substring(3); cmd = cmd.substring(3);
int pos = cmd.indexOf(" "); int pos = cmd.indexOf(" ");
if (pos == -1) { if (pos == -1) {
...@@ -103,18 +104,26 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -103,18 +104,26 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
MAX_SIMUL_PINGS = Integer.parseInt(cmd.substring(0, pos)); MAX_SIMUL_PINGS = Integer.parseInt(cmd.substring(0, pos));
cmd = cmd.substring(pos + 1); cmd = cmd.substring(pos + 1);
} }
} } else if (cmd.startsWith("-n ")) { // number of pings
if (cmd.startsWith("-c ")) { // "count" ping cmd = cmd.substring(3);
int pos = cmd.indexOf(" ");
if (pos == -1) {
l.log("Syntax error");
return;
} else {
PING_COUNT = Integer.parseInt(cmd.substring(0, pos));
cmd = cmd.substring(pos + 1);
}
} else if (cmd.startsWith("-c ")) { // "count" ping
countPing = true; countPing = true;
cmd = cmd.substring(3); cmd = cmd.substring(3);
} } else if (cmd.equals("-h")) { // ping all hosts
if (cmd.equals("-h")) { // ping all hosts
cmd = "-l hosts.txt"; cmd = "-l hosts.txt";
} } else if (cmd.startsWith("-l ")) { // ping a list of hosts
if (cmd.startsWith("-l ")) { // ping a list of hosts
BufferedReader br = new BufferedReader(new FileReader(cmd.substring(3))); BufferedReader br = new BufferedReader(new FileReader(cmd.substring(3)));
String line; String line;
List pingHandlers = new ArrayList(); List pingHandlers = new ArrayList();
int i = 0;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
if (line.startsWith("#")) continue; // comments if (line.startsWith("#")) continue; // comments
if (line.startsWith(";")) continue; if (line.startsWith(";")) continue;
...@@ -123,17 +132,21 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -123,17 +132,21 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
line = line.substring(0, line.indexOf("=")); line = line.substring(0, line.indexOf("="));
} }
pingHandlers.add(new PingHandler(line)); pingHandlers.add(new PingHandler(line));
if (++i > 1)
reportTimes = false;
} }
br.close(); br.close();
for (Iterator it = pingHandlers.iterator(); it.hasNext();) { for (Iterator it = pingHandlers.iterator(); it.hasNext();) {
Thread t = (Thread) it.next(); Thread t = (Thread) it.next();
t.join(); t.join();
} }
return;
} else { } else {
Thread t = new PingHandler(cmd); Thread t = new PingHandler(cmd);
t.join(); t.join();
return;
} }
}
} }
public boolean close(boolean forced) { public boolean close(boolean forced) {
...@@ -163,7 +176,7 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -163,7 +176,7 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
} }
lastPingTime = System.currentTimeMillis(); lastPingTime = System.currentTimeMillis();
} }
boolean sent = sockMgr.ping(dest, PING_TIMEOUT); boolean sent = sockMgr.ping(dest, timeout);
synchronized (simulLock) { synchronized (simulLock) {
simulPings--; simulPings--;
simulLock.notifyAll(); simulLock.notifyAll();
...@@ -193,6 +206,9 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -193,6 +206,9 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
} }
return; return;
} }
int pass = 0;
int fail = 0;
long totalTime = 0;
int cnt = countPing ? CPING_COUNT : PING_COUNT; int cnt = countPing ? CPING_COUNT : PING_COUNT;
StringBuffer pingResults = new StringBuffer(2 * cnt + destination.length() + 3); StringBuffer pingResults = new StringBuffer(2 * cnt + destination.length() + 3);
for (int i = 0; i < cnt; i++) { for (int i = 0; i < cnt; i++) {
...@@ -206,10 +222,28 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -206,10 +222,28 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
pingResults.append("+ "); pingResults.append("+ ");
} }
} else { } else {
pingResults.append(sent ? "+ " : "- "); if (reportTimes) {
if (sent) {
pass++;
long rtt = System.currentTimeMillis() - lastPingTime;
totalTime += rtt;
l.log((i+1) + ": + " + rtt + " ms");
} else {
fail++;
l.log((i+1) + ": -");
}
} else {
pingResults.append(sent ? "+ " : "- ");
}
} }
// System.out.println(sent+" -> "+destination); // System.out.println(sent+" -> "+destination);
} }
if (reportTimes) {
pingResults.append(" ").append(pass).append(" received ");
if (pass > 0)
pingResults.append("(average time ").append(totalTime/pass).append(" ms) ");
pingResults.append("and ").append(fail).append(" lost for destination: ");
}
pingResults.append(" ").append(destination); pingResults.append(" ").append(destination);
synchronized (lock) { // Logger is not thread safe synchronized (lock) { // Logger is not thread safe
l.log(pingResults.toString()); l.log(pingResults.toString());
...@@ -219,4 +253,4 @@ public class I2Ping extends I2PTunnelTask implements Runnable { ...@@ -219,4 +253,4 @@ public class I2Ping extends I2PTunnelTask implements Runnable {
} }
} }
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment