4ace62ae63f375de0e46cc6bf21072251579d84a
[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-2009 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 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.
45 //
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.
51 //
52 //   All Rights Reserved.
53 //
54 //   Contributor(s): ______________________________________.
55 //
56 //---------------------------------------------------------------------------
57 using System;
58 using System.Threading;
59 using System.Collections;
60
61 using RabbitMQ.Client;
62 using RabbitMQ.Client.Exceptions;
63 using RabbitMQ.Util;
64
65 namespace RabbitMQ.Client.Impl
66 {
67     public class SessionManager
68     {
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;
73
74         public SessionManager(ConnectionBase connection)
75         {
76             m_connection = connection;
77         }
78
79         public ushort ChannelMax
80         {
81             get
82             {
83                 return m_channelMax;
84             }
85             set
86             {
87                 m_channelMax = value;
88             }
89         }
90
91         public bool AutoClose
92         {
93             get
94             {
95                 return m_autoClose;
96             }
97             set
98             {
99                 m_autoClose = value;
100                 CheckAutoClose();
101             }
102         }
103
104         public int Count
105         {
106             get
107             {
108                 return m_sessionMap.Count;
109             }
110         }
111
112         public ISession Lookup(int number)
113         {
114             lock (m_sessionMap)
115             {
116                 return (ISession) m_sessionMap[number];
117             }
118         }
119
120         public ISession Create()
121         {
122             lock (m_sessionMap)
123             {
124                 int channelNumber = Allocate();
125                 if (channelNumber == -1)
126                 {
127                     throw new ChannelAllocationException();
128                 }
129                 return Create(channelNumber);
130             }
131         }
132
133         public ISession Create(int channelNumber)
134         {
135             ISession session;
136             lock (m_sessionMap)
137             {
138                 if (m_sessionMap.ContainsKey(channelNumber))
139                 {
140                     throw new ChannelAllocationException(channelNumber);
141                 }
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;
146             }
147             return session;
148         }
149
150         ///<summary>Replace an active session slot with a new ISession
151         ///implementation. Used during channel quiescing.</summary>
152         ///<remarks>
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
155         /// exception.
156         ///</remarks>
157         public ISession Swap(int channelNumber, ISession replacement) {
158             lock (m_sessionMap)
159             {
160                 ISession previous = (ISession) m_sessionMap[channelNumber];
161                 previous.SessionShutdown -= new SessionShutdownEventHandler(HandleSessionShutdown);
162                 m_sessionMap[channelNumber] = replacement;
163                 replacement.SessionShutdown += new SessionShutdownEventHandler(HandleSessionShutdown);
164                 return previous;
165             }
166         }
167
168         ///<summary>Find an unused channel number. Must be called
169         ///while holding m_sessionMap lock!</summary>
170         ///<remarks>
171         /// Returns -1 if no unused channel numbers are available.
172         ///</remarks>
173         public int Allocate()
174         {
175             ushort maxChannels = (m_channelMax == 0) ? ushort.MaxValue : m_channelMax;
176             for (int candidate = 1; candidate <= maxChannels; candidate++)
177             {
178                 if (!m_sessionMap.ContainsKey(candidate))
179                 {
180                     return candidate;
181                 }
182             }
183             return -1;
184         }
185
186         public void HandleSessionShutdown(ISession session, ShutdownEventArgs reason)
187         {
188             //Console.WriteLine("SessionManager removing session "+session);
189             lock (m_sessionMap)
190             {
191                 m_sessionMap.Remove(session.ChannelNumber);
192                 CheckAutoClose();
193             }
194         }
195
196         ///<summary>If m_autoClose and there are no active sessions
197         ///remaining, Close()s the connection with reason code
198         ///200.</summary>
199         public void CheckAutoClose()
200         {
201             if (m_autoClose)
202             {
203                 lock (m_sessionMap)
204                 {
205                     if (m_sessionMap.Count == 0)
206                     {
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
215                         // reply to it.
216                         new Thread(new ThreadStart(AutoCloseConnection)).Start();
217                     }
218                 }
219             }
220         }
221
222         ///<summary>Called from CheckAutoClose, in a separate thread,
223         ///when we decide to close the connection.</summary>
224         public void AutoCloseConnection()
225         {
226             m_connection.Abort(200, "AutoClose", ShutdownInitiator.Library, Timeout.Infinite);
227         }
228     }
229 }