@ -4,12 +4,16 @@ import java.util.Scanner;
import java.util.List ;
import java.util.List ;
import clientP2P.ClientManagementUDP ;
import clientP2P.ClientManagementUDP ;
import clientP2P.ClientManagementTCP ;
import clientP2P.ClientManagementTCP ;
import serverP2P.ServerManagementUDP ;
import serverP2P.ServerManagementUDP ;
import serverP2P.ServerManagementTCP ;
import serverP2P.ServerManagementTCP ;
import tools.Logger ;
import tools.Logger ;
import tools.LogLevel ;
import tools.LogLevel ;
import tools.Directories ;
import tools.Directories ;
import tools.HostItem ;
import tools.HostItem ;
import tools.ServerPortRange ;
import tools.TrackerPortRange ;
/ * * Client + Server implementation .
/ * * Client + Server implementation .
* @author Louis Royer
* @author Louis Royer
@ -23,20 +27,17 @@ public class ClientP2P {
private String partsDir = ".parts/" ;
private String partsDir = ".parts/" ;
private Logger loggerServer ;
private Logger loggerServer ;
private Logger loggerClient ;
private Logger loggerClient ;
private String host ;
private int port ;
private Directories directories ;
private Directories directories ;
private static final int defaultPort = 20000 ;
private HostItem tracker ;
private HostItem tracker ;
private HostItem server ;
private Scanner scanner ;
private Scanner scanner ;
/ * * Initialize loggers if directories and logger are null ,
/ * * Initialize loggers if directories and logger are null ,
* else fail silently .
* else fail silently .
* /
* /
public void initDirectoriesAndLoggers ( ) {
public void initDirectoriesAndLoggers ( ) {
if ( directories = = null & & loggerServer = = null & & loggerClient = = null ) {
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 ) ;
directories . createSubdir ( logDir ) ;
loggerServer = new Logger ( directories . getDataHomeDirectory ( ) + logDir + "server.log" ) ;
loggerServer = new Logger ( directories . getDataHomeDirectory ( ) + logDir + "server.log" ) ;
loggerClient = new Logger ( directories . getDataHomeDirectory ( ) + logDir + "client.log" ) ;
loggerClient = new Logger ( directories . getDataHomeDirectory ( ) + logDir + "client.log" ) ;
@ -44,26 +45,34 @@ public class ClientP2P {
}
}
}
}
/ * * Constructor with portStr as parameter .
/ * * Constructor .
* @param portStr String containing port for server listenning .
* @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, S tring hostnameTracker , int portTracke r) {
scanner = new Scanner ( System . in ) ;
scanner = new Scanner ( System . in ) ;
tracker = new HostItem ( "localhost" , 30000 ) ; // TODO : make it configurable
server = new HostItem ( hostnameServer , portServer ) ;
try {
tracker = new HostItem ( hostnameTracker , portTracker ) ;
port = Integer . valueOf ( Integer . parseInt ( portStr ) ) ;
} catch ( NumberFormatException e ) {
int oldPort = port ;
port = defaultPort ;
initDirectoriesAndLoggers ( ) ;
initDirectoriesAndLoggers ( ) ;
System . err . println ( "Error incorrect port " + oldPort + " using default port " + defaultPort ) ;
System . out . println ( "Server will listen on port " + portServer + " and serve files from " + directories . getDataHomeDirectory ( ) ) ;
loggerServer . write ( "incorrect port " + oldPort + " using default port " + defaultPort , LogLevel . Info ) ;
}
initDirectoriesAndLoggers ( ) ;
host = "localhost" ;
System . out . println ( "Server will listen on port " + port + " and serve files from " + directories . getDataHomeDirectory ( ) ) ;
directories . askOpenDataHomeDirectory ( null , scanner ) ;
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 .
/ * * Main program entry point .
@ -72,25 +81,86 @@ public class ClientP2P {
* @param args server listenning port
* @param args server listenning port
* /
* /
public static void main ( String [ ] args ) {
public static void main ( String [ ] args ) {
if ( args [ 1 ] . equals ( "help" ) | | args [ 1 ] . equals ( "-h" ) | | args [ 1 ] . equals ( "h" ) ) {
final String defaultHostname = "localhost" ;
System . out . println ( "usage : java -ea clientP2P.ClientP2P -- <PORT> " ) ;
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 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 ) ;
}
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 ) ) ;
}
}
System . out . println ( "using hostname : " + hostnameServer ) ;
if ( serverPortRange . isPortInRange ( portServer ) ) {
System . out . println ( "using port : " + portServer ) ;
}
}
else {
else {
ClientP2P c ;
System . out . println ( "Port not in range. " + serverPortRange ) ;
try {
portServer = serverPortRange . getDefaultPort ( ) ;
c = new ClientP2P ( args [ 1 ] ) ;
}
} catch ( IndexOutOfBoundsException e ) {
System . out . println ( "tracker hostname : " + hostnameTracker ) ;
c = new ClientP2P ( "" + defaultPort ) ;
if ( trackerPortRange . isPortInRange ( portTracker ) ) {
System . out . println ( "using port : " + portTracker ) ;
}
else {
System . out . println ( "Port not in range. " + trackerPortRange ) ;
portTracker = trackerPortRange . getDefaultPort ( ) ;
}
}
// Server threads
ClientP2P c = new ClientP2P ( hostnameServer , portServer , hostnameTracker , portTracker ) ;
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 ) ;
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 ) ;
Thread tudp = new Thread ( smudp ) ;
tudp . setName ( "server UDP P2P-JAVA-PROJECT (port: " + c . port + ")" ) ;
tudp . setName ( "server UDP P2P-JAVA-PROJECT ") ;
tudp . start ( ) ;
tudp . start ( ) ;
Thread ttcp = new Thread ( smtcp ) ;
Thread ttcp = new Thread ( smtcp ) ;
ttcp . setName ( "server TCP P2P-JAVA-PROJECT (port: " + c . port + ")" ) ;
ttcp . setName ( "server TCP P2P-JAVA-PROJECT ") ;
ttcp . start ( ) ;
ttcp . start ( ) ;
// Wait a bit before printing client interface
// Wait a bit before printing client interface
@ -101,19 +171,15 @@ public class ClientP2P {
Thread . currentThread ( ) . interrupt ( ) ;
Thread . currentThread ( ) . interrupt ( ) ;
}
}
// initialize Host lists
Thread tclient ;
System . out . println ( "Client : Which transport protocol do you want to use? [TCP/udp]" ) ;
switch ( protocolClient ) {
c . scanner . hasNextLine ( ) ;
String transportchoosen = c . scanner . nextLine ( ) ;
Thread t ;
switch ( transportchoosen ) {
case "UDP" :
case "UDP" :
case "udp" :
case "udp" :
case "upd" : // alias typo
case "upd" : // to avoid users typos
case "2" :
case "2" :
System . out . println ( "Starting with UDP" ) ;
System . out . println ( "Starting with UDP" ) ;
ClientManagementUDP cmudp = new ClientManagementUDP ( c . directories . getDataHomeDirectory ( ) , c . tracker , c . directories . getDataHomeDirectory ( ) + c . partsDir , c . loggerClient , c . scanner ) ;
ClientManagementUDP cmudp = new ClientManagementUDP ( c . directories . getDataHomeDirectory ( ) , c . tracker , c . directories . getDataHomeDirectory ( ) + c . partsDir , c . loggerClient , c . scanner ) ;
t = new Thread ( cmudp ) ;
tclien t = new Thread ( cmudp ) ;
break ;
break ;
case "TCP" :
case "TCP" :
case "tcp" :
case "tcp" :
@ -121,18 +187,15 @@ public class ClientP2P {
default :
default :
System . out . println ( "Starting with TCP" ) ;
System . out . println ( "Starting with TCP" ) ;
ClientManagementTCP cmtcp = new ClientManagementTCP ( c . directories . getDataHomeDirectory ( ) , c . tracker , c . directories . getDataHomeDirectory ( ) + c . partsDir , c . loggerClient , c . scanner ) ;
ClientManagementTCP cmtcp = new ClientManagementTCP ( c . directories . getDataHomeDirectory ( ) , c . tracker , c . directories . getDataHomeDirectory ( ) + c . partsDir , c . loggerClient , c . scanner ) ;
t = new Thread ( cmtcp ) ;
tclien t = new Thread ( cmtcp ) ;
break ;
break ;
}
}
t. setName ( "client P2P-JAVA-PROJECT" ) ;
tclien t. setName ( "client P2P-JAVA-PROJECT" ) ;
t. start ( ) ;
tclien t. start ( ) ;
try {
try {
t. join ( ) ;
tclien t. join ( ) ;
} catch ( InterruptedException e ) { }
} catch ( InterruptedException e ) { }
c . scanner . close ( ) ;
smudp . setStop ( ) ;
smudp . setStop ( ) ;
smtcp . setStop ( ) ;
smtcp . setStop ( ) ;
}
}
}
}
}