correction syntaxiques de clientP2P
This commit is contained in:
parent
a326c33124
commit
572a75e3ae
BIN
src/clientP2P/ClientManagementUDP.class
Normal file
BIN
src/clientP2P/ClientManagementUDP.class
Normal file
Binary file not shown.
@ -1,14 +1,16 @@
|
|||||||
package client;
|
package clientP2P;
|
||||||
import exception.NotFound;
|
import exception.NotFound;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
import exception.InternalError;
|
import exception.InternalError;
|
||||||
import exception.TransmissionError;
|
import exception.TransmissionError;
|
||||||
import java.utils.Scanner;
|
import java.util.Scanner;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
|
||||||
|
|
||||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
||||||
* @author Louis Royer
|
* @author Louis Royer
|
||||||
@ -16,7 +18,7 @@ import java.io.IOException;
|
|||||||
* @author JS Auge
|
* @author JS Auge
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class ClientManagmentUDP implements Runnable {
|
public class ClientManagementUDP implements Runnable {
|
||||||
private String baseDirectory;
|
private String baseDirectory;
|
||||||
private int UDPPort;
|
private int UDPPort;
|
||||||
private String host;
|
private String host;
|
||||||
@ -27,7 +29,7 @@ public class ClientManagmentUDP implements Runnable {
|
|||||||
* @param baseDirectory the root directory where files are stored
|
* @param baseDirectory the root directory where files are stored
|
||||||
* @param UDPPort the server will listen on this port
|
* @param UDPPort the server will listen on this port
|
||||||
*/
|
*/
|
||||||
public ClientManagmentUDP(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;
|
||||||
@ -38,13 +40,15 @@ public class ClientManagmentUDP implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
System.out.println("Files present on the server:");
|
System.out.println("Files present on the server:");
|
||||||
try {
|
try {
|
||||||
String msg = sendMsg(sendFileList());
|
String msg = sendMsg(sendListDirectoryRequest());
|
||||||
System.out.println(listDirectory(msg));
|
System.out.println(listDirectory(msg));
|
||||||
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);
|
||||||
f = scanner.nextLine();
|
String f = scanner.nextLine();
|
||||||
msg = sendMsg(sendDownloadRequest(f));
|
msg = sendMsg(sendDownloadRequest(f));
|
||||||
download(msg, f);
|
download(msg, f);
|
||||||
|
} catch (TransmissionError e) {
|
||||||
|
System.out.println("TransmissionError");
|
||||||
} catch (ProtocolError e) {
|
} catch (ProtocolError e) {
|
||||||
System.out.println("Protocol error");
|
System.out.println("Protocol error");
|
||||||
} catch (NotFound e) {
|
} catch (NotFound e) {
|
||||||
@ -70,26 +74,26 @@ public class ClientManagmentUDP implements Runnable {
|
|||||||
* @throws ProtocolError
|
* @throws ProtocolError
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
*/
|
*/
|
||||||
private void download(String response, String filename) throws NotFound, ProtocolError, InternalError, IOException {
|
private void download(String response, String filename) throws TransmissionError, NotFound, ProtocolError, InternalError, IOException {
|
||||||
try {
|
try {
|
||||||
String r[] = r.split('\n', 3);
|
String r[] = response.split("\n", 3);
|
||||||
checkProtocolID(r[0]);
|
checkProtocolID(r[0]);
|
||||||
String r2[] = r[1].split(' ');
|
String r2[] = r[1].split(" ");
|
||||||
if (r2[0] != "LOAD") {
|
if (r2[0] != "LOAD") {
|
||||||
throw ProtocolError;
|
throw new ProtocolError();
|
||||||
}
|
}
|
||||||
int size = Integer.parseInt(r2[1]);
|
int size = Integer.parseInt(r2[1]);
|
||||||
if (r[2].length() != size) {
|
if (r[2].length() != size) {
|
||||||
throws TransmissionError;
|
throw new TransmissionError();
|
||||||
}
|
}
|
||||||
FileWriter fileWriter = new FileWriter(baseDirectory + f);
|
FileWriter fileWriter = new FileWriter(baseDirectory + filename);
|
||||||
fileWriter.write(r[2]);
|
fileWriter.write(r[2]);
|
||||||
fileWriter.close();
|
fileWriter.close();
|
||||||
|
|
||||||
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
|
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
|
||||||
throw ProtocolError;
|
throw new ProtocolError();
|
||||||
} catch (ParseException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw ProtocolError;
|
throw new ProtocolError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,14 +111,14 @@ public class ClientManagmentUDP implements Runnable {
|
|||||||
*/
|
*/
|
||||||
private String listDirectory(String response) throws ProtocolError {
|
private String listDirectory(String response) throws ProtocolError {
|
||||||
try {
|
try {
|
||||||
String r[] = r.split('\n');
|
String r[] = response.split("\n");
|
||||||
checkProtocolID(r[0]);
|
checkProtocolID(r[0]);
|
||||||
return r.split(protocolID + "\nLOAD \n")[1];
|
return response.split(protocolID + "\nLOAD \n")[1];
|
||||||
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
|
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
|
||||||
throw ProtocolError;
|
throw new ProtocolError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check client's protocol identifier matches message's protocol identifier.
|
/** Check client's protocol identifier matches message's protocol identifier.
|
||||||
* Throws a ProtocolError if mismatched.
|
* Throws a ProtocolError if mismatched.
|
||||||
* @param msgProtocolID part of the request containing protocol identifier
|
* @param msgProtocolID part of the request containing protocol identifier
|
||||||
@ -122,7 +126,7 @@ public class ClientManagmentUDP implements Runnable {
|
|||||||
*/
|
*/
|
||||||
private void checkProtocolID(String msgProtocolID) throws ProtocolError {
|
private void checkProtocolID(String msgProtocolID) throws ProtocolError {
|
||||||
if (protocolID != msgProtocolID) {
|
if (protocolID != msgProtocolID) {
|
||||||
throw ProtocolError;
|
throw new ProtocolError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,11 +134,20 @@ public class ClientManagmentUDP implements Runnable {
|
|||||||
* @param msg message to be send
|
* @param msg message to be send
|
||||||
* @return server's response
|
* @return server's response
|
||||||
*/
|
*/
|
||||||
private String sendMsg(String msg) {
|
private String sendMsg(String msg) throws TransmissionError {
|
||||||
InetAddress dst = InetAddress.getByName(host);
|
//TODO changer le gros try catch
|
||||||
byte [] bytesString = msg.getBytes();
|
try{
|
||||||
DatagramPacket emission = new DatagramPacket(bytesString, bytesString.length, dst, UDPPort);
|
InetAddress dst = InetAddress.getByName(host);
|
||||||
socket.send(emission);
|
byte [] buffer = msg.getBytes();
|
||||||
socket.receive(reception);
|
DatagramSocket socket = new DatagramSocket();
|
||||||
return new String(reception.getData(), 0, reception.getLength());
|
DatagramPacket reception = new DatagramPacket(buffer, buffer.length);
|
||||||
|
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){
|
||||||
|
throw new TransmissionError();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
BIN
src/clientP2P/ClientP2P.class
Normal file
BIN
src/clientP2P/ClientP2P.class
Normal file
Binary file not shown.
@ -1,10 +1,12 @@
|
|||||||
package client;
|
package clientP2P;
|
||||||
|
import java.io.File;
|
||||||
|
import clientP2P.ClientManagementUDP;
|
||||||
|
|
||||||
public class Client {
|
public class ClientP2P {
|
||||||
private String host;
|
private String host;
|
||||||
private int port;
|
private int port;
|
||||||
private String directory;
|
private String directory;
|
||||||
public Client() {
|
public ClientP2P() {
|
||||||
host = "localhost";
|
host = "localhost";
|
||||||
port = 40000;
|
port = 40000;
|
||||||
String d;
|
String d;
|
||||||
@ -13,12 +15,12 @@ public class Client {
|
|||||||
*/
|
*/
|
||||||
if (System.getProperty("os.name").equals("Linux")) {
|
if (System.getProperty("os.name").equals("Linux")) {
|
||||||
d = System.getenv().get("XDG_DATA_HOME");
|
d = System.getenv().get("XDG_DATA_HOME");
|
||||||
if (d == null || f.equals("")) {
|
if (d == null || d.equals("")) {
|
||||||
d = System.getenv().get("HOME");
|
d = System.getenv().get("HOME");
|
||||||
if (d != null && (!f.equals(""))) {
|
if (d != null && (!d.equals(""))) {
|
||||||
d += "/.local/share";
|
d += "/.local/share";
|
||||||
} else {
|
} else {
|
||||||
d += "."
|
d += ".";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -28,9 +30,9 @@ public class Client {
|
|||||||
// create directory if not already exists
|
// create directory if not already exists
|
||||||
new File(d).mkdirs();
|
new File(d).mkdirs();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
public static void main(String [] args) {
|
public static void main(String [] args) {
|
||||||
Client c = new Client("client");
|
ClientP2P c = new ClientP2P();
|
||||||
ClientManagementUDP cm = new ClientManagementUDP(c.directory, c.host, c.port);
|
ClientManagementUDP cm = new ClientManagementUDP(c.directory, c.host, c.port);
|
||||||
Thread t = new Thread(cm);
|
Thread t = new Thread(cm);
|
||||||
t.setName("client P2P-JAVA-PROJECT");
|
t.setName("client P2P-JAVA-PROJECT");
|
BIN
src/exception/TransmissionError.class
Normal file
BIN
src/exception/TransmissionError.class
Normal file
Binary file not shown.
@ -1,2 +1,2 @@
|
|||||||
package exception;
|
package exception;
|
||||||
public class TransmissionError extends Exceptions {}
|
public class TransmissionError extends Exception {}
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
package Server;
|
|
||||||
import java.io.File;
|
|
||||||
import server.ServerManagementUDP;
|
|
||||||
|
|
||||||
public class Server {
|
|
||||||
private int port;
|
|
||||||
private String directory;
|
|
||||||
public Server() {
|
|
||||||
port = 40000;
|
|
||||||
String d;
|
|
||||||
/* Follow XDG Base Directory Specification
|
|
||||||
* https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
||||||
*/
|
|
||||||
if (System.getProperty("os.name").equals("Linux")) {
|
|
||||||
d = System.getenv().get("XDG_DATA_HOME");
|
|
||||||
if (d == null || f.equals("")) {
|
|
||||||
d = System.getenv().get("HOME");
|
|
||||||
if (d != null && (!f.equals(""))) {
|
|
||||||
d += "/.local/share";
|
|
||||||
} else {
|
|
||||||
d += "."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
d = ".";
|
|
||||||
}
|
|
||||||
d += "P2P_JAVA_PROJECT_SERVER/";
|
|
||||||
// create directory if not already exists
|
|
||||||
new File(d).mkdirs();
|
|
||||||
}
|
|
||||||
public static void main(String [] args) {
|
|
||||||
Server s = new Server("server");
|
|
||||||
ServerManagementUDP sm = new ServerManagementUDP(s.directory, s.port);
|
|
||||||
Thread t = new Thread(sm);
|
|
||||||
t.setName("server P2P-JAVA-PROJECT");
|
|
||||||
t.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,172 +0,0 @@
|
|||||||
package server;
|
|
||||||
import java.util.Vector;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.Datagram.Packet;
|
|
||||||
import java.net.Datagram.Socket;
|
|
||||||
import exception.ProtocolError;
|
|
||||||
import exception.NotFound;
|
|
||||||
import exception.InternalError;
|
|
||||||
|
|
||||||
|
|
||||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
|
||||||
* @author Louis Royer
|
|
||||||
* @author Flavien Haas
|
|
||||||
* @author JS Auge
|
|
||||||
* @version 1.0
|
|
||||||
*/
|
|
||||||
public class ServerManagementUDP implements Runnable {
|
|
||||||
|
|
||||||
private Vector<String> fileList;
|
|
||||||
private String baseDirectory;
|
|
||||||
private int UDPPort;
|
|
||||||
private final String protocolID = "P2P-JAVA-PROJECT VERSION 1.0";
|
|
||||||
|
|
||||||
/** Constructor for UDP implementation, with baseDirectory and UDPPort parameters.
|
|
||||||
* @param baseDirectory the root directory where files are stored
|
|
||||||
* @param UDPPort the server will listen on this port
|
|
||||||
*/
|
|
||||||
public ServerManagementUDP(String baseDirectory, int UDPPort) {
|
|
||||||
this.baseDirectory = baseDirectory;
|
|
||||||
this.UDPPort = UDPPort;
|
|
||||||
fileList = new Vector<String>;
|
|
||||||
initFileList();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Implementation of runnable. This methods allows to run the server.
|
|
||||||
*/
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
// socket creation on port UDPPort
|
|
||||||
DatagramSocket socket = new DatagramSocket(UDPPort);
|
|
||||||
// buffer to receive UDP Datagram
|
|
||||||
final byte[] buffer = new byte[1024];
|
|
||||||
while(true) {
|
|
||||||
// java object to receive Datagram
|
|
||||||
DatagramPacket dgram = new DatagramPacket(tampon, tampon.length);
|
|
||||||
// wait and receive datagram
|
|
||||||
socket.receive(dgram);
|
|
||||||
// extract data
|
|
||||||
String str = new String(dgram.getData(), 0, dgram.getLength());
|
|
||||||
// process request
|
|
||||||
str = gestionProtocole.processRequest(str);
|
|
||||||
dgram.setData(chaine.getBytes());
|
|
||||||
dgram.setLength(chaine.getBytes().length);
|
|
||||||
// send response
|
|
||||||
socket.send(dgram);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO: treat exceptions
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Process the request received.
|
|
||||||
* @param request the request received
|
|
||||||
* @return data to be send as response
|
|
||||||
*/
|
|
||||||
String processRequest(String request) {
|
|
||||||
String res = protocolID + '\n';
|
|
||||||
String formattedRequest[] = request.split('\n');
|
|
||||||
try {
|
|
||||||
try {
|
|
||||||
checkProtocolID(formattedRequest[0]);
|
|
||||||
switch (formattedRequest[1]) {
|
|
||||||
"LIST":
|
|
||||||
res += sendFileList();
|
|
||||||
break;
|
|
||||||
"DOWNLOAD":
|
|
||||||
res += upload(formattedRequest[2]);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw ProtocolError;
|
|
||||||
}
|
|
||||||
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
|
|
||||||
throw ProtocolError;
|
|
||||||
}
|
|
||||||
} catch (ProtocolError e) {
|
|
||||||
// wrong version or wrong implementation
|
|
||||||
res += sendProtocolError();
|
|
||||||
} catch (InternalError e) {
|
|
||||||
res += sendInternalError();
|
|
||||||
} catch (NotFound e) {
|
|
||||||
res += sendNotFound();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Initialize local list of all files allowed to be shared.
|
|
||||||
*/
|
|
||||||
private void initFileList() {
|
|
||||||
File folder = new File(baseDirectory);
|
|
||||||
File[] files = folder.listFiles();
|
|
||||||
/* Add non-recursively files's names to fileList */
|
|
||||||
for (int i = 0; i < files.length; i++) {
|
|
||||||
if (files[i].isFile()) {
|
|
||||||
fileList.add(files[i].getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Check server'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 != msgProtocolID) {
|
|
||||||
throw ProtocolError;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Prepare the data to be send if a file is requested
|
|
||||||
* @param filename name of the file to be send
|
|
||||||
* @return data to be send
|
|
||||||
* @throws NotFound
|
|
||||||
* @throws InternalError
|
|
||||||
*/
|
|
||||||
private String upload(String filename) throws NotFound, InternalError {
|
|
||||||
File file = new File(filename);
|
|
||||||
if (!file.exists() || !file.isFile()) {
|
|
||||||
throw NotFound;
|
|
||||||
}
|
|
||||||
String res = "LOAD " + file.length(); + "\n"
|
|
||||||
try {
|
|
||||||
res += new String(Files.readAllBytes(Paths.get(filename)));
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw InternalError;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Prepare the data to be send if file list is requested
|
|
||||||
* @return data to be send
|
|
||||||
*/
|
|
||||||
private String sendFileList() {
|
|
||||||
String res = "LIST\n";
|
|
||||||
for (String f : fileList) {
|
|
||||||
res += (f + '\n');
|
|
||||||
}
|
|
||||||
return res + '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Prepare data to be send if protocol error is detected
|
|
||||||
* @return data to be send
|
|
||||||
*/
|
|
||||||
private String sendProtocolError() {
|
|
||||||
return "PROTOCOL ERROR\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Prepare data to be send if file is not found
|
|
||||||
* @return data to be send
|
|
||||||
*/
|
|
||||||
private String sendNotFound() {
|
|
||||||
return "NOT FOUND\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Prepare data to be send if internal error encounterred
|
|
||||||
* @return data to be send
|
|
||||||
*/
|
|
||||||
private String sendInternalError() {
|
|
||||||
return "INTERNAL ERROR\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user