@ -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 downloaded
* @param filename name of the file to download
* @return request to be send
* /
private String sendDownloadRequest ( String filename ) {
return protocolID + "\nDOWNLOAD\n" + filename + "\n" ;
}
/ * * Download file .
* @param response Servers ' s response
* @throws NotFound
* @throws NotFound
* @throws ProtocolError
* @throws InternalError
* @throws InternalError
* @throws UnknownHostException
* @throws IOException
* @throws TransmissionError
* @throws ProtocolError
* @throws VersionError
* @throws SizeError
* @throws InternalRemoteError
* @throws ProtocolRemoteError
* @throws VersionRemoteError
* /
* /
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" ) ;
} catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
System . out . println ( "Error: IndexOutOfBonds" ) ;
throw new ProtocolError ( ) ;
} catch ( NumberFormatException e ) {
System . out . println ( "Error: NumberFormat" ) ;
throw new ProtocolError ( ) ;
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 ( ) ;
}
}
/ * * 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 {
try {
String r [ ] = response . split ( "\n" ) ;
Files . write ( new File ( baseDirectory + filename ) . toPath ( ) , fp . getPartialContent ( ) ) ;
checkProtocolID ( r [ 0 ] ) ;
} catch ( IOException e ) {
return response . split ( protocolID + "\nLIST\n" ) [ 1 ] . split ( "\n\n" ) [ 0 ] ;
System . err . println ( "Error: cannot write file (" + baseDirectory + filename + ")" ) ;
} catch ( java . lang . ArrayIndexOutOfBoundsException e ) {
throw new ProtocolError ( ) ;
}
}
}
}
} catch ( EmptyDirectory e ) {
/ * * Check client ' s protocol identifier matches message ' s protocol identifier .
* Throws a ProtocolError if mismatched .
* @param msgProtocolID part of the request containing protocol identifier
* @throws ProtocolError
* /
private void checkProtocolID ( String msgProtocolID ) throws ProtocolError {
if ( ! protocolID . equals ( msgProtocolID ) ) {
throw new ProtocolError ( ) ;
throw new ProtocolError ( ) ;
}
}
}
}
/ * * Send message to server
/ * * list server ’ s directory content
* @param msg message to be send
* @return list of files
* @return server ' s response
* @throws InternalError
* @throws UnknowHostException
* @throws IOException
* @throws TransmissionError
* @throws ProtocolError
* @throws VersionError
* @throws SizeError
* @throws EmptyDirectory
* @throws InternalRemoteError
* @throws ProtocolRemoteError
* @throws VersionRemoteError
* /
* /
private String sendMsg ( String msg ) throws TransmissionError {
private String [ ] listDirectory ( ) throws EmptyDirectory , InternalError , UnknownHostException , IOException , TransmissionError , ProtocolError , VersionError , SizeError , InternalRemoteError , ProtocolRemoteError , VersionRemoteError {
//TODO changer le gros try catch
ProtocolP2PDatagram d = new ProtocolP2PDatagram ( new Payload ( RequestResponseCode . LIST_REQUEST ) ) ;
d . send ( socket , host , UDPPort ) ;
try {
try {
InetAddress dst = InetAddress . getByName ( host ) ;
Payload p = ProtocolP2PDatagram . receive ( socket ) . getPayload ( ) ;
byte [ ] buffer = msg . getBytes ( ) ;
assert p instanceof FileList : "This payload must be instance of Filelist" ;
byte [ ] buffer2 = new byte [ 1500 ] ;
if ( ! ( p instanceof FileList ) ) {
DatagramSocket socket = new DatagramSocket ( ) ;
throw new InternalError ( ) ;
DatagramPacket reception = new DatagramPacket ( buffer2 , 1500 ) ;
} else {
DatagramPacket emission = new DatagramPacket ( buffer , buffer . length , dst , UDPPort ) ;
return ( ( FileList ) p ) . getFileList ( ) ;
socket . send ( emission ) ;
}
socket . receive ( reception ) ;
} catch ( NotFound e ) {
return new String ( reception . getData ( ) , 0 , reception . getLength ( ) ) ;
throw new ProtocolError ( ) ;
} catch ( Exception e ) {
System . out . println ( e ) ;
throw new TransmissionError ( ) ;
}
}
}
}
}
}