2016-11-16 22:11:05 +00:00
|
|
|
|
using System;
|
2017-02-12 14:18:12 +00:00
|
|
|
|
using System.Linq;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
using Tapeti.Config;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
using Tapeti.Connection;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti
|
|
|
|
|
{
|
|
|
|
|
public class TapetiConnection : IDisposable
|
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
private readonly IConfig config;
|
2016-11-21 19:54:29 +00:00
|
|
|
|
public TapetiConnectionParams Params { get; set; }
|
|
|
|
|
|
|
|
|
|
private readonly Lazy<TapetiWorker> worker;
|
|
|
|
|
|
|
|
|
|
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public TapetiConnection(IConfig config)
|
2016-11-21 19:54:29 +00:00
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
this.config = config;
|
2017-02-05 22:22:34 +00:00
|
|
|
|
(config.DependencyResolver as IDependencyContainer)?.RegisterDefault(GetPublisher);
|
2016-12-11 14:08:58 +00:00
|
|
|
|
|
2017-02-12 20:43:30 +00:00
|
|
|
|
worker = new Lazy<TapetiWorker>(() => new TapetiWorker(config)
|
2016-11-21 19:54:29 +00:00
|
|
|
|
{
|
2017-07-14 10:33:09 +00:00
|
|
|
|
ConnectionParams = Params ?? new TapetiConnectionParams(),
|
|
|
|
|
ConnectionEventListener = new ConnectionEventListener(this)
|
2016-11-21 19:54:29 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
2017-07-14 10:33:09 +00:00
|
|
|
|
public event EventHandler Connected;
|
|
|
|
|
|
|
|
|
|
public event EventHandler Disconnected;
|
|
|
|
|
|
|
|
|
|
public event EventHandler Reconnected;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
2017-02-12 14:18:12 +00:00
|
|
|
|
public async Task<ISubscriber> Subscribe(bool startConsuming = true)
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2017-02-12 14:18:12 +00:00
|
|
|
|
var subscriber = new TapetiSubscriber(() => worker.Value, config.Queues.ToList());
|
|
|
|
|
await subscriber.BindQueues();
|
|
|
|
|
|
|
|
|
|
if (startConsuming)
|
|
|
|
|
await subscriber.Resume();
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
|
|
|
|
return subscriber;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-02-11 20:32:03 +00:00
|
|
|
|
public ISubscriber SubscribeSync()
|
|
|
|
|
{
|
|
|
|
|
return Subscribe().Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-11-16 22:11:05 +00:00
|
|
|
|
public IPublisher GetPublisher()
|
|
|
|
|
{
|
2016-12-05 07:00:09 +00:00
|
|
|
|
return new TapetiPublisher(() => worker.Value);
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Close()
|
|
|
|
|
{
|
2016-11-21 19:54:29 +00:00
|
|
|
|
if (worker.IsValueCreated)
|
|
|
|
|
await worker.Value.Close();
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Close().Wait();
|
|
|
|
|
}
|
2017-07-14 10:33:09 +00:00
|
|
|
|
|
|
|
|
|
private class ConnectionEventListener: IConnectionEventListener
|
|
|
|
|
{
|
|
|
|
|
private readonly TapetiConnection owner;
|
|
|
|
|
|
|
|
|
|
internal ConnectionEventListener(TapetiConnection owner)
|
|
|
|
|
{
|
|
|
|
|
this.owner = owner;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Connected()
|
|
|
|
|
{
|
|
|
|
|
owner.OnConnected(new EventArgs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Disconnected()
|
|
|
|
|
{
|
|
|
|
|
owner.OnDisconnected(new EventArgs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reconnected()
|
|
|
|
|
{
|
|
|
|
|
owner.OnReconnected(new EventArgs());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnConnected(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Connected?.Invoke(this, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnReconnected(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Reconnected?.Invoke(this, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnDisconnected(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Disconnected?.Invoke(this, e);
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|