fixes #18
This commit is contained in:
parent
cb94c15039
commit
e947fc7c46
@ -13,7 +13,7 @@ public class ClientP2P {
|
||||
host = "localhost";
|
||||
port = 40001;
|
||||
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) {
|
||||
|
@ -62,8 +62,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
||||
outputStream.write(packet);
|
||||
outputStream.flush();
|
||||
} catch (IOException e) {
|
||||
// closing socket
|
||||
System.err.println("Error: cannot send response, closing socket");
|
||||
// Error: cannot send response, closing socket
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e2) {
|
||||
@ -112,7 +111,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
||||
try {
|
||||
System.err.println("Reading " + ss.getInputStream().read(packet) + " bytes");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: cannot read request, closing socket");
|
||||
// Error: cannot read request, closing socket
|
||||
try {
|
||||
ss.close();
|
||||
} catch (IOException e2) {
|
||||
|
@ -28,6 +28,7 @@ import remoteException.ProtocolRemoteError;
|
||||
import remoteException.VersionRemoteError;
|
||||
import remoteException.EmptyFile;
|
||||
import java.util.Arrays;
|
||||
import tools.Logger;
|
||||
|
||||
|
||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for TCP.
|
||||
@ -42,12 +43,14 @@ public class ServerManagementTCP implements Runnable {
|
||||
private String baseDirectory;
|
||||
private int TCPPort;
|
||||
private ServerSocket socket;
|
||||
private Logger logger;
|
||||
|
||||
/** Constructor for TCP implementation, with baseDirectory and TCPPort parameters.
|
||||
* @param baseDirectory the root directory where files are stored
|
||||
* @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.TCPPort = TCPPort;
|
||||
initFileList();
|
||||
@ -65,10 +68,10 @@ public class ServerManagementTCP implements Runnable {
|
||||
/** Implementation of runnable. This methods allows to run the server.
|
||||
*/
|
||||
public void run() {
|
||||
logger.writeTCP("Server sucessfully started");
|
||||
do {
|
||||
try {
|
||||
Socket s = socket.accept();
|
||||
System.err.println("Accepting new connection");
|
||||
ClientHandler c = new ClientHandler(s);
|
||||
(new Thread(c)).start();
|
||||
} catch (IOException e) {
|
||||
@ -80,22 +83,60 @@ public class ServerManagementTCP implements Runnable {
|
||||
/** Private runnable class allowing to serve one client.
|
||||
*/
|
||||
private class ClientHandler implements Runnable {
|
||||
Socket s;
|
||||
private Socket s;
|
||||
private String addr;
|
||||
/** Constructor with a socket.
|
||||
* @param s Socket of this client
|
||||
*/
|
||||
public ClientHandler(Socket s) {
|
||||
this.s = s;
|
||||
this.addr = "[" +s.getInetAddress().getHostAddress() + "]:" + s.getPort() + " ";
|
||||
}
|
||||
|
||||
/** Implementation of runnable. This method allow to serve one client.
|
||||
*/
|
||||
public void run() {
|
||||
|
||||
boolean end = false;
|
||||
logger.writeTCP(addr + "New connection");
|
||||
do {
|
||||
end = handleRequest(s);
|
||||
end = handleRequest();
|
||||
} while(!end);
|
||||
System.err.println("End of thread");
|
||||
logger.writeTCP(addr + "End of connection");
|
||||
}
|
||||
|
||||
/** 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");
|
||||
sendLoadResponse(pd);
|
||||
break;
|
||||
case LIST_REQUEST:
|
||||
logger.writeTCP(addr + "LIST_REQUEST");
|
||||
sendListResponse(pd);
|
||||
break;
|
||||
default:
|
||||
logger.writeTCP(addr + "Received grabbage");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -197,37 +238,4 @@ public class ServerManagementTCP implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
/** 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,7 @@ import remoteException.ProtocolRemoteError;
|
||||
import remoteException.VersionRemoteError;
|
||||
import remoteException.EmptyFile;
|
||||
import java.util.Arrays;
|
||||
import tools.Logger;
|
||||
|
||||
|
||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
||||
@ -41,12 +42,14 @@ public class ServerManagementUDP implements Runnable {
|
||||
private String baseDirectory;
|
||||
private int UDPPort;
|
||||
private DatagramSocket socket;
|
||||
private Logger logger;
|
||||
|
||||
/** 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) {
|
||||
public ServerManagementUDP(String baseDirectory, int UDPPort, Logger logger) {
|
||||
this.logger = logger;
|
||||
this.baseDirectory = baseDirectory;
|
||||
this.UDPPort = UDPPort;
|
||||
initFileList();
|
||||
@ -61,6 +64,7 @@ public class ServerManagementUDP implements Runnable {
|
||||
/** Implementation of runnable. This methods allows to run the server.
|
||||
*/
|
||||
public void run() {
|
||||
logger.writeUDP("Server sucessfully started");
|
||||
while(true) {
|
||||
try {
|
||||
ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket);
|
||||
|
@ -2,28 +2,32 @@ package serverP2P;
|
||||
import serverP2P.ServerManagementUDP;
|
||||
import serverP2P.ServerManagementTCP;
|
||||
import tools.Directories;
|
||||
import tools.Logger;
|
||||
|
||||
public class ServerP2P {
|
||||
private int port;
|
||||
private Directories directories;
|
||||
static private final String subdir = "seeded/";
|
||||
private Logger logger;
|
||||
|
||||
|
||||
public ServerP2P() {
|
||||
directories = new Directories("P2P_JAVA_PROJECT_SERVER");
|
||||
logger = new Logger(directories.getDataHomeDirectory() + "server.log");
|
||||
port = 40001;
|
||||
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory());
|
||||
directories.askOpenDataHomeDirectory();
|
||||
System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir);
|
||||
directories.askOpenDataHomeDirectory(subdir);
|
||||
}
|
||||
public static void main(String [] args) {
|
||||
ServerP2P s = new ServerP2P();
|
||||
ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory(), s.port);
|
||||
ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory(), s.port);
|
||||
ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger);
|
||||
ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger);
|
||||
Thread tudp = new Thread(smudp);
|
||||
tudp.setName("server UDP P2P-JAVA-PROJECT");
|
||||
tudp.start();
|
||||
Thread ttcp = new Thread(smtcp);
|
||||
ttcp.setName("server TCP P2P-JAVA-PROJECT");
|
||||
ttcp.start();
|
||||
System.out.println("Server started.");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -57,14 +57,21 @@ public class Directories {
|
||||
|
||||
/** 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 {
|
||||
if (os.equals("Linux")) {
|
||||
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")) {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
runtime.exec(new String[] { "open", dataHomeDirectory });
|
||||
runtime.exec(new String[] { "open", d });
|
||||
}
|
||||
} catch (IOException e) {
|
||||
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.
|
||||
*/
|
||||
public void askOpenDataHomeDirectory() {
|
||||
public void askOpenDataHomeDirectory(String subdir) {
|
||||
if (os.equals("Linux") || os.equals("Mac") || os.equals("Mac OS X")) {
|
||||
System.out.println("Do you want to open this directory? (y/N)");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String resp = scanner.nextLine();
|
||||
if (resp.equals("y") || resp.equals("Y")) {
|
||||
System.out.println("Openning");
|
||||
openDataHomeDirectory();
|
||||
openDataHomeDirectory(subdir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
57
src/tools/Logger.java
Normal file
57
src/tools/Logger.java
Normal file
@ -0,0 +1,57 @@
|
||||
package tools;
|
||||
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) {
|
||||
System.err.println(text);
|
||||
try {
|
||||
Files.write(logFile, ("[" + new Timestamp(System.currentTimeMillis()) + "] " + text + "\n").getBytes(), StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: cannot write in logfile");
|
||||
}
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* Adds [TCP] in log line.
|
||||
* @param text Text to log
|
||||
*/
|
||||
public void writeTCP(String text) {
|
||||
write("[TCP] " + text);
|
||||
}
|
||||
|
||||
/** Appends log to filelog and print to stderr.
|
||||
* Adds [UDP] in log line.
|
||||
* @param text Text to log
|
||||
*/
|
||||
public void writeUDP(String text) {
|
||||
write("[UDP] " + text);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user