tristanable/source/tristanable/manager.d

118 lines
1.7 KiB
D
Raw Normal View History

2020-09-29 17:18:53 +00:00
module tristanable.manager;
2020-09-29 17:13:36 +00:00
import std.socket : Socket;
import core.sync.mutex : Mutex;
import bmessage : bSendMessage = sendMessage;
2020-09-29 17:18:53 +00:00
import tristanable.queue : Queue;
2020-10-16 15:11:26 +00:00
import tristanable.watcher;
2020-09-29 17:13:36 +00:00
2020-09-29 09:57:25 +00:00
public final class Manager
{
/* All queues */
private Queue[] queues;
private Mutex queuesLock;
/* TODO Add drop queue? */
2020-09-29 17:13:36 +00:00
/**
* The remote host
*/
private Socket socket;
2020-10-16 15:11:26 +00:00
private Watcher watcher;
2020-09-29 17:13:36 +00:00
/**
* Constructs a new Manager with the given
* endpoint Socket
*
*/
this(Socket socket)
2020-09-29 09:57:25 +00:00
{
2020-09-29 17:13:36 +00:00
/* Set the socket */
this.socket = socket;
/* Initialize the queues mutex */
queuesLock = new Mutex();
/* Initialize the watcher */
2020-10-16 15:11:26 +00:00
watcher = new Watcher(this, socket);
2020-09-29 09:57:25 +00:00
}
2020-09-29 17:13:36 +00:00
public Queue getQueue(ulong tag)
{
Queue matchingQueue;
queuesLock.lock();
foreach(Queue queue; queues)
{
if(queue.getTag() == tag)
{
matchingQueue = queue;
break;
}
}
queuesLock.unlock();
return matchingQueue;
}
2020-09-29 09:57:25 +00:00
2020-09-29 17:13:36 +00:00
public void addQueue(Queue queue)
2020-09-29 09:57:25 +00:00
{
2020-09-29 17:13:36 +00:00
queuesLock.lock();
/* Make sure such a tag does not exist already */
if(!isValidTag_callerThreadSafe(queue.getTag()))
{
queues ~= queue;
}
else
{
/* TODO: Throw an error here */
}
queuesLock.unlock();
2020-09-29 09:57:25 +00:00
}
private bool isValidTag_callerThreadSafe(ulong tag)
{
bool tagExists;
foreach(Queue queue; queues)
{
if(queue.getTag() == tag)
{
tagExists = true;
break;
}
}
return tagExists;
}
public bool isValidTag(ulong tag)
{
/* Whether or not such a tagged queue exists */
bool tagExists;
queuesLock.lock();
tagExists = isValidTag_callerThreadSafe(tag);
queuesLock.unlock();
return tagExists;
}
2020-09-29 17:13:36 +00:00
public void shutdown()
{
/* TODO: Implement me */
}
2020-09-29 09:57:25 +00:00
}