Esempio n. 1
0
 /// <summary>  Sends a SIF infrastructure message and returns the response.</summary>
 /// <remarks>
 /// The message content should consist of a complete <SIF_Message> element.
 /// This method sends whatever content is passed to it without any checking
 /// or validation of any kind.
 /// </remarks>
 /// <param name="msg">The message content</param>
 /// <returns> The response from the ZIS (expected to be a <SIF_Ack> message)
 /// </returns>
 /// <exception cref="AdkMessagingException"> is thrown if there is an error sending
 /// the message to the Zone Integration Server
 /// </exception>
 public IMessageInputStream Send(IMessageOutputStream msg)
 {
     try {
         return(TrySend(msg));
     }
     catch (WebException webEx) {
         if (webEx.Status == WebExceptionStatus.ConnectionClosed)
         {
             // Try one more time, the underlying keep-alive connection must have
             // been closed by the ZIS. Trying again will start with a fresh,
             // new connection
             try {
                 return(TrySend(msg));
             }
             catch (AdkException) {
                 throw;
             }
             catch (Exception ex) {
                 throw new AdkMessagingException
                           ("HttpProtocolHandler: Unexpected error sending message retry: " + ex,
                           fZone);
             }
         }
         else
         {
             // This code should never be hit because TrySend() should never emit this exception
             // Leaving the code here for defensive purposes
             throw new AdkMessagingException
                       ("HttpProtocolHandler: Unexpected error sending message: " + webEx, fZone);
         }
     }
 }
        //public string Send(string msg)
        //{
        //   lock (this)
        //   {
        //      fMessages.AddLast(msg);
        //   }
        //   return makeAck();
        //}

        public IMessageInputStream Send( IMessageOutputStream msg )
        {
            lock ( this )
            {
                try
                {
                    MemoryStream stream = new MemoryStream();
                    msg.CopyTo( stream );
                    stream.Seek( 0, SeekOrigin.Begin );
                    SifParser parser = SifParser.NewInstance();
                    SifMessagePayload smp = (SifMessagePayload) parser.Parse( stream, fZone );

                    fMessages.Add( smp );
                    parser = null;

                    SIF_Ack ack = smp.ackStatus( 0 );
                    SIF_Header hdr = ack.Header;
                    hdr.SIF_Timestamp = DateTime.Now;
                    hdr.SIF_MsgId = Adk.MakeGuid();
                    hdr.SIF_SourceId = fZone.Agent.Id;

                    StringWriter str = new StringWriter();
                    SifWriter writer = new SifWriter( str );
                    writer.Write( ack );
                    writer.Flush();
                    writer.Close();
                    writer = null;
                    return new MessageStreamImpl( str.ToString() );
                }
                catch( Exception ex )
                {
                    // Possible error parsing. Write the message to console out
                    Console.Out.WriteLine(msg.Decode());
                    throw new AdkMessagingException(ex.Message, fZone, ex);
                }
            }
        }
        //public string Send(string msg)
        //{
        //   lock (this)
        //   {
        //      fMessages.AddLast(msg);
        //   }
        //   return makeAck();
        //}
        public IMessageInputStream Send( IMessageOutputStream msg )
        {
            lock ( this )
            {
                try
                {
                    MemoryStream stream = new MemoryStream();
                    msg.CopyTo( stream );
                    stream.Seek( 0, SeekOrigin.Begin );
                    SifParser parser = SifParser.NewInstance();
                    SifMessagePayload smp = (SifMessagePayload) parser.Parse( stream, fZone );

                    fMessages.Add( smp );
                    parser = null;

                    SIF_Ack ack = smp.ackStatus( 0 );
                    SIF_Header hdr = ack.Header;
                    hdr.SIF_Timestamp = DateTime.Now;
                    hdr.SIF_MsgId = Adk.MakeGuid();
                    hdr.SIF_SourceId = fZone.Agent.Id;

                    StringWriter str = new StringWriter();
                    SifWriter writer = new SifWriter( str );
                    writer.Write( ack );
                    writer.Flush();
                    writer.Close();
                    writer = null;
                    return new MessageStreamImpl( str.ToString() );
                }
                catch( Exception ex )
                {
                    // Possible error parsing. Write the message to console out
                    Console.Out.WriteLine(msg.Decode());
                    throw new AdkMessagingException(ex.Message, fZone, ex);
                }
            }
        }
Esempio n. 4
0
        private IMessageInputStream TrySend(IMessageOutputStream msg)
        {
            MessageStreamImpl returnStream;
            Stream            reqStream;

            HttpWebRequest conn = GetConnection(fZoneUrl);

            conn.ContentLength = msg.Length;

            try {
                reqStream = conn.GetRequestStream();
            }
            catch (WebException webEx) {
                if (webEx.Status == WebExceptionStatus.ConnectionClosed)
                {
                    // This could be a keep-alive connection that was closed unexpectedly
                    // rethrow so that retry handling can take affect
                    throw;
                }
                else
                {
                    throw new AdkTransportException
                              ("Could not establish a connection to the ZIS (" + fZoneUrl.AbsoluteUri +
                              "): " + webEx, fZone, webEx);
                }
            }
            catch (Exception thr) {
                throw new AdkTransportException
                          ("Could not establish a connection to the ZIS (" + fZoneUrl.AbsoluteUri + "): " +
                          thr, fZone, thr);
            }

            try {
                if ((Adk.Debug & AdkDebugFlags.Transport) != 0)
                {
                    fZone.Log.Debug("Sending message (" + msg.Length + " bytes)");
                }
                if ((Adk.Debug & AdkDebugFlags.Message_Content) != 0)
                {
                    fZone.Log.Debug(msg.Decode());
                }
                try {
                    msg.CopyTo(reqStream);
                    reqStream.Flush();
                    reqStream.Close();
                }
                catch (Exception thr) {
                    throw new AdkMessagingException
                              ("HttpProtocolHandler: Unexpected error sending message: " + thr, fZone);
                }

                try {
                    using (WebResponse response = conn.GetResponse()) {
                        if ((Adk.Debug & AdkDebugFlags.Transport) != 0)
                        {
                            fZone.Log.Debug
                                ("Expecting reply (" + response.ContentLength + " bytes)");
                        }

                        returnStream = new MessageStreamImpl(response.GetResponseStream());

                        response.Close();

                        if ((Adk.Debug & AdkDebugFlags.Transport) != 0)
                        {
                            fZone.Log.Debug("Received reply (" + returnStream.Length + " bytes)");
                        }
                        if ((Adk.Debug & AdkDebugFlags.Message_Content) != 0)
                        {
                            fZone.Log.Debug(returnStream.Decode());
                        }
                    }
                }
                catch (Exception thr) {
                    throw new AdkTransportException
                              ("An unexpected error occurred while receiving data from the ZIS: " + thr,
                              fZone);
                }
            }
            catch (AdkException) {
                // rethrow anything that's already wrapped in an AdkException
                throw;
            }
            catch (Exception thr) {
                throw new AdkMessagingException
                          ("HttpProtocolHandler: Error receiving response to sent message: " + thr, fZone);
            }

            return(returnStream);
        }
        private IMessageInputStream TrySend( IMessageOutputStream msg )
        {
            MessageStreamImpl returnStream;
            Stream reqStream;

            HttpWebRequest conn = GetConnection( fZoneUrl );
            conn.ContentLength = msg.Length;

            try {
                reqStream = conn.GetRequestStream();
            }
            catch ( WebException webEx ) {
                if ( webEx.Status == WebExceptionStatus.ConnectionClosed ) {
                    // This could be a keep-alive connection that was closed unexpectedly
                    // rethrow so that retry handling can take affect
                    throw;
                }
                else {
                    throw new AdkTransportException
                        ( "Could not establish a connection to the ZIS (" + fZoneUrl.AbsoluteUri +
                          "): " + webEx, fZone, webEx );
                }
            }
            catch ( Exception thr ) {
                throw new AdkTransportException
                    ( "Could not establish a connection to the ZIS (" + fZoneUrl.AbsoluteUri + "): " +
                      thr, fZone, thr );
            }

            try {
                if ( (Adk.Debug & AdkDebugFlags.Transport) != 0 ) {
                    fZone.Log.Debug( "Sending message (" + msg.Length + " bytes)" );
                }
                if ( (Adk.Debug & AdkDebugFlags.Message_Content) != 0 ) {
                    fZone.Log.Debug( msg.Decode() );
                }
                try {
                    msg.CopyTo( reqStream );
                    reqStream.Flush();
                    reqStream.Close();
                }
                catch ( Exception thr ) {
                    throw new AdkMessagingException
                        ( "HttpProtocolHandler: Unexpected error sending message: " + thr, fZone );
                }

                try {
                    using ( WebResponse response = conn.GetResponse() ) {
                        if ( (Adk.Debug & AdkDebugFlags.Transport) != 0 ) {
                            fZone.Log.Debug
                                ( "Expecting reply (" + response.ContentLength + " bytes)" );
                        }

                        returnStream = new MessageStreamImpl( response.GetResponseStream() );

                        response.Close();

                        if ( (Adk.Debug & AdkDebugFlags.Transport) != 0 ) {
                            fZone.Log.Debug( "Received reply (" + returnStream.Length + " bytes)" );
                        }
                        if ( (Adk.Debug & AdkDebugFlags.Message_Content) != 0 ) {
                            fZone.Log.Debug( returnStream.Decode() );
                        }
                    }
                }
                catch ( Exception thr ) {
                    throw new AdkTransportException
                        ( "An unexpected error occurred while receiving data from the ZIS: " + thr,
                          fZone );
                }
            }
            catch ( AdkException ) {
                // rethrow anything that's already wrapped in an AdkException
                throw;
            }
            catch ( Exception thr ) {
                throw new AdkMessagingException
                    ( "HttpProtocolHandler: Error receiving response to sent message: " + thr, fZone );
            }

            return returnStream;
        }
 /// <summary>  Sends a SIF infrastructure message and returns the response.</summary>
 /// <remarks>
 /// The message content should consist of a complete <SIF_Message> element.
 /// This method sends whatever content is passed to it without any checking
 /// or validation of any kind.
 /// </remarks>
 /// <param name="msg">The message content</param>
 /// <returns> The response from the ZIS (expected to be a <SIF_Ack> message)
 /// </returns>
 /// <exception cref="AdkMessagingException"> is thrown if there is an error sending
 /// the message to the Zone Integration Server
 /// </exception>
 public IMessageInputStream Send( IMessageOutputStream msg )
 {
     try {
         return TrySend( msg );
     }
     catch ( WebException webEx ) {
         if ( webEx.Status == WebExceptionStatus.ConnectionClosed ) {
             // Try one more time, the underlying keep-alive connection must have
             // been closed by the ZIS. Trying again will start with a fresh,
             // new connection
             try {
                 return TrySend( msg );
             }
             catch ( AdkException ) {
                 throw;
             }
             catch ( Exception ex ) {
                 throw new AdkMessagingException
                     ( "HttpProtocolHandler: Unexpected error sending message retry: " + ex,
                       fZone );
             }
         }
         else {
             // This code should never be hit because TrySend() should never emit this exception
             // Leaving the code here for defensive purposes
             throw new AdkMessagingException
                 ( "HttpProtocolHandler: Unexpected error sending message: " + webEx, fZone );
         }
     }
 }