Beispiel #1
0
        private void ProcessIncomingRequest(HL7Request response)
        {
            HL7Request req = null;

            var v = m_OutgoingRequests.Where((request, b) =>
            {
                if (response.Request.IsResponseForRequest(request.Request))
                {
                    request.Response = response.Request;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }).ToList();


            if (v.Count() == 0)
            {
                m_HL7Server.Logger.Error("Unexpected response received");
            }
            else
            {
                req = v.FirstOrDefault();

                req.ResponseReceivedEvent.Set();
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ack"></param>
        /// <returns></returns>
        private void ProcessIncomingAck(IHL7Message ack)
        {
            HL7Request req = null;

            var v = m_OutgoingRequests.Where((request, b) =>
            {
                if (ack.IsAckForRequest(request.Request) && request.Acknowledgment == null)
                {
                    request.Acknowledgment = ack as IHL7Message;

                    return(true);
                }
                else
                {
                    return(false);
                }
            }).ToList();

            if (v.Count() == 0)
            {
                string log = "Unexpected ack received or ack received to late";
                m_HL7Server.Logger.Error(log);
            }
            else if (v.Count() > 1)
            {
                string log = "each ack should be bount to a single request";
                throw new HL7InterfaceException(log);
            }
            else
            {
                req = v.FirstOrDefault();

                req.AckReceivedEvent.Set();
            }
        }
Beispiel #3
0
        private bool SendMessageOne(HL7Request hl7Request, ref int responseRetries)
        {
            hl7Request.RequestCancellationToken.Token.ThrowIfCancellationRequested();
            int ackRetries = m_HL7Protocol.Config.MaxAckRetriesNumber;

            m_HL7Server.Logger.Info("Entering");

            lock (m_AckReceivedLock)
            {
                m_HL7Server.Logger.Info("Critical Area");
                while (!SendAndWaitForAck(hl7Request, ref ackRetries))
                {
                    ;
                }
            }
            m_HL7Server.Logger.Info("Exit");

            if (!Protocol.Config.IsResponseRequired)
            {
                return(true);
            }

            if (!hl7Request.ResponseReceivedEvent.Wait(Protocol.Config.ResponseTimeout, hl7Request.RequestCancellationToken.Token))
            {
                if (responseRetries-- > 0)
                {
                    return(false);;
                }
                else
                {
                    throw new HL7InterfaceException($"The message was not acknowledged after a total number of {ackRetries} retries");
                }
            }
            return(true);
        }
Beispiel #4
0
        private bool SendAndWaitForAck(HL7Request hl7Request, ref int ackRetries)
        {
            m_HL7Server.Logger.Info($"Sending {hl7Request.Request.ControlID}");
            GlobalWatch.Restart();

            m_EasyClient.Send(Encoding.ASCII.GetBytes(MLLP.CreateMLLPMessage(hl7Request.Request.Encode())));


            m_HL7Server.Logger.Info($"Sent {hl7Request.Request.ControlID}");

            string logMessage = string.Empty;

            logMessage = $"{hl7Request.Request.MessageID} sent (Ack RETRY) [{m_HL7Protocol.Config.MaxAckRetriesNumber}, {ackRetries}]";

            if (!Protocol.Config.IsAckRequired)
            {
                return(true);
            }

            if (!m_OutgoingRequests.Contains(hl7Request))
            {
                throw new OperationCanceledException("");
            }


            if (hl7Request.RequestCancellationToken.Token.IsCancellationRequested)
            {
                throw new OperationCanceledException(hl7Request.RequestCancellationToken.Token);
            }

            hl7Request.RequestCancellationToken.Token.ThrowIfCancellationRequested();

            m_HL7Server.Logger.Info($"Waiting for ack {hl7Request.Request.ControlID}");
            if (!hl7Request.AckReceivedEvent.Wait(Protocol.Config.AckTimeout, hl7Request.RequestCancellationToken.Token))
            {
                m_HL7Server.Logger.Info($"Ack received: {hl7Request.Request.ControlID}");
                if (ackRetries-- > 0)
                {
                    return(false);
                }
                else
                {
                    throw new HL7InterfaceException($"The message was not acknowledged after a total number of {m_HL7Protocol.Config.MaxAckRetriesNumber} retries");
                }
            }
            return(true);
        }
Beispiel #5
0
        public async Task <HL7Request> SendHL7MessageAsync(IHL7Message message)
        {
            HL7Request hl7Request = new HL7Request(message);

            if (!m_OutgoingRequests1.TryAdd(message.ControlID, hl7Request))
            {
                throw new HL7InterfaceException("m_OutgoingRequests");
            }

            m_OutgoingRequests.Add(hl7Request);

            TaskCompletionSource <HL7Request> senderTaskCompletionSource = new TaskCompletionSource <HL7Request>();

            hl7Request.SenderTask = Task.Run(() =>
            {
                bool success = false;

                int responseRetries = m_HL7Protocol.Config.MaxResponseRetriesNumber;

                try
                {
                    do
                    {
                        success = SendMessageOne(hl7Request, ref responseRetries);
                    }while (!success);
                }
                catch (Exception ex)
                {
                    if (ex is HL7InterfaceException)
                    {
                        throw ex;
                    }
                }
                finally { hl7Request.RequestCompletedEvent.Set(); }

                return(hl7Request);
            }, hl7Request.RequestCancellationToken.Token);

            return(await hl7Request.SenderTask); //.ConfigureAwait(false);
        }
Beispiel #6
0
 private void OnNewRequestReceived(HL7Session session, HL7Request requestInfo)
 {
     ProcessIncomingRequest(requestInfo);
 }