Beispiel #1
0
class Client:
    def __init__(self, host, port, spec=None, vhost=None):
        self.host = host
        self.port = port
        if spec:
            self.spec = spec
        else:
            from specs_config import amqp_spec_0_9
            self.spec = load(amqp_spec_0_9)
        self.structs = StructFactory(self.spec)
        self.sessions = {}

        self.mechanism = None
        self.response = None
        self.locale = None
        self.sasl = None

        self.vhost = vhost
        if self.vhost == None:
            self.vhost = "/"

        self.queues = {}
        self.lock = threading.Lock()

        self.closed = False
        self.reason = None
        self.started = threading.Event()
        self.peer = None

    def wait(self):
        self.started.wait()
        if self.closed:
            raise Closed(self.reason)

    def queue(self, key):
        self.lock.acquire()
        try:
            try:
                q = self.queues[key]
            except KeyError:
                q = Queue(0)
                self.queues[key] = q
        finally:
            self.lock.release()
        return q

    def start(self,
              response=None,
              mechanism=None,
              locale="en_US",
              tune_params=None,
              username=None,
              password=None,
              client_properties=None,
              connection_options=None,
              sasl_options=None,
              channel_options=None):
        self.mechanism = mechanism
        self.response = response
        self.username = username
        self.password = password
        self.locale = locale
        self.tune_params = tune_params
        self.client_properties = get_client_properties_with_defaults(
            provided_client_properties=client_properties,
            version_property_key="version")
        self.sasl_options = sasl_options
        self.socket = connect(self.host, self.port, connection_options)
        self.conn = Connection(self.socket, self.spec)
        self.peer = Peer(self.conn, ClientDelegate(self), Session,
                         channel_options)

        self.conn.init()
        self.peer.start()
        self.wait()
        self.channel(0).connection_open(self.vhost)

    def channel(self, id):
        self.lock.acquire()
        try:
            ssn = self.peer.channel(id)
            ssn.client = self
            self.sessions[id] = ssn
        finally:
            self.lock.release()
        return ssn

    def session(self):
        self.lock.acquire()
        try:
            id = None
            for i in xrange(1, 64 * 1024):
                if not self.sessions.has_key(i):
                    id = i
                    break
        finally:
            self.lock.release()
        if id == None:
            raise RuntimeError("out of channels")
        else:
            return self.channel(id)

    def close(self):
        if self.peer:
            try:
                if not self.closed:
                    channel = self.channel(0)
                    if channel and not channel._closed:
                        try:
                            channel.connection_close(reply_code=200)
                        except:
                            pass
                    self.closed = True
            finally:
                self.peer.stop()
Beispiel #2
0
class Client:

  def __init__(self, host, port, spec = None, vhost = None):
    self.host = host
    self.port = port
    if spec:
      self.spec = spec
    else:
      from specs_config import amqp_spec_0_9
      self.spec = load(amqp_spec_0_9)
    self.structs = StructFactory(self.spec)
    self.sessions = {}

    self.mechanism = None
    self.response = None
    self.locale = None
    self.sasl = None

    self.vhost = vhost
    if self.vhost == None:
      self.vhost = "/"

    self.queues = {}
    self.lock = threading.Lock()

    self.closed = False
    self.reason = None
    self.started = threading.Event()
    self.peer = None

  def wait(self):
    self.started.wait()
    if self.closed:
      raise Closed(self.reason)

  def queue(self, key):
    self.lock.acquire()
    try:
      try:
        q = self.queues[key]
      except KeyError:
        q = Queue(0)
        self.queues[key] = q
    finally:
      self.lock.release()
    return q

  def start(self, response=None, mechanism=None, locale="en_US", tune_params=None,
            username=None, password=None,
            client_properties=None, connection_options=None, sasl_options = None,
            channel_options=None):
    if response is not None and (username is not None or password is not None):
      raise RuntimeError("client must not specify both response and (username, password).")
    if response is not None:
      self.response = response
      authzid, self.username, self.password = response.split("\0")
    else:
      self.username = username
      self.password = password
    self.mechanism = mechanism
    self.locale = locale
    self.tune_params = tune_params
    self.client_properties=get_client_properties_with_defaults(provided_client_properties=client_properties, version_property_key="version")
    self.sasl_options = sasl_options
    self.socket = connect(self.host, self.port, connection_options)
    self.conn = Connection(self.socket, self.spec)
    self.peer = Peer(self.conn, ClientDelegate(self), Session, channel_options)

    self.conn.init()
    self.peer.start()
    self.wait()
    self.channel(0).connection_open(self.vhost)

  def channel(self, id):
    self.lock.acquire()
    try:
      ssn = self.peer.channel(id)
      ssn.client = self
      self.sessions[id] = ssn
    finally:
      self.lock.release()
    return ssn

  def session(self):
    self.lock.acquire()
    try:
      id = None
      for i in xrange(1, 64*1024):
        if not self.sessions.has_key(i):
          id = i
          break
    finally:
      self.lock.release()
    if id == None:
      raise RuntimeError("out of channels")
    else:
      return self.channel(id)

  def close(self):
    if self.peer:
      try:
        if not self.closed:
          channel = self.channel(0);
          if channel and not channel._closed:
             try:
               channel.connection_close(reply_code=200)
             except:
               pass
          self.closed = True
      finally:
        self.peer.stop()