Example #1
0
        protected override string GetMappedDtoJsonMsg(ISdkTicket sdkTicket)
        {
            var ticket = sdkTicket as ITicketCashout;
            var dto    = _ticketMapper.Map(ticket);

            return(dto.ToJson());
        }
Example #2
0
        private ISdkTicket SendTicketBlockingBase(ISdkTicket ticket)
        {
            var stopwatch       = Stopwatch.StartNew();
            var responseTimeout = SendTicketBase(ticket, true);

            var autoResetEvent = new AutoResetEvent(false);

            _autoResetEventsForBlockingRequests.TryAdd(ticket.TicketId, autoResetEvent);

            autoResetEvent.WaitOne(TimeSpan.FromMilliseconds(responseTimeout));

            ISdkTicket responseTicket;

            if (_responsesFromBlockingRequests.TryRemove(ticket.TicketId, out responseTicket))
            {
                stopwatch.Stop();
                ReleaseAutoResetEventFromDictionary(ticket.TicketId);
                _executionLog.LogDebug($"Sending in blocking mode and successfully received response for {ticket.GetType().Name} {ticket.TicketId} took {stopwatch.ElapsedMilliseconds} ms.");
                return(responseTicket);
            }
            stopwatch.Stop();
            ReleaseAutoResetEventFromDictionary(ticket.TicketId);
            _executionLog.LogDebug($"Sending in blocking mode and waiting for receiving response for {ticket.GetType().Name} {ticket.TicketId} took {stopwatch.ElapsedMilliseconds} ms. Response not received in required timeout.");
            var msg = $"The timeout for receiving response elapsed. Org. {ticket.GetType().Name}: {ticket.TicketId}.";

            _executionLog.LogInformation(msg);
            throw new TimeoutException(msg);
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TicketResponseReceivedEventArgs"/> class
        /// </summary>
        /// <param name="response">a <see cref="ISdkTicket"/> representing the received response</param>
        public TicketResponseReceivedEventArgs(ISdkTicket response)
        {
            Guard.Argument(response, nameof(response)).NotNull();

            Response = response;
            Type     = MtsTicketHelper.Convert(response);
        }
Example #4
0
        /// <summary>
        /// Sends the ticket to the MTS server. The response will raise TicketResponseReceived event
        /// </summary>
        /// <param name="ticket">The <see cref="ISdkTicket"/> to be send</param>
        public void SendTicket(ISdkTicket ticket)
        {
            Guard.Argument(ticket, nameof(ticket)).NotNull();

            Metric.Context("MtsSdk").Meter("SendTicket", Unit.Items).Mark();
            InteractionLog.Info($"Called SendTicket with ticketId={ticket.TicketId}.");
            SendTicketBase(ticket, false);
        }
Example #5
0
        /// <summary>
        /// Sends the ticket to the MTS server. The response will raise TicketResponseReceived event
        /// </summary>
        /// <param name="ticket">The <see cref="ISdkTicket"/> to be send</param>
        public void SendTicket(ISdkTicket ticket)
        {
            Guard.Argument(ticket, nameof(ticket)).NotNull();

            _metricsRoot.Measure.Meter.Mark(new MeterOptions {
                Context = "MtsSdk", Name = "SendTicket", MeasurementUnit = Unit.Calls
            });
            _interactionLog.LogInformation($"Called SendTicket with ticketId={ticket.TicketId}.");
            SendTicketBase(ticket, false);
        }
Example #6
0
        public static SdkTicketType GetTicketTypeFromTicket(ISdkTicket ticket)
        {
            if (ticket is ITicket)
            {
                return(SdkTicketType.Ticket);
            }
            if (ticket is ITicketCancel)
            {
                return(SdkTicketType.TicketCancel);
            }
            if (ticket is ITicketAck)
            {
                return(SdkTicketType.TicketAck);
            }
            if (ticket is ITicketCancelAck)
            {
                return(SdkTicketType.TicketCancelAck);
            }
            if (ticket is ITicketResponse)
            {
                return(SdkTicketType.TicketResponse);
            }
            if (ticket is ITicketCancelResponse)
            {
                return(SdkTicketType.TicketCancelResponse);
            }
            if (ticket is ITicketReofferCancel)
            {
                return(SdkTicketType.TicketReofferCancel);
            }
            if (ticket is ITicketCashout)
            {
                return(SdkTicketType.TicketCashout);
            }
            if (ticket is ITicketCashoutResponse)
            {
                return(SdkTicketType.TicketCashoutResponse);
            }
            if (ticket is ITicketNonSrSettleResponse)
            {
                return(SdkTicketType.TicketNonSrSettleResponse);
            }
            if (ticket is ITicketNonSrSettle)
            {
                return(SdkTicketType.TicketNonSrSettle);
            }

            throw new ArgumentOutOfRangeException(nameof(ticket), "Unknown ticket type");
        }
Example #7
0
        /// <summary>
        /// Gets the get cache timeout
        /// </summary>
        /// <value>The get cache timeout</value>
        public int GetCacheTimeout(ISdkTicket ticket)
        {
            if (ticket == null)
            {
                return(_rabbitMqChannelSettings.MaxPublishQueueTimeoutInMs);
            }

            if (ticket is ITicket t)
            {
                if (t.Selections.Any(a => a.Id.StartsWith("lcoo")))
                {
                    return(_rabbitMqChannelSettings.PublishQueueTimeoutInMs2);
                }
            }
            return(_rabbitMqChannelSettings.PublishQueueTimeoutInMs1);
        }
Example #8
0
        /// <summary>
        /// Sends the ticket
        /// </summary>
        /// <param name="sdkTicket">The <see cref="ISdkTicket"/> to be send</param>
        public void SendTicket(ISdkTicket sdkTicket)
        {
            var msg = GetByteMsg(sdkTicket);

            if (string.IsNullOrEmpty(sdkTicket.CorrelationId))
            {
                _feedLog.Warn($"Ticket: {sdkTicket.TicketId} is missing CorrelationId.");
            }

            var ticketCI = new TicketCacheItem(TicketHelper.GetTicketTypeFromTicket(sdkTicket), sdkTicket.TicketId, sdkTicket.CorrelationId, _mtsChannelSettings.ReplyToRoutingKey, null, sdkTicket);

            // we clear cache, since already sent ticket with the same ticketId are obsolete (example: sending ticket, ticketAck, ticketCancel, ticketCancelAck)
            if (_ticketCache.TryRemove(sdkTicket.TicketId, out _))
            {
                _executionLog.Debug($"Removed already sent ticket from cache {sdkTicket.TicketId}");
            }

            _ticketCache.TryAdd(sdkTicket.TicketId, ticketCI);
            _publisherChannel.Publish(sdkTicket.TicketId, msg: msg, routingKey: _mtsChannelSettings.PublishRoutingKey, correlationId: sdkTicket.CorrelationId, replyRoutingKey: _mtsChannelSettings.ReplyToRoutingKey);
        }
Example #9
0
        /// <summary>
        /// Gets the byte MSG
        /// </summary>
        /// <param name="sdkTicket">The SDK ticket</param>
        /// <returns>System.Byte[]</returns>
        protected byte[] GetByteMsg(ISdkTicket sdkTicket)
        {
            var json = GetMappedDtoJsonMsg(sdkTicket);

            if (_feedLog.IsDebugEnabled)
            {
                _feedLog.Debug($"Sending {sdkTicket.GetType().Name}: {json}");
            }
            else
            {
                _feedLog.Info($"Sending {sdkTicket.GetType().Name} with ticketId: {sdkTicket.TicketId}.");
            }
            if (_executionLog.IsDebugEnabled)
            {
                _executionLog.Debug($"Sending {sdkTicket.GetType().Name}: {json}");
            }
            else
            {
                _executionLog.Info($"Sending {sdkTicket.GetType().Name} with ticketId: {sdkTicket.TicketId}.");
            }
            var msg = Encoding.UTF8.GetBytes(json);

            return(msg);
        }
Example #10
0
 public static TicketResponseType Convert(ISdkTicket ticket)
 {
     if (ticket == null)
     {
         throw new ArgumentNullException(nameof(ticket), "Ticket must not be null.");
     }
     if (ticket is ITicketResponse)
     {
         return(TicketResponseType.Ticket);
     }
     if (ticket is ITicketCancelResponse)
     {
         return(TicketResponseType.TicketCancel);
     }
     if (ticket is ITicketCashoutResponse)
     {
         return(TicketResponseType.TicketCashout);
     }
     if (ticket is ITicketNonSrSettleResponse)
     {
         return(TicketResponseType.TicketNonSrSettle);
     }
     throw new InvalidEnumArgumentException($"Invalid ticket type. Ticket type: {ticket.GetType()}.");
 }
Example #11
0
        private int SendTicketBase(ISdkTicket ticket, bool waitForResponse)
        {
            if (!ConnectionStatus.IsConnected)
            {
                var msg = $"Sending {ticket.GetType().Name} with ticketId: {ticket.TicketId} failed. Reason: disconnected from server.";
                _executionLog.LogWarning(msg);
                throw new InvalidOperationException(msg);
            }
            _executionLog.LogInformation($"Sending {ticket.GetType().Name} with ticketId: {ticket.TicketId}.");
            var ticketType   = TicketHelper.GetTicketTypeFromTicket(ticket);
            var ticketSender = _ticketPublisherFactory.GetTicketSender(ticketType);

            ticketSender.SendTicket(ticket);
            var ticketCacheTimeout = ticketSender.GetCacheTimeout(ticket);

            if (waitForResponse)
            {
                return(ticketCacheTimeout);
            }
            else
            {
                lock (_lockForTicketsForNonBlockingRequestsCache)
                {
                    if (TicketResponseTimedOut != null)
                    {
                        _cacheItemPolicyForTicketsForNonBlockingRequestsCache = new CacheItemPolicy
                        {
                            AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMilliseconds(ticketCacheTimeout)),
                            RemovedCallback    = RemovedFromCacheForTicketsForNonBlockingRequestsCallback
                        };
                        _ticketsForNonBlockingRequests.Add(ticket.TicketId, ticket, _cacheItemPolicyForTicketsForNonBlockingRequestsCache);
                    }
                }
            }
            return(-1);
        }
Example #12
0
 /// <summary>
 /// Gets the mapped dto json MSG
 /// </summary>
 /// <param name="sdkTicket">The SDK ticket</param>
 /// <returns>System.String</returns>
 protected abstract string GetMappedDtoJsonMsg(ISdkTicket sdkTicket);
 /// <summary>
 /// Initializes a new instance of the <see cref="TicketMessageEventArgs"/> class
 /// </summary>
 /// <param name="id">The ticketId</param>
 /// <param name="ticket">The associated ticket</param>
 /// <param name="msg">The message</param>
 public TicketMessageEventArgs(string id, ISdkTicket ticket, string msg)
 {
     TicketId = id;
     Ticket   = ticket;
     Message  = msg;
 }
Example #14
0
 /// <summary>
 /// Serialize given ticket
 /// </summary>
 /// <param name="ticket">Ticket to be serialized</param>
 /// <returns>Json representation of the given ticket</returns>
 public static string Serialize(ISdkTicket ticket)
 {
     return(JsonConvert.SerializeObject(ticket));
 }
Example #15
0
        public static SdkTicketType GetTicketAckTypeFromTicket(ISdkTicket ticket)
        {
            var ticketType = GetTicketTypeFromTicket(ticket);

            return(ticketType == SdkTicketType.Ticket ? SdkTicketType.TicketAck : SdkTicketType.TicketCancelAck);
        }