1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
26 // The contents of this file are subject to the Mozilla Public License
27 // Version 1.1 (the "License"); you may not use this file except in
28 // compliance with the License. You may obtain a copy of the License at
29 // http://www.rabbitmq.com/mpl.html
31 // Software distributed under the License is distributed on an "AS IS"
32 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 // License for the specific language governing rights and limitations
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd,
39 // Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
41 // Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 // Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 // are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 // Technologies LLC, and Rabbit Technologies Ltd.
46 // Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 // Ltd. Portions created by Cohesive Financial Technologies LLC are
48 // Copyright (C) 2007-2009 Cohesive Financial Technologies
49 // LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 // (C) 2007-2009 Rabbit Technologies Ltd.
52 // All Rights Reserved.
54 // Contributor(s): ______________________________________.
56 //---------------------------------------------------------------------------
58 using System.Threading;
59 using System.Collections;
61 using RabbitMQ.Client;
62 using RabbitMQ.Client.Exceptions;
65 namespace RabbitMQ.Client.Impl
67 public class SessionManager
69 private readonly Hashtable m_sessionMap = new Hashtable();
70 private readonly ConnectionBase m_connection;
71 private ushort m_channelMax = 0;
72 private bool m_autoClose = false;
74 public SessionManager(ConnectionBase connection)
76 m_connection = connection;
79 public ushort ChannelMax
108 return m_sessionMap.Count;
112 public ISession Lookup(int number)
116 return (ISession) m_sessionMap[number];
120 public ISession Create()
124 int channelNumber = Allocate();
125 if (channelNumber == -1)
127 throw new ChannelAllocationException();
129 return Create(channelNumber);
133 public ISession Create(int channelNumber)
138 if (m_sessionMap.ContainsKey(channelNumber))
140 throw new ChannelAllocationException(channelNumber);
142 session = new Session(m_connection, channelNumber);
143 session.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
144 //Console.WriteLine("SessionManager adding session "+session);
145 m_sessionMap[channelNumber] = session;
150 ///<summary>Replace an active session slot with a new ISession
151 ///implementation. Used during channel quiescing.</summary>
153 /// Make sure you pass in a channelNumber that's currently in
154 /// use, as if the slot is unused, you'll get a null pointer
157 public ISession Swap(int channelNumber, ISession replacement) {
160 ISession previous = (ISession) m_sessionMap[channelNumber];
161 previous.SessionShutdown -= new SessionShutdownEventHandler(HandleSessionShutdown);
162 m_sessionMap[channelNumber] = replacement;
163 replacement.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
168 ///<summary>Find an unused channel number. Must be called
169 ///while holding m_sessionMap lock!</summary>
171 /// Returns -1 if no unused channel numbers are available.
173 public int Allocate()
175 ushort maxChannels = (m_channelMax == 0) ? ushort.MaxValue : m_channelMax;
176 for (int candidate = 1; candidate <= maxChannels; candidate++)
178 if (!m_sessionMap.ContainsKey(candidate))
186 public void HandleSessionShutdown(ISession session, ShutdownEventArgs reason)
188 //Console.WriteLine("SessionManager removing session "+session);
191 m_sessionMap.Remove(session.ChannelNumber);
196 ///<summary>If m_autoClose and there are no active sessions
197 ///remaining, Close()s the connection with reason code
199 public void CheckAutoClose()
205 if (m_sessionMap.Count == 0)
207 // Run this in a background thread, because
208 // usually CheckAutoClose will be called from
209 // HandleSessionShutdown above, which runs in
210 // the thread of the connection. If we were to
211 // attempt to close the connection from within
212 // the connection's thread, we would suffer a
213 // deadlock as the connection thread would be
214 // blocking waiting for its own mainloop to
216 new Thread(new ThreadStart(AutoCloseConnection)).Start();
222 ///<summary>Called from CheckAutoClose, in a separate thread,
223 ///when we decide to close the connection.</summary>
224 public void AutoCloseConnection()
226 m_connection.Abort(200, "AutoClose", ShutdownInitiator.Library, Timeout.Infinite);