Beispiel #1
0
 public async Task PublishAsync <TEvent>(TEvent @event, PublishSettings <Event <TEvent> > settings = null)
 {
     using (var hubClient = _hubClientFactory.Create(EndpointId.Generate()))
     {
         await hubClient.PublishAsync(new Event <TEvent>(@event, _id), settings);
     }
 }
Beispiel #2
0
        public async Task EmitAsync <TEvent>(TEvent @event, IEventSerializer <TEvent> serializer = null)
        {
            using (var hubClient = _hubClientFactory.Create(EndpointId.Generate()))
            {
                var settings = new PublishSettings <TEvent>
                {
                    DestinationEndpointId = _id,
                    Serializer            = serializer
                };

                await hubClient.PublishAsync(@event, settings);
            }
        }
Beispiel #3
0
        public async Task Execute(Action <ExecutionConfiguration> configurator = null)
        {
            var id            = EndpointId.Generate();
            var configuration = new ExecutionConfiguration();

            configurator?.Invoke(configuration);

            using (var hubClient = _hubClientFactory.Create(id))
            {
                var listeners = new List <IListener>();
                try
                {
                    listeners.AddRange((from dynamic handler in _handlers
                                        select hubClient.ListenFor(handler)).Cast <IListener>());

                    var startTime = DateTime.Now;
                    _context.KeepAlive();
                    await hubClient.PublishAsync(new Event <TAction>(_action, id), _publishSettings);

                    _context.KeepAlive();

                    while (listeners.Any() && !_context.Handled)
                    {
                        var currentTime = DateTime.Now;
                        if ((startTime.Add(configuration.ActionTimeout) < currentTime) ||
                            (_context.Usage.GetValueOrDefault().Add(configuration.EventTimeout) < currentTime))
                        {
                            if (_timeoutHandler != null)
                            {
                                await _timeoutHandler.HandleAsync();
                            }

                            break;
                        }

                        await Task.Delay(configuration.Tick);
                    }
                }
                finally
                {
                    foreach (var listener in listeners)
                    {
                        listener.Dispose();
                    }
                }
            }
        }
Beispiel #4
0
        public static async Task StartAsync()
        {
            using (await _queue.GetBlockingScope())
            {
                if (_initialized)
                {
                    throw new InvalidOperationException("Bootstrapper has already been invoked");
                }

                var constantSettings = ApplicationBootstrapper.ConstantSettingsProvider.Find();
                var directoryPath    = AppDomain.CurrentDomain.BaseDirectory;

                await WriteDatabaseInitializer.Initialize(new WriteDatabaseConfiguration(directoryPath, constantSettings.DatabaseFilePath, constantSettings.DatabaseEncryptionKey));

                await ReadDatabaseInitializer.Initialize();

                var applicationSettings    = await new ApplicationSettingsRepository(WriteDatabaseInitializer.DatabaseContext).LoadAsync();
                var applicationListenerUri = string.Format(constantSettings.ApplicationListenerUri, constantSettings.SystemIpAddress, applicationSettings.ApplicationCommunicationPort);

                var builder = new ContainerBuilder();

                var assemblies = new[] { Assembly.GetEntryAssembly() }
                .Union(new[]
                {
                    "iGP11.Library.dll",
                    "iGP11.Library.Component.dll",
                    "iGP11.Library.Component.DataAnnotations.dll",
                    "iGP11.Library.DDD.dll",
                    "iGP11.Library.EventPublisher.dll",
                    "iGP11.Library.File.dll",
                    "iGP11.Library.Hub.dll",
                    "iGP11.Library.Hub.Client.dll",
                    "iGP11.Library.Hub.Shared.dll",
                    "iGP11.Library.Hub.Transport.dll",
                    "iGP11.Library.Network.dll",
                    "iGP11.Library.Scheduler.dll",
                    "iGP11.Tool.Application.dll",
                    "iGP11.Tool.Application.Api.dll",
                    "iGP11.Tool.Domain.dll",
                    "iGP11.Tool.Infrastructure.Communication.dll",
                    "iGP11.Tool.Infrastructure.External.dll",
                    "iGP11.Tool.Infrastructure.Database.dll",
                    "iGP11.Tool.Localization.dll",
                    "iGP11.Tool.ReadModel.dll",
                    "iGP11.Tool.ReadModel.Api.dll",
                    "iGP11.Tool.Shared.dll"
                }
                       .Select(Assembly.LoadFrom))
                .ToArray();

                foreach (var assembly in assemblies)
                {
                    builder.RegisterAssemblyTypes(assembly)
                    .AsSelf()
                    .AsImplementedInterfaces();
                }

                builder.Register(
                    context =>
                {
                    var listener = new NetworkListener($"{applicationListenerUri}/", context.Resolve <ILogger>());
                    foreach (dynamic handler in assemblies.GetImplementations(typeof(INetworkCommandHandler)).Select(context.Resolve))
                    {
                        listener.Register(handler);
                    }

                    return(listener);
                })
                .SingleInstance()
                .AsSelf()
                .AsImplementedInterfaces();

                var proxyCommunicationPort = applicationSettings.ProxyCommunicationPort;
                var systemIpAddress        = constantSettings.SystemIpAddress;

                var eventPublisher = new EventPublisher();
                _scheduler = new BlockingScheduler(eventPublisher.Collect, Interval);
                _scheduler.Start();

                builder.Register(context => Logger.Current).SingleInstance().AsSelf().AsImplementedInterfaces();
                builder.Register(context => WriteDatabaseInitializer.DatabaseContext).SingleInstance().AsSelf().AsImplementedInterfaces();
                builder.Register(context => ReadDatabaseInitializer.Database).SingleInstance().AsSelf().AsImplementedInterfaces();
                builder.Register(context => new CommunicatorFactory(systemIpAddress, proxyCommunicationPort, Logger.Current)).AsSelf().AsImplementedInterfaces();
                builder.Register(context => eventPublisher).SingleInstance().AsSelf().AsImplementedInterfaces();
                builder.Register(context => constantSettings.Plugins).AsSelf().AsImplementedInterfaces();
                builder.Register(context => new NetworkPublisher(applicationListenerUri)).AsSelf().AsImplementedInterfaces();

                builder.Register(context => new GameSettingsProcessWatcher(
                                     constantSettings.Plugins,
                                     systemIpAddress,
                                     proxyCommunicationPort,
                                     context.Resolve <ComponentAssembler>(),
                                     context.Resolve <IDirectoryRepository>(),
                                     context.Resolve <IGameRepository>(),
                                     context.Resolve <IInjectionService>(),
                                     Logger.Current,
                                     context.Resolve <IProcessWatcher>()))
                .SingleInstance()
                .AsSelf()
                .AsImplementedInterfaces();

                var hubEventRepository      = new InMemoryEventRepository <HubEvent>();
                var endpointEventRepository = new InMemoryEventRepository <EndpointEvent>();
                var transport = new InMemoryTransport();
                var eventSerializerFactory = new DataContractEventSerializerFactory();
                var hubClientFactory       = new HubClientFactory(transport, eventSerializerFactory);

                _hub = new Hub(hubEventRepository, endpointEventRepository, transport, Logger.Current);
                var commandEndpointId = EndpointId.Generate();
                var queryEndpointId   = EndpointId.Generate();

                builder.Register(context => new DomainActionBuilder(new ActionBuilder(hubClientFactory, eventSerializerFactory), commandEndpointId)).AsSelf().AsImplementedInterfaces();
                DependencyResolver.Current = new AutofacResolver(builder.Build());

                var domainCommandHubHost = new DomainCommandHubHost(commandEndpointId, queryEndpointId, hubClientFactory);
                foreach (dynamic handler in assemblies.GetImplementations(typeof(IDomainCommandHandler <>)).Select(DependencyResolver.Current.Resolve))
                {
                    ListenFor(domainCommandHubHost, handler);
                }

                var domainEventHubHost = new DomainEventHubHost(queryEndpointId, hubClientFactory);
                foreach (dynamic handler in assemblies.GetImplementations(typeof(IDomainEventHandler <>)).Select(DependencyResolver.Current.Resolve))
                {
                    ListenFor(domainEventHubHost, handler);
                }

                domainCommandHubHost.Start();
                domainEventHubHost.Start();

                _initialized = true;
            }
        }