Beispiel #1
0
  def __init__(self, host, port=None, username="******", password="******",
               mechanism="PLAIN", heartbeat=None, **options):
    """
    Creates a connection. A newly created connection must be connected
    with the Connection.connect() method before it can be used.

    @type host: str
    @param host: the name or ip address of the remote host
    @type port: int
    @param port: the port number of the remote host
    @rtype: Connection
    @return: a disconnected Connection
    """
    self.host = host
    self.port = default(port, AMQP_PORT)
    self.username = username
    self.password = password
    self.mechanism = mechanism
    self.heartbeat = heartbeat

    self.id = str(uuid4())
    self.session_counter = 0
    self.sessions = {}
    self.reconnect = options.get("reconnect", False)
    self._connected = False
    self._lock = RLock()
    self._condition = Condition(self._lock)
    self._waiter = Waiter(self._condition)
    self._modcount = Serial(0)
    self.error = None
    from driver import Driver
    self._driver = Driver(self)
    self._driver.start()
Beispiel #2
0
 def __init__(self, auth=False):
   self.proto = ProtoConnection(self.session)
   self.auth = auth
   if self.auth:
     self.sasl = SASL(self.proto)
   else:
     self.sasl = self.proto
   self._lock = RLock()
   self.condition = Condition(self._lock)
   self.waiter = Waiter(self.condition)
   self.selector = Selector.default()
   self.timeout = 120
Beispiel #3
0
class Connection:

  """
  A Connection manages a group of L{Sessions<Session>} and connects
  them with a remote endpoint.
  """

  @static
  def open(host, port=None, username="******", password="******",
           mechanism="PLAIN", heartbeat=None, **options):
    """
    Creates an AMQP connection and connects it to the given host and port.

    @type host: str
    @param host: the name or ip address of the remote host
    @type port: int
    @param port: the port number of the remote host
    @rtype: Connection
    @return: a connected Connection
    """
    conn = Connection(host, port, username, password, mechanism, heartbeat, **options)
    conn.connect()
    return conn

  def __init__(self, host, port=None, username="******", password="******",
               mechanism="PLAIN", heartbeat=None, **options):
    """
    Creates a connection. A newly created connection must be connected
    with the Connection.connect() method before it can be used.

    @type host: str
    @param host: the name or ip address of the remote host
    @type port: int
    @param port: the port number of the remote host
    @rtype: Connection
    @return: a disconnected Connection
    """
    self.host = host
    self.port = default(port, AMQP_PORT)
    self.username = username
    self.password = password
    self.mechanism = mechanism
    self.heartbeat = heartbeat

    self.id = str(uuid4())
    self.session_counter = 0
    self.sessions = {}
    self.reconnect = options.get("reconnect", False)
    self._connected = False
    self._lock = RLock()
    self._condition = Condition(self._lock)
    self._waiter = Waiter(self._condition)
    self._modcount = Serial(0)
    self.error = None
    from driver import Driver
    self._driver = Driver(self)
    self._driver.start()

  def _wait(self, predicate, timeout=None):
    return self._waiter.wait(predicate, timeout=timeout)

  def _wakeup(self):
    self._modcount += 1
    self._driver.wakeup()

  def _check_error(self, exc=ConnectionError):
    if self.error:
      raise exc(*self.error)

  def _ewait(self, predicate, timeout=None, exc=ConnectionError):
    result = self._wait(lambda: self.error or predicate(), timeout)
    self._check_error(exc)
    return result

  @synchronized
  def session(self, name=None, transactional=False):
    """
    Creates or retrieves the named session. If the name is omitted or
    None, then a unique name is chosen based on a randomly generated
    uuid.

    @type name: str
    @param name: the session name
    @rtype: Session
    @return: the named Session
    """

    if name is None:
      name = "%s:%s" % (self.id, self.session_counter)
      self.session_counter += 1
    else:
      name = "%s:%s" % (self.id, name)

    if self.sessions.has_key(name):
      return self.sessions[name]
    else:
      ssn = Session(self, name, transactional)
      self.sessions[name] = ssn
      self._wakeup()
      return ssn

  @synchronized
  def _remove_session(self, ssn):
    del self.sessions[ssn.name]

  @synchronized
  def connect(self):
    """
    Connect to the remote endpoint.
    """
    self._connected = True
    self._wakeup()
    self._ewait(lambda: self._driver._connected, exc=ConnectError)

  @synchronized
  def disconnect(self):
    """
    Disconnect from the remote endpoint.
    """
    self._connected = False
    self._wakeup()
    self._ewait(lambda: not self._driver._connected)

  @synchronized
  def connected(self):
    """
    Return true if the connection is connected, false otherwise.
    """
    return self._connected

  @synchronized
  def close(self):
    """
    Close the connection and all sessions.
    """
    for ssn in self.sessions.values():
      ssn.close()
    self.disconnect()
Beispiel #4
0
class Connection:

  def __init__(self, auth=False):
    self.proto = ProtoConnection(self.session)
    self.auth = auth
    if self.auth:
      self.sasl = SASL(self.proto)
    else:
      self.sasl = self.proto
    self._lock = RLock()
    self.condition = Condition(self._lock)
    self.waiter = Waiter(self.condition)
    self.selector = Selector.default()
    self.timeout = 120

  def tracing(self, *args, **kwargs):
    self.proto.tracing(*args, **kwargs)
    self.sasl.tracing(*args, **kwargs)

  def trace(self, *args, **kwargs):
    self.proto.trace(*args, **kwargs)

  @synchronized
  def connect(self, host, port):
    sock = socket.socket()
    sock.connect((host, port))
    sock.setblocking(0)
    self.selector.register(ConnectionSelectable(sock, self, self.tick))

  @synchronized
  def pending(self):
    return self.sasl.pending()

  @synchronized
  def peek(self, n=None):
    return self.sasl.peek(n)

  @synchronized
  def read(self, n=None):
    return self.sasl.read(n)

  @synchronized
  def write(self, bytes):
    self.sasl.write(bytes)

  @synchronized
  def closed(self):
    self.sasl.closed()

  @synchronized
  def error(self, exc):
    self.sasl.error(exc)

  @synchronized
  def tick(self, connection):
    self.proto.tick()
    self.sasl.tick()
    self.waiter.notify()

  @synchronized
  def open(self, **kwargs):
    if not kwargs.get("container_id"):
      kwargs["container_id"] = str(uuid4())
    if "channel_max" not in kwargs:
      kwargs["channel_max"] = 65535
    mechanism = kwargs.pop("mechanism", "ANONYMOUS")
    username = kwargs.pop("username", None)
    password = kwargs.pop("password", None)
    if self.auth:
      self.sasl.client(mechanism=mechanism, username=username,
                       password=password)
    self.proto.open(**kwargs)
    if self.auth:
      self.wait(lambda: self.sasl.outcome is not None)
      if self.sasl.outcome != 0:
        raise Exception("authentication failed: %s" % self.sasl.outcome)

  def wait(self, predicate, timeout=DEFAULT):
    if timeout is DEFAULT:
      timeout = self.timeout
    self.selector.wakeup()
    if not self.waiter.wait(predicate, timeout):
      raise Timeout()

  @synchronized
  def session(self):
    ssn = Session(self)
    self.proto.add(ssn.proto)
    return ssn

  @synchronized
  def close(self):
    self.proto.close()
    self.wait(lambda: self.proto.close_rcvd)