/// <summary>
        /// Handles the arrival of a response to a message
        /// </summary>
        /// <param name="state">Is a RequestResponse object</param>
        private void HandleResponseThreadProc(object state)
        {
            RequestResponse response = (RequestResponse)state;

            if (response.Message != null)
            {
                Debug.WriteLine("Request - Handling response");
                MessageAccess msg   = new MessageAccess(response.Message.XmlData);
                string        msgId = msg.GetMessageId();
                if (msgId != null)
                {
                    _messagesLeft--;
                    string str = string.Format("Request - Response to {0}, {1}",
                                               msgId, response.Outcome);
                    Debug.WriteLine(str);
                    decimal id = decimal.Parse(msgId);
                    UpdateMessageStatus(id,
                                        (response.Outcome == RequestOutcome.SendOK) ?
                                        MessageStatus.Sent : MessageStatus.Failed);
                    if (_responseHandler != null)
                    {
                        _responseHandler(id, response.Outcome, response.Message);
                    }
                    if (_messagesLeft == 0)
                    {
                        FinalizeRequest();
                    }
                }
            }
        }
 /// <summary>
 /// Stores the identifiers of the messages to wait for
 /// </summary>
 /// <param name="ds"></param>
 private void StoreMessageIds(DataSet ds)
 {
     if (ds != null && ds.Tables.Count > 0)
     {
         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
         {
             string xml =
                 (string)ds.Tables[0].Rows[i][(int)MessagesObject.MsgsColumns.Xml];
             MessageAccess msg = new MessageAccess(xml);
             decimal       id  = decimal.Parse(msg.GetMessageId());
             _msgsResponses[id] = null;
         }
     }
 }
 private void OnChannelIncomingMessage(OPSTelegrama packet, IChannel channel)
 {
     if (MessageArrived != null)
     {
         if (packet != null)
         {
             XmlDocument doc = new XmlDocument();
             doc.LoadXml(packet.XmlData);
             XmlNode root = doc;
             if (root.ChildNodes.Count > 0)
             {
                 /// Is a packet containing messages
                 UpdateAddressCache(packet, channel.Uri);
                 IPacketizer pt = packet.Packetizer;
                 for (int i = 0; i < pt.PacketsCount; i++)
                 {
                     string msg = pt[i];
                     Debug.WriteLine("MsgProc - Arrived: " + msg);
                     MessageAccess acc   = new MessageAccess(msg);
                     string        msgId = acc.GetMessageId();
                     MessageArrived(Convert.ToDecimal(msgId), RequestOutcome.SendOK,
                                    msg, this, packet.Packetizer.PacketInfo.SourceId);
                 }
             }
             else
             {
                 /// Is an ACK eventually containing response data
                 MessageAccess acc   = new MessageAccess(packet.XmlData);
                 string        msgId = acc.GetMessageId();
                 MessageArrived(Convert.ToDecimal(msgId), RequestOutcome.SendOK,
                                packet.XmlData, this, packet.Packetizer.PacketInfo.SourceId);
             }
         }
     }
     else
     {
         CommMain.Logger.AddLog("WARNING: MessageProcessor.OnChannelIncomingMessage: MessageArrived is null.", LoggerSeverities.Debug);
     }
 }
        /// <summary>
        /// Handles notifications
        /// </summary>
        /// <param name="args">The notification data</param>
        private void DoNotify(NotificationArgs args)
        {
            if (args.Outcome == RequestOutcome.SendOK)
            {
                bool          notify    = true;
                MessageAccess msgAccess = new MessageAccess(args.Response.XmlData);
                string        msgId     = msgAccess.GetMessageId();
                if (msgId != null && msgId.Length > 0)
                {
                    if (_msgsIds.ContainsKey(Convert.ToDecimal(msgId)))
                    {
                        string msgName = msgAccess.GetMessageName();
                        notify = HandleAcks(msgName, ref args);
                        if (notify)
                        {
                            string str = string.Format("RequestAdapter - About to notify response: {0}, {1}",
                                                       args.Outcome, args.Response.XmlData);
                            Debug.WriteLine(str);
                            if (_responseHandler != null)
                            {
                                _responseHandler(args.Outcome, args.Response);
                            }

                            _messageCount--;
                            if (_messageCount == 0)
                            {
                                _waiting = false;
                            }
                        }
                        else
                        {
                            string str = string.Format("RequestChannelAdapter.NotifyThreadProc - ACK not notified: {0}",
                                                       args.Response.XmlData);
                            Debug.WriteLine(str);
                        }
                    }
                }
                else
                {
                    string str = string.Format("RequestChannelAdapter.NotifyThreadProc - Bad message: {0}",
                                               args.Response.XmlData);
                    Debug.WriteLine(str);
                }
            }
            else
            {
                string str = string.Format("RequestChannelAdapter.NotifyThreadProc - Notifying error: {0}",
                                           args.Outcome);
                Debug.WriteLine(str);
                if (_responseHandler != null)
                {
                    _responseHandler(args.Outcome, null);
                }

                UninitializeChannel(_channel);
                string uri = _channel.Uri;
                try
                {
                    str = string.Format("RequestChannelAdapter.NotifyThreadProc - Closing channel: {0}",
                                        uri);
                    Debug.WriteLine(str);
                    Parameterization.ChannelManager.CloseChannel(uri);

/*					str = string.Format("RequestChannelAdapter.NotifyThreadProc - Opening channel: {0}",
 *                                              uri);
 *                                      Debug.WriteLine(str);
 *                                      _channel = Parameterization.ChannelManager.OpenChannel(uri);
 *                                      InitializeChannel(_channel);
 *                                      str = string.Format("RequestChannelAdapter.NotifyThreadProc - Reattached to channel: {0}",
 *                                              uri);
 *                                      Debug.WriteLine(str);*/
                }
                catch (Exception ex)
                {
                    //str = string.Format("RequestChannelAdapter.NotifyThreadProc - Error reopening channnel: {0}", ex.Message);
                    //Debug.WriteLine(str);
                    CommMain.Logger.AddLog(ex);
                }
            }
        }