From 3387b90b14434d78ba95993032cfe643e6735680 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 20 Mar 2020 16:30:42 +0100 Subject: [PATCH] HostItems dans le server --- src/clientP2P/ClientP2P.java | 5 ++--- src/serverP2P/ServerManagementTCP.java | 30 +++++++++++++------------- src/serverP2P/ServerManagementUDP.java | 23 ++++++++++---------- src/serverP2P/ServerP2P.java | 5 ++--- src/tracker/TrackerManagementTCP.java | 5 ++--- src/tracker/TrackerManagementUDP.java | 1 - 6 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 139701f..a1700b9 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -72,7 +72,6 @@ public class ClientP2P { * @param args server listenning port */ public static void main(String [] args) { - if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ System.out.println("usage : java -ea clientP2P.ClientP2P -- "); } @@ -85,8 +84,8 @@ public class ClientP2P { } // Server threads - ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + c.subdir, c.port, c.loggerServer, c.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + c.subdir, c.port, c.loggerServer, c.tracker); + ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + c.subdir, "localhost", c.port, c.loggerServer, c.tracker); + ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + c.subdir, "localhost", c.port, c.loggerServer, c.tracker); Thread tudp = new Thread(smudp); tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")"); tudp.start(); diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index 69ce5d6..d04b544 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -47,26 +47,29 @@ public class ServerManagementTCP implements Runnable { private String[] fileList; private Map sha512 = new HashMap<>(); private String baseDirectory; - private int TCPPort; private ServerSocket socket; private Logger logger; private HostItem tracker; + private HostItem server; /** Constructor for TCP implementation, with baseDirectory and TCPPort parameters. * @param baseDirectory the root directory where files are stored + * @param hostName the server will bind on this address * @param TCPPort the server will listen on this port + * @param logger Logger item + * @param tracker Tracker */ - public ServerManagementTCP(String baseDirectory, int TCPPort, Logger logger, HostItem tracker) { + public ServerManagementTCP(String baseDirectory, String hostName, int port, Logger logger, HostItem tracker) { + server = new HostItem(hostName, port); this.tracker = tracker; this.logger = logger; this.baseDirectory = baseDirectory; - this.TCPPort = TCPPort; initFileList(); initSha512(); try { - socket = new ServerSocket(TCPPort, 10, InetAddress.getLocalHost()); + socket = new ServerSocket(server.getPort(), 10, server.getInetAddress()); } catch (SocketException e) { - logger.writeTCP("Error: cannot listen on port " + TCPPort, LogLevel.Error); + logger.writeTCP("Error: cannot listen on " + server, LogLevel.Error); System.exit(-1); } catch (IOException e) { logger.writeTCP("Error: cannot openning socket", LogLevel.Error); @@ -93,14 +96,12 @@ public class ServerManagementTCP implements Runnable { /** Private runnable class allowing to serve one client. */ private class ClientHandler implements Runnable { - private Socket s; - private String addr; + private HostItem addr; /** Constructor with a socket. * @param s Socket of this client */ public ClientHandler(Socket s) { - this.s = s; - this.addr = "[" +s.getInetAddress().getHostAddress() + "]:" + s.getPort() + " "; + addr = new HostItem(s); } /** Implementation of runnable. This method allow to serve one client. @@ -108,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. @@ -121,7 +122,7 @@ public class ServerManagementTCP implements Runnable { */ private boolean handleRequest() { try { - ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s); + ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket()); Payload p = pd.getPayload(); switch (p.getRequestResponseCode()) { case LOAD_REQUEST: @@ -350,13 +351,12 @@ public class ServerManagementTCP implements Runnable { * @throws SocketClosed */ private void registerTracker() throws InternalError, IOException, SocketClosed { - HostItem host = new HostItem(InetAddress.getLocalHost().getCanonicalHostName(), TCPPort); logger.writeTCP("Unregistering from tracker", LogLevel.Info); - ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Unregister(host)); + ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Unregister(server)); p.sendRequest((Object)tracker.getTCPSocket()); //tracker.closeTCPSocket(); logger.writeTCP("Registering into tracker", LogLevel.Info); - p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(host)); + p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(server)); p.sendRequest((Object)tracker.getTCPSocket()); logger.writeTCP("Registering completed", LogLevel.Debug); //tracker.closeTCPSocket(); diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index 2050916..e5f81db 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -46,30 +46,30 @@ public class ServerManagementUDP implements Runnable { private String[] fileList; private Map sha512 = new HashMap<>(); private String baseDirectory; - private int UDPPort; private DatagramSocket socket; private Logger logger; private HostItem tracker; + private HostItem server; /** Constructor for UDP implementation, with baseDirectory and UDPPort parameters. * @param baseDirectory the root directory where files are stored - * @param UDPPort the server will listen on this port + * @param hostName the server will bind on this address + * @param port the server will listen on this port + * @param logger Logger item + * @param tracker Tracker */ - public ServerManagementUDP(String baseDirectory, int UDPPort, Logger logger, HostItem tracker) { + public ServerManagementUDP(String baseDirectory, String hostName, int port, Logger logger, HostItem tracker) { + server = new HostItem(hostName, port); this.logger = logger; this.baseDirectory = baseDirectory; - this.UDPPort = UDPPort; this.tracker = tracker; initFileList(); initSha512(); try { - socket = new DatagramSocket(UDPPort, InetAddress.getLocalHost()); + socket = new DatagramSocket(server.getPort(), server.getInetAddress()); } catch (SocketException e) { - logger.writeUDP("Error: cannot listen on port " + UDPPort, LogLevel.Error); + logger.writeUDP("Error: cannot listen on " + server, LogLevel.Error); System.exit(-1); - } catch (UnknownHostException e) { - logger.writeUDP(e, LogLevel.Error); - System.exit(-2); } } @@ -291,12 +291,11 @@ public class ServerManagementUDP implements Runnable { * @throws SocketClosed */ private void registerTracker() throws InternalError, IOException, SocketClosed { - HostItem host = new HostItem(InetAddress.getLocalHost().getCanonicalHostName(), UDPPort); logger.writeUDP("Unregistering from tracker", LogLevel.Info); - ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new Unregister(host)); + ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new Unregister(server)); p.sendRequest((Object)tracker.getUDPSocket()); logger.writeUDP("Registering into tracker", LogLevel.Info); - p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new Register(host)); + p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new Register(server)); p.sendRequest((Object)tracker.getUDPSocket()); logger.writeUDP("Registering completed", LogLevel.Debug); tracker.closeUDPSocket(); diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index abfdb66..1b57958 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -43,8 +43,8 @@ public class ServerP2P { } else{ ServerP2P s = new ServerP2P(args[1], args[2], args[3]); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger, s.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger, s.tracker); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, "localhost", s.port, s.logger, s.tracker); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, "localhost", s.port, s.logger, s.tracker); Thread tudp = new Thread(smudp); tudp.setName("server UDP P2P-JAVA-PROJECT"); tudp.start(); @@ -53,5 +53,4 @@ public class ServerP2P { ttcp.start(); } } - } diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index 8ab4724..491b883 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -77,7 +77,6 @@ public class TrackerManagementTCP implements Runnable { /** Private runnable class allowing to serve one client. */ private class ClientHandler implements Runnable { - private Socket s; private HostItem addr; /** Constructor with a socket. * @param s Socket of this client @@ -198,7 +197,8 @@ public class TrackerManagementTCP implements Runnable { pLReq.sendRequest((Object)host.getTCPSocket()); logger.writeTCP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action); handleListResponse((ProtocolP2PPacketTCP)pLReq.receiveResponse(), host); - //host.closeTCPSocket(); + logger.writeTCP("Received LIST RESPONSE from host " + pd.getHostItem(), LogLevel.Action); + host.closeTCPSocket(); } catch (Exception e) { // remove from list because list request could not be send hostList.remove(host); @@ -254,7 +254,6 @@ public class TrackerManagementTCP implements Runnable { * @throws InternalError */ private void handleListResponse(ProtocolP2PPacketTCP pd, HostItem host) throws InternalError { - logger.writeTCP("Received LIST RESPONSE from host " + host, LogLevel.Action); Payload p = pd.getPayload(); assert p instanceof FileList: "payload must be an instance of FileList"; if (!(p instanceof FileList)) { diff --git a/src/tracker/TrackerManagementUDP.java b/src/tracker/TrackerManagementUDP.java index 41ff8d8..3a3cbc2 100644 --- a/src/tracker/TrackerManagementUDP.java +++ b/src/tracker/TrackerManagementUDP.java @@ -215,7 +215,6 @@ public class TrackerManagementUDP implements Runnable { * @throws InternalError */ private void handleListResponse(ProtocolP2PPacketUDP pd, HostItem host) throws InternalError { - logger.writeUDP("Received LIST RESPONSE from host " + host, LogLevel.Action); Payload p = pd.getPayload(); assert p instanceof FileList: "payload must be an instance of FileList"; if (!(p instanceof FileList)) {