- 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
This commit is contained in:
Tristan B. Velloza Kildaire 2023-05-03 21:20:19 +02:00
parent bed096df27
commit 0b1c8242fd
3 changed files with 65 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -1,3 +1,4 @@
module tasky;
public import tasky.engine;
public import tasky.engine;
public import tasky.request : Request, ResponseHandler;

30
source/tasky/request.d Normal file
View File

@ -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);
}
}