Line data Source code
1 : using System.Collections.Generic;
2 : using System.Linq;
3 : using System.Net;
4 : using System.Threading.Tasks;
5 : using Cqrs.Configuration;
6 : using EventStore.ClientAPI;
7 :
8 : namespace Cqrs.EventStore
9 : {
10 : public class EventStoreConnectionHelper<TAuthenticationToken> : IEventStoreConnectionHelper
11 0 : {
12 : protected IEventBuilder<TAuthenticationToken> EventBuilder { get; private set; }
13 :
14 : protected IConfigurationManager ConfigurationManager { get; private set; }
15 :
16 0 : public EventStoreConnectionHelper(IEventBuilder<TAuthenticationToken> eventBuilder, IConfigurationManager configurationManager)
17 : {
18 : EventBuilder = eventBuilder;
19 : ConfigurationManager = configurationManager;
20 : }
21 :
22 0 : public virtual IEventStoreConnection GetEventStoreConnection()
23 : {
24 : ConnectionSettings settings = ConnectionSettings.Create();
25 : IPEndPoint endPoint = GetEventStoreIpEndPoint();
26 : IEventStoreConnection connection = EventStoreConnection.Create(settings, endPoint);
27 : Task connecting = connection.ConnectAsync();
28 : connecting.Wait();
29 :
30 : EventData connectionEvent = EventBuilder.CreateClientConnectedEvent(GetEventStoreClientName());
31 : Task notify = connection.AppendToStreamAsync(GetEventStoreConnectionLogStreamName(), ExpectedVersion.Any, connectionEvent);
32 : notify.Wait();
33 :
34 : return connection;
35 : }
36 :
37 0 : protected virtual string GetEventStoreClientName()
38 : {
39 : return ConfigurationManager.GetSetting("Cqrs.EventStoreClientName") ?? "Cqrs Default Client";
40 : }
41 :
42 0 : protected virtual string GetEventStoreConnectionLogStreamName()
43 : {
44 : return ConfigurationManager.GetSetting("Cqrs.EventStoreConnectionLogStreamName") ?? "EventStore Connection Log Stream";
45 : }
46 :
47 0 : protected virtual IPEndPoint GetEventStoreIpEndPoint()
48 : {
49 : List<byte> eventStoreIp = (ConfigurationManager.GetSetting("Cqrs.EventStoreIp") ?? "127.0.0.1").Split('.').Select(ipPart => (byte)int.Parse(ipPart)).ToList();
50 : string eventStorePortValue = ConfigurationManager.GetSetting("Cqrs.EventStorePort");
51 : int eventStorePort = 1113;
52 : if (!string.IsNullOrWhiteSpace(eventStorePortValue))
53 : eventStorePort = int.Parse(eventStorePortValue);
54 : return new IPEndPoint(new IPAddress(new[] { eventStoreIp[0], eventStoreIp[1], eventStoreIp[2], eventStoreIp[3] }), eventStorePort);
55 : }
56 : }
57 : }
|