2013-03-03 22:26:41 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
using System.Linq;
|
2013-03-05 06:25:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2013-02-23 20:09:44 +00:00
|
|
|
|
using NLog;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
2013-04-24 01:56:00 +00:00
|
|
|
|
namespace NzbDrone.Common.Messaging
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-04-24 01:56:00 +00:00
|
|
|
|
public class MessageAggregator : IMessageAggregator
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
|
private readonly Logger _logger;
|
2013-04-24 01:56:00 +00:00
|
|
|
|
private readonly Func<IEnumerable<IProcessMessage>> _handlers;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
2013-04-24 01:56:00 +00:00
|
|
|
|
public MessageAggregator(Logger logger, Func<IEnumerable<IProcessMessage>> handlers)
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_handlers = handlers;
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
public void Publish<TEvent>(TEvent message) where TEvent : IEvent
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-02-23 20:09:44 +00:00
|
|
|
|
_logger.Trace("Publishing {0}", message.GetType().Name);
|
2013-02-19 02:37:16 +00:00
|
|
|
|
|
2013-03-05 06:25:05 +00:00
|
|
|
|
//call synchronous handlers first.
|
2013-03-03 22:26:41 +00:00
|
|
|
|
foreach (var handler in _handlers().OfType<IHandle<TEvent>>())
|
2013-02-19 02:37:16 +00:00
|
|
|
|
{
|
2013-03-05 06:25:05 +00:00
|
|
|
|
_logger.Debug("{0} -> {1}", message.GetType().Name, handler.GetType().Name);
|
2013-02-23 20:09:44 +00:00
|
|
|
|
handler.Handle(message);
|
2013-03-30 22:43:19 +00:00
|
|
|
|
_logger.Debug("{0} <- {1}", message.GetType().Name, handler.GetType().Name);
|
2013-03-05 06:25:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var handler in _handlers().OfType<IHandleAsync<TEvent>>())
|
|
|
|
|
{
|
|
|
|
|
var handlerLocal = handler;
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("{0} ~> {1}", message.GetType().Name, handlerLocal.GetType().Name);
|
|
|
|
|
handlerLocal.HandleAsync(message);
|
2013-03-30 22:43:19 +00:00
|
|
|
|
_logger.Debug("{0} <~ {1}", message.GetType().Name, handlerLocal.GetType().Name);
|
2013-03-05 06:25:05 +00:00
|
|
|
|
});
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-24 01:56:00 +00:00
|
|
|
|
|
|
|
|
|
public void Execute<TCommand>(TCommand message) where TCommand : ICommand
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2013-02-19 02:37:16 +00:00
|
|
|
|
}
|
2013-03-05 06:25:05 +00:00
|
|
|
|
}
|