Implement protocol
TODO : send method
This commit is contained in:
parent
1911c143eb
commit
e7b4d7a5c3
2
src/exception/ProtocolError.java
Normal file
2
src/exception/ProtocolError.java
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
package exception;
|
||||||
|
public class ProtocolError extends Exception {}
|
@ -1,12 +1,39 @@
|
|||||||
package Server;
|
package Server;
|
||||||
|
import java.io.File;
|
||||||
|
import server.ServerManagementUDP;
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
|
private int port;
|
||||||
|
private String directory;
|
||||||
public Server() {
|
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) {
|
public static void main(String [] args) {
|
||||||
Server s = new Server("server");
|
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,34 +0,0 @@
|
|||||||
package server;
|
|
||||||
import exception.NotFound;
|
|
||||||
import java.util.Vector;
|
|
||||||
|
|
||||||
public class ServerManagement {
|
|
||||||
|
|
||||||
private Vector<String> fileList;
|
|
||||||
private String baseDirectory;
|
|
||||||
|
|
||||||
public ServerManagement(String baseDirectory) {
|
|
||||||
this.baseDirectory = baseDirectory;
|
|
||||||
fileList = new Vector<String>;
|
|
||||||
initFileList();
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void upload(String filename) throws NotFound {
|
|
||||||
}
|
|
||||||
|
|
||||||
public String listDirectory() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
151
src/server/ServerManagementUDP.java
Normal file
151
src/server/ServerManagementUDP.java
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
/** 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 (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
|
||||||
|
*/
|
||||||
|
private String upload(String filename) throws NotFound {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user