diff --git a/source/tristanable/garbage.d b/source/tristanable/garbage.d index 5c48d14..4734141 100644 --- a/source/tristanable/garbage.d +++ b/source/tristanable/garbage.d @@ -19,6 +19,11 @@ public final class GarbageCollector : Thread */ private Request[]* requestQueueVariable; + /** + * Whether or not the watcher is active + */ + private bool isActive; + this(Manager manager) { /* Set the worker function */ @@ -29,12 +34,19 @@ public final class GarbageCollector : Thread /* Set the pointer */ requestQueueVariable = cast(Request[]*)manager.getQueueVariable(); + + isActive = true; + } + + public void stopGC() + { + isActive = false; } /* TODO: Add timeout ability */ private void cleaner() { - while(true) + while(isActive) { /* Lock the queue */ manager.lockQueue(); diff --git a/source/tristanable/manager.d b/source/tristanable/manager.d index ffe947d..d8c868a 100644 --- a/source/tristanable/manager.d +++ b/source/tristanable/manager.d @@ -60,6 +60,24 @@ public final class Manager gc.start(); } + public void stopManager() + { + /* Will caue watcher to not block */ + socket.close(); + + /* Stop watcher */ + watcher.stopWatcher(); + + /* Stop gc */ + gc.stopGC(); + + /* Wait for watcher thread to stop */ + watcher.join(); + + /* Wait for garbage collector thread to stop */ + gc.join(); + } + public void sendMessage(ulong tag, byte[] data) { /* Encode the message */ diff --git a/source/tristanable/watcher.d b/source/tristanable/watcher.d index f9fc36c..2396dc7 100644 --- a/source/tristanable/watcher.d +++ b/source/tristanable/watcher.d @@ -22,16 +22,27 @@ public final class Watcher : Thread */ private Socket endpoint; + /** + * Whether or not the watcher is active + */ + private bool isActive; + this(Manager manager, Socket endpoint) { super(&watchLoop); this.manager = manager; this.endpoint = endpoint; + isActive = true; + } + + public void stopWatcher() + { + isActive = false; } private void watchLoop() { - while(true) + while(isActive) { /* The received message (tag+data) */ byte[] receivedPayload;