Fix #33 partially

pull/86/head
Louis Royer 5 years ago
parent 97d173e8eb
commit f7b1216b25

@ -30,6 +30,7 @@ public abstract class FileWatcher implements Runnable {
protected HostItem tracker; protected HostItem tracker;
protected String baseDirectory; protected String baseDirectory;
protected Map<String, byte[]> sha512 = new HashMap<>(); protected Map<String, byte[]> sha512 = new HashMap<>();
protected Thread thread;
/** Constructor /** Constructor
@ -83,7 +84,7 @@ public abstract class FileWatcher implements Runnable {
try { try {
Thread.sleep(time); Thread.sleep(time);
} catch(InterruptedException e) { } catch(InterruptedException e) {
writeLog("File list watcher interrupted", LogLevel.Error); writeLog("File list watcher interrupted", LogLevel.Info);
setStop(); setStop();
} }
} }
@ -123,6 +124,7 @@ public abstract class FileWatcher implements Runnable {
*/ */
public void setStop() { public void setStop() {
stop = true; stop = true;
thread.interrupt();
} }
/** Init sha512 map. /** Init sha512 map.
@ -153,4 +155,11 @@ public abstract class FileWatcher implements Runnable {
*/ */
protected abstract void writeLog(Exception e, LogLevel logLevel); protected abstract void writeLog(Exception e, LogLevel logLevel);
/** Set thread
* @param thread Thread
*/
public void setThread(Thread thread) {
this.thread = thread;
}
} }

@ -57,8 +57,14 @@ public abstract class ServerManagement extends ServeErrors implements Runnable {
/** Stop the thread */ /** Stop the thread */
public void setStop() { public void setStop() {
stop = true; stop = true;
fileListWatcher.setStop();
sendUnregisterRequest();
closeSocket();
} }
/** Closes socket */
protected abstract void closeSocket();
/** Trigger a manual check of the file list /** Trigger a manual check of the file list
*/ */
public void updateFileList() { public void updateFileList() {

@ -79,18 +79,18 @@ public class ServerManagementTCP extends ServerManagement {
public void run() { public void run() {
writeLog("Server sucessfully started", LogLevel.Info); writeLog("Server sucessfully started", LogLevel.Info);
fileListWatcher = (FileWatcher)new FileWatcherTCP(logger, 10000, server, tracker, baseDirectory); // checking every 10 seconds 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) { while(!stop) {
try { try {
Socket s = socket.accept(); Socket s = socket.accept();
ClientHandler c = new ClientHandler(s); ClientHandler c = new ClientHandler(s);
(new Thread(c)).start(); (new Thread(c)).start();
} catch (IOException e) { } 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. /** Private runnable class allowing to serve one client.
@ -162,4 +162,13 @@ public class ServerManagementTCP extends ServerManagement {
protected Object getTrackerSocket() { protected Object getTrackerSocket() {
return (Object)tracker.getTCPSocket(); return (Object)tracker.getTCPSocket();
} }
/** Closes socket */
protected void closeSocket() {
try {
socket.close();
} catch (IOException e) {
writeLog("Could not close socket", LogLevel.Error);
}
}
} }

@ -74,7 +74,9 @@ public class ServerManagementUDP extends ServerManagement {
public void run() { public void run() {
logger.writeUDP("Server sucessfully started", LogLevel.Info); logger.writeUDP("Server sucessfully started", LogLevel.Info);
fileListWatcher = (FileWatcher)new FileWatcherUDP(logger, 10000, server, tracker, baseDirectory); // checking every 10 seconds 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) { while(!stop) {
try { try {
ProtocolP2PPacketUDP<?> pd = new ProtocolP2PPacketUDP<>((Object)socket); ProtocolP2PPacketUDP<?> pd = new ProtocolP2PPacketUDP<>((Object)socket);
@ -83,8 +85,6 @@ public class ServerManagementUDP extends ServerManagement {
} catch (LocalException e) { } catch (LocalException e) {
} }
} }
fileListWatcher.setStop();
sendUnregisterRequest();
} }
@ -116,4 +116,9 @@ public class ServerManagementUDP extends ServerManagement {
protected Object getTrackerSocket() { protected Object getTrackerSocket() {
return (Object)tracker.getUDPSocket(); return (Object)tracker.getUDPSocket();
} }
/** Closes socket */
protected void closeSocket() {
socket.close();
}
} }

Loading…
Cancel
Save