Merge branch 'logs' of flavien/Projet_JAVA_P2P_STRI2A into master
This commit is contained in:
commit
5a54663a7a
@ -13,7 +13,7 @@ public class ClientP2P {
|
|||||||
host = "localhost";
|
host = "localhost";
|
||||||
port = 40001;
|
port = 40001;
|
||||||
System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory());
|
System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory());
|
||||||
directories.askOpenDataHomeDirectory();
|
directories.askOpenDataHomeDirectory(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String [] args) {
|
public static void main(String [] args) {
|
||||||
|
@ -62,8 +62,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
|||||||
outputStream.write(packet);
|
outputStream.write(packet);
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// closing socket
|
// Error: cannot send response, closing socket
|
||||||
System.err.println("Error: cannot send response, closing socket");
|
|
||||||
try {
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
} catch (IOException e2) {
|
} catch (IOException e2) {
|
||||||
@ -112,7 +111,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
|||||||
try {
|
try {
|
||||||
System.err.println("Reading " + ss.getInputStream().read(packet) + " bytes");
|
System.err.println("Reading " + ss.getInputStream().read(packet) + " bytes");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error: cannot read request, closing socket");
|
// Error: cannot read request, closing socket
|
||||||
try {
|
try {
|
||||||
ss.close();
|
ss.close();
|
||||||
} catch (IOException e2) {
|
} catch (IOException e2) {
|
||||||
|
@ -28,6 +28,8 @@ import remoteException.ProtocolRemoteError;
|
|||||||
import remoteException.VersionRemoteError;
|
import remoteException.VersionRemoteError;
|
||||||
import remoteException.EmptyFile;
|
import remoteException.EmptyFile;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import tools.Logger;
|
||||||
|
import tools.LogLevel;
|
||||||
|
|
||||||
|
|
||||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for TCP.
|
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for TCP.
|
||||||
@ -42,22 +44,24 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
private String baseDirectory;
|
private String baseDirectory;
|
||||||
private int TCPPort;
|
private int TCPPort;
|
||||||
private ServerSocket socket;
|
private ServerSocket socket;
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
/** Constructor for TCP implementation, with baseDirectory and TCPPort parameters.
|
/** Constructor for TCP implementation, with baseDirectory and TCPPort parameters.
|
||||||
* @param baseDirectory the root directory where files are stored
|
* @param baseDirectory the root directory where files are stored
|
||||||
* @param TCPPort the server will listen on this port
|
* @param TCPPort the server will listen on this port
|
||||||
*/
|
*/
|
||||||
public ServerManagementTCP(String baseDirectory, int TCPPort) {
|
public ServerManagementTCP(String baseDirectory, int TCPPort, Logger logger) {
|
||||||
|
this.logger = logger;
|
||||||
this.baseDirectory = baseDirectory;
|
this.baseDirectory = baseDirectory;
|
||||||
this.TCPPort = TCPPort;
|
this.TCPPort = TCPPort;
|
||||||
initFileList();
|
initFileList();
|
||||||
try {
|
try {
|
||||||
socket = new ServerSocket(TCPPort);
|
socket = new ServerSocket(TCPPort);
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
System.err.println("Error: cannot listen on port " + TCPPort + " (TCP)");
|
logger.writeTCP("Error: cannot listen on port " + TCPPort, LogLevel.Error);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error: cannot openning TCP socket");
|
logger.writeTCP("Error: cannot openning socket", LogLevel.Error);
|
||||||
System.exit(-2);
|
System.exit(-2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,14 +69,14 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
/** Implementation of runnable. This methods allows to run the server.
|
/** Implementation of runnable. This methods allows to run the server.
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
|
logger.writeTCP("Server sucessfully started", LogLevel.Info);
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
Socket s = socket.accept();
|
Socket s = socket.accept();
|
||||||
System.err.println("Accepting new connection");
|
|
||||||
ClientHandler c = new ClientHandler(s);
|
ClientHandler c = new ClientHandler(s);
|
||||||
(new Thread(c)).start();
|
(new Thread(c)).start();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error while accepting new connection");
|
logger.writeTCP("Error while accepting new connection", LogLevel.Warning);
|
||||||
}
|
}
|
||||||
} while(true);
|
} while(true);
|
||||||
}
|
}
|
||||||
@ -80,22 +84,60 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
/** Private runnable class allowing to serve one client.
|
/** Private runnable class allowing to serve one client.
|
||||||
*/
|
*/
|
||||||
private class ClientHandler implements Runnable {
|
private class ClientHandler implements Runnable {
|
||||||
Socket s;
|
private Socket s;
|
||||||
|
private String addr;
|
||||||
/** Constructor with a socket.
|
/** Constructor with a socket.
|
||||||
* @param s Socket of this client
|
* @param s Socket of this client
|
||||||
*/
|
*/
|
||||||
public ClientHandler(Socket s) {
|
public ClientHandler(Socket s) {
|
||||||
this.s = s;
|
this.s = s;
|
||||||
|
this.addr = "[" +s.getInetAddress().getHostAddress() + "]:" + s.getPort() + " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Implementation of runnable. This method allow to serve one client.
|
/** Implementation of runnable. This method allow to serve one client.
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
boolean end = false;
|
boolean end = false;
|
||||||
|
logger.writeTCP(addr + "New connection", LogLevel.Action);
|
||||||
do {
|
do {
|
||||||
end = handleRequest(s);
|
end = handleRequest();
|
||||||
} while(!end);
|
} while(!end);
|
||||||
System.err.println("End of thread");
|
logger.writeTCP(addr + "End of connection", LogLevel.Action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Respond to next request incomming on socket s.
|
||||||
|
* @param s Socket used to read request and send response
|
||||||
|
* @return true if cannot expect another request (ie, socket is closed)
|
||||||
|
*/
|
||||||
|
private boolean handleRequest() {
|
||||||
|
try {
|
||||||
|
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s);
|
||||||
|
Payload p = pd.getPayload();
|
||||||
|
switch (p.getRequestResponseCode()) {
|
||||||
|
case LOAD_REQUEST:
|
||||||
|
logger.writeTCP(addr + "LOAD_REQUEST", LogLevel.Action);
|
||||||
|
sendLoadResponse(pd);
|
||||||
|
break;
|
||||||
|
case LIST_REQUEST:
|
||||||
|
logger.writeTCP(addr + "LIST_REQUEST", LogLevel.Action);
|
||||||
|
sendListResponse(pd);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
logger.writeTCP(addr + "Received grabbage", LogLevel.Action);
|
||||||
|
sendInternalError(pd);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
return true;
|
||||||
|
} catch (SocketClosed e) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (TransmissionError e) {}
|
||||||
|
catch (ProtocolError e) {}
|
||||||
|
catch (VersionError e) {}
|
||||||
|
catch (InternalError e) {}
|
||||||
|
catch (SizeError e) {}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,10 +162,11 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
* @param pd ProtocolP2PPacketTCP to respond
|
* @param pd ProtocolP2PPacketTCP to respond
|
||||||
*/
|
*/
|
||||||
private void sendInternalError(ProtocolP2PPacketTCP pd) {
|
private void sendInternalError(ProtocolP2PPacketTCP pd) {
|
||||||
|
logger.writeTCP("Internal Error", LogLevel.Warning);
|
||||||
try {
|
try {
|
||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(e);
|
logger.writeTCP(e, LogLevel.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,14 +176,14 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
private void sendListResponse(ProtocolP2PPacketTCP pd) {
|
private void sendListResponse(ProtocolP2PPacketTCP pd) {
|
||||||
try {
|
try {
|
||||||
if (fileList.length == 0) {
|
if (fileList.length == 0) {
|
||||||
System.err.println("Sending EMPTY_DIRECTORY");
|
logger.writeTCP("Sending EMPTY_DIRECTORY", LogLevel.Action);
|
||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Sending LIST_RESPONSE");
|
logger.writeTCP("Sending LIST_RESPONSE", LogLevel.Action);
|
||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)(new FileList(fileList))));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)(new FileList(fileList))));
|
||||||
}
|
}
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
System.err.println(e2);
|
logger.writeTCP(e2, LogLevel.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,13 +203,13 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
byte[] fullLoad = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
byte[] fullLoad = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
||||||
long sizeToSend = 0;
|
long sizeToSend = 0;
|
||||||
if (fullLoad.length - offset < maxSizePartialContent) {
|
if (fullLoad.length - offset < maxSizePartialContent) {
|
||||||
System.out.println("Sending last partialContent");
|
logger.writeTCP("Sending last partialContent", LogLevel.Debug);
|
||||||
sizeToSend = fullLoad.length - offset;
|
sizeToSend = fullLoad.length - offset;
|
||||||
} else {
|
} else {
|
||||||
sizeToSend = maxSizePartialContent;
|
sizeToSend = maxSizePartialContent;
|
||||||
}
|
}
|
||||||
System.out.println("maxSizePartialContent: " + maxSizePartialContent);
|
logger.writeTCP("maxSizePartialContent: " + maxSizePartialContent, LogLevel.Debug);
|
||||||
System.out.println("Sending " + filename + " from " + offset + " to " + (offset + sizeToSend));
|
logger.writeTCP("Sending " + filename + " from " + offset + " to " + (offset + sizeToSend), LogLevel.Debug);
|
||||||
byte[] load = Arrays.copyOfRange(fullLoad, (int)offset, (int)(offset + sizeToSend));
|
byte[] load = Arrays.copyOfRange(fullLoad, (int)offset, (int)(offset + sizeToSend));
|
||||||
if (Arrays.binarySearch(fileList, filename) >= 0) {
|
if (Arrays.binarySearch(fileList, filename) >= 0) {
|
||||||
try {
|
try {
|
||||||
@ -176,13 +219,13 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)(new FilePart(filename, fullLoad.length, offset, load))));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)(new FilePart(filename, fullLoad.length, offset, load))));
|
||||||
}
|
}
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
System.err.println(e2);
|
logger.writeTCP(e2, LogLevel.Error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.err.println("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename));
|
logger.writeTCP("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename), LogLevel.Debug);
|
||||||
System.err.println("File list:");
|
logger.writeTCP("File list:", LogLevel.Debug);
|
||||||
for (String f: fileList) {
|
for (String f: fileList) {
|
||||||
System.err.println("- " + f);
|
logger.writeTCP("- " + f, LogLevel.Debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IOException(); // to send a NOT_FOUND in the catch block
|
throw new IOException(); // to send a NOT_FOUND in the catch block
|
||||||
@ -191,43 +234,10 @@ public class ServerManagementTCP implements Runnable {
|
|||||||
try {
|
try {
|
||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.NOT_FOUND)));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.NOT_FOUND)));
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
System.err.println(e2);
|
logger.writeTCP(e2, LogLevel.Debug);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Respond to next request incomming on socket s.
|
|
||||||
* @param s Socket used to read request and send response
|
|
||||||
* @return true if cannot expect another request (ie, socket is closed)
|
|
||||||
*/
|
|
||||||
private boolean handleRequest(Socket s) {
|
|
||||||
try {
|
|
||||||
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s);
|
|
||||||
Payload p = pd.getPayload();
|
|
||||||
switch (p.getRequestResponseCode()) {
|
|
||||||
case LOAD_REQUEST:
|
|
||||||
System.out.println("Received LOAD_REQUEST");
|
|
||||||
sendLoadResponse(pd);
|
|
||||||
break;
|
|
||||||
case LIST_REQUEST:
|
|
||||||
System.out.println("Received LIST_REQUEST");
|
|
||||||
sendListResponse(pd);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
sendInternalError(pd);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
return true;
|
|
||||||
} catch (SocketClosed e) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (TransmissionError e) {}
|
|
||||||
catch (ProtocolError e) {}
|
|
||||||
catch (VersionError e) {}
|
|
||||||
catch (InternalError e) {}
|
|
||||||
catch (SizeError e) {}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,8 @@ import remoteException.ProtocolRemoteError;
|
|||||||
import remoteException.VersionRemoteError;
|
import remoteException.VersionRemoteError;
|
||||||
import remoteException.EmptyFile;
|
import remoteException.EmptyFile;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import tools.Logger;
|
||||||
|
import tools.LogLevel;
|
||||||
|
|
||||||
|
|
||||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
||||||
@ -41,19 +43,21 @@ public class ServerManagementUDP implements Runnable {
|
|||||||
private String baseDirectory;
|
private String baseDirectory;
|
||||||
private int UDPPort;
|
private int UDPPort;
|
||||||
private DatagramSocket socket;
|
private DatagramSocket socket;
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
/** Constructor for UDP implementation, with baseDirectory and UDPPort parameters.
|
/** Constructor for UDP implementation, with baseDirectory and UDPPort parameters.
|
||||||
* @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 ServerManagementUDP(String baseDirectory, int UDPPort) {
|
public ServerManagementUDP(String baseDirectory, int UDPPort, Logger logger) {
|
||||||
|
this.logger = logger;
|
||||||
this.baseDirectory = baseDirectory;
|
this.baseDirectory = baseDirectory;
|
||||||
this.UDPPort = UDPPort;
|
this.UDPPort = UDPPort;
|
||||||
initFileList();
|
initFileList();
|
||||||
try {
|
try {
|
||||||
socket = new DatagramSocket(UDPPort);
|
socket = new DatagramSocket(UDPPort);
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
System.err.println("Error: cannot listen on port " + UDPPort + " (UDP)");
|
logger.writeUDP("Error: cannot listen on port " + UDPPort, LogLevel.Error);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,13 +65,14 @@ public class ServerManagementUDP implements Runnable {
|
|||||||
/** Implementation of runnable. This methods allows to run the server.
|
/** Implementation of runnable. This methods allows to run the server.
|
||||||
*/
|
*/
|
||||||
public void run() {
|
public void run() {
|
||||||
|
logger.writeUDP("Server sucessfully started", LogLevel.Info);
|
||||||
while(true) {
|
while(true) {
|
||||||
try {
|
try {
|
||||||
ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket);
|
ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket);
|
||||||
Payload p = pd.getPayload();
|
Payload p = pd.getPayload();
|
||||||
switch (p.getRequestResponseCode()) {
|
switch (p.getRequestResponseCode()) {
|
||||||
case LOAD_REQUEST:
|
case LOAD_REQUEST:
|
||||||
System.out.println("Received LOAD_REQUEST");
|
logger.writeUDP("Received LOAD_REQUEST", LogLevel.Action);
|
||||||
assert p instanceof LoadRequest : "payload must be an instance of LoadRequest";
|
assert p instanceof LoadRequest : "payload must be an instance of LoadRequest";
|
||||||
if (!(p instanceof LoadRequest)) {
|
if (!(p instanceof LoadRequest)) {
|
||||||
sendInternalError(pd);
|
sendInternalError(pd);
|
||||||
@ -79,13 +84,13 @@ public class ServerManagementUDP implements Runnable {
|
|||||||
byte[] fullLoad = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
byte[] fullLoad = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
||||||
long sizeToSend = 0;
|
long sizeToSend = 0;
|
||||||
if (fullLoad.length - offset < maxSizePartialContent) {
|
if (fullLoad.length - offset < maxSizePartialContent) {
|
||||||
System.out.println("Sending last partialContent");
|
logger.writeUDP("Sending last partialContent", LogLevel.Debug);
|
||||||
sizeToSend = fullLoad.length - offset;
|
sizeToSend = fullLoad.length - offset;
|
||||||
} else {
|
} else {
|
||||||
sizeToSend = maxSizePartialContent;
|
sizeToSend = maxSizePartialContent;
|
||||||
}
|
}
|
||||||
System.out.println("maxSizePartialContent: " + maxSizePartialContent);
|
logger.writeUDP("maxSizePartialContent: " + maxSizePartialContent, LogLevel.Debug);
|
||||||
System.out.println("Sending " + filename + " from " + offset + " to " + (offset + sizeToSend));
|
logger.writeUDP("Sending " + filename + " from " + offset + " to " + (offset + sizeToSend), LogLevel.Debug);
|
||||||
byte[] load = Arrays.copyOfRange(fullLoad, (int)offset, (int)(offset + sizeToSend));
|
byte[] load = Arrays.copyOfRange(fullLoad, (int)offset, (int)(offset + sizeToSend));
|
||||||
if (Arrays.binarySearch(fileList, filename) >= 0) {
|
if (Arrays.binarySearch(fileList, filename) >= 0) {
|
||||||
try {
|
try {
|
||||||
@ -95,13 +100,13 @@ public class ServerManagementUDP implements Runnable {
|
|||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FilePart(filename, fullLoad.length, offset, load))));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FilePart(filename, fullLoad.length, offset, load))));
|
||||||
}
|
}
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
System.err.println(e2);
|
logger.writeUDP(e2, LogLevel.Error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.err.println("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename));
|
logger.writeUDP("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename), LogLevel.Debug);
|
||||||
System.err.println("File list:");
|
logger.writeUDP("File list:", LogLevel.Debug);
|
||||||
for (String f: fileList) {
|
for (String f: fileList) {
|
||||||
System.err.println("- " + f);
|
logger.writeUDP("- " + f, LogLevel.Debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IOException(); // to send a NOT_FOUND in the catch block
|
throw new IOException(); // to send a NOT_FOUND in the catch block
|
||||||
@ -110,23 +115,23 @@ public class ServerManagementUDP implements Runnable {
|
|||||||
try {
|
try {
|
||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.NOT_FOUND)));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.NOT_FOUND)));
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
System.err.println(e2);
|
logger.writeUDP(e2, LogLevel.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LIST_REQUEST:
|
case LIST_REQUEST:
|
||||||
System.out.println("Received LIST_REQUEST");
|
logger.writeUDP("Received LIST_REQUEST", LogLevel.Action);
|
||||||
try {
|
try {
|
||||||
if (fileList.length == 0) {
|
if (fileList.length == 0) {
|
||||||
System.err.println("Sending EMPTY_DIRECTORY");
|
logger.writeUDP("Sending EMPTY_DIRECTORY", LogLevel.Action);
|
||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Sending LIST_RESPONSE");
|
logger.writeUDP("Sending LIST_RESPONSE", LogLevel.Action);
|
||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FileList(fileList))));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FileList(fileList))));
|
||||||
}
|
}
|
||||||
} catch (Exception e2) {
|
} catch (Exception e2) {
|
||||||
System.err.println(e2);
|
logger.writeUDP(e2, LogLevel.Error);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -166,10 +171,11 @@ public class ServerManagementUDP implements Runnable {
|
|||||||
* @param pd ProtocolP2PPacketUDP to respond
|
* @param pd ProtocolP2PPacketUDP to respond
|
||||||
*/
|
*/
|
||||||
private void sendInternalError(ProtocolP2PPacketUDP pd) {
|
private void sendInternalError(ProtocolP2PPacketUDP pd) {
|
||||||
|
logger.writeUDP("Internal Error", LogLevel.Warning);
|
||||||
try {
|
try {
|
||||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(e);
|
logger.writeUDP(e, LogLevel.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,28 +2,32 @@ package serverP2P;
|
|||||||
import serverP2P.ServerManagementUDP;
|
import serverP2P.ServerManagementUDP;
|
||||||
import serverP2P.ServerManagementTCP;
|
import serverP2P.ServerManagementTCP;
|
||||||
import tools.Directories;
|
import tools.Directories;
|
||||||
|
import tools.Logger;
|
||||||
|
|
||||||
public class ServerP2P {
|
public class ServerP2P {
|
||||||
private int port;
|
private int port;
|
||||||
private Directories directories;
|
private Directories directories;
|
||||||
|
static private final String subdir = "seeded/";
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
|
|
||||||
public ServerP2P() {
|
public ServerP2P() {
|
||||||
directories = new Directories("P2P_JAVA_PROJECT_SERVER");
|
directories = new Directories("P2P_JAVA_PROJECT_SERVER");
|
||||||
|
logger = new Logger(directories.getDataHomeDirectory() + "server.log");
|
||||||
port = 40001;
|
port = 40001;
|
||||||
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory());
|
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir);
|
||||||
directories.askOpenDataHomeDirectory();
|
directories.askOpenDataHomeDirectory(subdir);
|
||||||
}
|
}
|
||||||
public static void main(String [] args) {
|
public static void main(String [] args) {
|
||||||
ServerP2P s = new ServerP2P();
|
ServerP2P s = new ServerP2P();
|
||||||
ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory(), s.port);
|
ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger);
|
||||||
ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory(), s.port);
|
ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger);
|
||||||
Thread tudp = new Thread(smudp);
|
Thread tudp = new Thread(smudp);
|
||||||
tudp.setName("server UDP P2P-JAVA-PROJECT");
|
tudp.setName("server UDP P2P-JAVA-PROJECT");
|
||||||
tudp.start();
|
tudp.start();
|
||||||
Thread ttcp = new Thread(smtcp);
|
Thread ttcp = new Thread(smtcp);
|
||||||
ttcp.setName("server TCP P2P-JAVA-PROJECT");
|
ttcp.setName("server TCP P2P-JAVA-PROJECT");
|
||||||
ttcp.start();
|
ttcp.start();
|
||||||
System.out.println("Server started.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -57,14 +57,21 @@ public class Directories {
|
|||||||
|
|
||||||
/** Opens dataHomeDirectory if supported.
|
/** Opens dataHomeDirectory if supported.
|
||||||
*/
|
*/
|
||||||
private void openDataHomeDirectory() {
|
private void openDataHomeDirectory(String subdir) {
|
||||||
|
String d = dataHomeDirectory;
|
||||||
|
if (subdir != null) {
|
||||||
|
d += subdir;
|
||||||
|
if (!subdir.endsWith("/")) {
|
||||||
|
d += "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if (os.equals("Linux")) {
|
if (os.equals("Linux")) {
|
||||||
Runtime runtime = Runtime.getRuntime();
|
Runtime runtime = Runtime.getRuntime();
|
||||||
runtime.exec(new String[] { "xdg-open", dataHomeDirectory });
|
runtime.exec(new String[] { "xdg-open", d });
|
||||||
} else if (os.equals("Mac")||os.equals("Mac OS X")) {
|
} else if (os.equals("Mac")||os.equals("Mac OS X")) {
|
||||||
Runtime runtime = Runtime.getRuntime();
|
Runtime runtime = Runtime.getRuntime();
|
||||||
runtime.exec(new String[] { "open", dataHomeDirectory });
|
runtime.exec(new String[] { "open", d });
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Error encountered while trying to open directory");
|
System.out.println("Error encountered while trying to open directory");
|
||||||
@ -73,14 +80,14 @@ public class Directories {
|
|||||||
|
|
||||||
/** Asks the user to choose opening dataHomeDirectory or not.
|
/** Asks the user to choose opening dataHomeDirectory or not.
|
||||||
*/
|
*/
|
||||||
public void askOpenDataHomeDirectory() {
|
public void askOpenDataHomeDirectory(String subdir) {
|
||||||
if (os.equals("Linux") || os.equals("Mac") || os.equals("Mac OS X")) {
|
if (os.equals("Linux") || os.equals("Mac") || os.equals("Mac OS X")) {
|
||||||
System.out.println("Do you want to open this directory? (y/N)");
|
System.out.println("Do you want to open this directory? (y/N)");
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
String resp = scanner.nextLine();
|
String resp = scanner.nextLine();
|
||||||
if (resp.equals("y") || resp.equals("Y")) {
|
if (resp.equals("y") || resp.equals("Y")) {
|
||||||
System.out.println("Openning");
|
System.out.println("Openning");
|
||||||
openDataHomeDirectory();
|
openDataHomeDirectory(subdir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
src/tools/LogLevel.java
Normal file
8
src/tools/LogLevel.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package tools;
|
||||||
|
public enum LogLevel {
|
||||||
|
Error,
|
||||||
|
Info,
|
||||||
|
Warning,
|
||||||
|
Action,
|
||||||
|
Debug
|
||||||
|
}
|
121
src/tools/Logger.java
Normal file
121
src/tools/Logger.java
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
package tools;
|
||||||
|
import tools.LogLevel;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
|
||||||
|
/** Helper to log.
|
||||||
|
* @author Louis Royer
|
||||||
|
* @author Flavien Haas
|
||||||
|
* @author JS Auge
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public class Logger {
|
||||||
|
private Path logFile;
|
||||||
|
|
||||||
|
public Logger(String logFile) {
|
||||||
|
assert logFile != null : "Logfile name is null";
|
||||||
|
this.logFile = Paths.get(logFile);
|
||||||
|
try {
|
||||||
|
this.logFile.toFile().createNewFile();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error: cannot initialize logfile");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Appends log to filelog and print to stderr.
|
||||||
|
* @param text Text to log
|
||||||
|
*/
|
||||||
|
private void write(String text, LogLevel logLevel) {
|
||||||
|
String msg = "[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n";
|
||||||
|
String level = null;
|
||||||
|
switch (logLevel) {
|
||||||
|
case Error:
|
||||||
|
level = "[Error]";
|
||||||
|
break;
|
||||||
|
case Info:
|
||||||
|
level = "[Info]";
|
||||||
|
break;
|
||||||
|
case Warning:
|
||||||
|
level = "[Warning]";
|
||||||
|
break;
|
||||||
|
case Action:
|
||||||
|
level = "[Action]";
|
||||||
|
break;
|
||||||
|
case Debug:
|
||||||
|
level = "[Debug]";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.err.println("Error: incorrect logLevel");
|
||||||
|
}
|
||||||
|
if (level != null) {
|
||||||
|
msg = level + " " + msg;
|
||||||
|
}
|
||||||
|
switch (logLevel) {
|
||||||
|
case Error:
|
||||||
|
case Info:
|
||||||
|
case Warning:
|
||||||
|
case Debug:
|
||||||
|
default:
|
||||||
|
System.err.println(text);
|
||||||
|
break;
|
||||||
|
case Action:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
switch (logLevel) {
|
||||||
|
case Debug:
|
||||||
|
break;
|
||||||
|
case Error:
|
||||||
|
case Info:
|
||||||
|
case Warning:
|
||||||
|
default:
|
||||||
|
try {
|
||||||
|
Files.write(logFile, msg.getBytes(), StandardOpenOption.APPEND);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error: cannot write in logfile");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Appends log to filelog and print to stderr.
|
||||||
|
* Adds [TCP] in log line.
|
||||||
|
* @param text Text to log
|
||||||
|
* @param logLevel Type of log
|
||||||
|
*/
|
||||||
|
public void writeTCP(String text, LogLevel logLevel) {
|
||||||
|
write("[TCP] " + text, logLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Appends log to filelog and print to stderr.
|
||||||
|
* Adds [TCP] in log line.
|
||||||
|
* @param text Text to log
|
||||||
|
* @param logLevel Type of log
|
||||||
|
*/
|
||||||
|
public void writeTCP(Exception e, LogLevel logLevel) {
|
||||||
|
writeTCP(e.toString(), logLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Appends log to filelog and print to stderr.
|
||||||
|
* Adds [UDP] in log line.
|
||||||
|
* @param text Text to log
|
||||||
|
* @param logLevel Type of log
|
||||||
|
*/
|
||||||
|
public void writeUDP(String text, LogLevel logLevel) {
|
||||||
|
write("[UDP] " + text, logLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Appends log to filelog and print to stderr.
|
||||||
|
* Adds [UDP] in log line.
|
||||||
|
* @param text Text to log
|
||||||
|
* @param logLevel Type of log
|
||||||
|
*/
|
||||||
|
public void writeUDP(Exception e, LogLevel logLevel) {
|
||||||
|
writeUDP(e.toString(), logLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user