Esempio n. 1
0
        /// <summary>
        /// Constructs a new instance of the <see cref="MtsSdk"/> class
        /// </summary>
        /// <param name="config">A <see cref="ISdkConfiguration"/> instance representing feed configuration</param>
        /// <param name="loggerFactory">A <see cref="ILoggerFactory"/> used to create <see cref="ILogger"/> used within sdk</param>
        /// <param name="metricsRoot">A <see cref="IMetricsRoot"/> used to provide metrics within sdk</param>
        public MtsSdk(ISdkConfiguration config, ILoggerFactory loggerFactory = null, IMetricsRoot metricsRoot = null)
        {
            Guard.Argument(config, nameof(config)).NotNull();

            LogInit();

            _isDisposed = false;
            _isOpened   = 0;

            _unityContainer = new UnityContainer();
            _unityContainer.RegisterTypes(config, loggerFactory, metricsRoot);
            _config = _unityContainer.Resolve <ISdkConfigurationInternal>();

            _executionLog   = SdkLoggerFactory.GetLoggerForExecution(typeof(MtsSdk));
            _interactionLog = SdkLoggerFactory.GetLoggerForExecution(typeof(MtsSdk));

            LogInit();

            _metricsRoot          = _unityContainer.Resolve <IMetricsRoot>();
            _metricsLogger        = SdkLoggerFactory.GetLoggerForStats(typeof(MtsSdk));
            _metricsTaskScheduler = new AppMetricsTaskScheduler(
                TimeSpan.FromSeconds(_config.StatisticsTimeout),
                async() => { await LogMetricsAsync(); });


            _connectionValidator = _unityContainer.Resolve <ConnectionValidator>();

            BuilderFactory          = _unityContainer.Resolve <IBuilderFactory>();
            _ticketPublisherFactory = _unityContainer.Resolve <ITicketSenderFactory>();

            _entitiesMapper = _unityContainer.Resolve <EntitiesMapper>();

            _rabbitMqMessageReceiverForTickets           = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketCancels     = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketCancelResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketCashouts    = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketCashoutResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketNonSrSettle = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketNonSrSettleResponseMessageReceiver");

            ClientApi        = _unityContainer.Resolve <IMtsClientApi>();
            CustomBetManager = _unityContainer.Resolve <ICustomBetManager>();
            ConnectionStatus = _unityContainer.Resolve <IConnectionStatus>();
            ReportManager    = _unityContainer.Resolve <IReportManager>();

            _autoResetEventsForBlockingRequests        = new ConcurrentDictionary <string, AutoResetEvent>();
            _responsesFromBlockingRequests             = new ConcurrentDictionary <string, ISdkTicket>();
            _ticketsForNonBlockingRequests             = new MemoryCache("TicketsForNonBlockingRequests");
            _lockForTicketsForNonBlockingRequestsCache = new object();

            foreach (var t in Enum.GetValues(typeof(SdkTicketType)))
            {
                var publisher = _ticketPublisherFactory.GetTicketSender((SdkTicketType)t);
                if (publisher != null)
                {
                    publisher.TicketSendFailed += PublisherOnTicketSendFailed;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Constructs a new instance of the <see cref="MtsSdk"/> class
        /// </summary>
        /// <param name="config">A <see cref="ISdkConfiguration"/> instance representing feed configuration</param>
        public MtsSdk(ISdkConfiguration config)
        {
            Guard.Argument(config, nameof(config)).NotNull();

            LogInit();

            _isDisposed = false;
            _isOpened   = 0;

            _unityContainer = new UnityContainer();
            SdkUnityBootstrapper.RegisterTypes(_unityContainer, config);

            _config = _unityContainer.Resolve <ISdkConfigurationInternal>();
            _connectionValidator = _unityContainer.Resolve <ConnectionValidator>();

            BuilderFactory          = _unityContainer.Resolve <IBuilderFactory>();
            _ticketPublisherFactory = _unityContainer.Resolve <ITicketSenderFactory>();

            _entitiesMapper = _unityContainer.Resolve <EntitiesMapper>();

            _rabbitMqMessageReceiverForTickets           = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketCancels     = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketCancelResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketCashouts    = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketCashoutResponseMessageReceiver");
            _rabbitMqMessageReceiverForTicketNonSrSettle = _unityContainer.Resolve <IRabbitMqMessageReceiver>("TicketNonSrSettleResponseMessageReceiver");

            ClientApi        = _unityContainer.Resolve <IMtsClientApi>();
            CustomBetManager = _unityContainer.Resolve <ICustomBetManager>();
            ConnectionStatus = _unityContainer.Resolve <IConnectionStatus>();
            ReportManager    = _unityContainer.Resolve <IReportManager>();

            _autoResetEventsForBlockingRequests        = new ConcurrentDictionary <string, AutoResetEvent>();
            _responsesFromBlockingRequests             = new ConcurrentDictionary <string, ISdkTicket>();
            _ticketsForNonBlockingRequests             = new MemoryCache("TicketsForNonBlockingRequests");
            _lockForTicketsForNonBlockingRequestsCache = new object();

            foreach (var t in Enum.GetValues(typeof(SdkTicketType)))
            {
                var publisher = _ticketPublisherFactory.GetTicketSender((SdkTicketType)t);
                if (publisher != null)
                {
                    publisher.TicketSendFailed += PublisherOnTicketSendFailed;
                }
            }
        }
        private static void RegisterTicketSenders(IUnityContainer container)
        {
            var ticketCache = new ConcurrentDictionary <string, TicketCacheItem>();

            container.RegisterType <ITicketSender>(new ContainerControlledLifetimeManager());
            var ticketSender = new TicketSender(new TicketMapper(),
                                                container.Resolve <IRabbitMqPublisherChannel>("TicketPublisherChannel"),
                                                ticketCache,
                                                container.Resolve <IMtsChannelSettings>("TicketChannelSettings"),
                                                container.Resolve <IRabbitMqChannelSettings>("TicketChannelSettings"));
            var ticketAckSender = new TicketAckSender(new TicketAckMapper(),
                                                      container.Resolve <IRabbitMqPublisherChannel>("TicketAckPublisherChannel"),
                                                      ticketCache,
                                                      container.Resolve <IMtsChannelSettings>("TicketAckChannelSettings"),
                                                      container.Resolve <IRabbitMqChannelSettings>("TicketChannelSettings"));
            var ticketCancelSender = new TicketCancelSender(new TicketCancelMapper(),
                                                            container.Resolve <IRabbitMqPublisherChannel>("TicketCancelPublisherChannel"),
                                                            ticketCache,
                                                            container.Resolve <IMtsChannelSettings>("TicketCancelChannelSettings"),
                                                            container.Resolve <IRabbitMqChannelSettings>("TicketCancelChannelSettings"));
            var ticketCancelAckSender = new TicketCancelAckSender(new TicketCancelAckMapper(),
                                                                  container.Resolve <IRabbitMqPublisherChannel>("TicketCancelAckPublisherChannel"),
                                                                  ticketCache,
                                                                  container.Resolve <IMtsChannelSettings>("TicketCancelAckChannelSettings"),
                                                                  container.Resolve <IRabbitMqChannelSettings>("TicketCancelChannelSettings"));
            var ticketReofferCancelSender = new TicketReofferCancelSender(new TicketReofferCancelMapper(),
                                                                          container.Resolve <IRabbitMqPublisherChannel>("TicketReofferCancelPublisherChannel"),
                                                                          ticketCache,
                                                                          container.Resolve <IMtsChannelSettings>("TicketReofferCancelChannelSettings"),
                                                                          container.Resolve <IRabbitMqChannelSettings>("TicketCancelChannelSettings"));
            var ticketCashoutSender = new TicketCashoutSender(new TicketCashoutMapper(),
                                                              container.Resolve <IRabbitMqPublisherChannel>("TicketCashoutPublisherChannel"),
                                                              ticketCache,
                                                              container.Resolve <IMtsChannelSettings>("TicketCashoutChannelSettings"),
                                                              container.Resolve <IRabbitMqChannelSettings>("TicketCashoutChannelSettings"));
            var ticketNonSrSettleSender = new TicketNonSrSettleSender(new TicketNonSrSettleMapper(),
                                                                      container.Resolve <IRabbitMqPublisherChannel>("TicketNonSrSettlePublisherChannel"),
                                                                      ticketCache,
                                                                      container.Resolve <IMtsChannelSettings>("TicketNonSrSettleChannelSettings"),
                                                                      container.Resolve <IRabbitMqChannelSettings>("TicketNonSrSettleChannelSettings"));

            container.RegisterInstance <ITicketSender>("TicketSender", ticketSender);
            container.RegisterInstance <ITicketSender>("TicketAckSender", ticketAckSender);
            container.RegisterInstance <ITicketSender>("TicketCancelSender", ticketCancelSender);
            container.RegisterInstance <ITicketSender>("TicketCancelAckSender", ticketCancelAckSender);
            container.RegisterInstance <ITicketSender>("TicketReofferCancelSender", ticketReofferCancelSender);
            container.RegisterInstance <ITicketSender>("TicketCashoutSender", ticketCashoutSender);
            container.RegisterInstance <ITicketSender>("TicketNonSrSettleSender", ticketNonSrSettleSender);

            var senders = new Dictionary <SdkTicketType, ITicketSender>
            {
                { SdkTicketType.Ticket, container.Resolve <ITicketSender>("TicketSender") },
                { SdkTicketType.TicketAck, container.Resolve <ITicketSender>("TicketAckSender") },
                { SdkTicketType.TicketCancel, container.Resolve <ITicketSender>("TicketCancelSender") },
                { SdkTicketType.TicketCancelAck, container.Resolve <ITicketSender>("TicketCancelAckSender") },
                { SdkTicketType.TicketReofferCancel, container.Resolve <ITicketSender>("TicketReofferCancelSender") },
                { SdkTicketType.TicketCashout, container.Resolve <ITicketSender>("TicketCashoutSender") },
                { SdkTicketType.TicketNonSrSettle, container.Resolve <ITicketSender>("TicketNonSrSettleSender") },
            };

            var senderFactory = new TicketSenderFactory(senders);

            container.RegisterType <ITicketSenderFactory, TicketSenderFactory>(new ContainerControlledLifetimeManager());
            container.RegisterInstance(senderFactory);

            var entityMapper = new EntitiesMapper(ticketAckSender, ticketCancelAckSender);

            container.RegisterType <EntitiesMapper>(new ContainerControlledLifetimeManager());
            container.RegisterInstance(entityMapper);
        }