From ad2b79607130e1cf2c1d2c069b08505ed77af194 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 20 Mar 2020 17:41:24 +0100 Subject: [PATCH] Fix tcp --- src/protocolP2P/ProtocolP2PPacketTCP.java | 20 ++++++++++++++++++-- src/serverP2P/ServerManagementTCP.java | 14 ++++++++++---- src/tracker/TrackerManagementTCP.java | 5 +++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/protocolP2P/ProtocolP2PPacketTCP.java b/src/protocolP2P/ProtocolP2PPacketTCP.java index 9272ea6..b99e83f 100644 --- a/src/protocolP2P/ProtocolP2PPacketTCP.java +++ b/src/protocolP2P/ProtocolP2PPacketTCP.java @@ -108,11 +108,14 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { Socket ss = (Socket)socket; byte[] packet = new byte[1024]; try { - System.err.println("Reading " + ss.getInputStream().read(packet) + " bytes"); + if (-1 == ss.getInputStream().read(packet)) { + throw new IOException(); + } } catch (IOException e) { // Error: cannot read request, closing socket try { ss.close(); + throw new SocketClosed(); } catch (IOException e2) { System.err.println("Cannot close socket"); throw new SocketClosed(); @@ -208,7 +211,20 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { } // reception byte[] packet = new byte[8192]; - requestSocket.getInputStream().read(packet); + try { + if (-1== requestSocket.getInputStream().read(packet)) { + throw new IOException(); + } + } catch (IOException e) { + // Error: cannot read request, closing socket + try { + requestSocket.close(); + throw new SocketClosed(); + } catch (IOException e2) { + System.err.println("Cannot close socket"); + throw new SocketClosed(); + } + } // contruction try { ProtocolP2PPacketTCP p = new ProtocolP2PPacketTCP(packet); diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index d04b544..359d616 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -109,11 +109,11 @@ public class ServerManagementTCP implements Runnable { public void run() { boolean end = false; - logger.writeTCP("[ " + addr + "] New connection", LogLevel.Action); + logger.writeTCP("[" + addr + "] New connection", LogLevel.Action); do { end = handleRequest(); } while(!end); - logger.writeTCP("[ " + addr + "] End of connection", LogLevel.Action); + logger.writeTCP("[" + addr + "] End of connection", LogLevel.Action); } /** Respond to next request incomming on socket s. @@ -354,12 +354,18 @@ public class ServerManagementTCP implements Runnable { logger.writeTCP("Unregistering from tracker", LogLevel.Info); ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Unregister(server)); p.sendRequest((Object)tracker.getTCPSocket()); - //tracker.closeTCPSocket(); + // FIXME: this is a hack + // ProtocolP2PPacketTCP reads 1024 bytes but if 2 request comes at the same time InputStream is cleared fully + // and we keep waiting forever on the other side + // a fix could be to read only the header first, and read the required size after + try { + Thread.sleep(100); + } catch (InterruptedException e) {} logger.writeTCP("Registering into tracker", LogLevel.Info); p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(server)); p.sendRequest((Object)tracker.getTCPSocket()); logger.writeTCP("Registering completed", LogLevel.Debug); - //tracker.closeTCPSocket(); + tracker.closeTCPSocket(); } } diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index 491b883..5f75aa2 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -24,6 +24,7 @@ import protocolP2P.HashRequest; import localException.InternalError; import java.net.UnknownHostException; import java.net.InetAddress; +import localException.SocketClosed; /** Tracker management implementation with tcp @@ -119,9 +120,11 @@ public class TrackerManagementTCP implements Runnable { sendNotFound(pd); break; case REGISTER: + logger.writeTCP("Received REGISTER from host " + pd.getHostItem(), LogLevel.Debug); handleRegister(pd); break; case UNREGISTER: + logger.writeTCP("Received UNREGISTER from host " + pd.getHostItem(), LogLevel.Debug); handleUnregister(pd); break; case DISCOVER_REQUEST: @@ -135,6 +138,8 @@ public class TrackerManagementTCP implements Runnable { } catch (IOException e) { logger.writeTCP(e, LogLevel.Warning); return true; + } catch (SocketClosed e) { + return true; } catch (LocalException e) { logger.writeTCP(e, LogLevel.Warning); return true;