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