Exemple #1
0
    def call(self, cmd):
        if isinstance(cmd, string_types):
            raise DeprecationWarning('call() takes a mapping')

        call_id = uuid.uuid4().hex
        cmd['id'] = call_id
        try:
            cmd = json.dumps(cmd)
        except ValueError as e:
            raise CallError(str(e))

        try:
            yield tornado.gen.Task(self.stream.send, cmd)
        except zmq.ZMQError as e:
            raise CallError(str(e))

        while True:
            messages = yield tornado.gen.Task(self.stream.on_recv)
            for message in messages:
                try:
                    res = json.loads(message)
                    if res.get('id') != call_id:
                        # we got the wrong message
                        continue
                    raise tornado.gen.Return(res)
                except ValueError as e:
                    raise CallError(str(e))
Exemple #2
0
class CircusClient(object):
    def __init__(self,
                 context=None,
                 endpoint=DEFAULT_ENDPOINT_DEALER,
                 timeout=5.0,
                 ssh_server=None,
                 ssh_keyfile=None):
        self.context = context or zmq.Context.instance()
        self.endpoint = endpoint
        self._id = uuid.uuid4().hex
        self.socket = self.context.socket(zmq.DEALER)
        self.socket.setsockopt(zmq.IDENTITY, self._id)
        self.socket.setsockopt(zmq.LINGER, 0)
        get_connection(self.socket, endpoint, ssh_server, ssh_keyfile)
        self.poller = zmq.Poller()
        self.poller.register(self.socket, zmq.POLLIN)
        self._timeout = timeout
        self.timeout = timeout * 1000

    def stop(self):
        self.socket.close()

    def send_message(self, command, **props):
        return self.call(make_message(command, **props))

    def call(self, cmd):
        if not isinstance(cmd, string_types):
            try:
                cmd = json.dumps(cmd)
            except ValueError as e:
                raise CallError(str(e))

        try:
            self.socket.send(cmd)
        except zmq.ZMQError, e:
            raise CallError(str(e))

        while True:
            try:
                events = dict(self.poller.poll(self.timeout))
            except zmq.ZMQError as e:
                if e.errno == errno.EINTR:
                    continue
                else:
                    raise CallError(str(e))
            else:
                break

        if len(events) == 0:
            raise CallError("Timed out.")

        for socket in events:
            msg = socket.recv()
            try:
                return json.loads(msg)
            except ValueError as e:
                raise CallError(str(e))
Exemple #3
0
    def call(self, cmd):
        if not isinstance(cmd, string_types):
            try:
                cmd = json.dumps(cmd)
            except ValueError as e:
                raise CallError(str(e))

        try:
            self.socket.send(cmd)
        except zmq.ZMQError, e:
            raise CallError(str(e))
Exemple #4
0
class CircusClient(object):
    def __init__(self, context=None, endpoint='tcp://127.0.0.1:5555',
                 timeout=5.0):
        self.context = context or zmq.Context.instance()
        self.endpoint = endpoint
        self._id = uuid.uuid4().hex
        self.socket = self.context.socket(zmq.DEALER)
        self.socket.setsockopt(zmq.IDENTITY, self._id)
        self.socket.setsockopt(zmq.LINGER, 0)
        self.socket.connect(endpoint)
        self.poller = zmq.Poller()
        self.poller.register(self.socket, zmq.POLLIN)
        self.timeout = timeout * 1000

    def stop(self):
        self.socket.close()

    def call(self, cmd):
        if not isinstance(cmd, string_types):
            try:
                cmd = json.dumps(cmd)
            except ValueError as e:
                raise CallError(str(e))

        try:
            self.socket.send(cmd)
        except zmq.ZMQError, e:
            raise CallError(str(e))

        while True:
            try:
                events = dict(self.poller.poll(self.timeout))
            except zmq.ZMQError as e:
                if e.errno == errno.EINTR:
                    continue
                else:
                    raise CallError(str(e))
            else:
                break

        if len(events) == 0:
            raise CallError("Timed out")

        for socket in events:
            msg = socket.recv()
            try:
                return json.loads(msg)
            except ValueError as e:
                raise CallError(str(e))
Exemple #5
0
    def call(self, cmd):
        if isinstance(cmd, string_types):
            raise DeprecationWarning('call() takes a mapping')

        call_id = uuid.uuid4().hex
        cmd['id'] = call_id
        try:
            cmd = json.dumps(cmd)
        except ValueError as e:
            raise CallError(str(e))

        try:
            self.socket.send(cmd)
        except zmq.ZMQError, e:
            raise CallError(str(e))
Exemple #6
0
    def call(self, cmd):
        if isinstance(cmd, string_types):
            raise DeprecationWarning('call() takes a mapping')

        call_id = uuid.uuid4().hex
        cmd['id'] = call_id
        try:
            cmd = json.dumps(cmd)
        except ValueError as e:
            raise CallError(str(e))

        try:
            self.socket.send(cmd)
        except zmq.ZMQError as e:
            raise CallError(str(e))

        while True:
            try:
                events = dict(self.poller.poll(self.timeout))
            except zmq.ZMQError as e:
                if e.errno == errno.EINTR:
                    continue
                else:
                    print(str(e))
                    raise CallError(str(e))

            if len(events) == 0:
                raise CallError("Timed out.")

            for socket in events:
                msg = socket.recv()
                try:
                    res = json.loads(msg)
                    if res.get('id') != call_id:
                        # we got the wrong message
                        continue
                    return res
                except ValueError as e:
                    raise CallError(str(e))
Exemple #7
0
    def call(self, cmd, callback):
        if not isinstance(cmd, string_types):
            try:
                cmd = json.dumps(cmd)
            except ValueError as e:
                raise CallError(str(e))

        socket = self.context.socket(zmq.DEALER)
        socket.setsockopt_string(zmq.IDENTITY, uuid.uuid4().hex)
        socket.setsockopt(zmq.LINGER, 0)
        get_connection(socket, self.endpoint, self.ssh_server,
                       self.ssh_keyfile)

        if callback:
            stream = ZMQStream(socket, self.loop)

            def timeout_callback():
                stream.stop_on_recv()
                stream.close()
                raise CallError('Call timeout for cmd', cmd)

            timeout = self.loop.add_timeout(timedelta(seconds=5),
                                            timeout_callback)

            def recv_callback(msg):
                self.loop.remove_timeout(timeout)
                stream.stop_on_recv()
                stream.close()
                callback(json.loads(msg[0].decode('utf-8')))

            stream.on_recv(recv_callback)

        try:
            socket.send_string(cmd)
        except zmq.ZMQError as e:
            raise CallError(str(e))

        if not callback:
            return json.loads(socket.recv().decode('utf-8'))
Exemple #8
0
    def call(self, cmd):
        if isinstance(cmd, str):
            raise DeprecationWarning('call() takes a mapping')

        call_id = uuid.uuid4().hex
        cmd['id'] = call_id
        try:
            cmd = json.dumps(cmd)
        except ValueError as e:
            raise CallError(str(e))

        try:
            future = concurrent.Future()

            def cb(msg, status):
                future.set_result(msg)

            self.stream.send(cmd, callback=cb)
            yield future
        except zmq.ZMQError as e:
            raise CallError(str(e))

        while True:
            future = concurrent.Future()
            self.stream.on_recv(future.set_result)
            messages = yield future

            for message in messages:
                try:
                    res = json.loads(message)
                    if res.get('id') != call_id:
                        # we got the wrong message
                        continue
                    raise tornado.gen.Return(res)
                except ValueError as e:
                    raise CallError(str(e))
Exemple #9
0
 def timeout_callback():
     stream.stop_on_recv()
     stream.close()
     raise CallError('Call timeout for cmd', cmd)