Example #1
0
        public void MethodReceived(AMQStateManager stateManager, AMQMethodEvent evt)
        {
            _logger.Debug("ConnectionTune frame received");
            ConnectionTuneBody frame   = (ConnectionTuneBody)evt.Method;
            AMQProtocolSession session = evt.ProtocolSession;

            ConnectionTuneParameters parameters = session.ConnectionTuneParameters;

            if (parameters == null)
            {
                parameters = new ConnectionTuneParameters();
            }

            _logger.Debug(String.Format("ConnectionTune.heartbeat = {0}.", frame.Heartbeat));

            parameters.FrameMax              = frame.FrameMax;
            parameters.Heartbeat             = frame.Heartbeat;
            session.ConnectionTuneParameters = parameters;

            stateManager.ChangeState(AMQState.CONNECTION_NOT_OPENED);
            session.WriteFrame(ConnectionTuneOkBody.CreateAMQFrame(
                                   evt.ChannelId, frame.ChannelMax, frame.FrameMax, frame.Heartbeat));
            session.WriteFrame(ConnectionOpenBody.CreateAMQFrame(
                                   evt.ChannelId, session.AMQConnection.VirtualHost, null, true));

            if (frame.Heartbeat > 0)
            {
                evt.ProtocolSession.AMQConnection.StartHeartBeatThread(frame.Heartbeat);
            }
        }
Example #2
0
        public void Initialize(AMQProtocolSession session)
        {
            if (session == null)
            {
                throw new ArgumentNullException("session");
            }

            _session = session;
        }
        public void MethodReceived(AMQStateManager stateManager, AMQMethodEvent evt)
        {
            ConnectionStartBody body = (ConnectionStartBody)evt.Method;
            AMQProtocolSession  ps   = evt.ProtocolSession;

            try
            {
                if (body.Mechanisms == null)
                {
                    throw new AMQException("mechanism not specified in ConnectionStart method frame");
                }
                string mechanisms        = Encoding.UTF8.GetString(body.Mechanisms);
                string selectedMechanism = ChooseMechanism(mechanisms);
                if (selectedMechanism == null)
                {
                    throw new AMQException("No supported security mechanism found, passed: " + mechanisms);
                }

                byte[] saslResponse = DoAuthentication(selectedMechanism, ps);

                if (body.Locales == null)
                {
                    throw new AMQException("Locales is not defined in Connection Start method");
                }
                string   allLocales = Encoding.ASCII.GetString(body.Locales);
                string[] locales    = allLocales.Split(' ');
                string   selectedLocale;
                if (locales != null && locales.Length > 0)
                {
                    selectedLocale = locales[0];
                }
                else
                {
                    throw new AMQException("No locales sent from server, passed: " + locales);
                }

                stateManager.ChangeState(AMQState.CONNECTION_NOT_TUNED);
                FieldTable clientProperties = new FieldTable();
                clientProperties["product"]  = "Apache.Qpid.NET";
                clientProperties["version"]  = "1.0";
                clientProperties["platform"] = GetFullSystemInfo();
                clientProperties["instance"] = ps.ClientID;
                AMQFrame frame = ConnectionStartOkBody.CreateAMQFrame(
                    evt.ChannelId, clientProperties, selectedMechanism,
                    saslResponse, selectedLocale);
                ps.WriteFrame(frame);
            }
            catch (Exception e)
            {
                throw new AMQException(_log, "Unable to decode data: " + e, e);
            }
        }
        private IAMQCallbackHandler CreateCallbackHandler(string mechanism, AMQProtocolSession session)
        {
            Type type = CallbackHandlerRegistry.Instance.GetCallbackHandler(mechanism);
            IAMQCallbackHandler handler =
                (IAMQCallbackHandler)Activator.CreateInstance(type);

            if (handler == null)
            {
                throw new AMQException("Unable to create callback handler: " + mechanism);
            }
            handler.Initialize(session);
            return(handler);
        }
        private void MakeBrokerConnection(IBrokerInfo brokerDetail)
        {
            try
            {
                _stateManager     = new AMQStateManager();
                _protocolListener = new AMQProtocolListener(this, _stateManager);
                _protocolListener.AddFrameListener(_stateManager);

                /*
                 * // Currently there is only one transport option - BlockingSocket.
                 * String assemblyName = "Apache.Qpid.Client.Transport.Socket.Blocking.dll";
                 * String transportType = "Apache.Qpid.Client.Transport.Socket.Blocking.BlockingSocketTransport";
                 *
                 * // Load the transport assembly dynamically.
                 * _transport = LoadTransportFromAssembly(brokerDetail.getHost(), brokerDetail.getPort(), assemblyName, transportType);
                 */

                _transport = new BlockingSocketTransport();

                // Connect.
                _transport.Connect(brokerDetail, this);
                _protocolWriter  = new ProtocolWriter(_transport.ProtocolWriter, _protocolListener);
                _protocolSession = new AMQProtocolSession(_transport.ProtocolWriter, _transport, this);
                _protocolListener.ProtocolSession = _protocolSession;

                // Now start the connection "handshake".
                _transport.ProtocolWriter.Write(new ProtocolInitiation());

                // Blocks until the connection has been opened.
                _stateManager.AttainState(AMQState.CONNECTION_OPEN);

                _failoverPolicy.attainedConnection();

                // XXX: Again this should be changed to a suitable notify.
                _connected = true;
            }
            catch (AMQException e)
            {
                _lastAMQException = e;
                throw; // rethrow
            }
        }
        private byte[] DoAuthentication(string selectedMechanism, AMQProtocolSession ps)
        {
            ISaslClient saslClient = Sasl.Sasl.CreateClient(
                new string[] { selectedMechanism }, null, "AMQP", "localhost",
                new Hashtable(), CreateCallbackHandler(selectedMechanism, ps)
                );

            if (saslClient == null)
            {
                throw new AMQException("Client SASL configuration error: no SaslClient could be created for mechanism " +
                                       selectedMechanism);
            }
            ps.SaslClient = saslClient;
            try
            {
                return(saslClient.HasInitialResponse ?
                       saslClient.EvaluateChallenge(new byte[0]) : null);
            } catch (Exception ex)
            {
                ps.SaslClient = null;
                throw new AMQException("Unable to create SASL client", ex);
            }
        }