diff --git a/src/clientP2P/ClientManagementUDP.class b/src/clientP2P/ClientManagementUDP.class new file mode 100644 index 0000000..e4928ec Binary files /dev/null and b/src/clientP2P/ClientManagementUDP.class differ diff --git a/src/client/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java similarity index 67% rename from src/client/ClientManagementUDP.java rename to src/clientP2P/ClientManagementUDP.java index 6dc1a4f..63d4374 100644 --- a/src/client/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -1,14 +1,16 @@ -package client; +package clientP2P; import exception.NotFound; import exception.ProtocolError; import exception.InternalError; import exception.TransmissionError; -import java.utils.Scanner; +import java.util.Scanner; import java.net.InetAddress; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.io.File; import java.io.IOException; +import java.io.FileWriter; + /** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP. * @author Louis Royer @@ -16,7 +18,7 @@ import java.io.IOException; * @author JS Auge * @version 1.0 */ -public class ClientManagmentUDP implements Runnable { +public class ClientManagementUDP implements Runnable { private String baseDirectory; private int UDPPort; private String host; @@ -27,7 +29,7 @@ public class ClientManagmentUDP implements Runnable { * @param baseDirectory the root directory where files are stored * @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.host = host; this.UDPPort = UDPPort; @@ -38,13 +40,15 @@ public class ClientManagmentUDP implements Runnable { public void run() { System.out.println("Files present on the server:"); try { - String msg = sendMsg(sendFileList()); + String msg = sendMsg(sendListDirectoryRequest()); System.out.println(listDirectory(msg)); System.out.println("Name of the file to download:"); Scanner scanner = new Scanner(System.in); - f = scanner.nextLine(); + String f = scanner.nextLine(); msg = sendMsg(sendDownloadRequest(f)); download(msg, f); + } catch (TransmissionError e) { + System.out.println("TransmissionError"); } catch (ProtocolError e) { System.out.println("Protocol error"); } catch (NotFound e) { @@ -70,26 +74,26 @@ public class ClientManagmentUDP implements Runnable { * @throws ProtocolError * @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 { - String r[] = r.split('\n', 3); + String r[] = response.split("\n", 3); checkProtocolID(r[0]); - String r2[] = r[1].split(' '); + String r2[] = r[1].split(" "); if (r2[0] != "LOAD") { - throw ProtocolError; + throw new ProtocolError(); } int size = Integer.parseInt(r2[1]); 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.close(); - + } catch (java.lang.ArrayIndexOutOfBoundsException e) { - throw ProtocolError; - } catch (ParseException e) { - throw ProtocolError; + throw new ProtocolError(); + } catch (NumberFormatException e) { + throw new ProtocolError(); } } @@ -107,14 +111,14 @@ public class ClientManagmentUDP implements Runnable { */ private String listDirectory(String response) throws ProtocolError { try { - String r[] = r.split('\n'); + String r[] = response.split("\n"); checkProtocolID(r[0]); - return r.split(protocolID + "\nLOAD \n")[1]; + return response.split(protocolID + "\nLOAD \n")[1]; } catch (java.lang.ArrayIndexOutOfBoundsException e) { - throw ProtocolError; + throw new ProtocolError(); } } - + /** Check client's protocol identifier matches message's protocol identifier. * Throws a ProtocolError if mismatched. * @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 { if (protocolID != msgProtocolID) { - throw ProtocolError; + throw new ProtocolError(); } } @@ -130,11 +134,20 @@ public class ClientManagmentUDP implements Runnable { * @param msg message to be send * @return server's response */ - private String sendMsg(String msg) { - InetAddress dst = InetAddress.getByName(host); - byte [] bytesString = msg.getBytes(); - DatagramPacket emission = new DatagramPacket(bytesString, bytesString.length, dst, UDPPort); - socket.send(emission); - socket.receive(reception); - return new String(reception.getData(), 0, reception.getLength()); + private String sendMsg(String msg) throws TransmissionError { + //TODO changer le gros try catch + try{ + InetAddress dst = InetAddress.getByName(host); + byte [] buffer = msg.getBytes(); + DatagramSocket socket = new DatagramSocket(); + 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(); + } + + } } diff --git a/src/clientP2P/ClientP2P.class b/src/clientP2P/ClientP2P.class new file mode 100644 index 0000000..c0f85c6 Binary files /dev/null and b/src/clientP2P/ClientP2P.class differ diff --git a/src/client/Client.java b/src/clientP2P/ClientP2P.java similarity index 75% rename from src/client/Client.java rename to src/clientP2P/ClientP2P.java index 3d60b9d..e1a32c5 100644 --- a/src/client/Client.java +++ b/src/clientP2P/ClientP2P.java @@ -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 int port; private String directory; - public Client() { + public ClientP2P() { host = "localhost"; port = 40000; String d; @@ -13,12 +15,12 @@ public class Client { */ if (System.getProperty("os.name").equals("Linux")) { d = System.getenv().get("XDG_DATA_HOME"); - if (d == null || f.equals("")) { + if (d == null || d.equals("")) { d = System.getenv().get("HOME"); - if (d != null && (!f.equals(""))) { + if (d != null && (!d.equals(""))) { d += "/.local/share"; } else { - d += "." + d += "."; } } } else { @@ -28,9 +30,9 @@ public class Client { // create directory if not already exists new File(d).mkdirs(); } - } + 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); Thread t = new Thread(cm); t.setName("client P2P-JAVA-PROJECT"); diff --git a/src/exception/TransmissionError.class b/src/exception/TransmissionError.class new file mode 100644 index 0000000..ec43b93 Binary files /dev/null and b/src/exception/TransmissionError.class differ diff --git a/src/exception/TransmissionError.java b/src/exception/TransmissionError.java index 67a01c7..5c64987 100644 --- a/src/exception/TransmissionError.java +++ b/src/exception/TransmissionError.java @@ -1,2 +1,2 @@ package exception; -public class TransmissionError extends Exceptions {} +public class TransmissionError extends Exception {} diff --git a/src/server/Server.java b/src/server/Server.java deleted file mode 100644 index 4c6c533..0000000 --- a/src/server/Server.java +++ /dev/null @@ -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(); - } - -} diff --git a/src/server/ServerManagementUDP.java b/src/server/ServerManagementUDP.java deleted file mode 100644 index 1bddbd0..0000000 --- a/src/server/ServerManagementUDP.java +++ /dev/null @@ -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 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; - 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" - } - -}