summaryrefslogtreecommitdiff
path: root/dotnet/Qpid.Client/Client/Protocol/AMQProtocolSession.cs
blob: 1fb3d407ebe609abd691026c754db561f855317d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 *
 * 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 Apache.Qpid.Client.Message;
using Apache.Qpid.Client.Transport;
using Apache.Qpid.Framing;
using Apache.Qpid.Sasl;

namespace Apache.Qpid.Client.Protocol
{
    public class AMQProtocolSession
    {
        private static readonly ILog _logger = LogManager.GetLogger(typeof(AMQProtocolSession));

        private readonly IProtocolWriter _protocolWriter;
        private readonly IConnectionCloser _connectionCloser;

        /// <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 string ClientID { get { return _connection.ClientID; } }

        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;
            }
        }

        private ISaslClient _saslClient;
        public ISaslClient SaslClient
        {
            get { return _saslClient; }
            set { _saslClient = value; }
        }
        
        /// <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.ClosedWithException(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()
        {
        	return "ntmp_" + System.Guid.NewGuid();
        }
    }
}