Source

amqpev / amqpev / channel_fsm.py

import sys
from eventlet.channel import channel
from spec.spec import spec_0_8 as spec
import errors


def connection_init(ch, auth="guest\x00guest", locale="en_US", vhost="/",
        insist=False):
    """
    Connection initialization logic. Assumes that the transport has just been
    opened, and has not yet received a start method from the server. Will start
    and secure the channel and open the given vhost.
    """
    ch.wait_method(spec.connection.start)
    # TODO: Do something with connection params
    ch.send_method(spec.connection.start_ok,
            client_properties={
                'platform': "Python " + sys.version.split(' ')[0],
                'product': "AMQPev Client",
                'version': "0.0.9",
                'copyright': "Copyright (c) 2009 Kyle Schaffrick",
                'information': "Non-blocking AMQP client for Eventlet" },
            mechanism="PLAIN",
            response=auth,
            locale=locale)

    while True:
        rx_m = ch.wait_method(spec.connection.tune,
                spec.connection.secure)

        if rx_m.method == spec.connection.secure:
            # TODO: Handle challenge-response
            ch.send_method(spec.connection.secure_ok, response="")
            rx_m = ch.wait_method(spec.connection.tune)

        elif rx_m.method == spec.connection.tune:
            tune_params = rx_m.args.copy()
            ch.send_method(spec.connection.tune_ok, **tune_params)
            ch.send_method(spec.connection.open,
                    virtual_host=vhost,
                    capabilities="",
                    insist=insist)
            break

    rx_m = ch.wait_method(spec.connection.open_ok, spec.connection.redirect)

    if rx_m.method == spec.connection.open_ok:
        return ('open', tune_params)
    elif rx_m.method == spec.connection.redirect:
        redirected_to = rx_m.args['host']
        ch.send_method(spec.connection.close)
        ch.wait_method(spec.connection.close_ok)
        return ('redirected', redirected_to)