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