Fix #33 partially

This commit is contained in:
Louis Royer 2020-03-27 19:37:31 +01:00
parent 97d173e8eb
commit f7b1216b25
4 changed files with 37 additions and 8 deletions

View File

@ -30,6 +30,7 @@ public abstract class FileWatcher implements Runnable {
protected HostItem tracker;
protected String baseDirectory;
protected Map<String, byte[]> 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;
}
}

View File

@ -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() {

View File

@ -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);
}
}
}

View File

@ -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();
}
}