mirror of https://github.com/deavmi/tasky.git
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
This commit is contained in:
parent
bed096df27
commit
0b1c8242fd
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
module tasky;
|
||||
|
||||
public import tasky.engine;
|
||||
public import tasky.engine;
|
||||
public import tasky.request : Request, ResponseHandler;
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue