/// <summary>
/// run the broker - if not bound to endpoint automatically binds to known endpoint
/// </summary>
/// <param name="token">CancellationToken to cancel the method</param>
/// <exception cref="InvalidOperationException">Can't start same broker more than once!</exception>
public void RunSynchronous(CancellationToken token)
{
if (m_isRunning)
{
throw new InvalidOperationException("Can't start same broker more than once!");
}
if (!m_isBound)
{
Bind();
}
m_isRunning = true;
using (var poller = new NetMQ.Poller())
{
Socket.ReceiveReady += ProcessReceiveMessage;
// get timer for scheduling heartbeat
var timer = new NetMQTimer(HeartbeatInterval);
// send every 'HeartbeatInterval' a heartbeat to all not expired workers
timer.Elapsed += (s, e) => SendHeartbeat();
poller.AddSocket(Socket);
poller.AddTimer(timer);
Log("[BROKER] Starting to listen for incoming messages ...");
// start the poller and wait for the return, which will happen once token is
// signalling Cancel(!)
Task.Factory.StartNew(poller.Start, token).Wait();
Log("[BROKER] ... Stopped!");
// clean up
poller.RemoveTimer(timer);
poller.RemoveSocket(Socket);
// unregister event handler
Socket.ReceiveReady -= ProcessReceiveMessage;
}
m_isRunning = false;
}