Beispiel #1
0
 def handle_autodiscover_message(self, fd_no, type):
     data, address = self.udp_socket.recvfrom(1024)
     data = json.loads(data)
     self.udp_socket.sendto(
         json.dumps({
             'endpoint': self.endpoint
         }), address)
Beispiel #2
0
    def notify_event(self, topic, msg):
        """Publish a message on the event publisher channel"""

        json_msg = json.dumps(msg)
        if isinstance(json_msg, unicode):
            json_msg = json_msg.encode('utf8')

        if isinstance(self.res_name, unicode):
            name = self.res_name.encode('utf8')
        else:
            name = self.res_name

        multipart_msg = ["watcher.%s.%s" % (name, topic), json.dumps(msg)]

        if self.evpub_socket is not None and not self.evpub_socket.closed:
            self.evpub_socket.send_multipart(multipart_msg)
Beispiel #3
0
    def send_msg(self, topic, msg):
        """send msg"""

        json_msg = json.dumps(msg)
        if isinstance(json_msg, unicode):
            json_msg = json_msg.encode('utf8')

        if isinstance(self.res_name, unicode):
            name = self.res_name.encode('utf8')
        else:
            name = self.res_name

        multipart_msg = ["watcher.%s.%s" % (name, topic), json.dumps(msg)]

        if not self.evpub_socket.closed:
            self.evpub_socket.send_multipart(multipart_msg)
Beispiel #4
0
    def notify_event(self, topic, msg):
        """Publish a message on the event publisher channel"""

        json_msg = json.dumps(msg)
        if isinstance(json_msg, unicode):
            json_msg = json_msg.encode('utf8')

        if isinstance(self.res_name, unicode):
            name = self.res_name.encode('utf8')
        else:
            name = self.res_name

        multipart_msg = ["watcher.%s.%s" % (name, topic), json.dumps(msg)]

        if self.evpub_socket is not None and not self.evpub_socket.closed:
            self.evpub_socket.send_multipart(multipart_msg)
Beispiel #5
0
    def send_msg(self, topic, msg):
        """send msg"""

        json_msg = json.dumps(msg)
        if isinstance(json_msg, unicode):
            json_msg = json_msg.encode("utf8")

        if isinstance(self.res_name, unicode):
            name = self.res_name.encode("utf8")
        else:
            name = self.res_name

        multipart_msg = ["watcher.%s.%s" % (name, topic), json.dumps(msg)]

        if not self.evpub_socket.closed:
            self.evpub_socket.send_multipart(multipart_msg)
Beispiel #6
0
    def cast(self, command, **props):
        """Fire-and-forget a command to **circusd**

        Options:

        - **command** -- the command to call
        - **props** -- keyword arguments to add to the call
        """
        msg = cast_message(command, **props)
        self.client.send(json.dumps(msg))
Beispiel #7
0
    def cast(self, command, **props):
        """Fire-and-forget a command to **circusd**

        Options:

        - **command** -- the command to call
        - **props** -- keywords argument to add to the call
        """
        msg = cast_message(command, **props)
        self.client.send(json.dumps(msg))
Beispiel #8
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))
Beispiel #9
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))
Beispiel #10
0
    def call(self, command, **props):
        """Sends the command to **circusd**

        Options:

        - **command** -- the command to call
        - **props** -- keyword arguments to add to the call

        Returns the JSON mapping sent back by **circusd**
        """
        msg = make_message(command, **props)
        self.client.send(json.dumps(msg))
        msg = self.client.recv()
        return json.loads(msg)
Beispiel #11
0
    def call(self, command, **props):
        """Sends to **circusd** the command.

        Options:

        - **command** -- the command to call
        - **props** -- keywords argument to add to the call

        Returns the JSON mapping sent back by **circusd**
        """
        msg = make_message(command, **props)
        self.client.send(json.dumps(msg))
        msg = self.client.recv()
        return json.loads(msg)
Beispiel #12
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))
Beispiel #13
0
    def send_response(self, cid,  msg, resp):
        if cid is None:
            return

        if not isinstance(resp, string_types):
            resp = json.dumps(resp)

        if isinstance(resp, unicode):
            resp = resp.encode('utf8')

        try:
            self.stream.send(cid, zmq.SNDMORE)
            self.stream.send(resp)
        except zmq.ZMQError as e:
            logger.debug("Received %r - Could not send back %r - %s", msg,
                         resp, str(e))
Beispiel #14
0
    def send_response(self, cid, msg, resp, cast=False):
        if cast:
            return

        if cid is None:
            return

        if not isinstance(resp, string_types):
            resp = json.dumps(resp)

        if isinstance(resp, unicode):
            resp = resp.encode('utf8')

        try:
            self.stream.send(cid, zmq.SNDMORE)
            self.stream.send(resp)
        except zmq.ZMQError as e:
            logger.debug("Received %r - Could not send back %r - %s", msg,
                         resp, str(e))
Beispiel #15
0
    def log(self, entry):
        """Log an entry.

        Will check if the handler contains filters and apply them if needed
        (so that if multiple handlers have the same filters, they are only
        applied once). Likewise, it will check if the log entry needs to be
        serialized to JSON data, and make sure that the data is only
        serialized once for each set of filters.

        Parameters
        ----------
        entry : dict
            A log entry is a (JSON-compatible) dict.

        """
        # Store entry for retrieval
        self._entries.append(entry)

        # For each set of filters, we store the JSON serialized entry
        filtered_entries = {}
        serialized_entries = {}
        for handler in self.handlers:
            filters = frozenset(handler.filters)

            # If the content needs to be filtered, do so
            if filters:
                if filters not in filtered_entries:
                    filtered_entries[filters] = handler.filter(entry)
                filtered_entry = filtered_entries[filters]
            else:
                filtered_entry = entry

            # If the handler wants JSON data, give it
            if handler.JSON:
                if filters not in serialized_entries:
                    serialized_entries[filters] = json.dumps(
                        filtered_entry, **self.json_kwargs)
                serialized_entry = serialized_entries[filters]
                handler.log(serialized_entry)
            else:
                handler.log(filtered_entry)
Beispiel #16
0
    def log(self, entry):
        """Log an entry.

        Will check if the handler contains filters and apply them if needed
        (so that if multiple handlers have the same filters, they are only
        applied once). Likewise, it will check if the log entry needs to be
        serialized to JSON data, and make sure that the data is only
        serialized once for each set of filters.

        Parameters
        ----------
        entry : dict
            A log entry is a (JSON-compatible) dict.

        """
        # Store entry for retrieval
        self._entries.append(entry)

        # For each set of filters, we store the JSON serialized entry
        filtered_entries = {}
        serialized_entries = {}
        for handler in self.handlers:
            filters = frozenset(handler.filters)

            # If the content needs to be filtered, do so
            if filters:
                if filters not in filtered_entries:
                    filtered_entries[filters] = handler.filter(entry)
                filtered_entry = filtered_entries[filters]
            else:
                filtered_entry = entry

            # If the handler wants JSON data, give it
            if handler.JSON:
                if filters not in serialized_entries:
                    serialized_entries[filters] = json.dumps(
                        filtered_entry, **self.json_kwargs)
                serialized_entry = serialized_entries[filters]
                handler.log(serialized_entry)
            else:
                handler.log(filtered_entry)
Beispiel #17
0
    def send_response(self, mid, cid, msg, resp, cast=False):
        if cast:
            return

        if cid is None:
            return

        if isinstance(resp, string_types):
            raise DeprecationWarning('Takes only a mapping')

        resp['id'] = mid
        resp = json.dumps(resp)

        if isinstance(resp, unicode):
            resp = resp.encode('utf8')

        try:
            self.stream.send(cid, zmq.SNDMORE)
            self.stream.send(resp)
        except zmq.ZMQError as e:
            logger.debug("Received %r - Could not send back %r - %s", msg,
                         resp, str(e))
Beispiel #18
0
    def send_response(self, mid, cid, msg, resp, cast=False):
        if cast:
            return

        if cid is None:
            return

        if isinstance(resp, string_types):
            raise DeprecationWarning('Takes only a mapping')

        resp['id'] = mid
        resp = json.dumps(resp)

        if isinstance(resp, unicode):
            resp = resp.encode('utf8')

        try:
            self.stream.send(cid, zmq.SNDMORE)
            self.stream.send(resp)
        except zmq.ZMQError as e:
            logger.debug("Received %r - Could not send back %r - %s", msg,
                         resp, str(e))
Beispiel #19
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(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]))

            stream.on_recv(recv_callback)

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

        if not callback:
            return json.loads(socket.recv())
Beispiel #20
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'))
Beispiel #21
0
 def cast(self, command, **props):
     msg = cast_message(command, **props)
     self.client.send(json.dumps(msg))
Beispiel #22
0
 def handle_autodiscover_message(self, fd_no, type):
     data, address = self.udp_socket.recvfrom(1024)
     data = json.loads(data)
     self.udp_socket.sendto(json.dumps({'endpoint': self.endpoint}),
                            address)
Beispiel #23
0
def make_json(command, **props):
    return json.dumps(make_message(command, **props))
Beispiel #24
0
 def cast(self, cmd):
     self.client.send(json.dumps(cmd))
Beispiel #25
0
 def call(self, command, **props):
     msg = make_message(command, **props)
     self.client.send(json.dumps(msg))
     msg = self.client.recv()
     return json.loads(msg)
Beispiel #26
0
 def cast(self, command, **props):
     msg = cast_message(command, **props)
     self.client.send(json.dumps(msg))
Beispiel #27
0
 def call(self, cmd):
     self.client.send(json.dumps(cmd))
     msg = self.client.recv()
     return json.loads(msg)
Beispiel #28
0
def make_json(command, **props):
    return json.dumps(make_message(command, **props))
Beispiel #29
0
 def call(self, command, **props):
     msg = make_message(command, **props)
     self.client.send(json.dumps(msg))
     msg = self.client.recv()
     return json.loads(msg)