181 lines
4.8 KiB
Java
181 lines
4.8 KiB
Java
package serverP2P;
|
|
import java.util.Vector;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.net.DatagramPacket;
|
|
import java.net.DatagramSocket;
|
|
import exception.ProtocolError;
|
|
import exception.NotFound;
|
|
import exception.InternalError;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Files;
|
|
|
|
|
|
/** 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(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) {
|
|
System.out.print(request);
|
|
String res = protocolID + "\n";
|
|
String formattedRequest[] = request.split("\n");
|
|
try {
|
|
try {
|
|
checkProtocolID(formattedRequest[0]);
|
|
System.out.print("juste avant le switch");
|
|
switch (formattedRequest[1]) {
|
|
case "LIST":
|
|
System.out.print("liste detectee");
|
|
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() {
|
|
System.out.println(baseDirectory);
|
|
File folder = new File(baseDirectory);
|
|
File[] files = folder.listFiles();
|
|
/* Add non-recursively files's names to fileList */
|
|
for (File f : files) {
|
|
if (f.isFile()) {
|
|
fileList.add(f.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 {
|
|
System.out.print(msgProtocolID + "\n" + protocolID);
|
|
if (!protocolID.equals(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";
|
|
}
|
|
|
|
}
|