1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 04:22:05 +01:00

UDP Echo improvement

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7250 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Thiago Camargo 2007-02-23 16:36:50 +00:00 committed by thiago
parent 6cccdccac8
commit 1d11b2179f

View file

@ -650,6 +650,8 @@ public abstract class TransportCandidate {
byte password[] = null; byte password[] = null;
List<DatagramListener> listeners = new ArrayList<DatagramListener>(); List<DatagramListener> listeners = new ArrayList<DatagramListener>();
boolean enabled = true; boolean enabled = true;
boolean ended = false;
long tries = 10;
public CandidateEcho(TransportCandidate candidate) throws UnknownHostException, SocketException { public CandidateEcho(TransportCandidate candidate) throws UnknownHostException, SocketException {
this.socket = new DatagramSocket(candidate.getPort(), InetAddress.getByName("0.0.0.0")); this.socket = new DatagramSocket(candidate.getPort(), InetAddress.getByName("0.0.0.0"));
@ -674,9 +676,21 @@ public abstract class TransportCandidate {
packet.setAddress(packet.getAddress()); packet.setAddress(packet.getAddress());
packet.setPort(packet.getPort()); packet.setPort(packet.getPort());
long delay = 1000 / tries / 2;
if (delay < 0) delay = 10;
if (!Arrays.equals(packet.getData(), password)) if (!Arrays.equals(packet.getData(), password))
for (int i = 0; i < 3; i++) for (int i = 0; i < tries; i++) {
socket.send(packet); socket.send(packet);
if (!enabled) break;
try {
Thread.sleep(delay);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
} }
catch (UnknownHostException uhe) { catch (UnknownHostException uhe) {
@ -704,6 +718,8 @@ public abstract class TransportCandidate {
public boolean test(final InetAddress address, final int port, int timeout) { public boolean test(final InetAddress address, final int port, int timeout) {
ended=false;
final TestResults testResults = new TestResults(); final TestResults testResults = new TestResults();
DatagramListener listener = new DatagramListener() { DatagramListener listener = new DatagramListener() {
@ -711,10 +727,12 @@ public abstract class TransportCandidate {
if (datagramPacket.getAddress().equals(address) && datagramPacket.getPort() == port) { if (datagramPacket.getAddress().equals(address) && datagramPacket.getPort() == port) {
if (Arrays.equals(datagramPacket.getData(), password)) { if (Arrays.equals(datagramPacket.getData(), password)) {
testResults.setResult(true); testResults.setResult(true);
ended = true;
return true; return true;
} }
} }
testResults.setResult(false); testResults.setResult(false);
ended = true;
return false; return false;
} }
}; };
@ -726,20 +744,24 @@ public abstract class TransportCandidate {
packet.setAddress(address); packet.setAddress(address);
packet.setPort(port); packet.setPort(port);
try { long delay = timeout / tries;
for (int i = 0; i < 3; i++) if (delay < 0) delay = 10;
socket.send(packet);
}
catch (IOException e) {
e.printStackTrace();
}
try { try {
Thread.sleep(timeout); for (int i = 0; i < tries; i++) {
socket.send(packet);
if (ended) break;
try {
Thread.sleep(delay);
} }
catch (InterruptedException e) { catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
}
catch (IOException e) {
e.printStackTrace();
}
this.removeListener(listener); this.removeListener(listener);