Ejemplo n.º 1
0
    def handshake(self, stream):
        self.stream = MessageStream(stream, self.serializer, self.io_loop)
        self.stream.set_close_callback(self.on_close)
        # read version
        print 'waiting for version'
        response = yield gen.Task(self._accept_handshake)
        self.mediator_version = response['version']
        print "Protocol Version:", self.mediator_version

        if not response['is_supported_version']:
            raise IOError("Unsupported version of server. Update builder? (got: %d; expected: %d)" % (
                self.mediator_version, self.serializer.version,
            ))

        self.delegate.handshake(self)
Ejemplo n.º 2
0
    def handle_stream(self, stream, address, server):
        print 'Connection from', address

        # send version
        yield gen.Task(self.send_version, stream)

        msg_stream = MessageStream(stream, self.serializer, self.io_loop)
        client = ClientHandler(msg_stream, address, self.tracker, self.auth)
        self.tracker.add(client)
        client.handshake()
Ejemplo n.º 3
0
    def handshake(self, stream):
        self.stream = MessageStream(stream, self.serializer, self.io_loop)
        self.stream.set_close_callback(self.on_close)
        # read version
        print "waiting for version"
        response = yield gen.Task(self._accept_handshake)
        self.mediator_version = response["version"]
        print "Protocol Version:", self.mediator_version

        if not response["is_supported_version"]:
            raise IOError(
                "Unsupported version of server. Update builder? (got: %d; expected: %d)"
                % (self.mediator_version, self.serializer.version)
            )

        self.delegate.handshake(self)
Ejemplo n.º 4
0
class MediatorClient(object):
    "Handles the communication between the builder and mediator."

    def __init__(self, client, serializer, delegate=None, autoreconnect=True):
        self.serializer = serializer
        self.client = client
        self.delegate = delegate
        self.mediator_version = None
        self.stream = None
        self.wants_to_close = False
        self.autoreconnect = autoreconnect

    def write(self, msg, callback=None):
        self.stream.write(msg, callback=None)

    def read(self, callback=None):
        self.stream.read(callback=callback)

    def create(self, io_loop=None):
        self.client.create(self.handshake, self.on_close, io_loop)
        self.io_loop = self.client.io_loop

    def login(self, account, machine, type="builder", callback=None):
        "Logs into a given account, with specific machine credentials."
        cmd = LoginMessage(username=account.username, password=account.password_hash, machine=machine, type=type)
        self.stream.write_and_read(cmd, callback=callback)

    def register(self, account, callback=None):
        "Registers an account with the mediator server."
        cmd = RegisterMessage(account.username, account.password_hash)
        self.stream.write_and_read(cmd, callback=callback)

    def send(self, machine, command, callback=None):
        "sends a given message to a target machine. Expects no response from target."
        if callable(getattr(command, "to_network", None)):
            command = command.to_network()

        cmd = SendMessage(machine=machine, command=command)
        self.stream.write(cmd, callback=callback)

    def request(self, machine, command, callback=None):
        "Sends a given message to a target machine. Expects a response from target."
        if callable(getattr(command, "to_network", None)):
            command = command.to_network()

        cmd = RequestMessage(machine=machine, command=command)
        self.stream.write_and_read(cmd, callback=callback)

    def write_response(self, command, callback):
        "Sends a given command back as a response."
        if not callable(getattr(command, "to_network", None)):
            command = ResponseMessage.create(command)
        self.stream.write(command, callback=callback)
        print "mediator <-", command

    def clients(self, callback=None):
        "Returns all clients connected using the given account."
        cmd = ClientsMessage()
        self.stream.write_and_read(cmd, callback=callback)

    def _accept_handshake(self, callback):
        self.serializer.accept_handshake(self.stream.iostream, callback=callback)

    @gen.engine
    def handshake(self, stream):
        self.stream = MessageStream(stream, self.serializer, self.io_loop)
        self.stream.set_close_callback(self.on_close)
        # read version
        print "waiting for version"
        response = yield gen.Task(self._accept_handshake)
        self.mediator_version = response["version"]
        print "Protocol Version:", self.mediator_version

        if not response["is_supported_version"]:
            raise IOError(
                "Unsupported version of server. Update builder? (got: %d; expected: %d)"
                % (self.mediator_version, self.serializer.version)
            )

        self.delegate.handshake(self)

    def on_close(self):
        if self.wants_to_close:
            return  # continue
        if self.autoreconnect:
            if self.client.is_connected:
                print "Connection lost. Reconnecting..."
            self.create()

    def close(self, stop_ioloop=True):
        "Closes the client connection."
        self.wants_to_close = True
        self.stream.close()
        if stop_ioloop:
            self.io_loop.stop()
Ejemplo n.º 5
0
class MediatorClient(object):
    "Handles the communication between the builder and mediator."
    def __init__(self, client, serializer, delegate=None, autoreconnect=True):
        self.serializer = serializer
        self.client = client
        self.delegate = delegate
        self.mediator_version = None
        self.stream = None
        self.wants_to_close = False
        self.autoreconnect = autoreconnect

    def write(self, msg, callback=None):
        self.stream.write(msg, callback=None)

    def read(self, callback=None):
        self.stream.read(callback=callback)

    def create(self, io_loop=None):
        self.client.create(self.handshake, self.on_close, io_loop)
        self.io_loop = self.client.io_loop

    def login(self, account, machine, type='builder', callback=None):
        "Logs into a given account, with specific machine credentials."
        cmd = LoginMessage(username=account.username,
                password=account.password_hash,
                machine=machine,
                type=type)
        self.stream.write_and_read(cmd, callback=callback)

    def register(self, account, callback=None):
        "Registers an account with the mediator server."
        cmd = RegisterMessage(account.username, account.password_hash)
        self.stream.write_and_read(cmd, callback=callback)

    def send(self, machine, command, callback=None):
        "sends a given message to a target machine. Expects no response from target."
        if callable(getattr(command, 'to_network', None)):
            command = command.to_network()

        cmd = SendMessage(machine=machine, command=command)
        self.stream.write(cmd, callback=callback)

    def request(self, machine, command, callback=None):
        "Sends a given message to a target machine. Expects a response from target."
        if callable(getattr(command, 'to_network', None)):
            command = command.to_network()

        cmd = RequestMessage(machine=machine, command=command)
        self.stream.write_and_read(cmd, callback=callback)

    def write_response(self, command, callback):
        "Sends a given command back as a response."
        if not callable(getattr(command, 'to_network', None)):
            command = ResponseMessage.create(command)
        self.stream.write(command, callback=callback)
        print "mediator <-", command

    def clients(self, callback=None):
        "Returns all clients connected using the given account."
        cmd = ClientsMessage()
        self.stream.write_and_read(cmd, callback=callback)

    def _accept_handshake(self, callback):
        self.serializer.accept_handshake(self.stream.iostream, callback=callback)

    @gen.engine
    def handshake(self, stream):
        self.stream = MessageStream(stream, self.serializer, self.io_loop)
        self.stream.set_close_callback(self.on_close)
        # read version
        print 'waiting for version'
        response = yield gen.Task(self._accept_handshake)
        self.mediator_version = response['version']
        print "Protocol Version:", self.mediator_version

        if not response['is_supported_version']:
            raise IOError("Unsupported version of server. Update builder? (got: %d; expected: %d)" % (
                self.mediator_version, self.serializer.version,
            ))

        self.delegate.handshake(self)


    def on_close(self):
        if self.wants_to_close:
            return # continue
        if self.autoreconnect:
            if self.client.is_connected:
                print "Connection lost. Reconnecting..."
            self.create()

    def close(self, stop_ioloop=True):
        "Closes the client connection."
        self.wants_to_close = True
        self.stream.close()
        if stop_ioloop:
            self.io_loop.stop()