Skip to content

Commit

Permalink
Merge branch 'nextgen' into nextgen_listener
Browse files Browse the repository at this point in the history
  • Loading branch information
deavmi committed Oct 2, 2023
2 parents e356c06 + 42c6b11 commit c5c01c3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 24 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/d.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ name: D

on:
push:
branches: [ "master" ]
branches: [ "**" ]
pull_request:
branches: [ "master", "nextgen" ]
branches: [ "**" ]

permissions:
contents: read
Expand All @@ -22,11 +22,20 @@ jobs:
- uses: actions/checkout@v3
- uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7

- name: Install Doveralls (code coverage tool)
run: |
dub fetch doveralls
sudo apt install libcurl4-openssl-dev
- name: 'Build & Test'
run: |
# Build the project, with its main file included, without unittests
dub build --compiler=$DC
# Build and run tests, as defined by `unittest` configuration
# In this mode, `mainSourceFile` is excluded and `version (unittest)` are included
# See https://dub.pm/package-format-json.html#configurations
dub test --compiler=$DC
dub test --compiler=$DC --coverage
- name: Coverage upload
run: |
dub run doveralls -- -t ${{secrets.COVERALLS_REPO_TOKEN}}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
tristanable
===========

[![D](https://github.com/deavmi/tristanable/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/tristanable/actions/workflows/d.yml)
[![D](https://github.com/deavmi/tristanable/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/tristanable/actions/workflows/d.yml) ![DUB](https://img.shields.io/dub/v/tristanable?color=%23c10000ff%20&style=flat-square) ![DUB](https://img.shields.io/dub/dt/tristanable?style=flat-square) ![DUB](https://img.shields.io/dub/l/tristanable?style=flat-square) [![Coverage Status](https://coveralls.io/repos/github/deavmi/tristanable/badge.svg?branch=master)](https://coveralls.io/github/deavmi/tristanable?branch=master)


**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 respectively tagged queues indicated by their _"type"_ or `id`.

Expand Down
3 changes: 1 addition & 2 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
],
"copyright": "Copyright © 2023, Tristan B. Kildaire",
"dependencies": {
"bformat": "4.1.0",
"libsnooze": "0.3.3"
"bformat": ">=4.1.1"
},
"description": "Tristanable network message queuing framework",
"homepage": "https://deavmi.assigned.network/projects/tristanable",
Expand Down
12 changes: 11 additions & 1 deletion source/tristanable/exceptions.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ public enum ErrorType
/**
* If no default queue is configured
*/
NO_DEFAULT_QUEUE
NO_DEFAULT_QUEUE,

/**
* The blocking call to `dequeue()`, somehow, failed
*/
DEQUEUE_FAILED,

/**
* The call to `enqueue()`, somehow, failed
*/
ENQUEUE_FAILED
}

/**
Expand Down
89 changes: 72 additions & 17 deletions source/tristanable/queue/queue.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ module tristanable.queue.queue;

import tristanable.queue.listener : TListener;

// TODO: Examine the below import which seemingly fixes stuff for libsnooze
import libsnooze.clib;
import libsnooze;

import core.sync.mutex : Mutex;
import core.sync.condition : Condition;
import core.sync.exception : SyncError;
import std.container.slist : SList;
import tristanable.encoding;
import core.thread : dur;
import core.time : Duration, dur;
import tristanable.exceptions;

version(unittest)
{
Expand All @@ -35,8 +34,15 @@ public class Queue
/**
* The libsnooze event used to sleep/wake
* on queue events
* Mutex for the condition variable
*/
private Mutex mutex;

/**
* The condition variable used to sleep/wake
* on queue of events
*/
private Event event;
private Condition signal;

/**
* The queue of messages
Expand All @@ -60,6 +66,15 @@ public class Queue

// TODO: Add listener add/remove methods
// TODO: On queue actions add a notificaiton call to the listeners
/**
* If a message is enqueued prior
* to us sleeping then we won't
* wake up and return for it.
*
* Therefore a periodic wakeup
* is required.
*/
private Duration wakeInterval;

/**
* Constructs a new Queue and immediately sets up the notification
Expand All @@ -75,21 +90,47 @@ public class Queue
/* Initialize the queue lock */
this.queueLock = new Mutex();

/* Initialize the event */
this.event = new Event();
/* Initialize the condition variable */
this.mutex = new Mutex();
this.signal = new Condition(this.mutex);

/* Set the queue id */
this.queueID = queueID;

/* Ensure pipe existence (see https://deavmi.assigned.network/git/deavmi/tristanable/issues/5) */
event.wait(dur!("seconds")(0));
/* Set the slumber interval */
this.wakeInterval = dur!("msecs")(50); // TODO: Decide on value
}

/**
* Returns the current wake interval
* for the queue checker
*
* Returns: the `Duration`
*/
public Duration getWakeInterval()
{
return this.wakeInterval;
}

/**
* Sets the wake up interval
*
* Params:
* interval = the new interval
*/
public void setWakeInterval(Duration interval)
{
this.wakeInterval = interval;
}

/**
* Enqueues the provided tagged message onto this queue
* and then wakes up any thread that has called dequeue
* on this queue as well
*
* On error enqueueing a `TristanableException` will be
* thrown.
*
* Params:
* message = the TaggedMessage to enqueue
*/
Expand Down Expand Up @@ -121,11 +162,12 @@ public class Queue
try
{
// TODO: Make us wait on the event (optional with a time-out)
event.notifyAll();
signal.notifyAll();
}
catch(SnoozeError snozErr)
catch(SyncError snozErr)
{
// TODO: Add error handling for libsnooze exceptions here
// Throw an exception on a fatal exception
throw new TristanableException(ErrorType.ENQUEUE_FAILED);
}
}

Expand All @@ -134,6 +176,9 @@ public class Queue
/**
* Blocks till a message can be dequeued from this queue
*
* On error dequeueing a `TristanableException` will be
* thrown.
*
* Returns: the dequeued TaggedMessage
*/
public TaggedMessage dequeue()
Expand All @@ -157,16 +202,26 @@ public class Queue
/* Block till we dequeue a message successfully */
while(dequeuedMessage is null)
{
scope(exit)
{
// Unlock the mutex
this.mutex.unlock();
}

// Lock the mutex
this.mutex.lock();

try
{
// TODO: Make us wait on the event (optional with a time-out)
event.wait();
this.signal.wait(this.wakeInterval);
}
catch(SnoozeError snozErr)
catch(SyncError e)
{
// TODO: Add error handling for libsnooze exceptions here
// Throw an exception on a fatal exception
throw new TristanableException(ErrorType.DEQUEUE_FAILED);
}


/* Lock the item queue */
queueLock.lock();

Expand Down

0 comments on commit c5c01c3

Please sign in to comment.