From e4f637f31707de4fec08c23805b3d29fd8ad267b Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 30 Mar 2020 15:34:50 +0200 Subject: [PATCH] programs quit when ports entered are wrong --- src/clientP2P/ClientP2P.java | 113 +++++++++++++++++------------------ src/serverP2P/ServerP2P.java | 48 +++++++-------- src/tracker/Tracker.java | 18 +++--- 3 files changed, 84 insertions(+), 95 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index ec63ef3..f7a236b 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -132,68 +132,63 @@ public class ClientP2P { } } - System.out.println("using hostname : " + hostnameServer); - if(serverPortRange.isPortInRange(portServer)){ - System.out.println("using port : " + portServer); - } - else { - System.out.println("Port not in range. " + serverPortRange); - portServer = serverPortRange.getDefaultPort(); - } - System.out.println("tracker hostname : " + hostnameTracker); - - if(trackerPortRange.isPortInRange(portTracker)){ - System.out.println("using port : " + portTracker); - } - else { - System.out.println("Port not in range. " + trackerPortRange); - portTracker = trackerPortRange.getDefaultPort(); - } + if (serverPortRange.isPortInRange(portServer)){ + if(trackerPortRange.isPortInRange(portTracker)){ + System.out.println("using hostname : " + hostnameServer); + System.out.println("using port : " + portServer); + System.out.println("tracker hostname : " + hostnameTracker); + System.out.println("tracker port : " + portTracker); + ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + Thread tudp = new Thread(smudp); + tudp.setName("server UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(smtcp); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.start(); - ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); + // Wait a bit before printing client interface + // This is not required, but allow to have a cleaner interface + try { + Thread.sleep(200); + } catch(InterruptedException e) { + Thread.currentThread().interrupt(); + } - ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); - ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); - Thread tudp = new Thread(smudp); - tudp.setName("server UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT"); - ttcp.start(); - - // Wait a bit before printing client interface - // This is not required, but allow to have a cleaner interface - try { - Thread.sleep(200); - } catch(InterruptedException e) { - Thread.currentThread().interrupt(); + Thread tclient; + switch(protocolClient){ + case "UDP": + case "udp": + case "upd": // to avoid users typos + case "2" : + System.out.println("Starting with UDP"); + ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); + tclient = new Thread(cmudp); + break; + case "TCP": + case "tcp": + case "1": + default: + System.out.println("Starting with TCP"); + ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); + tclient = new Thread(cmtcp); + break; + } + tclient.setName("client P2P-JAVA-PROJECT"); + tclient.start(); + try { + tclient.join(); + } catch (InterruptedException e) {} + smudp.setStop(); + smtcp.setStop(); + } + else{ + System.out.println("\n TRACKER: Port not in range. " + trackerPortRange); + } } - - Thread tclient; - switch(protocolClient){ - case "UDP": - case "udp": - case "upd": // to avoid users typos - case "2" : - System.out.println("Starting with UDP"); - ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); - tclient = new Thread(cmudp); - break; - case "TCP": - case "tcp": - case "1": - default: - System.out.println("Starting with TCP"); - ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server); - tclient = new Thread(cmtcp); - break; + else{ + System.out.println("\n SERVER: Port not in range. " + serverPortRange); } - tclient.setName("client P2P-JAVA-PROJECT"); - tclient.start(); - try { - tclient.join(); - } catch (InterruptedException e) {} - smudp.setStop(); - smtcp.setStop(); } } diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 7c5a3d3..a1c02ee 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -90,39 +90,35 @@ public class ServerP2P { System.out.println("enter tracker's port (default = " + trackerPortRange.getDefaultPort() + "): "); String portTrackerStr = scanner.nextLine(); if(portTrackerStr.equals("")){ - portTracker = serverPortRange.getDefaultPort(); + portTracker = trackerPortRange.getDefaultPort(); System.out.println("using default port : " + portTracker); } else { portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr)); } } - System.out.println("using hostname : " + hostnameServer); - if(serverPortRange.isPortInRange(portServer)) { - System.out.println("using port : " + portServer); - } - else { - System.out.println("Port not in range. " + serverPortRange); - portServer = serverPortRange.getDefaultPort(); - } - System.out.println("tracker hostname : " + hostnameTracker); - - if(trackerPortRange.isPortInRange(portTracker)) { - System.out.println("using port : " + portTracker); + if (serverPortRange.isPortInRange(portServer)){ + if(trackerPortRange.isPortInRange(portTracker)){ + System.out.println("using hostname : " + hostnameServer); + System.out.println("using port : " + portServer); + System.out.println("tracker hostname : " + hostnameTracker); + System.out.println("tracker port : " + portTracker); + ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); + Thread tudp = new Thread(smudp); + tudp.setName("server UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(smtcp); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.start(); + } + else{ + System.out.println("\n TRACKER: Port not in range. " + trackerPortRange); + } } - else { - System.out.println("Port not in range. " + trackerPortRange); - portTracker = trackerPortRange.getDefaultPort(); + else{ + System.out.println("\n SERVER: Port not in range. " + serverPortRange); } - - ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); - Thread tudp = new Thread(smudp); - tudp.setName("server UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT"); - ttcp.start(); } } diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 92ff5f5..8b096a6 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -75,19 +75,17 @@ public class Tracker { if(trackerPortRange.isPortInRange(port)) { System.out.println("using port : " + port); t = new Tracker(hostname, port); + TrackerManagementUDP tmudp = new TrackerManagementUDP(t.tracker, t.logger); + TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.tracker, t.logger); + Thread tudp = new Thread(tmudp); + tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(tmtcp); + ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); + ttcp.start(); } else { System.out.println("Port not in range. " + trackerPortRange); - t = new Tracker(hostname, trackerPortRange.getDefaultPort()); } - - TrackerManagementUDP tmudp = new TrackerManagementUDP(t.tracker, t.logger); - TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.tracker, t.logger); - Thread tudp = new Thread(tmudp); - tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(tmtcp); - ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); - ttcp.start(); } }