Пример #1
0
 def readable(self, selector):
     sock, addr = self.sock.accept()
     conn = Connection(lambda properties: Session(link, properties))
     conn.tracing(*self.traces)
     if self.auth:
         sasl = SASL(conn)
         sasl.tracing(*self.traces)
         sasl.server(self.mechanisms, passwords=self.passwords)
         sel = sasl
     else:
         sel = conn
     selector.register(ConnectionSelectable(sock, sel, self.tick, self.period, self.timeout))
Пример #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
Пример #3
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)