class SessionDB(object): def __init__(self, serverId, sessionDuration): self.serverId = serverId self.sessionDuration = sessionDuration self.sessions = SessionList() self.updated = Event() def requestSession(self, source, destination): key = SessionKey(server=self.serverId, uuid=uuid.uuid4()) now = time.time() validUntil = now + self.sessionDuration s = Session(key, now, source, destination.key, validUntil) _log.debug("Created session {}".format(key)) self.sessions[key] = s self.updated.fire([key]) return s def validateSession(self, sessionKey, destination): try: s = self.sessions[sessionKey] except KeyError: raise DatabaseException("Session {0} not found".format(sessionKey)) if s.destination != destination.key: raise DatabaseException("Requested destination " "{0} does not match session " "{1}".format(destination, s)) return True def merger(self): return SessionDBMerger(self)
class HostDB(object): def __init__(self): self.hosts = {} self.updated = Event() def validateHost(self, address, cname): if address not in self.hosts: # Remember invalid hosts anyway, to ease administration self.hosts[address] = Host(address, time.time(), cname, True) self.updated.fire([address]) raise DatabaseException("Unknown host {0}, " "remembering".format(address)) h = self.hosts[address] if not h.isAllowed: raise DatabaseException("Forbidden host {0}".format(h)) if h.name != cname: raise DatabaseException("Bad computer name {0} " "for host {1}".format(cname, h)) return h def queryHost(self, address): if address not in self.hosts: raise DatabaseException("Unknown host {0}".format(address)) h = self.hosts[address] if not h.isAllowed: raise DatabaseException("Forbidden host {0}".format(h)) return h def merger(self): return HostDBMerger(self)
class SignalingMixin(object): def __init__(self, *args, **kwargs): super(SignalingMixin, self).__init__(*args, **kwargs) self.dataEvent = Event() self.disconnectEvent = Event() self.connectEvent = Event() def dataReceived(self, data): self.dataEvent.fire(data) super(SignalingMixin, self).dataReceived(data) def connectionLost(self, reason): self.disconnectEvent.fire(reason) super(SignalingMixin, self).connectionLost(reason) def connectionMade(self): self.connectEvent.fire() super(SignalingMixin, self).connectionMade()