|
|
|
@ -20,6 +20,7 @@ import exception.ProtocolError;
|
|
|
|
|
import exception.SizeError;
|
|
|
|
|
import exception.TransmissionError;
|
|
|
|
|
import exception.VersionError;
|
|
|
|
|
import exception.SocketClosed;
|
|
|
|
|
import remoteException.EmptyDirectory;
|
|
|
|
|
import remoteException.InternalRemoteError;
|
|
|
|
|
import remoteException.NotFound;
|
|
|
|
@ -64,17 +65,90 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
/** Implementation of runnable. This methods allows to run the server.
|
|
|
|
|
*/
|
|
|
|
|
public void run() {
|
|
|
|
|
// TODO: handling multiple clients
|
|
|
|
|
do {
|
|
|
|
|
try {
|
|
|
|
|
Socket s = socket.accept();
|
|
|
|
|
System.err.println("Accepting new connection");
|
|
|
|
|
while(true) {
|
|
|
|
|
ClientHandler c = new ClientHandler(s);
|
|
|
|
|
(new Thread(c)).start();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("Error while accepting new connection");
|
|
|
|
|
}
|
|
|
|
|
} while(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Private runnable class allowing to serve one client.
|
|
|
|
|
*/
|
|
|
|
|
private class ClientHandler implements Runnable {
|
|
|
|
|
Socket s;
|
|
|
|
|
/** Constructor with a socket.
|
|
|
|
|
* @param s Socket of this client
|
|
|
|
|
*/
|
|
|
|
|
public ClientHandler(Socket s) {
|
|
|
|
|
this.s = s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Implementation of runnable. This method allow to serve one client.
|
|
|
|
|
*/
|
|
|
|
|
public void run() {
|
|
|
|
|
boolean end = false;
|
|
|
|
|
do {
|
|
|
|
|
end = handleRequest(s);
|
|
|
|
|
} while(!end);
|
|
|
|
|
System.err.println("End of thread");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Initialize local list of all files allowed to be shared.
|
|
|
|
|
*/
|
|
|
|
|
private void initFileList() {
|
|
|
|
|
File folder = new File(baseDirectory);
|
|
|
|
|
Vector<String> v = new Vector<String>();
|
|
|
|
|
File[] files = folder.listFiles();
|
|
|
|
|
/* Add non-recursively files's names to fileList */
|
|
|
|
|
for (File f : files) {
|
|
|
|
|
if (f.isFile()) {
|
|
|
|
|
v.add(f.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fileList = new String[v.size()];
|
|
|
|
|
v.toArray(fileList);
|
|
|
|
|
Arrays.sort(fileList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Send an internal error message.
|
|
|
|
|
* @param pd ProtocolP2PPacketTCP to respond
|
|
|
|
|
*/
|
|
|
|
|
private void sendInternalError(ProtocolP2PPacketTCP pd) {
|
|
|
|
|
try {
|
|
|
|
|
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s);
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Send response to list request
|
|
|
|
|
* @param pd Request received
|
|
|
|
|
*/
|
|
|
|
|
private void sendListResponse(ProtocolP2PPacketTCP pd) {
|
|
|
|
|
try {
|
|
|
|
|
if (fileList.length == 0) {
|
|
|
|
|
System.err.println("Sending EMPTY_DIRECTORY");
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("Sending LIST_RESPONSE");
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)(new FileList(fileList))));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
System.err.println(e2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Send response to load request
|
|
|
|
|
* @param pd Request received
|
|
|
|
|
*/
|
|
|
|
|
private void sendLoadResponse(ProtocolP2PPacketTCP pd) {
|
|
|
|
|
Payload p = pd.getPayload();
|
|
|
|
|
switch (p.getRequestResponseCode()) {
|
|
|
|
|
case LOAD_REQUEST:
|
|
|
|
|
System.out.println("Received LOAD_REQUEST");
|
|
|
|
|
assert p instanceof LoadRequest : "payload must be an instance of LoadRequest";
|
|
|
|
|
if (!(p instanceof LoadRequest)) {
|
|
|
|
|
sendInternalError(pd);
|
|
|
|
@ -121,65 +195,39 @@ 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");
|
|
|
|
|
try {
|
|
|
|
|
if (fileList.length == 0) {
|
|
|
|
|
System.err.println("Sending EMPTY_DIRECTORY");
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("Sending LIST_RESPONSE");
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)(new FileList(fileList))));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
|
System.err.println(e2);
|
|
|
|
|
}
|
|
|
|
|
sendListResponse(pd);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
sendInternalError(pd);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
} catch (TransmissionError e) {
|
|
|
|
|
} catch (ProtocolError e) {
|
|
|
|
|
} catch (VersionError e) {
|
|
|
|
|
} catch (InternalError e) {
|
|
|
|
|
} catch (SizeError e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Initialize local list of all files allowed to be shared.
|
|
|
|
|
*/
|
|
|
|
|
private void initFileList() {
|
|
|
|
|
File folder = new File(baseDirectory);
|
|
|
|
|
Vector<String> v = new Vector<String>();
|
|
|
|
|
File[] files = folder.listFiles();
|
|
|
|
|
/* Add non-recursively files's names to fileList */
|
|
|
|
|
for (File f : files) {
|
|
|
|
|
if (f.isFile()) {
|
|
|
|
|
v.add(f.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fileList = new String[v.size()];
|
|
|
|
|
v.toArray(fileList);
|
|
|
|
|
Arrays.sort(fileList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Send an internal error message.
|
|
|
|
|
* @param pd ProtocolP2PPacketTCP to respond
|
|
|
|
|
*/
|
|
|
|
|
private void sendInternalError(ProtocolP2PPacketTCP pd) {
|
|
|
|
|
try {
|
|
|
|
|
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println(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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|