Merge pull request 'clitracker' (#79) from clitracker into etape4
Fix #80
This commit is contained in:
commit
e0bf3782c0
@ -6,6 +6,12 @@ Lien vers le [document original](https://stri-online.net/FTLV/mod/resource/view.
|
||||
**But** : le but de ce projet est de créer une application répartie en Java de téléchargement de fichier en mode P2P (peer to peer ou poste à poste).
|
||||
Les étapes suivantes sont conseillées.
|
||||
|
||||
# Usage
|
||||
|
||||
tracker : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- <hostname> <PORT> (default port 6969 (range 6000 -> 6999))
|
||||
server : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- <serveurHOSTNAME> <serveurPORT> <trackerHOSTNAME> <trackerPORT> (default server port: server 7070 (range 7000->7070) and tracker port 6969 (range 7000 -> 7999))
|
||||
client/serveur : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- <clientTransportProtocol> <integratedServerHOSTNAME> <integratedServerPORT> <trackerHOSTNAME> <trackerPORT> (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070))
|
||||
|
||||
## Étape 1 : Téléchargement à la FTP
|
||||
|
||||
La première étape doit permettre de télécharger un fichier en intégralité d'une machine vers une autre machine de façon similaire aux applications suivant le protocole FTP.
|
||||
@ -47,4 +53,4 @@ Options :
|
||||
- Permettre la recherche de fichiers à partir de leur nom ou de toute autre caractéristique. À l'issu de la recherche on devra pouvoir connaître un ensemble d'application possédant le fichier et commencer le téléchargement.
|
||||
- Gérer le protocole d'une application de téléchargement P2P existante (bittorrent, emule ou autre).
|
||||
|
||||
Note : toute fonctionnalité supplémentaire ne sera prise en compte dans la notation que si toutes les étapes ont été correctement traitées.
|
||||
Note : toute fonctionnalité supplémentaire ne sera prise en compte dans la notation que si toutes les étapes ont été correctement traitées.
|
||||
|
@ -4,12 +4,16 @@ import java.util.Scanner;
|
||||
import java.util.List;
|
||||
import clientP2P.ClientManagementUDP;
|
||||
import clientP2P.ClientManagementTCP;
|
||||
|
||||
import serverP2P.ServerManagementUDP;
|
||||
import serverP2P.ServerManagementTCP;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
import tools.Directories;
|
||||
import tools.HostItem;
|
||||
import tools.ServerPortRange;
|
||||
import tools.TrackerPortRange;
|
||||
|
||||
|
||||
/** Client + Server implementation.
|
||||
* @author Louis Royer
|
||||
@ -23,20 +27,17 @@ public class ClientP2P {
|
||||
private String partsDir = ".parts/";
|
||||
private Logger loggerServer;
|
||||
private Logger loggerClient;
|
||||
private String host;
|
||||
private int port;
|
||||
private Directories directories;
|
||||
private static final int defaultPort = 20000;
|
||||
private HostItem tracker;
|
||||
private HostItem server;
|
||||
private Scanner scanner;
|
||||
|
||||
|
||||
|
||||
/** Initialize loggers if directories and logger are null,
|
||||
* else fail silently.
|
||||
*/
|
||||
public void initDirectoriesAndLoggers() {
|
||||
if (directories == null && loggerServer == null && loggerClient == null) {
|
||||
directories = new Directories("P2P_JAVA_PROJECT_" + port);
|
||||
directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort());
|
||||
directories.createSubdir(logDir);
|
||||
loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log");
|
||||
loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log");
|
||||
@ -44,26 +45,34 @@ public class ClientP2P {
|
||||
}
|
||||
}
|
||||
|
||||
/** Constructor with portStr as parameter.
|
||||
* @param portStr String containing port for server listenning.
|
||||
/** Constructor.
|
||||
* @param hostnameServer hostname to bind
|
||||
* @param portServer port to bind
|
||||
* @param hostnameTracker hostname of tracker
|
||||
* @param portTracker port of tracker
|
||||
*/
|
||||
public ClientP2P(String portStr) {
|
||||
public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) {
|
||||
scanner = new Scanner(System.in);
|
||||
tracker = new HostItem("localhost", 30000); // TODO : make it configurable
|
||||
try{
|
||||
port = Integer.valueOf(Integer.parseInt(portStr));
|
||||
} catch (NumberFormatException e){
|
||||
int oldPort = port;
|
||||
port = defaultPort;
|
||||
initDirectoriesAndLoggers();
|
||||
System.err.println("Error incorrect port " + oldPort + " using default port " + defaultPort);
|
||||
loggerServer.write("incorrect port " + oldPort + " using default port " + defaultPort, LogLevel.Info);
|
||||
}
|
||||
server = new HostItem(hostnameServer, portServer);
|
||||
tracker = new HostItem(hostnameTracker, portTracker);
|
||||
initDirectoriesAndLoggers();
|
||||
host = "localhost";
|
||||
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory());
|
||||
System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory());
|
||||
directories.askOpenDataHomeDirectory(null, scanner);
|
||||
System.out.println("Please enter list of servers to use; first one will be used to ask list of files");
|
||||
}
|
||||
|
||||
/** Print cli usage
|
||||
* @param serverPortRange range of server ports
|
||||
* @param trackerPortRange range of tracker ports
|
||||
*/
|
||||
private static void printUsage(ServerPortRange serverPortRange, TrackerPortRange trackerPortRange) {
|
||||
System.out.println("usage :");
|
||||
System.out.println("\tjava clientP2P.ClientP2P");
|
||||
System.out.println("or");
|
||||
System.out.println("java clientP2P.ClientP2P -- " +
|
||||
"<clientTransportProtocol> " +
|
||||
"<integratedServerHOSTNAME> <integratedServerPORT> " +
|
||||
"<trackerHOSTNAME> <trackerPORT> ");
|
||||
System.out.println("(" + trackerPortRange + " and " + serverPortRange +")");
|
||||
}
|
||||
|
||||
/** Main program entry point.
|
||||
@ -72,67 +81,121 @@ 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 -- <PORT> ");
|
||||
final String defaultHostname = "localhost";
|
||||
String hostnameServer = "";
|
||||
int portServer = 0;
|
||||
String hostnameTracker = "";
|
||||
int portTracker = 0;
|
||||
String protocolClient = "";
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
final ServerPortRange serverPortRange = new ServerPortRange();
|
||||
final TrackerPortRange trackerPortRange = new TrackerPortRange();
|
||||
|
||||
if ((args.length != 6) && (args.length != 0)){
|
||||
ClientP2P.printUsage(serverPortRange, trackerPortRange);
|
||||
System.exit(1);
|
||||
}
|
||||
else{
|
||||
ClientP2P c;
|
||||
try {
|
||||
c = new ClientP2P(args[1]);
|
||||
} catch (IndexOutOfBoundsException e){
|
||||
c = new ClientP2P("" + defaultPort);
|
||||
else if(args.length == 6){
|
||||
protocolClient = args[1];
|
||||
hostnameServer = args[2];
|
||||
portServer = Integer.valueOf(Integer.parseInt(args[3]));
|
||||
hostnameTracker = args[4];
|
||||
portTracker = Integer.valueOf(Integer.parseInt(args[5]));
|
||||
} else{
|
||||
System.out.println("Client, wich transport protocol do you want to use (default = TCP): ");
|
||||
protocolClient = scanner.nextLine();
|
||||
System.out.println("server side, enter hostname to bind (default = localhost): ");
|
||||
hostnameServer = scanner.nextLine();
|
||||
if(hostnameServer.equals("")){
|
||||
hostnameServer = defaultHostname;
|
||||
System.out.println("using default hostname : " + hostnameServer);
|
||||
}
|
||||
|
||||
// Server threads
|
||||
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), "localhost", c.port, c.loggerServer, c.tracker);
|
||||
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), "localhost", c.port, c.loggerServer, c.tracker);
|
||||
Thread tudp = new Thread(smudp);
|
||||
tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")");
|
||||
tudp.start();
|
||||
Thread ttcp = new Thread(smtcp);
|
||||
ttcp.setName("server TCP P2P-JAVA-PROJECT (port: " + c.port + ")");
|
||||
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();
|
||||
System.out.println("enter port (default = " + serverPortRange.getDefaultPort() +"): ");
|
||||
String portServerStr = scanner.nextLine();
|
||||
if(portServerStr.equals("")){
|
||||
portServer = serverPortRange.getDefaultPort();
|
||||
System.out.println("using default port : " + portServer);
|
||||
} else {
|
||||
portServer = Integer.valueOf(Integer.parseInt(portServerStr));
|
||||
}
|
||||
System.out.println("enter hostname of tracker (default = localhost): ");
|
||||
hostnameTracker = scanner.nextLine();
|
||||
if(hostnameTracker.equals("")){
|
||||
hostnameTracker = defaultHostname;
|
||||
System.out.println("tracker default hostname : " + hostnameTracker);
|
||||
}
|
||||
System.out.println("enter tracker's port (default = "+trackerPortRange.getDefaultPort() + "): ");
|
||||
String portTrackerStr = scanner.nextLine();
|
||||
if(portTrackerStr.equals("")){
|
||||
portTracker = trackerPortRange.getDefaultPort();
|
||||
System.out.println("using default port : " + portTracker);
|
||||
} else {
|
||||
portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr));
|
||||
}
|
||||
|
||||
// initialize Host lists
|
||||
System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]");
|
||||
c.scanner.hasNextLine();
|
||||
String transportchoosen = c.scanner.nextLine();
|
||||
Thread t;
|
||||
switch(transportchoosen){
|
||||
case "UDP":
|
||||
case "udp":
|
||||
case "upd": // alias typo
|
||||
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);
|
||||
t = 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);
|
||||
t = new Thread(cmtcp);
|
||||
break;
|
||||
}
|
||||
t.setName("client P2P-JAVA-PROJECT");
|
||||
t.start();
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e) {}
|
||||
c.scanner.close();
|
||||
smudp.setStop();
|
||||
smtcp.setStop();
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
// 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);
|
||||
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);
|
||||
tclient = new Thread(cmtcp);
|
||||
break;
|
||||
}
|
||||
tclient.setName("client P2P-JAVA-PROJECT");
|
||||
tclient.start();
|
||||
try {
|
||||
tclient.join();
|
||||
} catch (InterruptedException e) {}
|
||||
smudp.setStop();
|
||||
smtcp.setStop();
|
||||
}
|
||||
}
|
||||
|
@ -56,8 +56,8 @@ public class ServerManagementTCP extends ServerManagement {
|
||||
* @param logger Logger item
|
||||
* @param tracker Tracker
|
||||
*/
|
||||
public ServerManagementTCP(String baseDirectory, String hostName, int port, Logger logger, HostItem tracker) {
|
||||
super(baseDirectory, new HostItem(hostName, port), tracker, logger);
|
||||
public ServerManagementTCP(String baseDirectory, HostItem server, HostItem tracker, Logger logger) {
|
||||
super(baseDirectory, server, tracker, logger);
|
||||
assert baseDirectory != null : "baseDirectory is null";
|
||||
assert server != null : "server is null";
|
||||
assert tracker != null : "tracker is null";
|
||||
|
@ -54,8 +54,8 @@ public class ServerManagementUDP extends ServerManagement {
|
||||
* @param logger Logger item
|
||||
* @param tracker Tracker
|
||||
*/
|
||||
public ServerManagementUDP(String baseDirectory, String hostName, int port, Logger logger, HostItem tracker) {
|
||||
super(baseDirectory, new HostItem(hostName, port), tracker, logger);
|
||||
public ServerManagementUDP(String baseDirectory, HostItem server, HostItem tracker, Logger logger) {
|
||||
super(baseDirectory, server, tracker, logger);
|
||||
assert baseDirectory != null : "baseDirectory is null";
|
||||
assert server != null : "server is null";
|
||||
assert tracker != null : "tracker is null";
|
||||
|
@ -1,10 +1,14 @@
|
||||
package serverP2P;
|
||||
|
||||
import java.util.Scanner;
|
||||
import serverP2P.ServerManagementUDP;
|
||||
import serverP2P.ServerManagementTCP;
|
||||
import tools.Directories;
|
||||
import tools.Logger;
|
||||
import tools.HostItem;
|
||||
import java.util.Scanner;
|
||||
import tools.ServerPortRange;
|
||||
import tools.TrackerPortRange;
|
||||
|
||||
|
||||
/** Server only implementation
|
||||
* First argument of main method is port listened by the server, and is mandatory.
|
||||
@ -14,46 +18,111 @@ import java.util.Scanner;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class ServerP2P {
|
||||
private int port;
|
||||
private Directories directories;
|
||||
static private final String subdir = "seeded/";
|
||||
private Logger logger;
|
||||
private HostItem tracker;
|
||||
private HostItem server;
|
||||
|
||||
/** Constructor with portStr containing a port number.
|
||||
* @param portStr String containing port number of listening.
|
||||
/** Constructor with portServerstr containing a port number.
|
||||
* @param hostnameServer binded hostname
|
||||
* @param portServer binded port
|
||||
* @param hostnameTracker tracker hostname
|
||||
* @param portTracker tracker port
|
||||
*/
|
||||
public ServerP2P(String portStr, String trackerHostname, String trackerPortStr) {
|
||||
port = Integer.valueOf(Integer.parseInt(portStr));
|
||||
tracker = new HostItem(trackerHostname, Integer.valueOf(Integer.parseInt(trackerPortStr)));
|
||||
directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + port);
|
||||
public ServerP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
server = new HostItem(hostnameServer, portServer);
|
||||
tracker = new HostItem(hostnameTracker, portTracker);
|
||||
directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + portServer);
|
||||
directories.createSubdir(subdir);
|
||||
logger = new Logger(directories.getDataHomeDirectory() + "server.log");
|
||||
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir);
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory() + subdir);
|
||||
directories.askOpenDataHomeDirectory(subdir, scanner);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
/** Main program entry point
|
||||
* first parameter is port number and is mandatory
|
||||
* to test, run with: java -ea serverP2P.ServerP2P -- <portNumber> <TrackerHostname> <TrackerPort>
|
||||
* to test, run with: java serverP2P.ServerP2P
|
||||
* @param args parameters
|
||||
*/
|
||||
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 serveurP2P.ServeurP2P -- <serveurPORT> <trackerHOSTNAME> <trackerPORT>");
|
||||
final String defaultHostname = "localhost";
|
||||
final ServerPortRange serverPortRange = new ServerPortRange();
|
||||
final TrackerPortRange trackerPortRange = new TrackerPortRange();
|
||||
String hostnameServer = "";
|
||||
int portServer = 0;
|
||||
String hostnameTracker = "";
|
||||
int portTracker = 0;
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
if ((args.length != 5) && (args.length != 0)){
|
||||
System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- <serveurHOSTNAME> <serveurPORT> <trackerHOSTNAME> <trackerPORT> (" + serverPortRange + " and " + trackerPortRange +")");
|
||||
System.exit(1);
|
||||
}
|
||||
else{
|
||||
ServerP2P s = new ServerP2P(args[1], args[2], args[3]);
|
||||
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();
|
||||
Thread ttcp = new Thread(smtcp);
|
||||
ttcp.setName("server TCP P2P-JAVA-PROJECT");
|
||||
ttcp.start();
|
||||
else if(args.length == 5){
|
||||
hostnameServer = args[1];
|
||||
portServer = Integer.valueOf(Integer.parseInt(args[2]));
|
||||
hostnameTracker = args[3];
|
||||
portTracker = Integer.valueOf(Integer.parseInt(args[4]));
|
||||
} else {
|
||||
System.out.println("Server, enter hostname to bind (default = localhost): ");
|
||||
hostnameServer = scanner.nextLine();
|
||||
if(hostnameServer.equals("")){
|
||||
hostnameServer = defaultHostname;
|
||||
System.out.println("using default hostname : " + hostnameServer);
|
||||
}
|
||||
System.out.println("enter port (default = " + serverPortRange.getDefaultPort() + "): ");
|
||||
String portServerStr = scanner.nextLine();
|
||||
if(portServerStr.equals("")){
|
||||
portServer = serverPortRange.getDefaultPort();
|
||||
System.out.println("using default port : " + portServer);
|
||||
} else {
|
||||
portServer = Integer.valueOf(Integer.parseInt(portServerStr));
|
||||
}
|
||||
System.out.println("enter hostname of tracker (default = localhost): ");
|
||||
hostnameTracker = scanner.nextLine();
|
||||
if(hostnameTracker.equals("")){
|
||||
hostnameTracker = defaultHostname;
|
||||
System.out.println("tracker default hostname : " + hostnameTracker);
|
||||
}
|
||||
System.out.println("enter tracker's port (default = " + trackerPortRange.getDefaultPort() + "): ");
|
||||
String portTrackerStr = scanner.nextLine();
|
||||
if(portTrackerStr.equals("")){
|
||||
portTracker = serverPortRange.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);
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
49
src/tools/PortRange.java
Normal file
49
src/tools/PortRange.java
Normal file
@ -0,0 +1,49 @@
|
||||
package tools;
|
||||
|
||||
/** Test ports.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class PortRange {
|
||||
|
||||
protected int portMax;
|
||||
protected int portMin;
|
||||
protected int defaultPort;
|
||||
protected String type;
|
||||
|
||||
/** Port range constructor
|
||||
* @param portMin minimum port
|
||||
* @param portMax maximum port
|
||||
* @param defaultPort default port
|
||||
* @param type type of range
|
||||
*/
|
||||
public PortRange(int portMin, int portMax, int defaultPort, String type) {
|
||||
this.portMax = portMax;
|
||||
this.portMin = portMin;
|
||||
this.defaultPort = defaultPort;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/** test if port given correspond a range : registered ports, can be used without superuser privileges
|
||||
* @return true if port was valid
|
||||
*/
|
||||
public boolean isPortInRange(int port) {
|
||||
return ((port >= portMin) && (port <= portMax));
|
||||
}
|
||||
|
||||
/** To String
|
||||
* @return String representation
|
||||
*/
|
||||
public String toString() {
|
||||
return "default " + type + "port: " + defaultPort + "(range: " + portMin + " -> " + portMax + ")";
|
||||
}
|
||||
|
||||
/** Default port getter
|
||||
* @return default port
|
||||
*/
|
||||
public int getDefaultPort() {
|
||||
return defaultPort;
|
||||
}
|
||||
}
|
10
src/tools/ServerPortRange.java
Normal file
10
src/tools/ServerPortRange.java
Normal file
@ -0,0 +1,10 @@
|
||||
package tools;
|
||||
import tools.PortRange;
|
||||
|
||||
public class ServerPortRange extends PortRange {
|
||||
|
||||
/** Constructor */
|
||||
public ServerPortRange() {
|
||||
super(7000, 7999, 7070, "server");
|
||||
}
|
||||
}
|
10
src/tools/TrackerPortRange.java
Normal file
10
src/tools/TrackerPortRange.java
Normal file
@ -0,0 +1,10 @@
|
||||
package tools;
|
||||
import tools.PortRange;
|
||||
|
||||
public class TrackerPortRange extends PortRange {
|
||||
|
||||
/** Constructor */
|
||||
public TrackerPortRange() {
|
||||
super(6000, 6999, 6969, "tracker");
|
||||
}
|
||||
}
|
@ -1,9 +1,13 @@
|
||||
package tracker;
|
||||
|
||||
import java.util.Scanner;
|
||||
import tracker.TrackerManagementTCP;
|
||||
import tracker.TrackerManagementUDP;
|
||||
import tools.Directories;
|
||||
import tools.Logger;
|
||||
import java.util.Scanner;
|
||||
import tools.LogLevel;
|
||||
import tools.TrackerPortRange;
|
||||
import tools.HostItem;
|
||||
|
||||
/** Tracker implementation
|
||||
* First argument of main method is port listened by the tracker, and is mandatory.
|
||||
@ -13,15 +17,16 @@ import java.util.Scanner;
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Tracker {
|
||||
private int port;
|
||||
private HostItem tracker;
|
||||
private Directories directories;
|
||||
private Logger logger;
|
||||
|
||||
/** Constructor with portStr containing a port number.
|
||||
* @param portStr String containing port number of listening.
|
||||
* @param hostname hostname to bind
|
||||
* @param port port to bind
|
||||
*/
|
||||
public Tracker(String portStr) {
|
||||
port = Integer.valueOf(Integer.parseInt(portStr));
|
||||
public Tracker(String hostname, int port) {
|
||||
tracker = new HostItem(hostname, port);
|
||||
directories = new Directories("P2P_JAVA_PROJECT_TRACKER_" + port);
|
||||
logger = new Logger(directories.getDataHomeDirectory() + "tracker.log");
|
||||
System.out.println("Tracker will listen on port " + port + " and write logs into " + directories.getDataHomeDirectory());
|
||||
@ -32,13 +37,52 @@ public class Tracker {
|
||||
|
||||
/** Main program entry point
|
||||
* first parameter is port number and is mandatory
|
||||
* to test, run with: java -ea serverP2P.ServerP2P -- <portNumber>
|
||||
* to test, run with: java serverP2P.ServerP2P
|
||||
* @param args parameters
|
||||
*/
|
||||
public static void main(String [] args) {
|
||||
Tracker t = new Tracker(args[1]);
|
||||
TrackerManagementUDP tmudp = new TrackerManagementUDP(t.port, t.logger);
|
||||
TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.port, t.logger);
|
||||
public static void main(String [] args) {
|
||||
final TrackerPortRange trackerPortRange = new TrackerPortRange();
|
||||
final String defaultHostname = "localhost";
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String hostname = "";
|
||||
int port = 0;
|
||||
Tracker t;
|
||||
|
||||
if ((args.length != 3) && (args.length != 0)){
|
||||
System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- <hostname> <PORT> (" + trackerPortRange +")");
|
||||
System.exit(1);
|
||||
} else if (args.length == 3){
|
||||
hostname = args[1];
|
||||
port = Integer.valueOf(Integer.parseInt(args[2]));
|
||||
} else {
|
||||
System.out.println("Tracker Server, enter hostname to bind (default = localhost): ");
|
||||
hostname = scanner.nextLine();
|
||||
if(hostname.equals("")){
|
||||
hostname = defaultHostname;
|
||||
System.out.println("using default hostname : " + hostname);
|
||||
}
|
||||
System.out.println("enter port (default = " + trackerPortRange.getDefaultPort() +"): ");
|
||||
String portStr = scanner.nextLine();
|
||||
if(portStr.equals("")){
|
||||
port = trackerPortRange.getDefaultPort();
|
||||
System.out.println("using default port : " + port);
|
||||
} else {
|
||||
port = Integer.valueOf(Integer.parseInt(portStr));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("using hostname : " + hostname);
|
||||
if(trackerPortRange.isPortInRange(port)) {
|
||||
System.out.println("using port : " + port);
|
||||
t = new Tracker(hostname, port);
|
||||
}
|
||||
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();
|
||||
@ -46,5 +90,4 @@ public class Tracker {
|
||||
ttcp.setName("Tracker TCP P2P-JAVA-PROJECT");
|
||||
ttcp.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,32 +1,32 @@
|
||||
package tracker;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.InetAddress;
|
||||
import protocolP2P.ProtocolP2PPacketTCP;
|
||||
import protocolP2P.ProtocolP2PPacket;
|
||||
import protocolP2P.RequestResponseCode;
|
||||
import protocolP2P.Payload;
|
||||
import protocolP2P.Register;
|
||||
import protocolP2P.Unregister;
|
||||
import tools.HostItem;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import exception.LocalException;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import protocolP2P.DiscoverRequest;
|
||||
import protocolP2P.DiscoverResponse;
|
||||
import protocolP2P.FileList;
|
||||
import localException.InternalError;
|
||||
import exception.LocalException;
|
||||
import remoteException.EmptyDirectory;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.InetAddress;
|
||||
import localException.InternalError;
|
||||
import localException.SocketClosed;
|
||||
import tracker.TrackerManagement;
|
||||
|
||||
import tools.HostItem;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
|
||||
/** Tracker management implementation with tcp
|
||||
* @author Louis Royer
|
||||
@ -38,11 +38,11 @@ public class TrackerManagementTCP extends TrackerManagement {
|
||||
private ServerSocket socket;
|
||||
|
||||
/** Constructor with port and logger.
|
||||
* @param port Port used to listen.
|
||||
* @param tracker hostitem of the tracker.
|
||||
* @param logger Logger object
|
||||
*/
|
||||
public TrackerManagementTCP(int port, Logger logger) {
|
||||
super(new HostItem("localhost", port), logger);
|
||||
public TrackerManagementTCP(HostItem tracker, Logger logger) {
|
||||
super(tracker, logger);
|
||||
try {
|
||||
socket = new ServerSocket(tracker.getPort(), 10, tracker.getInetAddress());
|
||||
} catch (SocketException e) {
|
||||
@ -114,6 +114,7 @@ public class TrackerManagementTCP extends TrackerManagement {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Implementation of writeLog
|
||||
* @param text Text to log
|
||||
* @param logLevel level of logging
|
||||
@ -144,7 +145,7 @@ public class TrackerManagementTCP extends TrackerManagement {
|
||||
protected Object getHostItemSocket(HostItem hostItem) {
|
||||
return (Object)hostItem.getTCPSocket();
|
||||
}
|
||||
|
||||
|
||||
/** Close HostItem socket
|
||||
* @param hostItem HostItem
|
||||
*/
|
||||
|
@ -34,15 +34,15 @@ import tracker.TrackerManagement;
|
||||
public class TrackerManagementUDP extends TrackerManagement {
|
||||
private DatagramSocket socket;
|
||||
/** Constructor with port and logger.
|
||||
* @param port Port used to listen.
|
||||
* @param tracker hostitem of the tracker.
|
||||
* @param logger Logger object
|
||||
*/
|
||||
public TrackerManagementUDP(int port, Logger logger) {
|
||||
super(new HostItem("localhost", port), logger);
|
||||
public TrackerManagementUDP(HostItem tracker, Logger logger) {
|
||||
super(tracker, logger);
|
||||
try {
|
||||
socket = new DatagramSocket(tracker.getPort(), tracker.getInetAddress());
|
||||
} catch (SocketException e) {
|
||||
logger.writeUDP("Error: cannot listen on port " + port, LogLevel.Error);
|
||||
logger.writeUDP("Error: cannot listen on " + tracker, LogLevel.Error);
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user