corrected logs
This commit is contained in:
parent
b994f691a8
commit
7fa7bbb612
@ -8,113 +8,113 @@ import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Ack {
|
||||
private final static Logger LOGGER = Logger.getLogger(Ack.class.getName());
|
||||
private final static Logger LOGGER = Logger.getLogger(Ack.class.getName());
|
||||
|
||||
// timeout in seconds
|
||||
// timeout in seconds
|
||||
private final int TIMEOUT = 1000;
|
||||
|
||||
private int id;
|
||||
private SocketAddress address;
|
||||
private ByteBuffer buf;
|
||||
private int id;
|
||||
private SocketAddress address;
|
||||
private ByteBuffer buf;
|
||||
|
||||
private TimeoutThread timeout;
|
||||
private volatile Thread thread;
|
||||
private TimeoutThread timeout;
|
||||
private volatile Thread thread;
|
||||
|
||||
// The channel to re-send the message on
|
||||
private DatagramChannel channel;
|
||||
// The channel to re-send the message on
|
||||
private DatagramChannel channel;
|
||||
|
||||
public Ack(int id, SocketAddress address, DatagramChannel channel) {
|
||||
this.id = id;
|
||||
this.address = address;
|
||||
this.channel = channel;
|
||||
startThread();
|
||||
}
|
||||
public Ack(int id, SocketAddress address, DatagramChannel channel) {
|
||||
this.id = id;
|
||||
this.address = address;
|
||||
this.channel = channel;
|
||||
startThread();
|
||||
}
|
||||
|
||||
private void startThread() {
|
||||
LOGGER.log(Level.INFO, "Starting timeout thread for ack #" + id);
|
||||
timeout = new TimeoutThread();
|
||||
thread = new Thread(timeout);
|
||||
thread.start();
|
||||
}
|
||||
private void startThread() {
|
||||
LOGGER.log(Level.FINE, "Starting timeout thread for ack #" + id);
|
||||
timeout = new TimeoutThread();
|
||||
thread = new Thread(timeout);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean check(SocketAddress address) {
|
||||
return this.address.toString().equals(address.toString());
|
||||
}
|
||||
public boolean check(SocketAddress address) {
|
||||
return this.address.toString().equals(address.toString());
|
||||
}
|
||||
|
||||
public ByteBuffer getBuf() {
|
||||
return buf;
|
||||
}
|
||||
public ByteBuffer getBuf() {
|
||||
return buf;
|
||||
}
|
||||
|
||||
public void setBuf(ByteBuffer buf) {
|
||||
this.buf = buf;
|
||||
}
|
||||
public void setBuf(ByteBuffer buf) {
|
||||
this.buf = buf;
|
||||
}
|
||||
|
||||
public void setReceived() {
|
||||
// Stop thread
|
||||
try {
|
||||
if (thread != null) {
|
||||
timeout.terminate();
|
||||
thread.join();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void setReceived() {
|
||||
// Stop thread
|
||||
try {
|
||||
if (thread != null) {
|
||||
timeout.terminate();
|
||||
thread.join();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private class TimeoutThread implements Runnable {
|
||||
private volatile boolean notReceived = true;
|
||||
private class TimeoutThread implements Runnable {
|
||||
private volatile boolean notReceived = true;
|
||||
|
||||
// When do we stop expecting an ack
|
||||
private long timeToStop = System.currentTimeMillis() + TIMEOUT;
|
||||
// When do we stop expecting an ack
|
||||
private long timeToStop = System.currentTimeMillis() + TIMEOUT;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (notReceived && System.currentTimeMillis() < timeToStop) {
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
while (notReceived && System.currentTimeMillis() < timeToStop) {
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Timeout hit -> re-send
|
||||
if (notReceived) {
|
||||
try {
|
||||
LOGGER.log(Level.INFO, "Absent ack #" + id
|
||||
+ ". Resending to " + address.toString());
|
||||
// Timeout hit -> re-send
|
||||
if (notReceived) {
|
||||
try {
|
||||
LOGGER.log(Level.INFO, "Absent ack #{0}. Resending to {1}",
|
||||
new Object[] { id, address.toString() });
|
||||
|
||||
/**
|
||||
* TODO: This would be the intuitive order (first re-send,
|
||||
* then start the new TimeoutThread), right? Unfortunately
|
||||
* this gives ugly log outputs, because the re-sent message
|
||||
* arrives before the new thread is constructed, so we get:
|
||||
*
|
||||
* <pre>
|
||||
* [2012-11-28 07:53:05 PM] node.Node INFO: Initialized node /127.0.0.1:37179
|
||||
* a spawn b
|
||||
* [2012-11-28 07:53:15 PM] node.Node INFO: Initialized node /127.0.0.1:35358
|
||||
* [2012-11-28 07:53:15 PM] node.Ack INFO: Starting timeout thread for ack #-1276001492
|
||||
* [2012-11-28 07:53:15 PM] node.Node INFO: /127.0.0.1:35358 received invite from /127.0.0.1:37179
|
||||
* [2012-11-28 07:53:20 PM] node.Ack INFO: Absent ack #-1276001492). Resending to /127.0.0.1:35358
|
||||
* </pre>
|
||||
*
|
||||
* No big deal, and we could just swap the statements, but
|
||||
* meh...
|
||||
*/
|
||||
channel.send(buf, address);
|
||||
startThread();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* TODO: This would be the intuitive order (first re-send,
|
||||
* then start the new TimeoutThread), right? Unfortunately
|
||||
* this gives ugly log outputs, because the re-sent message
|
||||
* arrives before the new thread is constructed, so we get:
|
||||
*
|
||||
* <pre>
|
||||
* [2012-11-28 07:53:05 PM] node.Node INFO: Initialized node /127.0.0.1:37179
|
||||
* a spawn b
|
||||
* [2012-11-28 07:53:15 PM] node.Node INFO: Initialized node /127.0.0.1:35358
|
||||
* [2012-11-28 07:53:15 PM] node.Ack INFO: Starting timeout thread for ack #-1276001492
|
||||
* [2012-11-28 07:53:15 PM] node.Node INFO: /127.0.0.1:35358 received invite from /127.0.0.1:37179
|
||||
* [2012-11-28 07:53:20 PM] node.Ack INFO: Absent ack #-1276001492). Resending to /127.0.0.1:35358
|
||||
* </pre>
|
||||
*
|
||||
* No big deal, and we could just swap the statements, but
|
||||
* meh...
|
||||
*/
|
||||
channel.send(buf, address);
|
||||
startThread();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void terminate() {
|
||||
notReceived = false;
|
||||
}
|
||||
}
|
||||
public void terminate() {
|
||||
notReceived = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -33,9 +33,12 @@ public class UDPHandler implements Runnable {
|
||||
}
|
||||
|
||||
private void receiveInvite(SocketAddress from) {
|
||||
LOGGER.log(Level.INFO, "{0}: received invite from {1}", new Object[] {
|
||||
node.getName(), from.toString() });
|
||||
int ack_id = buf.getInt();
|
||||
|
||||
LOGGER.log(Level.INFO,
|
||||
"{0}: received invite from {1}. Sending ack #{2}",
|
||||
new Object[] { node.getName(), from.toString(), ack_id });
|
||||
|
||||
node.sendAck(from, ack_id);
|
||||
node.addNeighbor(from);
|
||||
}
|
||||
@ -58,11 +61,12 @@ public class UDPHandler implements Runnable {
|
||||
}
|
||||
|
||||
private void receiveLeave(SocketAddress from) {
|
||||
LOGGER.log(Level.INFO, "{0}: {1} is leaving. Deleting...",
|
||||
new Object[] { node.getName(), from.toString() });
|
||||
|
||||
if (node.removeNeighbor(from)) {
|
||||
int ack_id = buf.getInt();
|
||||
|
||||
LOGGER.log(Level.INFO, "{0}: {1} is leaving. Sending ack #{2}",
|
||||
new Object[] { node.getName(), from.toString(), ack_id });
|
||||
|
||||
node.sendAck(from, ack_id);
|
||||
}
|
||||
// If we don't know that neighbor, we don't have to
|
||||
@ -147,9 +151,9 @@ public class UDPHandler implements Runnable {
|
||||
|
||||
LOGGER.log(
|
||||
Level.INFO,
|
||||
"{0}: from {1} received new neighbor:{2}",
|
||||
"{0}: from {1} received new neighbor:{2}. Sending ack #{3}",
|
||||
new Object[] { node.getName(), from.toString(),
|
||||
newNeighbor.toString() });
|
||||
newNeighbor.toString(), ack_id });
|
||||
|
||||
node.sendAck(from, ack_id);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user