From 2d8008abf68460cc89541fff87d4b4d68545c7a1 Mon Sep 17 00:00:00 2001 From: senft-desktop Date: Fri, 30 Nov 2012 15:12:05 +0100 Subject: [PATCH] Added max. retries for ack --- ws2012/P2P/uebungen/4/src/node/Ack.java | 46 +++++++++++-------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/ws2012/P2P/uebungen/4/src/node/Ack.java b/ws2012/P2P/uebungen/4/src/node/Ack.java index 4ffc8cf8..83afb02c 100644 --- a/ws2012/P2P/uebungen/4/src/node/Ack.java +++ b/ws2012/P2P/uebungen/4/src/node/Ack.java @@ -13,6 +13,12 @@ public class Ack { // timeout in seconds private final int TIMEOUT = 1000; + private final int MAX_RETRIES = 5; + + // the number of times we already sent this ack (IRGENDWANN IST AUCH MAL + // SCHLUSS!) + private int retries = 1; + private int id; private SocketAddress address; private ByteBuffer buf; @@ -73,6 +79,7 @@ public class Ack { @Override public void run() { + retries++; while (notReceived && System.currentTimeMillis() < timeToStop) { try { Thread.sleep(10); @@ -83,32 +90,19 @@ public class Ack { // 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(); + if (retries < MAX_RETRIES) { + try { + LOGGER.log(Level.INFO, + "Absent ack #{0}. Resending to {1}", + new Object[] { id, address.toString() }); + channel.send(buf, address); + startThread(); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + LOGGER.log(Level.SEVERE, "Dropping ack #{0}", + new Object[] { id }); } } }