@ -29,8 +29,9 @@ public class ServerP2P {
* @param portServer binded port
* @param hostnameTracker tracker hostname
* @param portTracker tracker port
* @param isInteractive true if application in interactive mode
* /
public ServerP2P ( String hostnameServer , int portServer , String hostnameTracker , int portTracker ) {
public ServerP2P ( String hostnameServer , int portServer , String hostnameTracker , int portTracker , boolean isInteractive ) {
Scanner scanner = new Scanner ( System . in ) ;
server = new HostItem ( hostnameServer , portServer ) ;
tracker = new HostItem ( hostnameTracker , portTracker ) ;
@ -38,7 +39,9 @@ public class ServerP2P {
directories . createSubdir ( subdir ) ;
logger = new Logger ( directories . getDataHomeDirectory ( ) + "server.log" ) ;
System . out . println ( "Server will listen on port " + portServer + " and serve files from " + directories . getDataHomeDirectory ( ) + subdir ) ;
if ( isInteractive ) {
directories . askOpenDataHomeDirectory ( subdir , scanner ) ;
}
scanner . close ( ) ;
}
@ -56,66 +59,60 @@ public class ServerP2P {
String hostnameTracker = "" ;
int portTracker = 0 ;
Scanner scanner = new Scanner ( System . in ) ;
boolean isInteractive = false ;
if ( ( args . length ! = 5 ) & & ( args . length ! = 0 ) ) {
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 if ( args . length = = 5 ) {
else if ( args . length = = 5 ) {
isInteractive = false ;
hostnameServer = args [ 1 ] ;
portServer = Integer . valueOf ( Integer . parseInt ( args [ 2 ] ) ) ;
hostnameTracker = args [ 3 ] ;
portTracker = Integer . valueOf ( Integer . parseInt ( args [ 4 ] ) ) ;
} else {
isInteractive = true ;
// configure application
System . out . println ( "Server, enter hostname to bind (default = localhost): " ) ;
hostnameServer = scanner . nextLine ( ) ;
if ( hostnameServer . equals ( "" ) ) {
if ( hostnameServer . equals ( "" ) ) {
hostnameServer = defaultHostname ;
System . out . println ( "using default hostname : " + hostnameServer ) ;
}
System . out . println ( " e nter port (default = " + serverPortRange . getDefaultPort ( ) + "): " ) ;
System . out . println ( " E nter port (default = " + serverPortRange . getDefaultPort ( ) + "): " ) ;
String portServerStr = scanner . nextLine ( ) ;
if ( portServerStr . equals ( "" ) ) {
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): " ) ;
// configure tracker
System . out . println ( "Enter hostname of tracker (default = localhost): " ) ;
hostnameTracker = scanner . nextLine ( ) ;
if ( hostnameTracker . equals ( "" ) ) {
if ( hostnameTracker . equals ( "" ) ) {
hostnameTracker = defaultHostname ;
System . out . println ( "tracker default hostname : " + hostnameTracker ) ;
}
System . out . println ( " e nter tracker's port (default = " + trackerPortRange . getDefaultPort ( ) + "): " ) ;
System . out . println ( " E nter tracker's port (default = " + trackerPortRange . getDefaultPort ( ) + "): " ) ;
String portTrackerStr = scanner . nextLine ( ) ;
if ( portTrackerStr . equals ( "" ) ) {
portTracker = serv erPortRange. getDefaultPort ( ) ;
if ( portTrackerStr . equals ( "" ) ) {
portTracker = track erPortRange. getDefaultPort ( ) ;
System . out . println ( "using default port : " + portTracker ) ;
} else {
portTracker = Integer . valueOf ( Integer . parseInt ( portTrackerStr ) ) ;
}
}
boolean serverInRange = serverPortRange . isPortInRange ( portServer ) ;
boolean trackerInRange = trackerPortRange . isPortInRange ( portTracker ) ;
if ( serverInRange & & trackerInRange ) {
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 ) ;
System . out . println ( "tracker port : " + portTracker ) ;
ServerP2P s = new ServerP2P ( hostnameServer , portServer , hostnameTracker , portTracker , isInteractive ) ;
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 ) ;
@ -124,5 +121,14 @@ public class ServerP2P {
Thread ttcp = new Thread ( smtcp ) ;
ttcp . setName ( "server TCP P2P-JAVA-PROJECT" ) ;
ttcp . start ( ) ;
} else {
if ( ! serverInRange ) {
System . err . println ( "SERVER: Port not in range. " + serverPortRange ) ;
}
if ( ! trackerInRange ) {
System . err . println ( "TRACKER: Port not in range. " + trackerPortRange ) ;
}
System . exit ( 2 ) ;
}
}
}