mirror of
https://github.com/deavmi/tristanable.git
synced 2024-09-21 21:42:56 +02:00
Manager
- Changed from using D's dynamic arrays for the array of `Queue` objects to using an `SList!(T)` where `T` is the `Queue` type - Implemented `getQueue(ulong)` which returns the `Queue` object with the matching id/tag, else throws an instance of `TristanabaleException` - Implemented `registerQueue(Queue)` which will attempt to add the provided `Queue` given that a queue does not already exist with the provided queue's id; if that is the case then an instance of `TristanableException` is thrown Queue - Made the constructor take in the `ulong` queue ID - Made the constructor publically accessible - Implemented `getID()` which returns the `Queue`'s id as a `ulong` - Removed the static method `newQueue(ulong)` Unit test - Added a unit test to test `getQueue(ulong)` when the queue cannot be found - Added a unit test to test adding a queue and successfully retrieving it
This commit is contained in:
parent
454e7dd18e
commit
99c14bc699
@ -8,6 +8,8 @@ import tristanable.queue : Queue;
|
||||
import core.sync.mutex : Mutex;
|
||||
import tristanable.manager.watcher : Watcher;
|
||||
import tristanable.encoding : TaggedMessage;
|
||||
import tristanable.exceptions;
|
||||
import std.container.slist : SList;
|
||||
|
||||
/**
|
||||
* Manages a provided socket by spawning
|
||||
@ -29,7 +31,7 @@ public class Manager
|
||||
*
|
||||
* NOTE: Make a ulong map to this later
|
||||
*/
|
||||
private Queue[] queues;
|
||||
private SList!(Queue) queues;
|
||||
private Mutex queuesLock;
|
||||
|
||||
/**
|
||||
@ -60,14 +62,73 @@ public class Manager
|
||||
watcher.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the queue mathcing the provided id
|
||||
*
|
||||
* Params:
|
||||
* id = the id to lookup by
|
||||
* Returns: the Queue
|
||||
* Throws: TristanableException if the queue is not found
|
||||
*/
|
||||
public Queue getQueue(ulong id)
|
||||
{
|
||||
/* The found queue */
|
||||
Queue queue;
|
||||
|
||||
/* Lock the queue of queues */
|
||||
queuesLock.lock();
|
||||
|
||||
/* On return or error */
|
||||
scope(exit)
|
||||
{
|
||||
/* Unlock the queue of queues */
|
||||
queuesLock.unlock();
|
||||
}
|
||||
|
||||
/* Search for the queue */
|
||||
foreach(Queue curQueue; queues)
|
||||
{
|
||||
if(curQueue.getID() == id)
|
||||
{
|
||||
queue = curQueue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* If no queue is found then throw an error */
|
||||
if(queue is null)
|
||||
{
|
||||
throw new TristanableException(ErrorType.QUEUE_NOT_FOUND);
|
||||
}
|
||||
|
||||
return queue;
|
||||
}
|
||||
|
||||
public void registerQueue(Queue queue)
|
||||
{
|
||||
// TODO: Lock queue
|
||||
/* Lock the queue of queues */
|
||||
queuesLock.lock();
|
||||
|
||||
/* On return or error */
|
||||
scope(exit)
|
||||
{
|
||||
/* Unlock the queue of queues */
|
||||
queuesLock.unlock();
|
||||
}
|
||||
|
||||
// TODO: Insert queue only if non-existent, else throw an exception
|
||||
|
||||
// TODO: Unlock queue
|
||||
/* Search for the queue, throw an exception if it exists */
|
||||
foreach(Queue curQueue; queues)
|
||||
{
|
||||
if(curQueue.getID() == queue.getID())
|
||||
{
|
||||
throw new TristanableException(ErrorType.QUEUE_ALREADY_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
/* Insert the queue as it does not exist */
|
||||
queues.insertAfter(queues[], queue);
|
||||
}
|
||||
|
||||
public void sendMessage(TaggedMessage tag)
|
||||
@ -91,3 +152,52 @@ unittest
|
||||
// TODO: register tristanable quues
|
||||
// TODO: make server then send something to us and chekc if queues active
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieving a queue which does not
|
||||
* exist
|
||||
*/
|
||||
unittest
|
||||
{
|
||||
/* Create a manager */
|
||||
Manager manager = new Manager(null);
|
||||
|
||||
/* Shouldn't be found */
|
||||
try
|
||||
{
|
||||
manager.getQueue(69);
|
||||
assert(false);
|
||||
}
|
||||
catch(TristanableException e)
|
||||
{
|
||||
assert(e.getError() == ErrorType.QUEUE_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test registering a queue and then fetching it
|
||||
*/
|
||||
unittest
|
||||
{
|
||||
/* Create a manager */
|
||||
Manager manager = new Manager(null);
|
||||
|
||||
/* Create a new queue with tag 69 */
|
||||
Queue queue = new Queue(69);
|
||||
|
||||
try
|
||||
{
|
||||
/* Register the queue */
|
||||
manager.registerQueue(queue);
|
||||
|
||||
/* Fetch the queue */
|
||||
Queue fetchedQueue = manager.getQueue(69);
|
||||
|
||||
/* Ensure the queue we fetched is the one we stored (the references would be equal) */
|
||||
assert(fetchedQueue == queue);
|
||||
}
|
||||
catch(TristanableException e)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
@ -24,13 +24,16 @@ public class Queue
|
||||
private ulong queueID;
|
||||
|
||||
|
||||
private this()
|
||||
this(ulong queueID)
|
||||
{
|
||||
/* Initialize the queue lock */
|
||||
this.queueLock = new Mutex();
|
||||
|
||||
/* Initialize the event */
|
||||
this.event = new Event();
|
||||
|
||||
/* Set the queue id */
|
||||
this.queueID = queueID;
|
||||
}
|
||||
|
||||
public void dequeue()
|
||||
@ -54,12 +57,8 @@ public class Queue
|
||||
queueLock.unlock();
|
||||
}
|
||||
|
||||
public static Queue newQueue(ulong queueID)
|
||||
public ulong getID()
|
||||
{
|
||||
Queue queue;
|
||||
|
||||
// TODO: Implement me
|
||||
|
||||
return queue;
|
||||
return queueID;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user