From 0b1c8242fd72dc9413fc1c4451e21e92609535df Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Wed, 3 May 2023 21:20:19 +0200 Subject: [PATCH] Engine - Consumes a tristanable `Manager` - Provides a `makeRequest(Request)` function which will generate a unique tristanable `Queue`, construct a `TaggedMessage` of the queue's ID with the gievn request data, it will then send the message via the tristanable `Manager`'s `sendMessage(TaggedMessage)` method, then await on the queue and lastly run the `Request`'s `ResponseHandler` function with the received data from the dequeued `TaggedMessage` Request - Defines a request with data to send and a function to handle a response --- source/tasky/engine.d | 33 +++++++++++++++++++++++++++++++++ source/tasky/package.d | 3 ++- source/tasky/request.d | 30 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 source/tasky/request.d diff --git a/source/tasky/engine.d b/source/tasky/engine.d index 3cdd4f6..a0b7ebd 100644 --- a/source/tasky/engine.d +++ b/source/tasky/engine.d @@ -1,2 +1,35 @@ module tasky.engine; +import tristanable : Manager, Queue, TaggedMessage; +import tasky.request : Request; + +public class Engine +{ + private Manager tManager; + + this(Manager tristanableManager) + { + this.tManager = tristanableManager; + } + + // TODO: Continue working on this + + public void makeRequest(Request req) + { + /* Get a unique queue */ + Queue newQueue = tManager.getUniqueQueue(); + + /* Create a tagged message with the tag */ + ulong tag = newQueue.getID(); + TaggedMessage tReq = new TaggedMessage(tag, req.getRequestData()); + + /* Send the message */ + tManager.sendMessage(tReq); + + /* Await for a response */ + byte[] resp = newQueue.dequeue().getPayload(); + + /* Run the response handler with the response */ + req.process(resp); + } +} \ No newline at end of file diff --git a/source/tasky/package.d b/source/tasky/package.d index 05da23b..fad8859 100644 --- a/source/tasky/package.d +++ b/source/tasky/package.d @@ -1,3 +1,4 @@ module tasky; -public import tasky.engine; \ No newline at end of file +public import tasky.engine; +public import tasky.request : Request, ResponseHandler; \ No newline at end of file diff --git a/source/tasky/request.d b/source/tasky/request.d new file mode 100644 index 0000000..d1b33f9 --- /dev/null +++ b/source/tasky/request.d @@ -0,0 +1,30 @@ +module tasky.request; + +import tristanable.encoding : TaggedMessage; + +public alias ResponseHandler = void function(byte[]); + +public abstract class Request +{ + private byte[] requestMessage; + + // TODO: Define the below with an alias for a function pointer that accepts a byte[] (the response data) + + private ResponseHandler respFunc; + + protected this(byte[] requestMessage, ResponseHandler respFunc) + { + this.requestMessage = requestMessage; + this.respFunc = respFunc; + } + + package final byte[] getRequestData() + { + return requestMessage; + } + + package final void process(byte[] responseData) + { + respFunc(responseData); + } +} \ No newline at end of file