From a326c33124544c48ac72c9e5b34179c855936f89 Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 14 Jan 2020 10:32:55 +0100 Subject: [PATCH] fix syntax in serverP2P --- src/exception/InternalError.class | Bin 0 -> 213 bytes src/exception/NotFound.class | Bin 0 -> 203 bytes src/exception/ProtocolError.class | Bin 0 -> 213 bytes src/serverP2P/ServerManagementUDP.class | Bin 0 -> 3973 bytes src/serverP2P/ServerManagementUDP.java | 175 ++++++++++++++++++++++++ src/serverP2P/ServerP2P.class | Bin 0 -> 1480 bytes src/serverP2P/ServerP2P.java | 39 ++++++ 7 files changed, 214 insertions(+) create mode 100644 src/exception/InternalError.class create mode 100644 src/exception/NotFound.class create mode 100644 src/exception/ProtocolError.class create mode 100644 src/serverP2P/ServerManagementUDP.class create mode 100644 src/serverP2P/ServerManagementUDP.java create mode 100644 src/serverP2P/ServerP2P.class create mode 100644 src/serverP2P/ServerP2P.java diff --git a/src/exception/InternalError.class b/src/exception/InternalError.class new file mode 100644 index 0000000000000000000000000000000000000000..90c81e92c94dd5c44f78aef8852685f973f2391b GIT binary patch literal 213 zcmX^0Z`VEs1_l!bUM>b^1}=66ZgvJ9Mg}&U%)HDJJ4Oa(4b3n{1{UZ1lvG9rexJ;| zRKL>Pq|~C2#H1Xc2v=}^X;E^jTPBDjN1nDYDk!v zDmJyw7iV!30c<#gp}c4ES}CKWzfBl5fv6Dr6m9=DCg|`B%@8k_8C_=MmIKcG33{}) O$LO*Qt`NHI?fn4swI|vD literal 0 HcmV?d00001 diff --git a/src/exception/ProtocolError.class b/src/exception/ProtocolError.class new file mode 100644 index 0000000000000000000000000000000000000000..ab341e7cc91cbdf461e98c54e5161a85350dd7a3 GIT binary patch literal 213 zcmX^0Z`VEs1_l!bUM>b^1}=66ZgvJ9Mg}&U%)HDJJ4Oa(4b3n{1{UZ1lvG9rexJ;| zRKL>Pq|~C2#H1Xc2v=}^X;E^jTPBDj6i}33lAoNP<62adU!<3nSeD4cz{0@F$RM6t zk(^pkl9`{UkD`W=K^UY;KPNFSUEdX^hCz{m3FsIQU<5*-2|$t!$dd*085mf#wlgqp V1WU66Nj9J`15gVi0|%Jq1OU|KEJ6SP literal 0 HcmV?d00001 diff --git a/src/serverP2P/ServerManagementUDP.class b/src/serverP2P/ServerManagementUDP.class new file mode 100644 index 0000000000000000000000000000000000000000..da550c6b95102e326c3259e0dd5310b31fec014e GIT binary patch literal 3973 zcmaJ^d301~75}}=WZvZQl8^zy-Ug@;vJe_D&;+U3XcH$hX%d2gTKkebl7Zn(I`f7_ ztxG|KwyxczDk_Spw$)I}gpv|y>(**rDkxf)y6+y(>G2PHwDEV}yveQ%bHC-@@2d8dkZY4G8IEbi1u-mT(28Z^9D#rrhqxJwp? zRUDDU`-L3UkdF_D>@k@gm%Y1%+#{m*s`#La5Bc$7KRzP-kBa2SWOkn)_sjTz9}lW{ zNW)4T5b|-6I3cr#W%dabpVUx{11dfx>ys)zE#qfYd{)L&Dn6%SJw7kww2&_d8I(94 zk=YkDtdm&3B#TEy?_(Ms$CovH1w-P%85K`xC=;2l%Bd&k;4D%?hDG|EA5Zyl-j5L# zPpi0~z`r{ZHG`3atsoHGW9&C7l6E9o(P{SBv3QMwoc2hcW!TBMsbKXCfi3BssA2V0 zwA=BB)mO8XhjP1(gxL^@J4z0XYo08oK;7QZ+8T?~u+pSJ8;Hm3SWhh4)WGxgF)Lvk zmfdMYlQitx60suoRt1H{GYvMa?Ns2ckM&Y8KNzvhP;y_l8SgNQ2<5ih zM$g_BW5D4N4Jt-iP?l9e9_{y-iA0-uN79sRte(Zx+L_h~9~Bo_PnMR|WFZyk;_Ws! zuPmvGOLVQL-|X4jIw{Hpvss{~h`B%~F$D407P zS~{BX79D+}YN5llOuM4Nu#LXBv9CSWv)8o6(zCO%TUo|5`#Bw7!`CzVCjm^yH}Fjz z-@@~v=8{(D7p- z`*1s@%?vmyrWLq&B97X4+&I)^^_mC6Njto|PD-8_4^BrM15-3j!GR82*ntT{8Pw=V z2uF7eMPd~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2RG1E;&}nP;u>$wAAwIy8K8PFekmGcobdos^`0jX(T_{VQajsiSIm&(Z s1R6zIx}*A>t&4K*z$d}&?!JDQFK1PwG 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(buffer, buffer.length); + // wait and receive datagram + socket.receive(dgram); + // extract data + String str = new String(dgram.getData(), 0, dgram.getLength()); + // process request + str = processRequest(str); + dgram.setData(str.getBytes()); + dgram.setLength(str.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]) { + case "LIST": + res += sendFileList(); + break; + case "DOWNLOAD": + res += upload(formattedRequest[2]); + break; + default: + throw new ProtocolError(); + } + } catch (java.lang.ArrayIndexOutOfBoundsException e) { + throw new ProtocolError(); + } + } catch (ProtocolError e) { + // wrong version or wrong implementation + res += sendProtocolError(); + } catch (InternalError e) { + res += sendInternalError(); + } catch (NotFound e) { + res += sendNotFound(); + } + return res; + } + + /** 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 new 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 new NotFound(); + } + String res = "LOAD " + file.length() + "\n"; + try { + res += new String(Files.readAllBytes(Paths.get(filename))); + } catch (IOException e) { + throw new 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"; + } + +} diff --git a/src/serverP2P/ServerP2P.class b/src/serverP2P/ServerP2P.class new file mode 100644 index 0000000000000000000000000000000000000000..aa96009f76e95a0a2105943677aecb57f41c0647 GIT binary patch literal 1480 zcmZuxU31e`5Ixs6mZB(M3W<|IXu_ux#}>GhwuI7{BtU{48`}g7ZC%B_I09R8WjRx( zGyDkN$}seiXJ%TPHZz?*^?{$nblUFq2crZ=ld&%0 zx`GMZ5aye;cxQyUEg{PgsN0Uqz&02L%BI6huI)Tw z(2|dgJwvY=Rz=Ubj%ihv8G=_#%XF_Y_`?g^v~<-jbB2+mY4NRQZHGH~W2Z`*vOp9SM{NO`u0jK2brc>hAO#v~SBp!&MaVJ^cy)X5G?4C9S{fBO4Q8DgKl z3qQ>*)J2#hN>M>6+M}f6p{QL!_`urq&Li6K1FqbYhRbUVgB9*(9J|gPm-KKp{EIKQxVMgJHtEqFZNq;XWl0F1Pj=#=;9-fTn9!by5o{Y^XD@zJr8D$%l&fp*+QV zF2XkIb#BpkqTb>?{r0MPB;DIzsw=y8H;dtDpOz<)G7hxUMd6H1HFkT-Oy8+wkJ zeGC`c7>Tqns*RCp(OvKgvtIZ3dv{P9PkJLqdy#!i6e4X*wh+4ex?I75l@Y)I z#Pt=z2uXSrag1RF8d4ZXneMQkFpZ};hG%q3y~GJbXw@_V|52h(!jgn2aN!*${}G>2 z0*gWK&V){XfF59JA5+xEG_jj0M1I9F`uW>9p4!K3VX2K1TT$(k7UrTY%nO1Hg5V5- ZWao%tcMs-i{^w{1B1DtnJjHyt@IR6iQdj^0 literal 0 HcmV?d00001 diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java new file mode 100644 index 0000000..33338e3 --- /dev/null +++ b/src/serverP2P/ServerP2P.java @@ -0,0 +1,39 @@ +package serverP2P; +import java.io.File; +import serverP2P.ServerManagementUDP; + +public class ServerP2P { + private int port; + private String directory; + public ServerP2P() { + 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 || d.equals("")) { + d = System.getenv().get("HOME"); + if (d != null && (!d.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) { + ServerP2P s = new ServerP2P(); + ServerManagementUDP sm = new ServerManagementUDP(s.directory, s.port); + Thread t = new Thread(sm); + t.setName("server P2P-JAVA-PROJECT"); + t.start(); + } + +}