@ -89,28 +89,28 @@ public class ClientP2P {
final ServerPortRange serverPortRange = new ServerPortRange ( ) ;
final ServerPortRange serverPortRange = new ServerPortRange ( ) ;
final TrackerPortRange trackerPortRange = new TrackerPortRange ( ) ;
final TrackerPortRange trackerPortRange = new TrackerPortRange ( ) ;
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 ] ) ) ;
} 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 ( ) ;
System . out . println ( "server side, enter hostname to bind (default = localhost): " ) ;
System . out . println ( "server side, enter hostname to bind (default = localhost): " ) ;
hostnameServer = scanner . nextLine ( ) ;
hostnameServer = scanner . nextLine ( ) ;
if ( hostnameServer . equals ( "" ) ) {
if ( hostnameServer . equals ( "" ) ) {
hostnameServer = defaultHostname ;
hostnameServer = defaultHostname ;
System . out . println ( "using default hostname : " + hostnameServer ) ;
System . out . println ( "using default hostname : " + hostnameServer ) ;
}
}
System . out . println ( "enter port (default = " + serverPortRange . getDefaultPort ( ) + "): " ) ;
System . out . println ( "enter port (default = " + serverPortRange . getDefaultPort ( ) + "): " ) ;
String portServerStr = scanner . nextLine ( ) ;
String portServerStr = scanner . nextLine ( ) ;
if ( portServerStr . equals ( "" ) ) {
if ( portServerStr . equals ( "" ) ) {
portServer = serverPortRange . getDefaultPort ( ) ;
portServer = serverPortRange . getDefaultPort ( ) ;
System . out . println ( "using default port : " + portServer ) ;
System . out . println ( "using default port : " + portServer ) ;
} else {
} else {
@ -118,13 +118,13 @@ public class ClientP2P {
}
}
System . out . println ( "enter hostname of tracker (default = localhost): " ) ;
System . out . println ( "enter hostname of tracker (default = localhost): " ) ;
hostnameTracker = scanner . nextLine ( ) ;
hostnameTracker = scanner . nextLine ( ) ;
if ( hostnameTracker . equals ( "" ) ) {
if ( hostnameTracker . equals ( "" ) ) {
hostnameTracker = defaultHostname ;
hostnameTracker = defaultHostname ;
System . out . println ( "tracker default hostname : " + hostnameTracker ) ;
System . out . println ( "tracker default hostname : " + hostnameTracker ) ;
}
}
System . out . println ( "enter tracker's port (default = " + trackerPortRange . getDefaultPort ( ) + "): " ) ;
System . out . println ( "enter tracker's port (default = " + trackerPortRange . getDefaultPort ( ) + "): " ) ;
String portTrackerStr = scanner . nextLine ( ) ;
String portTrackerStr = scanner . nextLine ( ) ;
if ( portTrackerStr . equals ( "" ) ) {
if ( portTrackerStr . equals ( "" ) ) {
portTracker = trackerPortRange . getDefaultPort ( ) ;
portTracker = trackerPortRange . getDefaultPort ( ) ;
System . out . println ( "using default port : " + portTracker ) ;
System . out . println ( "using default port : " + portTracker ) ;
} else {
} else {
@ -132,68 +132,66 @@ public class ClientP2P {
}
}
}
}
System . out . println ( "using hostname : " + hostnameServer ) ;
boolean serverInRange = serverPortRange . isPortInRange ( portServer ) ;
if ( serverPortRange . isPortInRange ( portServer ) ) {
boolean trackerInRange = trackerPortRange . isPortInRange ( portTracker ) ;
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 ) ;
if ( serverInRange & & trackerInRange ) {
ServerManagementTCP smtcp = new ServerManagementTCP ( c . directories . getDataHomeDirectory ( ) , c . server , c . tracker , c . loggerServer ) ;
System . out . println ( "using hostname : " + hostnameServer ) ;
Thread tudp = new Thread ( smudp ) ;
System . out . println ( "using port : " + portServer ) ;
tudp . setName ( "server UDP P2P-JAVA-PROJECT" ) ;
System . out . println ( "tracker hostname : " + hostnameTracker ) ;
tudp . start ( ) ;
System . out . println ( "tracker port : " + portTracker ) ;
Thread ttcp = new Thread ( smtcp ) ;
ClientP2P c = new ClientP2P ( hostnameServer , portServer , hostnameTracker , portTracker ) ;
ttcp . setName ( "server TCP P2P-JAVA-PROJECT" ) ;
ServerManagementUDP smudp = new ServerManagementUDP ( c . directories . getDataHomeDirectory ( ) , c . server , c . tracker , c . loggerServer ) ;
ttcp . start ( ) ;
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
// Wait a bit before printing client interface
// This is not required, but allow to have a cleaner interface
// This is not required, but allow to have a cleaner interface
try {
try {
Thread . sleep ( 200 ) ;
Thread . sleep ( 200 ) ;
} catch ( InterruptedException e ) {
} catch ( InterruptedException e ) {
Thread . currentThread ( ) . interrupt ( ) ;
Thread . currentThread ( ) . interrupt ( ) ;
}
}
Thread tclient ;
Thread tclient ;
switch ( protocolClient ) {
switch ( protocolClient ) {
case "UDP" :
case "UDP" :
case "udp" :
case "udp" :
case "upd" : // to avoid users typos
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 , c . server ) ;
ClientManagementUDP cmudp = new ClientManagementUDP ( c . directories . getDataHomeDirectory ( ) , c . tracker , c . directories . getDataHomeDirectory ( ) + c . partsDir , c . loggerClient , c . scanner , c . server ) ;
tclient = new Thread ( cmudp ) ;
tclient = new Thread ( cmudp ) ;
break ;
break ;
case "TCP" :
case "TCP" :
case "tcp" :
case "tcp" :
case "1" :
case "1" :
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 , c . server ) ;
ClientManagementTCP cmtcp = new ClientManagementTCP ( c . directories . getDataHomeDirectory ( ) , c . tracker , c . directories . getDataHomeDirectory ( ) + c . partsDir , c . loggerClient , c . scanner , c . server ) ;
tclient = new Thread ( cmtcp ) ;
tclient = new Thread ( cmtcp ) ;
break ;
break ;
}
tclient . setName ( "client P2P-JAVA-PROJECT" ) ;
tclient . start ( ) ;
try {
tclient . join ( ) ;
} catch ( InterruptedException e ) { }
smudp . setStop ( ) ;
smtcp . setStop ( ) ;
} 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 ) ;
}
}
tclient . setName ( "client P2P-JAVA-PROJECT" ) ;
tclient . start ( ) ;
try {
tclient . join ( ) ;
} catch ( InterruptedException e ) { }
smudp . setStop ( ) ;
smtcp . setStop ( ) ;
}
}
}
}