|
From: <cr...@us...> - 2008-05-11 14:18:23
|
Revision: 4085
http://jnode.svn.sourceforge.net/jnode/?rev=4085&view=rev
Author: crawley
Date: 2008-05-11 07:18:18 -0700 (Sun, 11 May 2008)
Log Message:
-----------
Converted PingCommand
Modified Paths:
--------------
trunk/net/descriptors/org.jnode.net.command.xml
trunk/net/src/net/org/jnode/net/command/PingCommand.java
Modified: trunk/net/descriptors/org.jnode.net.command.xml
===================================================================
--- trunk/net/descriptors/org.jnode.net.command.xml 2008-05-11 13:52:42 UTC (rev 4084)
+++ trunk/net/descriptors/org.jnode.net.command.xml 2008-05-11 14:18:18 UTC (rev 4085)
@@ -63,6 +63,9 @@
<syntax alias="netstat">
<empty description="Print statistics for all network devices"/>
</syntax>
+ <syntax alias="ping">
+ <argument argLabel="host" description="Ping a remote host"/>
+ </syntax>
<syntax alias="tcpinout">
<argument argLabel="localPort" description="Run tcpinout in server mode"/>
<sequence description="Run tcpinout in client mode">
Modified: trunk/net/src/net/org/jnode/net/command/PingCommand.java
===================================================================
--- trunk/net/src/net/org/jnode/net/command/PingCommand.java 2008-05-11 13:52:42 UTC (rev 4084)
+++ trunk/net/src/net/org/jnode/net/command/PingCommand.java 2008-05-11 14:18:18 UTC (rev 4085)
@@ -21,7 +21,8 @@
package org.jnode.net.command;
-import java.net.InetAddress;
+import java.io.InputStream;
+import java.io.PrintStream;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
@@ -38,79 +39,70 @@
import org.jnode.net.ipv4.icmp.ICMPProtocol;
import org.jnode.net.ipv4.layer.IPv4NetworkLayer;
import org.jnode.net.util.NetUtils;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Parameter;
-import org.jnode.shell.help.ParsedArguments;
-import org.jnode.shell.help.Syntax;
-import org.jnode.shell.help.argument.HostNameArgument;
+import org.jnode.shell.AbstractCommand;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.HostNameArgument;
/**
* @author JPG
*/
-public class PingCommand implements ICMPListener {
-
+public class PingCommand extends AbstractCommand implements ICMPListener {
+ // FIXME Some of the following could be command parameters ...
private final Statistics stat = new Statistics();
-
private boolean wait = true;
-
private int count = 4;
-
private boolean dontFragment = false;
-
- private final IPv4Address dst;
-
+ private IPv4Address dst;
private boolean flood = false;
-
private int interval = 6000;
-
private int size = 64;
-
private long timeout = 5000;
-
private int ttl = 255;
- static final HostNameArgument DST = new HostNameArgument("host", "the target host");
+ private final HostNameArgument ARG_HOST =
+ new HostNameArgument("host", Argument.MANDATORY, "the target host");
- public static Help.Info HELP_INFO = new Help.Info(
- "ping",
- new Syntax[] { new Syntax("Ping the specified host",
- new Parameter[] { new Parameter(DST, Parameter.MANDATORY)})});
+ public PingCommand() {
+ super("Ping the specified host");
+ registerArguments(ARG_HOST);
+ }
public static void main(String[] args) throws Exception {
- final ParsedArguments cmdLine = HELP_INFO.parse(args);
- final PingCommand cmd = new PingCommand(DST.getAddress(cmdLine));
- cmd.execute();
+ new PingCommand().execute(args);
}
- public PingCommand(InetAddress destination) {
- this.dst = new IPv4Address(destination);
- }
-
- public void execute() throws SocketException, InterruptedException {
- final IPv4Header netHeader = new IPv4Header(0, this.ttl, IPv4Constants.IPPROTO_ICMP, this.dst, 8);
+ public void execute(CommandLine commandLine, InputStream in,
+ PrintStream out, PrintStream err)
+ throws SocketException, InterruptedException {
+ this.dst = new IPv4Address(ARG_HOST.getValue());
+ final IPv4Header netHeader =
+ new IPv4Header(0, this.ttl, IPv4Constants.IPPROTO_ICMP, this.dst, 8);
netHeader.setDontFragment(this.dontFragment);
final IPv4NetworkLayer netLayer = (IPv4NetworkLayer) NetUtils.getNLM()
- .getNetworkLayer(EthernetConstants.ETH_P_IP);
- final ICMPProtocol icmpProtocol = (ICMPProtocol) netLayer
- .getProtocol(ICMPProtocol.IPPROTO_ICMP);
+ .getNetworkLayer(EthernetConstants.ETH_P_IP);
+ final ICMPProtocol icmpProtocol =
+ (ICMPProtocol) netLayer.getProtocol(ICMPProtocol.IPPROTO_ICMP);
icmpProtocol.addListener(this);
try {
int id_count = 0;
int seq_count = 0;
while (this.count != 0) {
- System.out.println("Ping " + dst + " attempt " + seq_count);
+ out.println("Ping " + dst + " attempt " + seq_count);
- if (!this.flood) this.wait = true;
+ if (!this.flood) {
+ this.wait = true;
+ }
SocketBuffer packet = new SocketBuffer();
packet.insert(this.size);
- ICMPEchoHeader transportHeader = new ICMPEchoHeader(8,
- id_count, seq_count);
+ ICMPEchoHeader transportHeader =
+ new ICMPEchoHeader(8, id_count, seq_count);
transportHeader.prefixTo(packet);
- Request r = new Request(this.stat, this.timeout, System
- .currentTimeMillis(), id_count, seq_count);
+ Request r = new Request(this.stat, this.timeout,
+ System.currentTimeMillis(), id_count, seq_count);
registerRequest(r);
netLayer.transmit(netHeader, packet);
@@ -120,13 +112,13 @@
this.wait = false;
}
Thread.sleep(500);
- synchronized(this){
- if(response){
- System.out.print("Reply from " + dst.toString() + ": ");
- System.out.print(hdr1.getDataLength() - 8 + "bytes of data ");
- System.out.print("ttl=" + hdr1.getTtl() + " ");
- System.out.print("seq=" + hdr2.getSeqNumber() + " ");
- System.out.println("time=" + (roundt) + "ms");
+ synchronized (this) {
+ if (response) {
+ out.print("Reply from " + dst.toString() + ": ");
+ out.print(hdr1.getDataLength() - 8 + "bytes of data ");
+ out.print("ttl=" + hdr1.getTtl() + " ");
+ out.print("seq=" + hdr2.getSeqNumber() + " ");
+ out.println("time=" + (roundt) + "ms");
response = false;
}
}
@@ -138,19 +130,22 @@
while (!isEmpty()) {
Thread.sleep(100);
}
- } finally {
+ }
+ finally {
icmpProtocol.removeListener(this);
}
- System.out.println("-> Packet statistics");
- System.out.println(this.stat.getStatistics());
+ out.println("-> Packet statistics");
+ out.println(this.stat.getStatistics());
}
private long match(int id, int seq, Request r) {
- if ((r != null) && (id == r.getId()))
+ if (r != null && id == r.getId()) {
return r.getTimestamp();
- else
+ }
+ else {
return -1;
+ }
}
public void packetReceived(SocketBuffer skbuf) {
@@ -161,7 +156,9 @@
int seq = hdr2.getSeqNumber();
Request r = removeRequest(seq);
- if ((r == null) || (r.Obsolete())) return;
+ if (r == null || r.Obsolete()) {
+ return;
+ }
long timestamp = match(hdr2.getIdentifier(), seq, r);
@@ -169,7 +166,8 @@
gotResponse(timestamp, hdr1, hdr2, roundtrip);
}
- private synchronized void gotResponse(long timestamp, IPv4Header hdr1, ICMPEchoHeader hdr2, long roundtrip) {
+ private synchronized void gotResponse(
+ long timestamp, IPv4Header hdr1, ICMPEchoHeader hdr2, long roundtrip) {
if (timestamp != -1) {
this.hdr1 = hdr1;
this.hdr2 = hdr2;
@@ -179,8 +177,8 @@
wait = false;
this.stat.recordPacket(roundtrip);
}
-
- //respose data
+
+ //response data
private boolean response;
private long roundt;
private IPv4Header hdr1;
@@ -201,15 +199,10 @@
}
class Request extends TimerTask {
-
private Timer timer = new Timer();
-
private boolean obsolete = false;
-
private Statistics stat;
-
private long timestamp;
-
private int id, seq;
Request(Statistics stat, long timeout, long timestamp, int id, int seq) {
@@ -234,8 +227,10 @@
this.obsolete = true;
this.timer.cancel();
return false;
- } else
+ }
+ else {
return true;
+ }
}
long getTimestamp() {
@@ -250,42 +245,35 @@
return seq;
}
}
-}
-class Statistics {
+ private class Statistics {
+ private int received = 0, lost = 0;
+ private long min = Integer.MAX_VALUE, max = 0;
+ private long sum;
- private int received = 0, lost = 0;
+ void recordPacket(long roundtrip) {
+ received++;
+ if (roundtrip < min) {
+ min = roundtrip;
+ }
+ if (roundtrip > max) {
+ max = roundtrip;
+ }
+ sum += roundtrip;
+ }
- private long min = Integer.MAX_VALUE, max = 0;
+ void recordLost() {
+ lost++;
+ }
- private long sum;
-
- void recordPacket(long roundtrip) {
- received++;
-
- if (roundtrip < min) min = roundtrip;
- if (roundtrip > max) max = roundtrip;
-
- sum += roundtrip;
+ String getStatistics() {
+ int packets = received + lost;
+ float avg = sum / packets;
+ return (packets + " packets transmitted, " +
+ received +" packets received\n" +
+ "round-trip min/avg/max = " + min + "/" + avg +
+ "/" + max + " ms");
+ }
}
-
- void recordLost() {
- lost++;
- }
-
- String getStatistics() {
- int packets = received + lost;
- //float percent = 0;
- // if (packets != 0){
- // percent = lost/packets;
- // percent *= 100;
- // }
- float avg = sum / packets;
- return packets + " packets transmitted, " + received
- + " packets received\n" +
- // percent +"% packet loss\n"+
- "round-trip min/avg/max = " + min + "/" + avg + "/" + max
- + " ms";
- }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|