From f75604eca915de8eee7c8714fe569d4c6746e0e1 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 27 Mar 2023 15:41:50 +0200 Subject: [PATCH] - Attempt merge --- source/tristanable/exceptions.d | 15 ++++++++++ source/tristanable/manager.d | 50 +++++++++++++++++++++++++++++---- source/tristanable/package.d | 3 +- source/tristanable/queue.d | 24 ++++++++++++---- source/tristanable/queueitem.d | 6 ++++ source/tristanable/watcher.d | 37 ++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 source/tristanable/exceptions.d create mode 100644 source/tristanable/queueitem.d create mode 100644 source/tristanable/watcher.d diff --git a/source/tristanable/exceptions.d b/source/tristanable/exceptions.d new file mode 100644 index 0000000..0e7926c --- /dev/null +++ b/source/tristanable/exceptions.d @@ -0,0 +1,15 @@ +module tristanable.exceptions; + +public enum Error +{ + QueueExists +} + +public class TristanableException : Exception +{ + this(Error err) + { + // TODO: Do this + super("TODO: Do this"); + } +} \ No newline at end of file diff --git a/source/tristanable/manager.d b/source/tristanable/manager.d index e3b6c37..bf6f85a 100644 --- a/source/tristanable/manager.d +++ b/source/tristanable/manager.d @@ -1,15 +1,55 @@ +/** + * Management of a tristanable instance + */ module tristanable.manager; +import std.socket; import tristanable.queue : Queue; +import core.sync.mutex : Mutex; /** - * Allows one to add new queues, control - * existing ones by waiting on them etc + * Manages a provided socket by spawning + * a watcher thread to read from it and file + * mail into the corresponding queues. + * + * Queues are managed via this an instance + * of a manager. */ public class Manager { - /* Queues */ - private Queue[] queues; + /** + * The underlying socket to read from + */ + private Socket socket; - + /** + * Currently registered queues + * + * NOTE: Make a ulong map to this later + */ + private Queue[] queues; + private Mutex queuesLock; + + /** + * Constructs a new manager which will read from + * this socket and file mail for us + * + * Params: + * socket = the underlying socket to use + */ + this(Socket socket) + { + this.socket = socket; + this.queuesLock = new Mutex(); + } + + + public void registerQueue(Queue queue) + { + // TODO: Lock queue + + // TODO: Insert queue only if non-existent, else throw an exception + + // TODO: Unlock queue + } } \ No newline at end of file diff --git a/source/tristanable/package.d b/source/tristanable/package.d index bdbabde..3f504ce 100644 --- a/source/tristanable/package.d +++ b/source/tristanable/package.d @@ -5,4 +5,5 @@ module tristanable; public import tristanable.manager : Manager; -public import tristanable.queue : Queue, QueueItem; \ No newline at end of file +public import tristanable.queue : Queue; +public import tristanable.queueitem : QueueItem; \ No newline at end of file diff --git a/source/tristanable/queue.d b/source/tristanable/queue.d index cc3e954..61ce8c5 100644 --- a/source/tristanable/queue.d +++ b/source/tristanable/queue.d @@ -1,6 +1,10 @@ module tristanable.queue; +// TODO: Examine the below import which seemingly fixes stuff for libsnooze +import libsnooze.clib; import libsnooze; + +import tristanable.queueitem : QueueItem; import core.sync.mutex : Mutex; public class Queue @@ -13,6 +17,11 @@ public class Queue private QueueItem queue; private Mutex queueLock; + + /** + * This queue's unique ID + */ + private ulong queueID; private this() @@ -26,7 +35,15 @@ public class Queue public void dequeue() { - // TODO: Make us wait on the event (optional with a time-out) + try + { + // TODO: Make us wait on the event (optional with a time-out) + event.wait(); + } + catch(SnoozeError snozErr) + { + // TODO: Add error handling for libsnooze exceptions here + } // TODO: Lock queue queueLock.lock(); @@ -45,9 +62,4 @@ public class Queue return queue; } -} - -public class QueueItem -{ - } \ No newline at end of file diff --git a/source/tristanable/queueitem.d b/source/tristanable/queueitem.d new file mode 100644 index 0000000..4f6337d --- /dev/null +++ b/source/tristanable/queueitem.d @@ -0,0 +1,6 @@ +module tristanable.queueitem; + +public class QueueItem +{ + +} \ No newline at end of file diff --git a/source/tristanable/watcher.d b/source/tristanable/watcher.d new file mode 100644 index 0000000..9550b40 --- /dev/null +++ b/source/tristanable/watcher.d @@ -0,0 +1,37 @@ +module tristanable.watcher; + +import core.thread : Thread; +import tristanable.manager : Manager; +import std.socket; + +/** + * Watches the socket on a thread of its own, + * performs the decoding of the incoming messages + * and places them into the correct queues via + * the associated Manager instance + */ +public class Watcher : Thread +{ + /** + * The associated manager to use + * such that we can place new mail + * into their respective inboxes (queues) + */ + private Manager manager; + + /** + * The underlying socket to read from + */ + private Socket socket; + + + + + private void watch() + { + while(true) + { + // TODO: Implement me + } + } +} \ No newline at end of file