From 7fa7bbb61238dabb3c2c689cf13ddd69c4598b07 Mon Sep 17 00:00:00 2001 From: senft-desktop Date: Fri, 30 Nov 2012 13:36:14 +0100 Subject: [PATCH] corrected logs --- ws2012/P2P/uebungen/4/src/node/Ack.java | 182 +++++++++--------- .../P2P/uebungen/4/src/node/UDPHandler.java | 18 +- 2 files changed, 102 insertions(+), 98 deletions(-) diff --git a/ws2012/P2P/uebungen/4/src/node/Ack.java b/ws2012/P2P/uebungen/4/src/node/Ack.java index b985eb21..4ffc8cf8 100644 --- a/ws2012/P2P/uebungen/4/src/node/Ack.java +++ b/ws2012/P2P/uebungen/4/src/node/Ack.java @@ -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: - * - *
-					 * [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
-					 * 
- * - * 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: + * + *
+                     * [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
+                     * 
+ * + * 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; + } + } } \ No newline at end of file diff --git a/ws2012/P2P/uebungen/4/src/node/UDPHandler.java b/ws2012/P2P/uebungen/4/src/node/UDPHandler.java index be06d4e7..ef48ba13 100644 --- a/ws2012/P2P/uebungen/4/src/node/UDPHandler.java +++ b/ws2012/P2P/uebungen/4/src/node/UDPHandler.java @@ -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); }