5ac3100a10bd49cce74bbcacf0c7a908a3818c53
[mono.git] / mcs / class / RabbitMQ.Client / src / client / impl / SessionManager.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007, 2008 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
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
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
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 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
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
30 //
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
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd.,
39 //   Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
40 //
41 //   Portions created by LShift Ltd., Cohesive Financial Technologies
42 //   LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007, 2008
43 //   LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
44 //   Technologies Ltd.;
45 //
46 //   All Rights Reserved.
47 //
48 //   Contributor(s): ______________________________________.
49 //
50 //---------------------------------------------------------------------------
51 using System;
52 using System.Threading;
53 using System.Collections;
54
55 using RabbitMQ.Client;
56 using RabbitMQ.Client.Exceptions;
57 using RabbitMQ.Util;
58
59 namespace RabbitMQ.Client.Impl
60 {
61     public class SessionManager
62     {
63         private readonly Hashtable m_sessionMap = new Hashtable();
64         private readonly ConnectionBase m_connection;
65         private ushort m_channelMax = 0;
66         private bool m_autoClose = false;
67
68         public SessionManager(ConnectionBase connection)
69         {
70             m_connection = connection;
71         }
72
73         public ushort ChannelMax
74         {
75             get
76             {
77                 return m_channelMax;
78             }
79             set
80             {
81                 if ((value < 2) && (value != 0))
82                 {
83                     // We currently interpret channel max as
84                     // *including* channel zero. We also think it
85                     // doesn't make sense to forbid opening of any
86                     // real usable channels, so by our ch0-including
87                     // assumption, the minimum *useful* value for
88                     // channel max is 2.
89                     //
90                     // This code is here to work around OpenAMQ
91                     // 1.2c4's channel max setting of 1.
92                     //
93                     // FIXME: warning? or is there something more
94                     // sensible we can do here?
95                     m_channelMax = 2;
96                 }
97                 else
98                 {
99                     m_channelMax = value;
100                 }
101             }
102         }
103
104         public bool AutoClose
105         {
106             get
107             {
108                 return m_autoClose;
109             }
110             set
111             {
112                 m_autoClose = value;
113                 CheckAutoClose();
114             }
115         }
116
117         public int Count
118         {
119             get
120             {
121                 return m_sessionMap.Count;
122             }
123         }
124
125         public ISession Lookup(int number)
126         {
127             lock (m_sessionMap)
128             {
129                 return (ISession) m_sessionMap[number];
130             }
131         }
132
133         public ISession Create()
134         {
135             lock (m_sessionMap)
136             {
137                 int channelNumber = Allocate();
138                 if (channelNumber == -1)
139                 {
140                     throw new ChannelAllocationException();
141                 }
142                 return Create(channelNumber);
143             }
144         }
145
146         public ISession Create(int channelNumber)
147         {
148             ISession session;
149             lock (m_sessionMap)
150             {
151                 if (m_sessionMap.ContainsKey(channelNumber))
152                 {
153                     throw new ChannelAllocationException(channelNumber);
154                 }
155                 session = new Session(m_connection, channelNumber);
156                 session.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
157                 //Console.WriteLine("SessionManager adding session "+session);
158                 m_sessionMap[channelNumber] = session;
159             }
160             return session;
161         }
162
163         ///<summary>Replace an active session slot with a new ISession
164         ///implementation. Used during channel quiescing.</summary>
165         ///<remarks>
166         /// Make sure you pass in a channelNumber that's currently in
167         /// use, as if the slot is unused, you'll get a null pointer
168         /// exception.
169         ///</remarks>
170         public ISession Swap(int channelNumber, ISession replacement) {
171             lock (m_sessionMap)
172             {
173                 ISession previous = (ISession) m_sessionMap[channelNumber];
174                 previous.SessionShutdown -= new SessionShutdownEventHandler(HandleSessionShutdown);
175                 m_sessionMap[channelNumber] = replacement;
176                 replacement.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
177                 return previous;
178             }
179         }
180
181         ///<summary>Find an unused channel number. Must be called
182         ///while holding m_sessionMap lock!</summary>
183         ///<remarks>
184         /// Returns -1 if no unused channel numbers are available.
185         ///</remarks>
186         public int Allocate()
187         {
188             ushort maxChannels = (m_channelMax == 0) ? ushort.MaxValue : m_channelMax;
189             for (int candidate = 1; candidate < maxChannels; candidate++)
190             {
191                 if (!m_sessionMap.ContainsKey(candidate))
192                 {
193                     return candidate;
194                 }
195             }
196             return -1;
197         }
198
199         public void HandleSessionShutdown(ISession session, ShutdownEventArgs reason)
200         {
201             //Console.WriteLine("SessionManager removing session "+session);
202             lock (m_sessionMap)
203             {
204                 m_sessionMap.Remove(session.ChannelNumber);
205                 CheckAutoClose();
206             }
207         }
208
209         ///<summary>If m_autoClose and there are no active sessions
210         ///remaining, Close()s the connection with reason code
211         ///200.</summary>
212         public void CheckAutoClose()
213         {
214             if (m_autoClose)
215             {
216                 lock (m_sessionMap)
217                 {
218                     if (m_sessionMap.Count == 0)
219                     {
220                         // Run this in a background thread, because
221                         // usually CheckAutoClose will be called from
222                         // HandleSessionShutdown above, which runs in
223                         // the thread of the connection. If we were to
224                         // attempt to close the connection from within
225                         // the connection's thread, we would suffer a
226                         // deadlock as the connection thread would be
227                         // blocking waiting for its own mainloop to
228                         // reply to it.
229                         new Thread(new ThreadStart(AutoCloseConnection)).Start();
230                     }
231                 }
232             }
233         }
234
235         ///<summary>Called from CheckAutoClose, in a separate thread,
236         ///when we decide to close the connection.</summary>
237         public void AutoCloseConnection()
238         {
239             m_connection.Abort(200, "AutoClose", ShutdownInitiator.Library, Timeout.Infinite);
240         }
241     }
242 }