Compare commits

...

37 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 86cfbdba83
Update README.md 2023-03-25 22:44:43 +02:00
Tristan B. Velloza Kildaire 999681cc01
Create d.yml 2023-03-25 22:44:06 +02:00
Tristan B. Velloza Kildaire 8d75e21055 Updated 2022-11-28 18:56:25 +02:00
Tristan B. Velloza Kildaire 0b56db2567 Added missing links 2022-11-28 18:53:46 +02:00
Tristan B. Velloza Kildaire c70fa62d22 Updated .gitignore 2022-11-28 13:39:29 +02:00
Tristan B. Velloza Kildaire 7621ee5343 Completely overhauled Eventy system for the v0.4.0 release
Removed the event-loop for a better system (for now) whereby we just dispatch signal handlers on the call to `push(Event)`.

In a future release I hope to bring the event loop back but in a signal-based manner, such that we can support deferred events and priorities and such
2022-11-28 13:39:06 +02:00
Tristan B. Velloza Kildaire 6392c92349 Cleaned up 2022-11-26 18:39:00 +02:00
Tristan B. Velloza Kildaire 28d64f799e Switch back to YIELD HoldOffMode for now (gives best performance)
We will have to investigate load average highness and ways to deal with it later
2022-11-26 18:19:21 +02:00
Tristan B. Velloza Kildaire cc75eefa15 Try 50ms 2022-11-26 18:16:07 +02:00
Tristan B. Velloza Kildaire e75e3f732d Added missing `eventy.config` import to the `package.d` module 2022-11-26 18:00:24 +02:00
Tristan B. Velloza Kildaire 4fed9fc780 Added missing comment to config.d 2022-11-26 17:58:15 +02:00
Tristan B. Velloza Kildaire 5bbb6d804a Added last missing doc header to a method in engine.d 2022-11-26 17:43:44 +02:00
Tristan B. Velloza Kildaire 6cfaa9824e Added missing comments 2022-11-26 17:37:38 +02:00
Tristan B. Velloza Kildaire 00b8eb82be Removed redundant imports 2022-11-26 17:36:11 +02:00
Tristan B. Velloza Kildaire a501de15d3 Removed commented out code
Moved testing imports within each unit test

Added a new unit test which tests the yield-based HoldOffMode

Neteaned up imports
2022-11-26 17:31:03 +02:00
Tristan B. Velloza Kildaire 59d0253b61 Implemented `agressiveTryLock` check so the user can choose whether or not to yield if a fail to grab the lock occurs 2022-11-26 17:26:05 +02:00
Tristan B. Velloza Kildaire 3603c567e8 Removed deprecated `setSleep(Duration)`
Added `getConfig()/setConfig(EngineSettings)` to retrieve and update the configuration of the Engine on the fly

Privated `getTypes()` method for now as it is not implemented yet
2022-11-26 17:23:40 +02:00
Tristan B. Velloza Kildaire be44514a51 Cleaned up `shutdown()` and added some TODOs 2022-11-26 17:14:20 +02:00
Tristan B. Velloza Kildaire e8b78156b7 Added support for graceful shutdown 2022-11-26 17:12:41 +02:00
Tristan B. Velloza Kildaire 490cfe85d6 Implemented `hasPendingEvents()` to check whether there are any pending events at all
Use `isPendingEvents()` is unit tests to ensure that we actually are able to see ALL events pushed dispatched before we, possibly, pre-maturely shutdown the engine with `shutdown()`

Removed unused function `runner(Event)`

Added new commented out test case

Formatted method documentation of some methods to conform to D's style
2022-11-26 17:07:17 +02:00
Tristan B. Velloza Kildaire 03dfe3ccfd Removed TODO comment
Added configuration system

Introduced two modes of "sleeping" the event loop, sleep-based and yield-based (with 200ms sleep-mode as the default)

Disabled `setSleep(Duration)` for now
2022-11-26 16:52:21 +02:00
Tristan B. Velloza Kildaire b67f61b848 Added isSignalExists(ulong id) to check whether a signal handler exists for a given event id 2022-05-26 14:04:34 +02:00
Tristan B. Velloza Kildaire 263188fa64 Remove todo that has since been completed 2022-05-19 16:50:56 +02:00
Tristan B. Velloza Kildaire d0c4d12155 Make unit testing method private within module 2022-05-19 16:49:52 +02:00
Tristan B. Velloza Kildaire 9a26ed7011 Removed swear words 2022-05-19 16:49:25 +02:00
Tristan B. Velloza Kildaire c6082f3941 Unit test: Shutdown engine after testing 2022-05-19 16:48:09 +02:00
Tristan B. Velloza Kildaire 692660772a Added rudimentary shutdown() method 2022-04-07 10:11:59 +02:00
Tristan B. Velloza Kildaire ea1e317431 Updated README 2022-01-16 15:02:01 +02:00
Tristan B. Velloza Kildaire f2f79e1cf8 Updated documentation 2022-01-16 14:56:26 +02:00
Tristan B. Velloza Kildaire f649a63a77 Re-worked event system 2022-01-16 14:32:00 +02:00
Tristan B. Velloza Kildaire f1a13623a6 Added comment 2022-01-16 14:22:18 +02:00
Tristan B. Velloza Kildaire c2bc5bc902 Added optional payload support 2022-01-16 14:21:09 +02:00
Tristan B. Kildaire 72a35976e1 Added a Javadoc 2021-12-27 13:04:30 +02:00
Tristan B. Kildaire 3e826aa1ac Added a Javadoc 2021-12-27 13:03:55 +02:00
Tristan B. Kildaire f87a7f44ca Added some Javadocs 2021-12-27 13:02:32 +02:00
Tristan B. Velloza Kildaire d2e9f5376c New logo 2021-12-27 12:29:47 +02:00
Tristan B. Kildaire e6c18021bc
Delete libeventdisp.a 2021-12-27 12:22:58 +02:00
14 changed files with 699 additions and 266 deletions

32
.github/workflows/d.yml vendored Normal file
View File

@ -0,0 +1,32 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: D
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7
- 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

2
.gitignore vendored
View File

@ -14,3 +14,5 @@ eventdisp-test-*
*.obj
*.lst
libeventdisp.a
eventy-test-library
libeventy.a

153
README.md
View File

@ -1,15 +1,156 @@
<center>
<img src="logos/logo.png" width=250>
<br>
![](logos/logo.png)
Eventy
======
### _Easy-to-use_ event-loop dispatcher framework for D-based applications
TODO: Add stuff here
---
[![D](https://github.com/deavmi/eventy/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/eventy/actions/workflows/d.yml)
## Getting started
</center>
### The _engine_
The first thing every Eventy-based application will need is an instance of the `Engine`.
This provides the user with a single object instance of the [`Engine` class](https://eventy.dpldocs.info/v0.4.3/eventy.engine.Engine.html) by which
the user can register _event types_, _signal handlers_ for said events and the ability
to trigger or _push_ events into the engine.
The simplest way to get a new _engine_ up and running is as follow:
```d
Engine engine = new Engine();
```
This will create a new engine initializing all of its internals such that it is ready for
use.
### Event types
_Event types_ are effectively just numbers. The use of these is to be able to connect events
pushed into the engine with their respective signal handlers (which are registered to handle
one or more event types).
Let's create two event types, with IDs `1` and `2`:
```d
engine.addEventType(new EventType(1));
engine.addEventType(new EventType(2));
```
This will tell the engine to create two new event types with tags `1` and `2` respectively.
### Signal handlers
We're almost done. So far we have created a new _engine_ for handling our event tyoes and
the triggering of events. What is missing is something to _handle those event types_ when
an event of one of those types is pushed into the engine. Such handlers are referred to as
_signal handlers_ and in Eventy these are instances of the [`Signal` class](https://eventy.dpldocs.info/v0.4.3/eventy.signal.Signal.html).
We're going to create a signal that can handle both of the event types `1` and `2` that we
registered earlier on. We can do this by creating a class that inherits from the `Signal`
base class:
```d
class SignalHandler1 : Signal
{
this()
{
super([1,2]);
}
public override void handler(Event e)
{
import std.stdio;
writeln("Running event", e.getID());
}
}
```
We need to tell the `Signal` class two things:
1. What _event typess_ it will handle
2. What to _run_ for said event types
---
The first of these two is very easy, this is what you see in the constructor `this()`:
```d
this()
{
super([1,2]);
}
```
The `super([1,2])` call tells the Signal class that this signal handler handles those
two IDs, namely `1` and `2`.
---
As for _what to run_, that is specified by overriding the `void handler(Event)` method
in the `Signal` class. In our case we make it write to the console the ID of the event
(which would end up either being `1` or `2` seeing as this handler is only registered
for those event types).
```d
import std.stdio;
writeln("Running event", e.id);
```
---
We're almost there, trust me. The last thing to do is to register this signal handler
with the engine, we do so as follows:
```d
Signal j = new SignalHandler1();
engine.addSignalHandler(j);
```
### Triggering events
Now comes the fun part, you can add events into the system by _pushing them to the core_
as follows:
```d
Event eTest = new Event(1);
engine.push(eTest);
eTest = new Event(2);
engine.push(eTest);
```
You will then see something like this:
```
Running event1
Running event2
```
or:
```
Running event1
Running event2
```
Despite us pushing the events into the engine in the order of `1` and _then_ `2`, the
scheduling of such threads is up to the Linux kernel and hence one could be run before
the other.
---
## Release notes
### `v0.4.3`
```
Completely overhauled Eventy system for the v0.4.3 release
Removed the event-loop for a better system (for now) whereby we just dispatch signal handlers on the call to `push(Event)`.
In a future release I hope to bring the event loop back but in a signal-based manner, such that we can support deferred events and priorities and such
```

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

35
source/eventy/config.d Normal file
View File

@ -0,0 +1,35 @@
module eventy.config;
import core.thread : Duration, dur;
/**
* Configuration system for eventy
*
* Allows the user to specify certain
* tweaks to the engine
*/
public struct EngineSettings
{
/* Agressive lock trying (can starve the loop-check) */
bool agressiveTryLock;
/* Hold-off mode */
HoldOffMode holdOffMode;
/* If `holdOffMode` is `SLEEP` then set the duration for the sleep */
Duration sleepTime;
/* Calling `shutdown()` will wait for any pending events to be dispatched before shutting down */
bool gracefulShutdown;
}
/**
* Hold off mode
*
* Method to use for "sleeping" the event loop
*/
public enum HoldOffMode
{
YIELD,
SLEEP
}

View File

@ -1,35 +1,52 @@
module eventy.engine;
import eventy.queues : Queue;
import eventy.signal : Signal, EventHandler;
import eventy.types : EventType;
import eventy.signal : Signal;
import eventy.event : Event;
import eventy.config;
import eventy.exceptions;
import std.container.dlist;
import core.sync.mutex : Mutex;
import core.thread : Thread, dur, Duration;
import eventy.exceptions;
import std.stdio;
/* TODO: Move elsewhere, this thing thinks it's a delegate in the unit test, idk why */
void runner(Event e)
{
import std.stdio;
writeln("Running event", e.id);
}
import std.conv : to;
unittest
{
import std.stdio;
Engine engine = new Engine();
engine.start();
engine.addQueue(1);
engine.addQueue(2);
/**
* Let the event engine know what typeIDs are
* allowed to be queued
*/
engine.addEventType(new EventType(1));
engine.addEventType(new EventType(2));
Signal j = new Signal([1,2], &runner);
/**
* Create a new Signal Handler that will handles
* event types `1` and `2` with the given `handler()`
* function
*/
class SignalHandler1 : Signal
{
this()
{
super([1, 2]);
}
public override void handler(Event e)
{
writeln("Running event", e.getID());
}
}
/**
* Tell the event engine that I want to register
* the following handler for its queues `1` and `2`
*/
Signal j = new SignalHandler1();
engine.addSignalHandler(j);
Event eTest = new Event(1);
@ -37,12 +54,72 @@ unittest
eTest = new Event(2);
engine.push(eTest);
Thread.sleep(dur!("seconds")(2));
engine.push(eTest);
writeln("naai");
writeln("done with main thread code");
while(engine.hasEventsRunning()) {}
/* TODO: Before shutting down, actually test it out (i.e. all events ran) */
engine.shutdown();
}
unittest
{
import std.stdio;
EngineSettings customSettings = {holdOffMode: HoldOffMode.YIELD};
Engine engine = new Engine(customSettings);
/**
* Let the event engine know what typeIDs are
* allowed to be queued
*/
engine.addEventType(new EventType(1));
engine.addEventType(new EventType(2));
/**
* Create a new Signal Handler that will handles
* event types `1` and `2` with the given `handler()`
* function
*/
class SignalHandler1 : Signal
{
this()
{
super([1, 2]);
}
public override void handler(Event e)
{
writeln("Running event", e.getID());
}
}
/**
* Tell the event engine that I want to register
* the following handler for its queues `1` and `2`
*/
Signal j = new SignalHandler1();
engine.addSignalHandler(j);
Event eTest = new Event(1);
engine.push(eTest);
eTest = new Event(2);
engine.push(eTest);
Thread.sleep(dur!("seconds")(2));
engine.push(eTest);
writeln("done with main thread code");
while(engine.hasEventsRunning()) {}
/* TODO: Before shutting down, actually test it out (i.e. all events ran) */
engine.shutdown();
}
/**
@ -55,48 +132,98 @@ unittest
* handlers, add signal handlers, among many
* other things
*/
public final class Engine : Thread
public final class Engine
{
/* TODO: Or use a queue data structure */
private DList!(Queue) queues;
private Mutex queueLock;
/* Registered queues */
private DList!(EventType) eventTypes;
private Mutex eventTypesLock;
/* TODO: Or use a queue data structure */
/* Registered signal handlers */
private DList!(Signal) handlers;
private Mutex handlerLock;
private Duration sleepTime;
/* Engine configuration */
private EngineSettings settings;
/* Whether engine is running or not */
private bool running;
/* Dispatched threads */
private DList!(DispatchWrapper) threadStore;
private Mutex threadStoreLock;
/**
* Instantiates a new Eventy engine with the provided
* configuration
*
* Params:
* settings = The EngineSettings to use
*/
this(EngineSettings settings)
{
eventTypesLock = new Mutex();
handlerLock = new Mutex();
threadStoreLock = new Mutex();
this.settings = settings;
}
/**
* Instantiates a new Eventy engine with the default
* settings
*/
this()
{
super(&run);
queueLock = new Mutex();
handlerLock = new Mutex();
EngineSettings defaultSettings;
/* Yield if a lock fails (prevent potential thread starvation) */
defaultSettings.agressiveTryLock = false;
// FIXME: Investigate ways to lower load average
// /* Make the event engine loop sleep (1) and for 50ms (2) (TODO: Adjust this) */
// defaultSettings.holdOffMode = HoldOffMode.SLEEP;
// defaultSettings.sleepTime = dur!("msecs")(50);
/* Use yeilding for most responsiveness */
defaultSettings.holdOffMode = HoldOffMode.YIELD;
/* Do not gracefully shutdown */
defaultSettings.gracefulShutdown = false;
this(defaultSettings);
}
/**
* Set the event loop sleep time
*
* The load average will sky rocket if it is 0,
* which is just because it is calculated on how
* full the run queue is, length but also over time
* and even just one task continousy in it will
* make the average high
*
* Reason why it's always runnable is the process
* (the "thread") is a tight loop with no sleeps
* that would dequeue it from the run queue and/or
* no I/O system calls that would put it into the
* waiting queue
*/
public void setSleep(Duration time)
/**
* Returns the current configuration paremeters being
* used by the engine
*
* Returns: The EngineSettings struct
*/
public EngineSettings getConfig()
{
sleepTime = time;
return settings;
}
/**
* Updates the current configuration of the engine
*
* Params:
* newSettings = The new EngineSettings struct to use
*/
public void setConfig(EngineSettings newSettings)
{
this.settings = newSettings;
}
/**
* Attaches a new signal handler to the engine
*
* Params:
* e = the signal handler to add
*/
public void addSignalHandler(Signal e)
{
/* Lock the signal-set */
/* Lock the signal-set */
handlerLock.lock();
/* Add the new handler */
@ -106,103 +233,161 @@ public final class Engine : Thread
handlerLock.unlock();
}
/**
* Event loop
*/
public void run()
/**
* Shuts down the event engine
*/
public void shutdown()
{
while(true)
/* TODO: Insert a lock here, that dispatch should adhere too as well */
/* FIXME: We should prevent adding of queues during shutdown */
/* FIXME: We should prevent pushing of events during shutdown */
/* Wait for any pendings events (if configured) */
if(settings.gracefulShutdown)
{
/* TODO: Implement me */
/**
* TODO: If lock fails, then yield
*/
/**
* Lock the queue-set
*
* Additionally:
* Don't waste time spinning on mutex,
* if it is not lockable then yield
*/
while(!queueLock.tryLock_nothrow())
{
yield();
}
foreach(Queue queue; queues)
{
/* If the queue has evenets queued */
if(queue.hasEvents())
{
/* TODO: Add different dequeuing techniques */
/* Pop the first Event */
Event headEvent = queue.popEvent();
/* Get all signal-handlers for this event type */
Signal[] handlersMatched = getSignalsForEvent(headEvent);
/* Dispatch the signal handlers */
dispatch(handlersMatched, headEvent);
}
}
/* Unlock the queue set */
queueLock.unlock();
/* Yield to stop mutex starvation */
yield();
/* TODO: Add yield to stop mutex starvation on a single thread */
/* Sleep the thread */
// sleepTime = dur!("seconds")(0);
// sleep(sleepTime);
while(hasEventsRunning()) {}
}
}
/**
* Dispatch(Signal[] set, Event e)
*
* Creates a new thread per signal and dispatches the event to them
*
* TODO: Add ability to dispatch on this thread
*/
/**
* Creates a new thread per signal and dispatches the event to them
*
* Params:
* signalSet = The signal handlers to use for dispatching
* e = the Event to be dispatched to each handler
*/
private void dispatch(Signal[] signalSet, Event e)
{
foreach(Signal signal; signalSet)
foreach (Signal signal; signalSet)
{
/* Create a new Thread */
Thread handlerThread = getThread(signal, e);
DispatchWrapper handlerThread = new DispatchWrapper(signal, e);
/**
* TODO
*
* When we call `shutdown()` there may very well be a case of
* where the threadStoreLock unlocks after the clean up
* loop, but storeThread hangs here during that time,
* then proceeds to start the thread, we should therefore,
* either block on running changed (solution 1, not as granular)
*
* Solution 2: Block on dispatch being called <- use this method rather
* But still needs a running check, it must not go ahead if running is now
* false
*/
/* Store the thread */
storeThread(handlerThread);
/* Start the thread */
handlerThread.start();
}
}
private Thread getThread(Signal signal, Event e)
/**
* Adds a thread to the thread store
*
* Params:
* t = the thread to add
*/
private void storeThread(DispatchWrapper t)
{
Thread signalHandlerThread = new class Thread
{
this()
{
super(&worker);
}
/**
* TODO: This can only be implemented if we use
* wrapper threads that exit, and we can signal
* removal from thread store then
*/
public void worker()
{
EventHandler handler = signal.getHandler();
handler(e);
}
};
/* Lock the thread store from editing */
threadStoreLock.lock();
return signalHandlerThread;
/* Add the thread */
threadStore ~= t;
/* Unlock the thread store for editing */
threadStoreLock.unlock();
}
/**
* Removes a thread from the thread store
*
* Params:
* t = the thread to remove
*/
private void removeThread(DispatchWrapper t)
{
/* Lock the thread store from editing */
threadStoreLock.lock();
/* Remove the thread */
threadStore.linearRemoveElement(t);
/* Unlock the thread store for editing */
threadStoreLock.unlock();
}
/**
* Checks whether or not there are still events
* running at the time of calling
*
* Returns: <code>true</code> if there are events
* still running, <code>false</code> otherwise
*/
public bool hasEventsRunning()
{
/* Whether there are events running or not */
bool has = false;
/* Lock the thread store */
threadStoreLock.lock();
has = !threadStore.empty();
/* Unlock the thread store */
threadStoreLock.unlock();
return has;
}
/**
* DispatchWrapper
*
* Effectively a thread but with the Signal,
* Event included with clean-up routines
*/
private class DispatchWrapper : Thread
{
private Signal signal;
private Event e;
this(Signal signal, Event e)
{
super(&run);
this.signal = signal;
this.e = e;
}
private void run()
{
/* Run the signal handler */
signal.handler(e);
/* Remove myself from the thread store */
removeThread(this);
}
}
/**
* Returns all the signal handlers responsible
* for handling the type of Event provided
*
* Params:
* e = the Event type to match to
* Returns: A Signal[] containing each handler
* registered to handle type <code>e</code>
*/
public Signal[] getSignalsForEvent(Event e)
{
/* Matched handlers */
@ -212,9 +397,9 @@ public final class Engine : Thread
handlerLock.lock();
/* Find all handlers matching */
foreach(Signal signal; handlers)
foreach (Signal signal; handlers)
{
if(signal.handles(e.id))
if (signal.handles(e.getID()))
{
matchedHandlers ~= signal;
}
@ -226,70 +411,127 @@ public final class Engine : Thread
return matchedHandlers;
}
/**
* push(Event e)
*
* Provided an Event, `e`, this will enqueue the event
* to
*/
/**
* Checks if there is a signal handler that handles
* the given event id
*
* Params:
* id = the event ID to check
* Returns: <code>true</code> if a signal handler does
* exist, <code>false</code> otherwise
*/
public bool isSignalExists(ulong id)
{
return getSignalsForEvent(new Event(id)).length != 0;
}
/**
* Pushes the given Event into the engine
* for eventual dispatch
*
* Params:
* e = the event to push
*/
public void push(Event e)
{
Queue matchedQueue = findQueue(e.id);
//TODO: New code goes below here
/**
* What we want to do here is to effectively
* wake up a checker thread and also (before that)
* perhaps we say what queue was modified
*
* THEN the checker thread goes to said queue and
* executes said event (dispatches it) and then sleep
* again till it is interrupted. We need Pids and kill etc for this
*
* Idea (2)
*
* If we cannot do a checker thread then we can spwan a thread here
* but then we get no control for priorities etc, although actually we could
* maybe? It depends, we don't want multiple dispathers at same time then
* (A checker thread would ensure we don't get this)
*/
if(matchedQueue)
/* Obtain all signal handlers for the given event */
Signal[] handlersMatched = getSignalsForEvent(e);
/* If we get signal handlers then dispatch them */
if(handlersMatched.length)
{
/* Append to the queue */
matchedQueue.add(e);
dispatch(handlersMatched, e);
}
/* If there are no matching events */
else
{
//TODO: Add default handler support
//TODO: Add error throwing in case where not true
}
}
public void addQueue(ulong id)
/**
* Registers a new EventType with the engine
* and then adds it.
*
* Throws EventyException if the id of the given
* EventType is is already in use by another
*
* Params:
* id = the id of the new event type to add
* Throws: EventyException
*/
public void addEventType(EventType evType)
{
/* Create a new queue with the given id */
Queue newQueue = new Queue(id);
/* Lock the queue collection */
queueLock.lock();
/* Lock the event types list */
eventTypesLock.lock();
/* If no such queue exists then add it (recursive mutex used) */
if(!findQueue(id))
if (!findEventType(evType.getID()))
{
/* Add the queue */
queues ~= newQueue;
/* Add the event types list */
eventTypes ~= evType;
}
else
{
throw new EventyException("Failure to add queue with ID already in use");
throw new EventyException("Failure to add EventType with id '"~to!(string)(evType.getID())~"\' as it is already in use");
}
/* Unlock the queue collection */
queueLock.unlock();
/* Unlock the event types list */
eventTypesLock.unlock();
}
public Queue findQueue(ulong id)
/**
* Given an if, this will return the EventType
* associated with said id
*
* Params:
* id = the id of the EventType
* Returns: The EventType if found, otherwise
* <code>null</code>
*/
public EventType findEventType(ulong id)
{
/* Lock the queue collection */
queueLock.lock();
/* Lock the EventType list */
eventTypesLock.lock();
/* Find the matching queue */
Queue matchedQueue;
foreach(Queue queue; queues)
/* Find the matching EventType */
EventType matchedEventType;
foreach (EventType eventType; eventTypes)
{
if(queue.id == id)
if (eventType.getID() == id)
{
matchedQueue = queue;
matchedEventType = eventType;
break;
}
}
/* Unlock the queue collection */
queueLock.unlock();
/* Unlock the EventType list */
eventTypesLock.unlock();
return matchedQueue;
return matchedEventType;
}
public ulong[] getTypes()
/* TODO: Add coumentation */
private ulong[] getTypes()
{
/* TODO: Implement me */
return null;

View File

@ -1,20 +1,34 @@
module eventy.event;
/**
* Event
*
* An Event represents a trigger for a given signal(s)
* handlers which associate with the given typeID
*/
/**
* Event
*
* An Event represents a trigger for a given signal(s)
* handlers which associate with the given typeID
*/
public class Event
{
/**
*
*/
/* The event's type id */
private ulong id;
/**
* Creates a new Event with the given typeID
*
* Params:
* typeID = the new Event's type ID
*/
this(ulong typeID)
{
this.id = typeID;
}
ulong id;
}
/**
* Returns the type ID of this Event
*
* Returns: The Event's type ID
*/
public final ulong getID()
{
return id;
}
}

View File

@ -1,5 +1,10 @@
module eventy.exceptions;
/**
* EventyException
*
* An Eventy runtime error
*/
public final class EventyException : Exception
{
this(string message)

View File

@ -3,5 +3,6 @@ module eventy;
public import eventy.event;
public import eventy.exceptions;
public import eventy.engine;
public import eventy.queues;
public import eventy.signal;
public import eventy.types;
public import eventy.signal;
public import eventy.config;

View File

@ -1,75 +0,0 @@
module eventy.queues;
import eventy.event : Event;
import core.sync.mutex : Mutex;
import std.container.dlist;
import std.range;
/**
* Queue
*
* Represents a queue with a given ID that can
* have Event-s enqueued to it
*/
public final class Queue
{
public ulong id;
/* TODO: Add queue of Event's here */
private DList!(Event) queue;
private Mutex queueLock;
this(ulong id)
{
this.id = id;
queueLock = new Mutex();
}
public DList!(Event).Range getKak()
{
return queue[];
}
public void add(Event e)
{
/* Lock the queue */
queueLock.lock();
queue.insert(e);
/* Unlock the queue */
queueLock.unlock();
}
public bool hasEvents()
{
bool has;
/* Lock the queue */
queueLock.lock();
has = !(queue[]).empty();
/* Unlock the queue */
queueLock.unlock();
return has;
}
public Event popEvent()
{
Event poppedEvent;
/* Lock the queue */
queueLock.lock();
poppedEvent = (queue[]).front();
queue.removeFront();
/* Unlock the queue */
queueLock.unlock();
return poppedEvent;
}
}

View File

@ -9,20 +9,19 @@ import eventy.event : Event;
* which means that it contains an associated function to be run
* on handling of a given Event
*/
alias EventHandler = void function(Event);
//alias EventHandler = void function(Event);
public class Signal
public abstract class Signal
{
/* TypeIDs this signal handler associates with */
private ulong[] typeIDs;
/* Signal handler */
private EventHandler handler;
//private EventHandler handler;
this(ulong[] typeIDs, EventHandler handler)
this(ulong[] typeIDs)
{
this.typeIDs = typeIDs;
this.handler = handler;
}
/**
@ -53,8 +52,5 @@ public class Signal
}
public EventHandler getHandler()
{
return handler;
}
}
public abstract void handler(Event);
}

40
source/eventy/types.d Normal file
View File

@ -0,0 +1,40 @@
module eventy.types;
import eventy.event : Event;
import core.sync.mutex : Mutex;
import std.container.dlist;
import std.range;
/**
* EventType
*
* Represents a type of event. Every Event has an EventType
* and Signal(s)-handlers register to one or more of these
* types to handle
*/
public final class EventType
{
/* The EventType's ID */
private ulong id;
/**
* Instantiates a new EventType with the given id
*
* Params:
* id = The EventType's id
*/
this(ulong id)
{
this.id = id;
}
/**
* Returns the id of this EventType
*
* Returns: The id of this EventType
*/
public ulong getID()
{
return id;
}
}