|
|
|
@ -47,26 +47,29 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
private String[] fileList;
|
|
|
|
|
private Map<String, byte[]> sha512 = new HashMap<>();
|
|
|
|
|
private String baseDirectory;
|
|
|
|
|
private int TCPPort;
|
|
|
|
|
private ServerSocket socket;
|
|
|
|
|
private Logger logger;
|
|
|
|
|
private HostItem tracker;
|
|
|
|
|
private HostItem server;
|
|
|
|
|
|
|
|
|
|
/** Constructor for TCP implementation, with baseDirectory and TCPPort parameters.
|
|
|
|
|
* @param baseDirectory the root directory where files are stored
|
|
|
|
|
* @param hostName the server will bind on this address
|
|
|
|
|
* @param TCPPort the server will listen on this port
|
|
|
|
|
* @param logger Logger item
|
|
|
|
|
* @param tracker Tracker
|
|
|
|
|
*/
|
|
|
|
|
public ServerManagementTCP(String baseDirectory, int TCPPort, Logger logger, HostItem tracker) {
|
|
|
|
|
public ServerManagementTCP(String baseDirectory, String hostName, int port, Logger logger, HostItem tracker) {
|
|
|
|
|
server = new HostItem(hostName, port);
|
|
|
|
|
this.tracker = tracker;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.baseDirectory = baseDirectory;
|
|
|
|
|
this.TCPPort = TCPPort;
|
|
|
|
|
initFileList();
|
|
|
|
|
initSha512();
|
|
|
|
|
try {
|
|
|
|
|
socket = new ServerSocket(TCPPort, 10, InetAddress.getLocalHost());
|
|
|
|
|
socket = new ServerSocket(server.getPort(), 10, server.getInetAddress());
|
|
|
|
|
} catch (SocketException e) {
|
|
|
|
|
logger.writeTCP("Error: cannot listen on port " + TCPPort, LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Error: cannot listen on " + server, LogLevel.Error);
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.writeTCP("Error: cannot openning socket", LogLevel.Error);
|
|
|
|
@ -93,14 +96,12 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
/** Private runnable class allowing to serve one client.
|
|
|
|
|
*/
|
|
|
|
|
private class ClientHandler implements Runnable {
|
|
|
|
|
private Socket s;
|
|
|
|
|
private String addr;
|
|
|
|
|
private HostItem 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() + " ";
|
|
|
|
|
addr = new HostItem(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Implementation of runnable. This method allow to serve one client.
|
|
|
|
@ -108,11 +109,11 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
boolean end = false;
|
|
|
|
|
logger.writeTCP(addr + "New connection", LogLevel.Action);
|
|
|
|
|
logger.writeTCP("[ " + addr + "] New connection", LogLevel.Action);
|
|
|
|
|
do {
|
|
|
|
|
end = handleRequest();
|
|
|
|
|
} while(!end);
|
|
|
|
|
logger.writeTCP(addr + "End of connection", LogLevel.Action);
|
|
|
|
|
logger.writeTCP("[ " + addr + "] End of connection", LogLevel.Action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Respond to next request incomming on socket s.
|
|
|
|
@ -121,7 +122,7 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
*/
|
|
|
|
|
private boolean handleRequest() {
|
|
|
|
|
try {
|
|
|
|
|
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s);
|
|
|
|
|
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket());
|
|
|
|
|
Payload p = pd.getPayload();
|
|
|
|
|
switch (p.getRequestResponseCode()) {
|
|
|
|
|
case LOAD_REQUEST:
|
|
|
|
@ -350,13 +351,12 @@ public class ServerManagementTCP implements Runnable {
|
|
|
|
|
* @throws SocketClosed
|
|
|
|
|
*/
|
|
|
|
|
private void registerTracker() throws InternalError, IOException, SocketClosed {
|
|
|
|
|
HostItem host = new HostItem(InetAddress.getLocalHost().getCanonicalHostName(), TCPPort);
|
|
|
|
|
logger.writeTCP("Unregistering from tracker", LogLevel.Info);
|
|
|
|
|
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Unregister(host));
|
|
|
|
|
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Unregister(server));
|
|
|
|
|
p.sendRequest((Object)tracker.getTCPSocket());
|
|
|
|
|
//tracker.closeTCPSocket();
|
|
|
|
|
logger.writeTCP("Registering into tracker", LogLevel.Info);
|
|
|
|
|
p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(host));
|
|
|
|
|
p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(server));
|
|
|
|
|
p.sendRequest((Object)tracker.getTCPSocket());
|
|
|
|
|
logger.writeTCP("Registering completed", LogLevel.Debug);
|
|
|
|
|
//tracker.closeTCPSocket();
|
|
|
|
|