programs quit when ports entered are wrong
All checks were successful
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/pr-master This commit looks good

This commit is contained in:
Flavien Haas 2020-03-30 15:34:50 +02:00
parent 2cc2868450
commit e4f637f317
3 changed files with 84 additions and 95 deletions

View File

@ -132,68 +132,63 @@ public class ClientP2P {
} }
} }
System.out.println("using hostname : " + hostnameServer); if (serverPortRange.isPortInRange(portServer)){
if(serverPortRange.isPortInRange(portServer)){ if(trackerPortRange.isPortInRange(portTracker)){
System.out.println("using port : " + portServer); System.out.println("using hostname : " + hostnameServer);
} System.out.println("using port : " + portServer);
else { System.out.println("tracker hostname : " + hostnameTracker);
System.out.println("Port not in range. " + serverPortRange); System.out.println("tracker port : " + portTracker);
portServer = serverPortRange.getDefaultPort(); ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker);
} ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer);
System.out.println("tracker hostname : " + hostnameTracker); 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();
if(trackerPortRange.isPortInRange(portTracker)){ // Wait a bit before printing client interface
System.out.println("using port : " + portTracker); // This is not required, but allow to have a cleaner interface
} try {
else { Thread.sleep(200);
System.out.println("Port not in range. " + trackerPortRange); } catch(InterruptedException e) {
portTracker = trackerPortRange.getDefaultPort(); Thread.currentThread().interrupt();
} }
ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); Thread tclient;
switch(protocolClient){
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); case "UDP":
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); case "udp":
Thread tudp = new Thread(smudp); case "upd": // to avoid users typos
tudp.setName("server UDP P2P-JAVA-PROJECT"); case "2" :
tudp.start(); System.out.println("Starting with UDP");
Thread ttcp = new Thread(smtcp); ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server);
ttcp.setName("server TCP P2P-JAVA-PROJECT"); tclient = new Thread(cmudp);
ttcp.start(); break;
case "TCP":
// Wait a bit before printing client interface case "tcp":
// This is not required, but allow to have a cleaner interface case "1":
try { default:
Thread.sleep(200); System.out.println("Starting with TCP");
} catch(InterruptedException e) { ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server);
Thread.currentThread().interrupt(); 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);
}
} }
else{
Thread tclient; System.out.println("\n SERVER: Port not in range. " + serverPortRange);
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();
} }
} }

View File

@ -90,39 +90,35 @@ public class ServerP2P {
System.out.println("enter tracker's port (default = " + trackerPortRange.getDefaultPort() + "): "); System.out.println("enter tracker's port (default = " + trackerPortRange.getDefaultPort() + "): ");
String portTrackerStr = scanner.nextLine(); String portTrackerStr = scanner.nextLine();
if(portTrackerStr.equals("")){ if(portTrackerStr.equals("")){
portTracker = serverPortRange.getDefaultPort(); portTracker = trackerPortRange.getDefaultPort();
System.out.println("using default port : " + portTracker); System.out.println("using default port : " + portTracker);
} else { } else {
portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr)); portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr));
} }
} }
System.out.println("using hostname : " + hostnameServer); if (serverPortRange.isPortInRange(portServer)){
if(serverPortRange.isPortInRange(portServer)) { if(trackerPortRange.isPortInRange(portTracker)){
System.out.println("using port : " + portServer); 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 { else{
System.out.println("Port not in range. " + serverPortRange); System.out.println("\n SERVER: 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();
}
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();
} }
} }

View File

@ -75,19 +75,17 @@ public class Tracker {
if(trackerPortRange.isPortInRange(port)) { if(trackerPortRange.isPortInRange(port)) {
System.out.println("using port : " + port); System.out.println("using port : " + port);
t = new Tracker(hostname, 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 { else {
System.out.println("Port not in range. " + trackerPortRange); 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();
} }
} }