tristanable ~queuePolicy
Tag-based asynchronous messaging framework
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
tristanable
Tristanable is a library for D-based libraries and applications that need a way to receive variable-length messages of different types (via a Socket
) and place these messages into their own resepctively tagged queues indicated by their "type" or id
.
What problems does it solve?
Human example
Say now you made a request to a server with a tag 1
and expect a reply with that same tag 1
. Now, for a moment, think about what would happen in a tagless system. You would be expecting a reply, say now the weather report for your area, but what if the server has another thread that writes an instant messenger notification to the server's socket before the weather message is sent? Now you will inetrpret those bytes as if they were a weather message.
Tristanable provides a way for you to receive the "IM notification first" but block and dequeue (when it arrives in the queue) for the "weather report". Irresepctoive of wether (no pun intended) the weather report arrives before the "IM notification" or after.
Code example
If we wanted to implement the following we would do the following. One note is that instead of waiting on messages of a specific "type" (or rather tag), tristanable provides not just a one-message lengthb uffer per tag but infact a full queue per tag, meaning any received message with tag 1
will be enqueued and not dropped after the first message of type 1
is buffered.
import tristanable.manager;
import tristanable.queue;
import tristanable.queueitem;
/* Create a manager to manage the socket for us */
Manager manager = new Manager(socket);
/* Create a Queue for all "weather messages" */
Queue weatherQueue = new Queue(1);
/* Create a Queue for all "IM notifications" */
Queue instantNotification = new Queue(2);
/* Tell the manager to look out for tagged messages `1` and `2` */
manager.addQueue(weatherQueue);
manager.addQueue(instantNotification);
/* Now we can block on this queue and return with its head */
QueueItem message = weatherQueue.dequeue();
Surely, there must be some sort of encoding mechanism too? The messages afterall need to be encoded. No problem!, we have that sorted:
import tristanable.encoding;
/* Let's send it with tag 1 and data "Hello" */
ulong tag = 1;
byte[] data = cast(byte[])"Hello";
/* When sending a message */
DataMessage tristanEncoded = new DataMessage(tag, data);
And let tristanable handle it! We even handle the message lengths and everything using another great project bformat.
Format
[4 bytes (size-2, little endian)][8 bytes - tag][(2-size) bytes - data]
Using tristanable in your D project
You can easily add the library (source-based) to your project by running the following command in your project's root:
dub add tristanable
Acknowledgments
I had to make amends with a friend for calling one of my projects by a certain name (of a certain someone) over one of her suggestions so I decided to take her suggestion, "tristanable", and call this project after it!
Thanks Gabby Smuts 😉️ (@kwarantuin - check out her really cool art)
- ~queuePolicy released 3 years ago
- deavmi/tristanable
- LGPL-3.0
- Copyright © 2020, Tristan B. Kildaire
- Authors:
- Dependencies:
- bformat
- Versions:
-
4.1.2-beta 2023-Oct-02 4.1.1-beta 2023-Oct-02 4.1.0-beta 2023-Oct-02 4.0.2-beta 2023-Oct-02 4.0.1-beta 2023-Oct-02 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
467 downloads total
-
- Score:
- 0.4
- Short URL:
- tristanable.dub.pm