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)
def __init__(self, localServerId, remoteServerId): super(ReplicationProtocol, self).__init__() _log.debug("Constructed") self._localServerId = localServerId self._remoteServerId = remoteServerId self._remoteServerIdD = Deferred() self.onUpdate = Event() self.onListRequest = Event() if self._remoteServerId: self._remoteServerIdD.callback(self._remoteServerId)
class ReplicationProtocol(SignalingMixin, StatefulProtocol): def __init__(self, localServerId, remoteServerId): super(ReplicationProtocol, self).__init__() _log.debug("Constructed") self._localServerId = localServerId self._remoteServerId = remoteServerId self._remoteServerIdD = Deferred() self.onUpdate = Event() self.onListRequest = Event() if self._remoteServerId: self._remoteServerIdD.callback(self._remoteServerId) @timeout() @inlineCallbacks def announce(self): _log.info('Announcing serverId to %s', self.transport.getPeer()) self.writeUInt8(0x02) self.writeString8(self._localServerId) reply = yield self.readUInt8() if reply != 0x04: raise ProtocolException("Invalid reply to SERVER_ID") self._remoteServerId = yield self.readString8() self._remoteServerIdD.callback(self._remoteServerId) @timeout() def list(self): self.writeUInt8(0xF0) keys = pb2.List() data = keys.SerializeToString() _log.debug('Requesting LIST: data=%s', data) self.writeBinary16(data) return self.onUpdate.waitFor1() def sendUpdates(self, (sessionUpdates, hostUpdates)): updatesList = pb2.List() updatesList.hosts.extend(hostUpdates) updatesList.sessions.extend(sessionUpdates) data = updatesList.SerializeToString() self.writeUInt8(0xF1) self.writeBinary16(data)
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()
def __init__(self, *args, **kwargs): super(SignalingMixin, self).__init__(*args, **kwargs) self.dataEvent = Event() self.disconnectEvent = Event() self.connectEvent = Event()
def __init__(self, serverId, sessionDuration): self.serverId = serverId self.sessionDuration = sessionDuration self.sessions = SessionList() self.updated = Event()
def __init__(self): self.hosts = {} self.updated = Event()