diff --git a/src/serverP2P/FileWatcher.java b/src/serverP2P/FileWatcher.java index d6bf632..ce428ac 100644 --- a/src/serverP2P/FileWatcher.java +++ b/src/serverP2P/FileWatcher.java @@ -30,6 +30,7 @@ public abstract class FileWatcher implements Runnable { protected HostItem tracker; protected String baseDirectory; protected Map sha512 = new HashMap<>(); + protected Thread thread; /** Constructor @@ -83,7 +84,7 @@ public abstract class FileWatcher implements Runnable { try { Thread.sleep(time); } catch(InterruptedException e) { - writeLog("File list watcher interrupted", LogLevel.Error); + writeLog("File list watcher interrupted", LogLevel.Info); setStop(); } } @@ -123,6 +124,7 @@ public abstract class FileWatcher implements Runnable { */ public void setStop() { stop = true; + thread.interrupt(); } /** Init sha512 map. @@ -153,4 +155,11 @@ public abstract class FileWatcher implements Runnable { */ protected abstract void writeLog(Exception e, LogLevel logLevel); + /** Set thread + * @param thread Thread + */ + public void setThread(Thread thread) { + this.thread = thread; + } + } diff --git a/src/serverP2P/ServerManagement.java b/src/serverP2P/ServerManagement.java index e9f8fcc..702b550 100644 --- a/src/serverP2P/ServerManagement.java +++ b/src/serverP2P/ServerManagement.java @@ -57,8 +57,14 @@ public abstract class ServerManagement extends ServeErrors implements Runnable { /** Stop the thread */ public void setStop() { stop = true; + fileListWatcher.setStop(); + sendUnregisterRequest(); + closeSocket(); } + /** Closes socket */ + protected abstract void closeSocket(); + /** Trigger a manual check of the file list */ public void updateFileList() { diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index fad6ddf..01be104 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -79,18 +79,18 @@ public class ServerManagementTCP extends ServerManagement { public void run() { writeLog("Server sucessfully started", LogLevel.Info); fileListWatcher = (FileWatcher)new FileWatcherTCP(logger, 10000, server, tracker, baseDirectory); // checking every 10 seconds - (new Thread(fileListWatcher)).start(); + Thread flwt = new Thread(fileListWatcher); + flwt.start(); + fileListWatcher.setThread(flwt); while(!stop) { try { Socket s = socket.accept(); ClientHandler c = new ClientHandler(s); (new Thread(c)).start(); } catch (IOException e) { - writeLog("Error while accepting new connection", LogLevel.Warning); + writeLog("Socket has been closed", LogLevel.Info); } } - fileListWatcher.setStop(); - sendUnregisterRequest(); } /** Private runnable class allowing to serve one client. @@ -162,4 +162,13 @@ public class ServerManagementTCP extends ServerManagement { protected Object getTrackerSocket() { return (Object)tracker.getTCPSocket(); } + + /** Closes socket */ + protected void closeSocket() { + try { + socket.close(); + } catch (IOException e) { + writeLog("Could not close socket", LogLevel.Error); + } + } } diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index 7ea6b39..52aa88e 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -74,7 +74,9 @@ public class ServerManagementUDP extends ServerManagement { public void run() { logger.writeUDP("Server sucessfully started", LogLevel.Info); fileListWatcher = (FileWatcher)new FileWatcherUDP(logger, 10000, server, tracker, baseDirectory); // checking every 10 seconds - (new Thread(fileListWatcher)).start(); + Thread flwt = new Thread(fileListWatcher); + flwt.start(); + fileListWatcher.setThread(flwt); while(!stop) { try { ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP<>((Object)socket); @@ -83,8 +85,6 @@ public class ServerManagementUDP extends ServerManagement { } catch (LocalException e) { } } - fileListWatcher.setStop(); - sendUnregisterRequest(); } @@ -116,4 +116,9 @@ public class ServerManagementUDP extends ServerManagement { protected Object getTrackerSocket() { return (Object)tracker.getUDPSocket(); } + + /** Closes socket */ + protected void closeSocket() { + socket.close(); + } }