@ -1,18 +1,30 @@
package clientP2P ;
package clientP2P ;
import exception.NotFound ;
import exception.ProtocolError ;
import exception.InternalError ;
import exception.InternalError ;
import exception.ProtocolError ;
import exception.SizeError ;
import exception.TransmissionError ;
import exception.TransmissionError ;
import exception.VersionError ;
import remoteException.EmptyDirectory ;
import remoteException.InternalRemoteError ;
import remoteException.NotFound ;
import remoteException.ProtocolRemoteError ;
import remoteException.VersionRemoteError ;
import java.net.UnknownHostException ;
import java.util.Scanner ;
import java.util.Scanner ;
import java.net.InetAddress ;
import java.net.InetAddress ;
import java.net.DatagramPacket ;
import java.net.DatagramSocket ;
import java.net.DatagramSocket ;
import java.io.File ;
import java. net.SocketException ;
import java.io.IOException ;
import java.io.IOException ;
import java.io.FileWriter ;
import java.nio.file.Files ;
import java.io.File ;
import protocolP2P.ProtocolP2PDatagram ;
import protocolP2P.Payload ;
import protocolP2P.RequestResponseCode ;
import protocolP2P.FileList ;
import protocolP2P.FilePart ;
import protocolP2P.LoadRequest ;
/ * * Implementation of P2P - JAVA - PROJECT VERSION 1.0 protocol for UDP .
/ * * Implementation of P2P - JAVA - PROJECT CLIENT
* @author Louis Royer
* @author Louis Royer
* @author Flavien Haas
* @author Flavien Haas
* @author JS Auge
* @author JS Auge
@ -22,142 +34,139 @@ public class ClientManagementUDP implements Runnable {
private String baseDirectory ;
private String baseDirectory ;
private int UDPPort ;
private int UDPPort ;
private String host ;
private String host ;
private final String protocolID = "P2P-JAVA-PROJECT VERSION 1.0" ;
private DatagramSocket socket ;
/ * * Constructor for UDP implementation , with baseDirectory and UDPPort parameters .
/ * * Constructor for UDP implementation , with baseDirectory and UDPPort parameters .
* @param baseDirectory the root directory where files are stored
* @param baseDirectory the root directory where files are stored
* @param host hostname of the server
* @param UDPPort the server will listen on this port
* @param UDPPort the server will listen on this port
* /
* /
public ClientManagementUDP ( String baseDirectory , String host , int UDPPort ) {
public ClientManagementUDP ( String baseDirectory , String host , int UDPPort ) {
this . baseDirectory = baseDirectory ;
this . baseDirectory = baseDirectory ;
this . host = host ;
this . host = host ;
this . UDPPort = UDPPort ;
this . UDPPort = UDPPort ;
try {
socket = new DatagramSocket ( ) ;
} catch ( SocketException e ) {
System . err . println ( "Error: No socket available." ) ;
System . exit ( - 1 ) ;
}
}
}
/ * * Implementation of Runnable
/ * * Implementation of Runnable
* /
* /
public void run ( ) {
public void run ( ) {
System . out . println ( "Files present on the server:" ) ;
try {
try {
String msg = sendMsg ( sendListDirectoryRequest ( ) ) ;
String [ ] list = listDirectory ( ) ;
System . out . println ( listDirectory ( msg ) ) ;
System . out . println ( "Files present on the server:" ) ;
for ( String listItem : list ) {
System . out . println ( listItem ) ;
}
System . out . println ( "Name of the file to download:" ) ;
System . out . println ( "Name of the file to download:" ) ;
Scanner scanner = new Scanner ( System . in ) ;
Scanner scanner = new Scanner ( System . in ) ;
String f = scanner . nextLine ( ) ;
String f = scanner . nextLine ( ) ;
msg = sendMsg ( sendDownloadRequest ( f ) ) ;
download ( f ) ;
download ( msg , f ) ;
System . out . println ( "File sucessfully downloaded" ) ;
} catch ( EmptyDirectory e ) {
System . err . println ( "Error: Server has no file in directory" ) ;
} catch ( InternalError e ) {
System . err . println ( "Error: Client internal error" ) ;
} catch ( UnknownHostException e ) {
System . err . println ( "Error: Server host is unknown" ) ;
} catch ( IOException e ) {
System . err . println ( "Error: Request cannot be send or response cannot be received" ) ;
} catch ( TransmissionError e ) {
} catch ( TransmissionError e ) {
System . out . println ( "Transmission error" ) ;
System . err. println ( "Error: Message received is too big ") ;
} catch ( ProtocolError e ) {
} catch ( ProtocolError e ) {
System . out . println ( "Protocol error" ) ;
System . err . println ( "Error: Cannot decode server’ s response" ) ;
} catch ( VersionError e ) {
System . err . println ( "Error: Server’ s response use bad version of the protocol" ) ;
} catch ( SizeError e ) {
System . err . println ( "Error: Cannot handle this packets because of internal representation limitations of numbers on the client" ) ;
} catch ( InternalRemoteError e ) {
System . err . println ( "Error: Server internal error" ) ;
} catch ( ProtocolRemoteError e ) {
System . err . println ( "Error: Server cannot decode client’ s request" ) ;
} catch ( VersionRemoteError e ) {
System . err . println ( "Error: Server cannot decode this version of the protocol" ) ;
} catch ( NotFound e ) {
} catch ( NotFound e ) {
System . out . println ( "File not found" ) ;
System . err . println ( "Error: Server have not this file in directory" ) ;
} catch ( InternalError e ) {
System . out . println ( "Server internal error" ) ;
} catch ( IOException e ) {
System . out . println ( "Cannot write to file" ) ;
}
}
}
}
/ * * Prepare request to download file
/ * * Try to download a file
* @param filename name of the file to be downloade d
* @param filename name of the file to download
* @ return request to be se nd
* @ throws NotFou nd
* /
* @throws InternalError
private String sendDownloadRequest ( String filename ) {
* @throws UnknownHostException
return protocolID + "\nDOWNLOAD\n" + filename + "\n" ;
* @throws IOException
}
* @throws TransmissionError
* @throws ProtocolError
/ * * Download file .
* @throws VersionError
* @ param response Servers ' s response
* @ throws SizeError
* @throws NotFound
* @throws InternalRemoteError
* @throws Protocol Error
* @throws ProtocolRemote Error
* @throws Internal Error
* @throws VersionRemote Error
* /
* /
private void download ( String response , String filename ) throws TransmissionError , NotFound , ProtocolError , InternalError , IOException {
private void download ( String filename ) throws NotFound , InternalError , UnknownHostException , IOException , TransmissionError , ProtocolError , VersionError , SizeError , InternalRemoteError , ProtocolRemoteError , VersionRemoteError {
ProtocolP2PDatagram d = new ProtocolP2PDatagram ( ( Payload ) new LoadRequest ( filename ) ) ;
d . send ( socket , host , UDPPort ) ;
try {
try {
String r [ ] = response . split ( "\n" ) ;
Payload p = ProtocolP2PDatagram . receive ( socket ) . getPayload ( ) ;
checkProtocolID ( r [ 0 ] ) ;
assert p instanceof FilePart : "This payload must be instance of FilePart" ;
switch ( r [ 1 ] ) {
if ( ! ( p instanceof FilePart ) ) {
case "LOAD" :
int size = Integer . parseInt ( r [ 2 ] ) ;
if ( r [ 3 ] . length ( ) ! = size - 1 ) {
throw new TransmissionError ( ) ;
}
FileWriter fileWriter = new FileWriter ( baseDirectory + filename ) ;
fileWriter . write ( r [ 3 ] ) ;
fileWriter . close ( ) ;
break ;
case "NOT FOUND" :
throw new NotFound ( ) ;
case "INTERNAL ERROR" :
throw new InternalError ( ) ;
throw new InternalError ( ) ;
default :
} else {
System . out . println ( "Error: Unknow format `" + r [ 1 ] + "`" ) ;
FilePart fp = ( FilePart ) p ;
throw new ProtocolError ( ) ;
if ( ! fp . getFilename ( ) . equals ( filename ) ) {
System . err . println ( "Error: wrong file received" ) ;
throw new ProtocolError ( ) ;
}
if ( fp . getOffset ( ) ! = 0 | | fp . getPartialContent ( ) . length ! = fp . getTotalSize ( ) ) {
System . err . println ( "Error: cannot handle partial files (not implemented)" ) ;
throw new InternalError ( ) ;
}
try {
Files . write ( new File ( baseDirectory + filename ) . toPath ( ) , fp . getPartialContent ( ) ) ;
} catch ( IOException e ) {
System . err . println ( "Error: cannot write file (" + baseDirectory + filename + ")" ) ;
}
}
}
} catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
} catch ( EmptyDirectory e ) {
System . out . println ( "Error: IndexOutOfBonds" ) ;
throw new ProtocolError ( ) ;
throw new ProtocolError ( ) ;
} catch ( NumberFormatException e ) {
System . out . println ( "Error: NumberFormat" ) ;
throw new ProtocolError ( ) ;
}
}
/ * * Prepare request to list files on server
* @return request to be send
* /
private String sendListDirectoryRequest ( ) {
return protocolID + "\nLIST\n" ;
}
}
/ * * Parse list of directory response from server
* @param response server ' s response
* @return list of files , separated by CRLF
* @throws ProtocolError
* /
private String listDirectory ( String response ) throws ProtocolError {
try {
String r [ ] = response . split ( "\n" ) ;
checkProtocolID ( r [ 0 ] ) ;
return response . split ( protocolID + "\nLIST\n" ) [ 1 ] . split ( "\n\n" ) [ 0 ] ;
} catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
throw new ProtocolError ( ) ;
}
}
}
/ * * Check client ' s protocol identifier matches message ' s protocol identifier .
/ * * list server ’ s directory content
* Throws a ProtocolError if mismatched .
* @return list of files
* @param msgProtocolID part of the request containing protocol identifier
* @throws InternalError
* @throws UnknowHostException
* @throws IOException
* @throws TransmissionError
* @throws ProtocolError
* @throws ProtocolError
* @throws VersionError
* @throws SizeError
* @throws EmptyDirectory
* @throws InternalRemoteError
* @throws ProtocolRemoteError
* @throws VersionRemoteError
* /
* /
private void checkProtocolID ( String msgProtocolID ) throws ProtocolError {
private String [ ] listDirectory ( ) throws EmptyDirectory , InternalError , UnknownHostException , IOException , TransmissionError , ProtocolError , VersionError , SizeError , InternalRemoteError , ProtocolRemoteError , VersionRemoteError {
if ( ! protocolID . equals ( msgProtocolID ) ) {
ProtocolP2PDatagram d = new ProtocolP2PDatagram ( new Payload ( RequestResponseCode . LIST_REQUEST ) ) ;
d . send ( socket , host , UDPPort ) ;
try {
Payload p = ProtocolP2PDatagram . receive ( socket ) . getPayload ( ) ;
assert p instanceof FileList : "This payload must be instance of Filelist" ;
if ( ! ( p instanceof FileList ) ) {
throw new InternalError ( ) ;
} else {
return ( ( FileList ) p ) . getFileList ( ) ;
}
} catch ( NotFound e ) {
throw new ProtocolError ( ) ;
throw new ProtocolError ( ) ;
}
}
}
}
/ * * Send message to server
* @param msg message to be send
* @return server ' s response
* /
private String sendMsg ( String msg ) throws TransmissionError {
//TODO changer le gros try catch
try {
InetAddress dst = InetAddress . getByName ( host ) ;
byte [ ] buffer = msg . getBytes ( ) ;
byte [ ] buffer2 = new byte [ 1500 ] ;
DatagramSocket socket = new DatagramSocket ( ) ;
DatagramPacket reception = new DatagramPacket ( buffer2 , 1500 ) ;
DatagramPacket emission = new DatagramPacket ( buffer , buffer . length , dst , UDPPort ) ;
socket . send ( emission ) ;
socket . receive ( reception ) ;
return new String ( reception . getData ( ) , 0 , reception . getLength ( ) ) ;
} catch ( Exception e ) {
System . out . println ( e ) ;
throw new TransmissionError ( ) ;
}
}
}
}