@ -29,6 +29,7 @@ public class ClientP2P {
private Directories directories ;
private Directories directories ;
private HostItem tracker ;
private HostItem tracker ;
private HostItem server ;
private HostItem server ;
private HostItem serverBinding ;
private Scanner scanner ;
private Scanner scanner ;
final static boolean DEBUG = false ;
final static boolean DEBUG = false ;
@ -46,14 +47,17 @@ public class ClientP2P {
}
}
/ * * Constructor .
/ * * Constructor .
* @param hostnameServer hostname to bind
* @param hostnameServer hostname used to register
* @param portServer port to bind
* @param portServer port used to register
* @param hostnameServerBinding hostname to bind
* @param portServerBinding port to bind
* @param hostnameTracker hostname of tracker
* @param hostnameTracker hostname of tracker
* @param portTracker port of tracker
* @param portTracker port of tracker
* /
* /
public ClientP2P ( String hostnameServer , int portServer , String hostname Tracker, int portTracker ) {
public ClientP2P ( String hostnameServer , int portServer , String hostname ServerBinding, int portServerBinding , String hostname Tracker, int portTracker ) {
scanner = new Scanner ( System . in ) ;
scanner = new Scanner ( System . in ) ;
server = new HostItem ( hostnameServer , portServer ) ;
server = new HostItem ( hostnameServer , portServer ) ;
serverBinding = new HostItem ( hostnameServerBinding , portServerBinding ) ;
tracker = new HostItem ( hostnameTracker , portTracker ) ;
tracker = new HostItem ( hostnameTracker , portTracker ) ;
initDirectoriesAndLoggers ( ) ;
initDirectoriesAndLoggers ( ) ;
System . out . println ( "Server will listen on port " + portServer + " and serve files from " + directories . getDataHomeDirectory ( ) ) ;
System . out . println ( "Server will listen on port " + portServer + " and serve files from " + directories . getDataHomeDirectory ( ) ) ;
@ -72,6 +76,12 @@ public class ClientP2P {
"<clientTransportProtocol> " +
"<clientTransportProtocol> " +
"<integratedServerHOSTNAME> <integratedServerPORT> " +
"<integratedServerHOSTNAME> <integratedServerPORT> " +
"<trackerHOSTNAME> <trackerPORT> " ) ;
"<trackerHOSTNAME> <trackerPORT> " ) ;
System . out . println ( "or" ) ;
System . out . println ( "\tjava clientP2P.ClientP2P -- " +
"<clientTransportProtocol> " +
"<integratedServerHOSTNAME> <integratedServerPORT> " +
"<trackerHOSTNAME> <trackerPORT> " +
"<integratedServerBindHOSTNAME> <integratedServerBindPORT>" ) ;
System . out . println ( "(" + trackerPortRange + " and " + serverPortRange + ")" ) ;
System . out . println ( "(" + trackerPortRange + " and " + serverPortRange + ")" ) ;
}
}
@ -82,6 +92,8 @@ public class ClientP2P {
* /
* /
public static void main ( String [ ] args ) {
public static void main ( String [ ] args ) {
final String defaultHostname = "localhost" ;
final String defaultHostname = "localhost" ;
String hostnameServBind = "" ;
int portServBind = 0 ;
String hostnameServer = "" ;
String hostnameServer = "" ;
int portServer = 0 ;
int portServer = 0 ;
String hostnameTracker = "" ;
String hostnameTracker = "" ;
@ -94,13 +106,22 @@ public class ClientP2P {
if ( ( args . length ! = 6 ) & & ( args . length ! = 0 ) ) {
if ( ( args . length ! = 6 ) & & ( args . length ! = 0 ) ) {
ClientP2P . printUsage ( serverPortRange , trackerPortRange ) ;
ClientP2P . printUsage ( serverPortRange , trackerPortRange ) ;
System . exit ( 1 ) ;
System . exit ( 1 ) ;
}
} else if ( args . length = = 6 ) {
else if ( args . length = = 6 ) {
protocolClient = args [ 1 ] ;
protocolClient = args [ 1 ] ;
hostnameServer = args [ 2 ] ;
hostnameServer = args [ 2 ] ;
portServer = Integer . valueOf ( Integer . parseInt ( args [ 3 ] ) ) ;
portServer = Integer . valueOf ( Integer . parseInt ( args [ 3 ] ) ) ;
hostnameTracker = args [ 4 ] ;
hostnameTracker = args [ 4 ] ;
portTracker = Integer . valueOf ( Integer . parseInt ( args [ 5 ] ) ) ;
portTracker = Integer . valueOf ( Integer . parseInt ( args [ 5 ] ) ) ;
hostnameServBind = hostnameServer ;
portServBind = portServer ;
} else if ( args . length = = 8 ) {
protocolClient = args [ 1 ] ;
hostnameServer = args [ 2 ] ;
portServer = Integer . valueOf ( Integer . parseInt ( args [ 3 ] ) ) ;
hostnameTracker = args [ 4 ] ;
portTracker = Integer . valueOf ( Integer . parseInt ( args [ 5 ] ) ) ;
hostnameServBind = args [ 6 ] ;
portServBind = Integer . valueOf ( Integer . parseInt ( args [ 7 ] ) ) ;
} else {
} else {
System . out . println ( "Client, wich transport protocol do you want to use (default = TCP): " ) ;
System . out . println ( "Client, wich transport protocol do you want to use (default = TCP): " ) ;
protocolClient = scanner . nextLine ( ) ;
protocolClient = scanner . nextLine ( ) ;
@ -132,19 +153,22 @@ public class ClientP2P {
} else {
} else {
portTracker = Integer . valueOf ( Integer . parseInt ( portTrackerStr ) ) ;
portTracker = Integer . valueOf ( Integer . parseInt ( portTrackerStr ) ) ;
}
}
hostnameServBind = hostnameServer ;
portServBind = portServer ;
}
}
boolean serverInRange = serverPortRange . isPortInRange ( portServer ) ;
boolean serverInRange = serverPortRange . isPortInRange ( portServer ) ;
boolean serverBindInRange = serverPortRange . isPortInRange ( portServBind ) ;
boolean trackerInRange = trackerPortRange . isPortInRange ( portTracker ) ;
boolean trackerInRange = trackerPortRange . isPortInRange ( portTracker ) ;
if ( serverInRange & & trackerInRange ) {
if ( serverInRange & & trackerInRange & serverBindInRange ) {
System . out . println ( "using hostname : " + hostnameServer ) ;
System . out . println ( "using hostname : " + hostnameServer ) ;
System . out . println ( "using port : " + portServer ) ;
System . out . println ( "using port : " + portServer ) ;
System . out . println ( "tracker hostname : " + hostnameTracker ) ;
System . out . println ( "tracker hostname : " + hostnameTracker ) ;
System . out . println ( "tracker port : " + portTracker ) ;
System . out . println ( "tracker port : " + portTracker ) ;
ClientP2P c = new ClientP2P ( hostnameServer , portServer , hostname Tracker, portTracker ) ;
ClientP2P c = new ClientP2P ( hostnameServer , portServer , hostname ServBind, portServBind , hostname Tracker, portTracker ) ;
ServerManagementUDP smudp = new ServerManagementUDP ( c . directories . getDataHomeDirectory ( ) , c . server , c . tracker , c . loggerServer ) ;
ServerManagementUDP smudp = new ServerManagementUDP ( c . directories . getDataHomeDirectory ( ) , c . server Binding, c . server , c . tracker , c . loggerServer ) ;
ServerManagementTCP smtcp = new ServerManagementTCP ( c . directories . getDataHomeDirectory ( ) , c . server , c . tracker , c . loggerServer ) ;
ServerManagementTCP smtcp = new ServerManagementTCP ( c . directories . getDataHomeDirectory ( ) , c . server Binding, c . server , c . tracker , c . loggerServer ) ;
Thread tudp = new Thread ( smudp ) ;
Thread tudp = new Thread ( smudp ) ;
tudp . setName ( "server UDP P2P-JAVA-PROJECT" ) ;
tudp . setName ( "server UDP P2P-JAVA-PROJECT" ) ;
tudp . start ( ) ;
tudp . start ( ) ;
@ -193,6 +217,9 @@ public class ClientP2P {
if ( ! trackerInRange ) {
if ( ! trackerInRange ) {
System . err . println ( "TRACKER: Port not in range. " + trackerPortRange ) ;
System . err . println ( "TRACKER: Port not in range. " + trackerPortRange ) ;
}
}
if ( ! serverBindInRange ) {
System . err . println ( "SERVER-BINDING: Port not in range. " + serverPortRange ) ;
}
System . exit ( 2 ) ;
System . exit ( 2 ) ;
}
}
}
}