diff --git a/source/tristanable/garbage.d b/source/tristanable/garbage.d new file mode 100644 index 0000000..d085363 --- /dev/null +++ b/source/tristanable/garbage.d @@ -0,0 +1,51 @@ +module tristanable.garbage; + +import tristanable.manager : Manager; +import tristanable.request : Request; +import std.socket : Socket; +import core.thread : Thread, Duration, dur; +import bmessage : receiveMessage; + +public final class GarbageCollector : Thread +{ + + /** + * The associated manager + */ + private Manager manager; + + /** + * The queue variable pointer + */ + private Request[]* requestQueueVariable; + + this(Manager manager) + { + /* Set the worker function */ + super(&cleaner); + + /* Set the manager */ + this.manager = manager; + + /* Set the pointer */ + requestQueueVariable = cast(Request[]*)manager.getQueueVariable(); + } + + private void cleaner() + { + while(true) + { + /* Lock the queue */ + manager.lockQueue(); + + /* TODO: Add clean up here */ + + /* Unlock the queue */ + manager.unlockQueue(); + + /* Sleep for 60 seconds after cleaning up */ + sleep(dur!("seconds")(60)); + + } + } +} \ No newline at end of file diff --git a/source/tristanable/manager.d b/source/tristanable/manager.d index a93b1fd..38ac6da 100644 --- a/source/tristanable/manager.d +++ b/source/tristanable/manager.d @@ -2,6 +2,7 @@ module tristanable.manager; import tristanable.watcher : Watcher; import tristanable.request : Request; +import tristanable.garbage : GarbageCollector; import std.socket : Socket; import core.sync.mutex : Mutex; import bmessage : bSendMessage = sendMessage; @@ -32,6 +33,11 @@ public final class Manager */ private Socket socket; + /** + * The garbage collector + */ + private GarbageCollector gc; + this(Socket endpoint) { /* Set the socket */ @@ -40,11 +46,17 @@ public final class Manager /* Create the watcher */ watcher = new Watcher(this, endpoint); + /* Create the garbage collector */ + gc = new GarbageCollector(this); + /* Initialize the `requestQueue` mutex */ queueMutex = new Mutex(); /* Start the watcher */ watcher.start(); + + /* Start the garbage collector */ + gc.start(); } public void sendMessage(ulong tag, byte[] data) @@ -135,6 +147,11 @@ public final class Manager return requestQueue; } + public ref Request[] getQueueVariable() + { + return requestQueue; + } + public void lockQueue() { queueMutex.lock();