You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by st...@apache.org on 2006/11/25 23:04:50 UTC

svn commit: r479211 [6/15] - in /incubator/qpid/trunk/qpid/dotnet: ./ Qpid.Buffer/ Qpid.Buffer/Properties/ Qpid.Client.Tests/ Qpid.Client.Tests/Common/ Qpid.Client.Tests/HeadersExchange/ Qpid.Client.Tests/MultiConsumer/ Qpid.Client.Tests/Properties/ Qp...

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidBytesMessageFactory.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidBytesMessageFactory.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidBytesMessageFactory.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidBytesMessageFactory.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,60 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Collections;
+using Qpid.Framing;
+
+namespace Qpid.Client.Message
+{
+    public class QpidBytesMessageFactory : AbstractQmsMessageFactory
+    {
+        protected override AbstractQmsMessage CreateMessageWithBody(ulong messageNbr,
+                                                                    ContentHeaderBody contentHeader,
+                                                                    IList bodies)
+        {
+            byte[] data;
+
+            // we optimise the non-fragmented case to avoid copying
+            if (bodies != null && bodies.Count == 1)
+            {
+                data = ((ContentBody)bodies[0]).Payload;
+            }
+            else
+            {
+                data = new byte[(long)contentHeader.BodySize];
+                int currentPosition = 0;
+                foreach (ContentBody cb in bodies)
+                {
+                    Array.Copy(cb.Payload, 0, data, currentPosition, cb.Payload.Length);
+                    currentPosition += cb.Payload.Length;
+                }
+            }
+
+            return new QpidBytesMessage(messageNbr, data, contentHeader);
+        }
+
+        public override AbstractQmsMessage CreateMessage()
+        {
+            return new QpidBytesMessage();
+        }
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidBytesMessageFactory.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessage.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessage.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessage.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessage.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,137 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Text;
+using Qpid.Framing;
+using Qpid.Messaging;
+
+namespace Qpid.Client.Message
+{
+    public class QpidTextMessage : AbstractQmsMessage, ITextMessage
+    {
+        private const string MIME_TYPE = "text/plain";
+
+        private byte[] _data;
+
+        private string _decodedValue;
+
+        public QpidTextMessage() : this(null, null)
+        {        
+        }
+
+        public QpidTextMessage(byte[] data, String encoding) : base()
+        {
+            // the superclass has instantied a content header at this point
+            ContentHeaderProperties.ContentType= MIME_TYPE;
+            _data = data;
+            ContentHeaderProperties.Encoding = encoding;
+        }
+
+        public QpidTextMessage(ulong messageNbr, byte[] data, BasicContentHeaderProperties contentHeader)
+            : base(messageNbr, contentHeader)
+        {            
+            contentHeader.ContentType = MIME_TYPE;
+            _data = data;
+        }
+
+        public QpidTextMessage(byte[] data) : this(data, null)
+        {            
+        }
+
+        public QpidTextMessage(string text)
+        {
+            Text = text;
+        }
+
+        public override void ClearBody()
+        {
+            _data = null;
+            _decodedValue = null;
+        }
+
+        public override string ToBodyString()
+        {
+            return Text;
+        }
+
+        public override byte[] Data
+        {
+            get
+            {
+                return _data;
+            }
+            set
+            {
+                _data = value;
+            }
+        }
+
+        public override string MimeType
+        {
+            get
+            {
+                return MIME_TYPE;
+            }
+        }        
+
+        public string Text
+        {
+            get
+            {
+                if (_data == null && _decodedValue == null)
+                {
+                    return null;
+                }
+                else if (_decodedValue != null)
+                {
+                    return _decodedValue;
+                }
+                else
+                {                    
+                    if (ContentHeaderProperties.Encoding != null)
+                    {
+                        // throw ArgumentException if the encoding is not supported
+                        _decodedValue = Encoding.GetEncoding(ContentHeaderProperties.Encoding).GetString(_data);
+                    }
+                    else
+                    {
+                        _decodedValue = Encoding.Default.GetString(_data);
+                    }
+                    return _decodedValue;                    
+                }
+            }
+
+            set
+            {            
+                if (ContentHeaderProperties.Encoding == null)
+                {
+                    _data = Encoding.Default.GetBytes(value);
+                }
+                else
+                {
+                    // throw ArgumentException if the encoding is not supported
+                    _data = Encoding.GetEncoding(ContentHeaderProperties.Encoding).GetBytes(value);
+                }
+                _decodedValue = value;
+            }
+        }
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessage.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessageFactory.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessageFactory.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessageFactory.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessageFactory.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,60 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Collections;
+using Qpid.Framing;
+
+namespace Qpid.Client.Message
+{
+    public class QpidTextMessageFactory : AbstractQmsMessageFactory
+    {
+        protected override AbstractQmsMessage CreateMessageWithBody(ulong messageNbr, ContentHeaderBody contentHeader,
+                                                                    IList bodies)
+        {
+            byte[] data;
+
+            // we optimise the non-fragmented case to avoid copying
+            if (bodies != null && bodies.Count == 1)
+            {
+                data = ((ContentBody)bodies[0]).Payload;
+            }
+            else
+            {
+                data = new byte[(int)contentHeader.BodySize];
+                int currentPosition = 0;
+                foreach (ContentBody cb in bodies)
+                {                
+                    Array.Copy(cb.Payload, 0, data, currentPosition, cb.Payload.Length);
+                    currentPosition += cb.Payload.Length;
+                }
+            }
+
+            return new QpidTextMessage(messageNbr, data, (BasicContentHeaderProperties)contentHeader.Properties);
+        }
+        
+     
+        public override AbstractQmsMessage CreateMessage()
+        {
+            return new QpidTextMessage();
+        }      
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/QpidTextMessageFactory.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnexpectedBodyReceivedException.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnexpectedBodyReceivedException.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnexpectedBodyReceivedException.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnexpectedBodyReceivedException.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,48 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using log4net;
+
+namespace Qpid.Client.Message
+{
+    /// <summary>
+    /// Raised when a message body is received unexpectedly by the client. This typically occurs when the
+    /// length of bodies received does not match with the declared length in the content header.
+    /// </summary>
+    public class UnexpectedBodyReceivedException : AMQException
+    {
+        public UnexpectedBodyReceivedException(ILog logger, string msg, Exception t)
+            : base(logger, msg, t)
+        {        
+        }
+
+        public UnexpectedBodyReceivedException(ILog logger, string msg)
+            : base(logger, msg)
+        {            
+        }
+
+        public UnexpectedBodyReceivedException(ILog logger, int errorCode, string msg)
+            : base(logger, errorCode, msg)
+        {                        
+        }
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnexpectedBodyReceivedException.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnprocessedMessage.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnprocessedMessage.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnprocessedMessage.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnprocessedMessage.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,56 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System.Collections;
+using Qpid.Framing;
+
+namespace Qpid.Client.Message
+{
+    public class UnprocessedMessage
+    {
+        private ulong _bytesReceived = 0;
+
+        public BasicDeliverBody DeliverBody;
+        public BasicReturnBody BounceBody;
+        public ushort ChannelId;
+        public ContentHeaderBody ContentHeader;
+
+        /// <summary>
+        /// List of ContentBody instances. Due to fragmentation you don't know how big this will be in general
+        /// </summary>
+        /// TODO: write and use linked list class
+        public IList Bodies = new ArrayList();
+
+        public void ReceiveBody(ContentBody body)
+        {
+            Bodies.Add(body);
+            if (body.Payload != null)
+            {
+                _bytesReceived += (uint)body.Payload.Length;
+            }
+        }
+
+        public bool IsAllBodyDataReceived()
+        {
+            return _bytesReceived == ContentHeader.BodySize;
+        }
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Message/UnprocessedMessage.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQMethodEvent.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQMethodEvent.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQMethodEvent.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQMethodEvent.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,75 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Text;
+using Qpid.Framing;
+
+namespace Qpid.Client.Protocol
+{
+    public class AMQMethodEvent
+    {
+        private AMQMethodBody _method;
+
+        private ushort _channelId;
+
+        private AMQProtocolSession _protocolSession;
+
+        public AMQMethodEvent(ushort channelId, AMQMethodBody method, AMQProtocolSession protocolSession)
+        {
+            _channelId = channelId;
+            _method = method;
+            _protocolSession = protocolSession;
+        }
+
+        public AMQMethodBody Method
+        {
+            get
+            {
+                return _method;
+            }
+        }
+
+        public ushort ChannelId
+        {
+            get
+            {
+                return _channelId;
+            }
+        }
+
+        public AMQProtocolSession ProtocolSession
+        {
+            get
+            {
+                return _protocolSession;
+            }            
+        }
+
+        public override String ToString()
+        {
+            StringBuilder buf = new StringBuilder("Method event: ");
+            buf.Append("\nChannel id: ").Append(_channelId);
+            buf.Append("\nMethod: ").Append(_method);
+            return buf.ToString();
+        }
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQMethodEvent.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolListener.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolListener.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolListener.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolListener.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,289 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Collections;
+using System.Threading;
+using log4net;
+using Qpid.Client.Failover;
+using Qpid.Client.Protocol.Listener;
+using Qpid.Client.State;
+using Qpid.Framing;
+
+namespace Qpid.Client.Protocol
+{
+    public class AMQProtocolListener : IProtocolListener
+    {
+        private static readonly ILog _log = LogManager.GetLogger(typeof(AMQProtocolListener));
+
+        /**
+         * We create the failover handler when the session is created since it needs a reference to the IoSession in order
+         * to be able to send errors during failover back to the client application. The session won't be available in the
+         * case where we failing over due to a Connection.Redirect message from the broker.
+         */
+        private FailoverHandler _failoverHandler;
+
+        /**
+         * This flag is used to track whether failover is being attempted. It is used to prevent the application constantly
+         * attempting failover where it is failing.
+         */
+        internal FailoverState _failoverState = FailoverState.NOT_STARTED;
+
+        internal FailoverState FailoverState
+        {
+            get { return _failoverState; }
+            set { _failoverState = value; }
+        }
+
+        internal ManualResetEvent FailoverLatch;
+
+        AMQConnection _connection;
+        AMQStateManager _stateManager;
+
+        public AMQStateManager StateManager
+        {
+            get { return _stateManager; }
+            set { _stateManager = value; }
+        }
+
+        //private readonly CopyOnWriteArraySet _frameListeners = new CopyOnWriteArraySet();
+        private readonly ArrayList _frameListeners = ArrayList.Synchronized(new ArrayList());
+        
+        AMQProtocolSession _protocolSession = null; // FIXME
+        public AMQProtocolSession ProtocolSession { set { _protocolSession = value; } } // FIXME: can this be fixed?
+        
+
+        private readonly Object _lock = new Object();
+
+        public AMQProtocolListener(AMQConnection connection, AMQStateManager stateManager)
+        {
+            _connection = connection;
+            _stateManager = stateManager;
+            _failoverHandler = new FailoverHandler(connection);
+        }
+
+        public void OnMessage(IDataBlock message)
+        {
+            // Handle incorrect protocol version.
+            if (message is ProtocolInitiation)
+            {
+                string error = String.Format("Protocol mismatch - {0}", message.ToString());
+                AMQException e = new AMQProtocolHeaderException(error);
+                _log.Error("Closing connection because of protocol mismatch", e);
+                //_protocolSession.CloseProtocolSession();
+                _stateManager.Error(e);
+                return;
+            }
+
+            AMQFrame frame = (AMQFrame)message;
+
+            if (frame.BodyFrame is AMQMethodBody)
+            {
+                if (_log.IsDebugEnabled)
+                {
+                    _log.Debug("Method frame received: " + frame);
+                }
+                AMQMethodEvent evt = new AMQMethodEvent(frame.Channel, (AMQMethodBody)frame.BodyFrame, _protocolSession);
+                try
+                {
+                    bool wasAnyoneInterested = false;
+                    lock (_frameListeners.SyncRoot)
+                    {
+                        foreach (IAMQMethodListener listener in _frameListeners)
+                        {
+                            wasAnyoneInterested = listener.MethodReceived(evt) || wasAnyoneInterested;
+                        }
+                    }
+                    if (!wasAnyoneInterested)
+                    {
+                        throw new AMQException("AMQMethodEvent " + evt + " was not processed by any listener.");
+                    }
+                }
+                catch (Exception e)
+                {
+                    foreach (IAMQMethodListener listener in _frameListeners)
+                    {
+                        listener.Error(e);
+                    }
+                }
+            }
+            else if (frame.BodyFrame is ContentHeaderBody)
+            {
+                _protocolSession.MessageContentHeaderReceived(frame.Channel,
+                                                              (ContentHeaderBody)frame.BodyFrame);
+            }
+            else if (frame.BodyFrame is ContentBody)
+            {
+                _protocolSession.MessageContentBodyReceived(frame.Channel,
+                                                            (ContentBody)frame.BodyFrame);
+            }
+            else if (frame.BodyFrame is HeartbeatBody)
+            {
+                _log.Debug("HeartBeat received");
+            }
+            //_connection.BytesReceived(_protocolSession.Channel.ReadBytes); // XXX: is this really useful?
+        }
+
+        public void OnException(Exception cause)
+        {
+            _log.Warn("Protocol Listener received exception", cause);
+            lock (_lock)
+            {
+                if (_failoverState == FailoverState.NOT_STARTED)
+                {
+                    if (!(cause is AMQUndeliveredException))
+                    {
+                        WhenClosed();
+                    }
+                }
+                    // We reach this point if failover was attempted and failed therefore we need to let the calling app
+                    // know since we cannot recover the situation.
+                else if (_failoverState == FailoverState.FAILED)
+                {
+                    // we notify the state manager of the error in case we have any clients waiting on a state
+                    // change. Those "waiters" will be interrupted and can handle the exception
+                    AMQException amqe = new AMQException("Protocol handler error: " + cause, cause);
+                    PropagateExceptionToWaiters(amqe);
+                    _connection.ExceptionReceived(cause);
+                }
+            }
+        }
+
+        /**
+         * When the broker connection dies we can either get sessionClosed() called or exceptionCaught() followed by
+         * sessionClosed() depending on whether we were trying to send data at the time of failure.
+         *
+         * @param session
+         * @throws Exception
+         */
+        void WhenClosed()
+        {
+            _connection.StopHeartBeatThread();
+
+            // TODO: Server just closes session with no warning if auth fails.
+            if (_connection.Closed)
+            {
+                _log.Info("Channel closed called by client");
+            }
+            else
+            {
+                _log.Info("Channel closed called with failover state currently " + _failoverState);
+
+                // Reconnectablility was introduced here so as not to disturb the client as they have made their intentions
+                // known through the policy settings.
+
+                if ((_failoverState != FailoverState.IN_PROGRESS) && _connection.IsFailoverAllowed)
+                {
+                    _log.Info("FAILOVER STARTING");
+                    if (_failoverState == FailoverState.NOT_STARTED)
+                    {
+                        _failoverState = FailoverState.IN_PROGRESS;
+                        startFailoverThread();
+                    }
+                    else
+                    {
+                        _log.Info("Not starting failover as state currently " + _failoverState);
+                    }
+                }
+                else
+                {
+                    _log.Info("Failover not allowed by policy.");
+
+                    if (_failoverState != FailoverState.IN_PROGRESS)
+                    {
+                        _log.Info("sessionClose() not allowed to failover");
+                        _connection.ExceptionReceived(
+                            new AMQDisconnectedException("Server closed connection and reconnection not permitted."));
+                    }
+                    else
+                    {
+                        _log.Info("sessionClose() failover in progress");
+                    }
+                }
+            }
+
+            _log.Info("Protocol Channel [" + this + "] closed");
+        }
+
+        /// <summary>
+        /// There are two cases where we have other threads potentially blocking for events to be handled by this
+        /// class. These are for the state manager (waiting for a state change) or a frame listener (waiting for a
+        /// particular type of frame to arrive). When an error occurs we need to notify these waiters so that they can
+        /// react appropriately.
+        /// 
+        /// <param name="e">the exception to propagate</param>
+        /// </summary>
+        public void PropagateExceptionToWaiters(Exception e)
+        {
+            // FIXME: not sure if required as StateManager is in _frameListeners. Probably something to do with fail-over.
+            _stateManager.Error(e);
+
+            foreach (IAMQMethodListener listener in _frameListeners)
+            {
+                listener.Error(e);
+            }
+        }
+
+        public void AddFrameListener(IAMQMethodListener listener)
+        {
+            _frameListeners.Add(listener);
+        }
+
+        public void RemoveFrameListener(IAMQMethodListener listener)
+        {
+            if (_log.IsDebugEnabled)
+            {
+                _log.Debug("Removing frame listener: " + listener.ToString());
+            }
+            _frameListeners.Remove(listener);
+        }
+
+        public void BlockUntilNotFailingOver()
+        {
+            if (FailoverLatch != null)
+            {
+                FailoverLatch.WaitOne();
+            }
+        }
+
+        /// <summary>
+        ///  "Failover" for redirection.
+        /// </summary>
+        /// <param name="host"></param>
+        /// <param name="port"></param>
+        public void Failover(string host, int port)
+        {
+            _failoverHandler.setHost(host);
+            _failoverHandler.setPort(port);
+            // see javadoc for FailoverHandler to see rationale for separate thread
+            startFailoverThread();
+        }
+
+        private void startFailoverThread()
+        {
+            Thread failoverThread = new Thread(new ThreadStart(_failoverHandler.Run));
+            failoverThread.Name = "Failover";
+            // Do not inherit daemon-ness from current thread as this can be a daemon
+            // thread such as a AnonymousIoService thread.
+            failoverThread.IsBackground = false;
+            failoverThread.Start();
+        }
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolListener.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolSession.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolSession.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolSession.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolSession.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,269 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Collections;
+using log4net;
+using Qpid.Client.Message;
+using Qpid.Client.Transport;
+using Qpid.Framing;
+
+namespace Qpid.Client.Protocol
+{
+    public class AMQProtocolSession
+    {
+        private static readonly ILog _logger = LogManager.GetLogger(typeof(AMQProtocolSession));
+
+        private readonly IProtocolWriter _protocolWriter;
+        private readonly IConnectionCloser _connectionCloser;
+
+        /**
+         * Counter to ensure unique queue names
+         */
+        private int _queueId = 1;
+        private readonly Object _queueIdLock = new Object();
+
+        /// <summary>
+        /// Maps from the channel id to the AmqChannel that it represents.
+        /// </summary>
+        //private ConcurrentMap _channelId2SessionMap = new ConcurrentHashMap();
+        private Hashtable _channelId2SessionMap = Hashtable.Synchronized(new Hashtable());
+
+        //private ConcurrentMap _closingChannels = new ConcurrentHashMap();
+        private Hashtable _closingChannels = Hashtable.Synchronized(new Hashtable());
+
+        /// <summary>
+        /// Maps from a channel id to an unprocessed message. This is used to tie together the
+        /// JmsDeliverBody (which arrives first) with the subsequent content header and content bodies.
+        /// </summary>
+        //private ConcurrentMap _channelId2UnprocessedMsgMap = new ConcurrentHashMap();
+        private Hashtable _channelId2UnprocessedMsgMap = Hashtable.Synchronized(new Hashtable());
+
+        private AMQConnection _connection;
+        
+        public AMQProtocolSession(IProtocolWriter protocolWriter, IConnectionCloser connectionCloser, AMQConnection connection)
+        {
+            _protocolWriter = protocolWriter;
+            _connectionCloser = connectionCloser;
+            _connection = connection;
+        }
+
+        public void Init()
+        {
+            // start the process of setting up the connection. This is the first place that
+            // data is written to the server.
+            _protocolWriter.Write(new ProtocolInitiation());
+        }
+
+        public string Username
+        {
+            get
+            {
+                return AMQConnection.Username;
+            }            
+        }
+
+        public string Password
+        {
+            get
+            {
+                return AMQConnection.Password;
+            }            
+        }
+
+        ConnectionTuneParameters _connectionTuneParameters; // TODO: should be able to have this in the Java too.
+        
+        public ConnectionTuneParameters ConnectionTuneParameters
+        {
+            get
+            {
+                return _connectionTuneParameters;
+            }            
+            set
+            {
+                _connectionTuneParameters = value;
+                AMQConnection con = AMQConnection;
+                con.SetMaximumChannelCount(value.ChannelMax);
+                con.MaximumFrameSize = value.FrameMax;
+            }
+        }
+        
+        /// <summary>
+        /// Callback invoked from the BasicDeliverMethodHandler when a message has been received.
+        /// This is invoked on the MINA dispatcher thread.
+        /// </summary>
+        /// <param name="message">the unprocessed message</param>
+        /// <exception cname="AMQException">if this was not expected</exception>         
+        public void UnprocessedMessageReceived(UnprocessedMessage message)
+        {
+            _channelId2UnprocessedMsgMap[message.ChannelId] = message;
+        }
+
+        public void MessageContentHeaderReceived(ushort channelId, ContentHeaderBody contentHeader)                
+        {
+            UnprocessedMessage msg = (UnprocessedMessage) _channelId2UnprocessedMsgMap[channelId];
+            if (msg == null)
+            {
+                throw new AMQException("Error: received content header without having received a JMSDeliver frame first");
+            }
+            if (msg.ContentHeader != null)
+            {
+                throw new AMQException("Error: received duplicate content header or did not receive correct number of content body frames");
+            }
+            msg.ContentHeader = contentHeader;
+            if (contentHeader.BodySize == 0)
+            {
+                DeliverMessageToAMQSession(channelId, msg);
+            }
+        }
+
+        public void MessageContentBodyReceived(ushort channelId, ContentBody contentBody)
+        {
+            UnprocessedMessage msg = (UnprocessedMessage) _channelId2UnprocessedMsgMap[channelId];
+            if (msg == null)
+            {
+                throw new AMQException("Error: received content body without having received a BasicDeliver frame first");
+            }
+            if (msg.ContentHeader == null)
+            {
+                _channelId2UnprocessedMsgMap.Remove(channelId);
+                throw new AMQException("Error: received content body without having received a ContentHeader frame first");
+            }
+            try
+            {
+                msg.ReceiveBody(contentBody);
+            }
+            catch (UnexpectedBodyReceivedException e)
+            {
+                _channelId2UnprocessedMsgMap.Remove(channelId);
+                throw e;
+            }
+            if (msg.IsAllBodyDataReceived())
+            {
+                DeliverMessageToAMQSession(channelId,  msg);
+            }
+        }
+
+        /// <summary>
+        /// Deliver a message to the appropriate session, removing the unprocessed message
+        /// from our map
+        /// <param name="channelId">the channel id the message should be delivered to</param>
+        /// <param name="msg"> the message</param>         
+        private void DeliverMessageToAMQSession(ushort channelId, UnprocessedMessage msg)
+        {
+            AmqChannel channel = (AmqChannel) _channelId2SessionMap[channelId];
+            channel.MessageReceived(msg);
+            _channelId2UnprocessedMsgMap.Remove(channelId);
+        }
+
+        /// <summary>
+        /// Convenience method that writes a frame to the protocol session. Equivalent
+        /// to calling getProtocolSession().write().
+        /// </summary>
+        /// <param name="frame">the frame to write</param>
+        public void WriteFrame(IDataBlock frame)
+        {
+            _protocolWriter.Write(frame);
+        }
+
+        public void AddSessionByChannel(ushort channelId, AmqChannel channel)
+        {            
+            if (channel == null)
+            {
+                throw new ArgumentNullException("Attempt to register a null channel");
+            }
+            _logger.Debug("Add channel with channel id  " + channelId);
+            _channelId2SessionMap[channelId] = channel;
+        }
+
+        public void RemoveSessionByChannel(ushort channelId)
+        {            
+            _logger.Debug("Removing session with channelId " + channelId);
+            _channelId2SessionMap.Remove(channelId);
+        }
+
+        /// <summary>
+        /// Starts the process of closing a channel
+        /// </summary>
+        /// <param name="channel" the AmqChannel being closed</param>         
+        public void CloseSession(AmqChannel channel)
+        {
+            _logger.Debug("closeSession called on protocol channel for channel " + channel.ChannelId);
+            ushort channelId = channel.ChannelId;
+            
+            // we need to know when a channel is closing so that we can respond
+            // with a channel.close frame when we receive any other type of frame
+            // on that channel
+            _closingChannels[channelId] = channel;
+
+        }
+
+        /// <summary>
+        /// Called from the ChannelClose handler when a channel close frame is received.
+        /// This method decides whether this is a response or an initiation. The latter
+        /// case causes the AmqChannel to be closed and an exception to be thrown if
+        /// appropriate.
+        /// </summary>
+        /// <param name="channelId">the id of the channel (session)</param>
+        /// <returns>true if the client must respond to the server, i.e. if the server
+        /// initiated the channel close, false if the channel close is just the server
+        /// responding to the client's earlier request to close the channel.</returns>         
+        public bool ChannelClosed(ushort channelId, int code, string text)
+        {            
+            // if this is not a response to an earlier request to close the channel            
+            if (!_closingChannels.ContainsKey(channelId))
+            {
+                _closingChannels.Remove(channelId);
+                AmqChannel channel = (AmqChannel) _channelId2SessionMap[channelId];
+                channel.Closed(new AMQException(_logger, code, text));
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+        public AMQConnection AMQConnection
+        {
+            get
+            {
+                return _connection;
+            }
+        }
+
+        public void CloseProtocolSession()
+        {
+            _logger.Debug("Closing protocol session");
+            _connectionCloser.Close();
+        }
+
+        internal string GenerateQueueName()
+        {
+            int id;
+            lock(_queueIdLock)
+            {
+                id = _queueId++;
+            }
+
+            return "tmp_" + _connection.Transport.getLocalEndPoint() + "_" + id;          
+        }
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/AMQProtocolSession.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IConnectionCloser.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IConnectionCloser.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IConnectionCloser.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IConnectionCloser.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,27 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+namespace Qpid.Client.Protocol
+{
+    public interface IConnectionCloser
+    {
+        void Close();
+    }
+}
\ No newline at end of file

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IConnectionCloser.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IProtocolListener.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IProtocolListener.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IProtocolListener.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IProtocolListener.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,36 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using Qpid.Client.Protocol.Listener;
+using Qpid.Framing;
+
+namespace Qpid.Client.Protocol
+{
+    public interface IProtocolListener
+    {
+        void OnMessage(IDataBlock message);
+        void OnException(Exception e);
+
+        // XXX: .NET way of doing listeners?
+        void AddFrameListener(IAMQMethodListener listener);
+        void RemoveFrameListener(IAMQMethodListener listener);
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/IProtocolListener.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/BlockingMethodFrameListener.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/BlockingMethodFrameListener.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/BlockingMethodFrameListener.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/BlockingMethodFrameListener.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,109 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Threading;
+using Qpid.Framing;
+
+namespace Qpid.Client.Protocol.Listener
+{
+    public abstract class BlockingMethodFrameListener : IAMQMethodListener
+    {
+        private ManualResetEvent _resetEvent;
+        
+        public abstract bool ProcessMethod(ushort channelId, AMQMethodBody frame);        
+
+        /// <summary>
+        /// This is set if there is an exception thrown from processCommandFrame and the
+        /// exception is rethrown to the caller of blockForFrame()
+        /// </summary>
+        private volatile Exception _error;
+
+        protected ushort _channelId;
+
+        protected AMQMethodEvent _doneEvt = null;
+
+        public BlockingMethodFrameListener(ushort channelId)
+        {
+            _channelId = channelId;
+            _resetEvent = new ManualResetEvent(false);
+        }
+
+        /// <summary>
+        /// This method is called by the MINA dispatching thread. Note that it could
+        /// be called before BlockForFrame() has been called.
+        /// </summary>
+        /// <param name="evt">the frame event</param>
+        /// <returns>true if the listener has dealt with this frame</returns>
+        /// <exception cref="AMQException"></exception>
+        public bool MethodReceived(AMQMethodEvent evt)
+        {
+            AMQMethodBody method = evt.Method;
+
+            try
+            {
+                bool ready = (evt.ChannelId == _channelId) && ProcessMethod(evt.ChannelId, method);
+                if (ready)
+                {                    
+                    _doneEvt = evt;                        
+                    _resetEvent.Set();
+                }
+
+                return ready;
+            }
+            catch (AMQException e)
+            {
+                Error(e);
+                // we rethrow the error here, and the code in the frame dispatcher will go round
+                // each listener informing them that an exception has been thrown
+                throw e;
+            }
+        }
+
+        /// <summary>
+        /// This method is called by the thread that wants to wait for a frame.
+        /// </summary>
+        public AMQMethodEvent BlockForFrame()
+        {
+            _resetEvent.WaitOne();
+            //at this point the event will have been signalled. The error field might or might not be set
+            // depending on whether an error occurred
+            if (_error != null)
+            {
+                throw _error;
+            }
+
+            return _doneEvt;
+        }
+
+        /// <summary>
+        /// This is a callback, called by the MINA dispatcher thread only. It is also called from within this
+        /// class to avoid code repetition but again is only called by the MINA dispatcher thread.
+        /// </summary>
+        /// <param name="e">the exception that caused the error</param>         
+        public void Error(Exception e)
+        {
+            // set the error so that the thread that is blocking (in BlockForFrame())
+            // can pick up the exception and rethrow to the caller
+            _error = e;
+            _resetEvent.Set();
+        }
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/BlockingMethodFrameListener.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/IAMQMethodListener.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/IAMQMethodListener.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/IAMQMethodListener.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/IAMQMethodListener.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,45 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+
+namespace Qpid.Client.Protocol.Listener
+{
+    public interface IAMQMethodListener
+    {
+        /// <summary>
+        /// Invoked when a method frame has been received
+        /// <param name="evt">the event</param>
+        /// <returns>true if the handler has processed the method frame, false otherwise. Note
+        /// that this does not prohibit the method event being delivered to subsequent listeners
+        /// but can be used to determine if nobody has dealt with an incoming method frame.</param>
+        /// <exception cname="AMQException">if an error has occurred. This exception will be delivered
+        /// to all registered listeners using the error() method (see below) allowing them to
+        /// perform cleanup if necessary.</exception>     
+        bool MethodReceived(AMQMethodEvent evt);
+
+        /// <summary>
+        /// Callback when an error has occurred. Allows listeners to clean up.
+        /// </summary>
+        /// <param name="e">the exception</param>         
+        void Error(Exception e);
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/IAMQMethodListener.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/SpecificMethodFrameListener.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/SpecificMethodFrameListener.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/SpecificMethodFrameListener.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/SpecificMethodFrameListener.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,41 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using Qpid.Framing;
+
+namespace Qpid.Client.Protocol.Listener
+{
+    public class SpecificMethodFrameListener : BlockingMethodFrameListener
+    {
+        private readonly Type _expectedClass;
+
+        public SpecificMethodFrameListener(ushort channelId, Type expectedClass) : base(channelId)
+        {        
+            _expectedClass = expectedClass;
+        }
+
+        public override bool ProcessMethod(ushort channelId, AMQMethodBody frame)
+        {
+            return _expectedClass.IsInstanceOfType(frame);
+        }
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/Listener/SpecificMethodFrameListener.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/ProtocolWriter.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/ProtocolWriter.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/ProtocolWriter.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/ProtocolWriter.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,78 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using Qpid.Client.Protocol.Listener;
+using Qpid.Client.Transport;
+using Qpid.Framing;
+
+namespace Qpid.Client.Protocol
+{
+    /// <summary>
+    /// A convenient interface to writing protocol frames.
+    /// </summary>
+    public class ProtocolWriter
+    {
+        IProtocolWriter _protocolWriter;
+        IProtocolListener _protocolListener;
+        
+        public ProtocolWriter(IProtocolWriter protocolWriter, IProtocolListener protocolListener)
+        {
+            _protocolWriter = protocolWriter;
+            _protocolListener = protocolListener;
+        }
+
+        public void WriteFrame(IDataBlock frame)
+        {
+            _protocolWriter.Write(frame);
+        }
+
+        /// <summary>
+        /// Convenience method that writes a frame to the protocol session and waits for
+        /// a particular response. Equivalent to calling getProtocolSession().write() then
+        /// waiting for the response.
+        /// </summary>
+        /// <param name="frame">the frame</param>
+        /// <param name="listener">the blocking listener. Note the calling thread will block.</param>         
+        private AMQMethodEvent SyncWrite(AMQFrame frame, BlockingMethodFrameListener listener)            
+        {
+            try
+            {
+                _protocolListener.AddFrameListener(listener);
+                _protocolWriter.Write(frame);
+                return listener.BlockForFrame();                
+            }
+            finally
+            {
+                _protocolListener.RemoveFrameListener(listener);
+            }
+            // When control resumes before this line, a reply will have been received
+            // that matches the criteria defined in the blocking listener
+        }
+
+        public AMQMethodEvent SyncWrite(AMQFrame frame, Type responseType)
+        {
+            // TODO: If each frame knew it's response type, then the responseType argument would
+            // TODO: not be neccesary.
+            return SyncWrite(frame, new SpecificMethodFrameListener(frame.Channel, responseType));
+        }
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/Protocol/ProtocolWriter.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/QpidConnectionInfo.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/QpidConnectionInfo.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/QpidConnectionInfo.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/QpidConnectionInfo.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,142 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Collections;
+using System.Net;
+using log4net;
+using Qpid.Client.qms;
+
+namespace Qpid.Client
+{
+    public class QpidConnectionInfo : ConnectionInfo
+    {
+        private static readonly ILog _logger = LogManager.GetLogger(typeof(QpidConnectionInfo));
+
+        string _username = "guest";
+        string _password = "guest";
+        string _virtualHost = "/default";
+
+        string _failoverMethod = null;
+        IDictionary _failoverOptions = new Hashtable();
+        IDictionary _options = new Hashtable();
+        IList _brokerInfos = new ArrayList(); // List<BrokerInfo>
+        string _clientName = String.Format("{0}{1:G}", Dns.GetHostName(), DateTime.Now.Ticks);
+
+        public string asUrl()
+        {
+            string result = "amqp://";
+            foreach (BrokerInfo info in _brokerInfos)
+            {
+                result += info.ToString();
+            }
+            return result;
+            
+        }
+
+        public string getFailoverMethod()
+        {
+            return _failoverMethod;
+        }
+
+        public string getFailoverOption(string key)
+        {
+            return (string) _failoverOptions[key];
+        }
+
+        public int getBrokerCount()
+        {
+            return _brokerInfos.Count;
+        }
+
+        public BrokerInfo GetBrokerDetails(int index)
+        {
+            return (BrokerInfo)_brokerInfos[index];
+        }
+
+        public void AddBrokerInfo(BrokerInfo brokerInfo)
+        {
+            if (!_brokerInfos.Contains(brokerInfo))
+            {
+                _brokerInfos.Add(brokerInfo);
+            }
+        }
+
+        public IList GetAllBrokerInfos()
+        {
+            return _brokerInfos;
+        }
+
+        public string GetClientName()
+        {
+            return _clientName;
+        }
+
+        public void SetClientName(string clientName)
+        {
+            _clientName = clientName;
+        }
+
+        public string getUsername()
+        {
+            return _username;
+        }
+
+        public void setUsername(string username)
+        {
+            _username = username;
+        }
+
+        public string getPassword()
+        {
+            return _password;
+        }
+
+        public void setPassword(string password)
+        {
+            _password = password;
+        }
+
+        public string getVirtualHost()
+        {
+            return _virtualHost;
+        }
+
+        public void setVirtualHost(string virtualHost)
+        {
+            _virtualHost = virtualHost;
+        }
+
+        public string getOption(string key)
+        {
+            return (string) _options[key];
+        }
+
+        public void setOption(string key, string value)
+        {
+            _options[key] = value;
+        }
+        
+        public override string ToString()
+        {
+            return asUrl();
+        }
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/QpidConnectionInfo.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQState.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQState.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQState.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQState.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,34 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+namespace Qpid.Client.State
+{
+    public enum AMQState
+    {
+        CONNECTION_NOT_STARTED,    
+        CONNECTION_NOT_TUNED,    
+        CONNECTION_NOT_OPENED,
+        CONNECTION_OPEN,
+        CONNECTION_CLOSING,    
+        CONNECTION_CLOSED,
+        ALL // all is a special state used in the state manager. It is not valid to be "in" the state "all".
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQState.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateChangedEvent.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateChangedEvent.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateChangedEvent.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateChangedEvent.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,52 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+namespace Qpid.Client.State
+{
+    public class AMQStateChangedEvent
+    {
+        private readonly AMQState _oldState;
+
+        private readonly AMQState _newState;
+
+        public AMQStateChangedEvent(AMQState oldState, AMQState newState)
+        {
+            _oldState = oldState;
+            _newState = newState;
+        }
+
+        public AMQState OldState
+        {
+            get
+            {
+                return _oldState;
+            }
+        }
+
+        public AMQState NewState
+        {
+            get
+            {
+                return _newState;
+            }
+        }
+
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateChangedEvent.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateManager.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateManager.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateManager.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateManager.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,225 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Collections;
+using log4net;
+using Qpid.Client.Handler;
+using Qpid.Client.Protocol;
+using Qpid.Client.Protocol.Listener;
+using Qpid.Framing;
+
+namespace Qpid.Client.State
+{
+    public class AMQStateManager : IAMQMethodListener
+    {
+        private static readonly ILog _logger = LogManager.GetLogger(typeof(AMQStateManager));
+
+        const bool InfoLoggingHack = true;
+            
+        /// <summary>
+        /// The current state
+        /// </summary>
+        private AMQState _currentState;
+
+        /// <summary>
+        /// Maps from an AMQState instance to a Map from Class to StateTransitionHandler.
+        /// The class must be a subclass of AMQFrame.
+        /// </summary>
+        private readonly IDictionary _state2HandlersMap = new Hashtable();
+
+        //private CopyOnWriteArraySet _stateListeners = new CopyOnWriteArraySet();
+        private ArrayList _stateListeners = ArrayList.Synchronized(new ArrayList(5));
+        
+        public AMQStateManager()
+        {
+            _currentState = AMQState.CONNECTION_NOT_STARTED;
+            RegisterListeners();
+        }
+
+        private void RegisterListeners()
+        {
+            IStateAwareMethodListener connectionStart = new ConnectionStartMethodHandler();
+            IStateAwareMethodListener connectionClose = new ConnectionCloseMethodHandler();
+            IStateAwareMethodListener connectionCloseOk = new ConnectionCloseOkHandler();
+            IStateAwareMethodListener connectionTune = new ConnectionTuneMethodHandler();
+            IStateAwareMethodListener connectionSecure = new ConnectionSecureMethodHandler();
+            IStateAwareMethodListener connectionOpenOk = new ConnectionOpenOkMethodHandler();
+            IStateAwareMethodListener channelClose = new ChannelCloseMethodHandler();
+            IStateAwareMethodListener basicDeliver = new BasicDeliverMethodHandler();
+            IStateAwareMethodListener basicReturn = new BasicReturnMethodHandler();
+            
+            // We need to register a map for the null (i.e. all state) handlers otherwise you get
+            // a stack overflow in the handler searching code when you present it with a frame for which
+            // no handlers are registered.
+            _state2HandlersMap[AMQState.ALL] = new Hashtable();
+
+            {
+                Hashtable notStarted = new Hashtable();
+                notStarted[typeof(ConnectionStartBody)] = connectionStart;
+                notStarted[typeof(ConnectionCloseBody)] = connectionClose;
+                _state2HandlersMap[AMQState.CONNECTION_NOT_STARTED] = notStarted;
+            }
+            {
+                Hashtable notTuned = new Hashtable();
+                notTuned[typeof(ConnectionTuneBody)] = connectionTune;
+                notTuned[typeof(ConnectionSecureBody)] = connectionSecure;
+                notTuned[typeof(ConnectionCloseBody)] = connectionClose;
+                _state2HandlersMap[AMQState.CONNECTION_NOT_TUNED] = notTuned;
+            }
+            {
+                Hashtable notOpened = new Hashtable();
+                notOpened[typeof(ConnectionOpenOkBody)] = connectionOpenOk;
+                notOpened[typeof(ConnectionCloseBody)] = connectionClose;
+                _state2HandlersMap[AMQState.CONNECTION_NOT_OPENED] = notOpened;
+            }
+            {
+                Hashtable open = new Hashtable();
+                open[typeof(ChannelCloseBody)] = channelClose;
+                open[typeof(ConnectionCloseBody)] = connectionClose;
+                open[typeof(BasicDeliverBody)] = basicDeliver;
+                open[typeof(BasicReturnBody)] = basicReturn;
+                _state2HandlersMap[AMQState.CONNECTION_OPEN] = open;
+            }
+            {
+                Hashtable closing = new Hashtable();
+                closing[typeof(ConnectionCloseOkBody)] = connectionCloseOk;
+                _state2HandlersMap[AMQState.CONNECTION_CLOSING] = closing;
+            }
+        }
+
+        public AMQState CurrentState
+        {
+            get
+            {
+                return _currentState;
+            }
+        }
+
+        /// <summary>
+        /// Changes the state.
+        /// </summary>
+        /// <param name="newState">The new state.</param>
+        /// <exception cref="AMQException">if there is an error changing state</exception>
+        public void ChangeState(AMQState newState)
+        {
+            if (InfoLoggingHack)
+            {
+                _logger.Info("State changing to " + newState + " from old state " + _currentState);
+            }
+            _logger.Debug("State changing to " + newState + " from old state " + _currentState);
+            AMQState oldState = _currentState;
+            _currentState = newState;
+
+            foreach (IStateListener l in _stateListeners)
+            {
+                l.StateChanged(oldState, newState);
+            }
+        }
+
+        public void Error(Exception e)
+        {
+            _logger.Debug("State manager receive error notification: " + e);
+            foreach (IStateListener l in _stateListeners)
+            {
+                l.Error(e);
+            }
+        }
+
+        public bool MethodReceived(AMQMethodEvent evt)
+        {
+            _logger.Debug(String.Format("Finding method handler. currentState={0} type={1}", _currentState, evt.Method.GetType()));            
+            IStateAwareMethodListener handler = FindStateTransitionHandler(_currentState, evt.Method);
+            if (handler != null)
+            {
+                handler.MethodReceived(this, evt);
+                return true;
+            }
+            return false;
+        }
+
+        /// <summary>
+        /// Finds the state transition handler.
+        /// </summary>
+        /// <param name="currentState">State of the current.</param>
+        /// <param name="frame">The frame.</param>
+        /// <returns></returns>
+        /// <exception cref="IllegalStateTransitionException">if the state transition if not allowed</exception>
+        private IStateAwareMethodListener FindStateTransitionHandler(AMQState currentState,
+                                                                     AMQMethodBody frame)                
+        {
+            Type clazz = frame.GetType();
+            if (_logger.IsDebugEnabled)
+            {
+                _logger.Debug("Looking for state transition handler for frame " + clazz);
+            }
+            IDictionary classToHandlerMap = (IDictionary) _state2HandlersMap[currentState];
+
+            if (classToHandlerMap == null)
+            {
+                // if no specialised per state handler is registered look for a
+                // handler registered for "all" states
+                return FindStateTransitionHandler(AMQState.ALL, frame);
+            }
+            IStateAwareMethodListener handler = (IStateAwareMethodListener) classToHandlerMap[clazz];
+            if (handler == null)
+            {
+                if (currentState == AMQState.ALL)
+                {
+                    _logger.Debug("No state transition handler defined for receiving frame " + frame);
+                    return null;
+                }
+                else
+                {
+                    // if no specialised per state handler is registered look for a
+                    // handler registered for "all" states
+                    return FindStateTransitionHandler(AMQState.ALL, frame);
+                }
+            }
+            else
+            {
+                return handler;
+            }
+        }
+
+        public void AddStateListener(IStateListener listener)
+        {
+            _logger.Debug("Adding state listener");
+            _stateListeners.Add(listener);
+        }
+
+        public void RemoveStateListener(IStateListener listener)
+        {
+            _stateListeners.Remove(listener);
+        }
+
+        public void AttainState(AMQState s)
+        {
+            if (_currentState != s)
+            {
+                _logger.Debug("Adding state wait to reach state " + s);
+                StateWaiter sw = new StateWaiter(s);
+                AddStateListener(sw);
+                sw.WaituntilStateHasChanged();
+                // at this point the state will have changed.
+            }
+        }        
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/AMQStateManager.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IAMQStateListener.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IAMQStateListener.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IAMQStateListener.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IAMQStateListener.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,28 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+namespace Qpid.Client.State
+{
+    public interface IAMQStateListener
+    {
+        void StateChanged(AMQStateChangedEvent evt);               
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IAMQStateListener.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateAwareMethodListener.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateAwareMethodListener.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateAwareMethodListener.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateAwareMethodListener.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,30 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using Qpid.Client.Protocol;
+
+namespace Qpid.Client.State
+{
+    public interface IStateAwareMethodListener
+    {
+        void MethodReceived(AMQStateManager stateManager, AMQMethodEvent evt);
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateAwareMethodListener.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateListener.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateListener.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateListener.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateListener.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,32 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+
+namespace Qpid.Client.State
+{
+    public interface IStateListener
+    {
+        void StateChanged(AMQState oldState, AMQState newState);
+
+        void Error(Exception e);
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IStateListener.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IllegalStateTransitionException.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IllegalStateTransitionException.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IllegalStateTransitionException.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IllegalStateTransitionException.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,56 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+
+namespace Qpid.Client.State
+{
+    public class IllegalStateTransitionException : AMQException
+    {
+        private AMQState _originalState;
+
+        private Type _frame;
+
+        public IllegalStateTransitionException(AMQState originalState, Type frame)
+            : base("No valid state transition defined for receiving frame " + frame +
+                   " from state " + originalState)
+        {            
+            _originalState = originalState;
+            _frame = frame;
+        }
+
+        public AMQState OriginalState            
+        {
+            get
+            {
+                return _originalState;
+            }
+        }
+
+        public Type FrameType
+        {
+            get
+            {
+                return _frame;
+            }
+        }
+    }
+}
+

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/IllegalStateTransitionException.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/StateWaiter.cs
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/StateWaiter.cs?view=auto&rev=479211
==============================================================================
--- incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/StateWaiter.cs (added)
+++ incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/StateWaiter.cs Sat Nov 25 14:04:39 2006
@@ -0,0 +1,99 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+using System;
+using System.Threading;
+using log4net;
+
+namespace Qpid.Client.State
+{
+    public class StateWaiter : IStateListener
+    {
+        private static readonly ILog _logger = LogManager.GetLogger(typeof(StateWaiter));
+
+        private readonly AMQState _state;
+
+        private volatile bool _newStateAchieved;
+
+        private volatile Exception _exception;
+
+        private ManualResetEvent _resetEvent = new ManualResetEvent(false);
+        
+        public StateWaiter(AMQState state)
+        {
+            _state = state;
+        }
+
+        public void StateChanged(AMQState oldState, AMQState newState)
+        {            
+            if (_logger.IsDebugEnabled)
+            {
+                _logger.Debug("stateChanged called");
+            }
+            if (_state == newState)
+            {
+                _newStateAchieved = true;
+
+                if (_logger.IsDebugEnabled)
+                {
+                    _logger.Debug("New state reached so notifying monitor");
+                }
+                _resetEvent.Set();
+            }            
+        }
+
+        public void Error(Exception e)
+        {            
+            if (_logger.IsDebugEnabled)
+            {
+                _logger.Debug("exceptionThrown called");
+            }
+
+            _exception = e;
+            _resetEvent.Set();
+        }
+        
+        public void WaituntilStateHasChanged()
+        {            
+            //
+            // The guard is required in case we are woken up by a spurious
+            // notify().
+            //
+            while (!_newStateAchieved && _exception == null)
+            {                    
+                _logger.Debug("State not achieved so waiting...");
+                _resetEvent.WaitOne();                    
+            }
+
+            if (_exception != null)
+            {
+                _logger.Debug("Throwable reached state waiter: " + _exception);
+                if (_exception is AMQException)
+                {
+                    throw _exception;
+                }
+                else
+                {
+                    throw new AMQException("Error: "  + _exception, _exception);
+                }
+            }
+        }
+    }
+}

Propchange: incubator/qpid/trunk/qpid/dotnet/Qpid.Client/Client/State/StateWaiter.cs
------------------------------------------------------------------------------
    svn:eol-style = native