Beispiel #1
0
 def __init__(self, earth, stop):
     """
     Initialise the ZMQ interface.
     """
     self._earth = earth
     self.stop = stop
     self._zmq_reply = MessagingRep()
     self._zmq_publish = MessagingEventPub('plugin_earth')
Beispiel #2
0
 def __init__(self, earth, stop):
     """
     Initialise the ZMQ interface.
     """
     self._earth = earth
     self.stop = stop
     self._zmq_reply = MessagingRep()
     self._zmq_publish = MessagingEventPub('plugin_earth')
Beispiel #3
0
class EarthZmq():
    """
    Interface to the ZMQ
    """
    plugin_name = "earth"
    category = "plugin.earth"

    def __init__(self, earth, stop):
        """
        Initialise the ZMQ interface.
        """
        self._earth = earth
        self.stop = stop
        self._zmq_reply = MessagingRep()
        self._zmq_publish = MessagingEventPub('plugin_earth')

    def __del__(self, earth, stop):
        """
        Delete zmq objects
        """
        del (self._zmq_reply)
        del (self._zmq_publish)

    def close(self):
        """
        Close the mq

        """
        self._zmq_reply.s_rep.close()
        self._zmq_publish.s_send.close()

    def get_reply_action(self, reqid):
        """
        Retrieve action from the id of request.

        """
        idxx = reqid.find(self.category)
        if idxx == -1:
            idxx = reqid.find("plugin.ping")
            if idxx == -1:
                return None
            else:
                return "ping"
        else:
            offset = len(self.category)
            temp = reqid[idxx + offset + 1:]
            path = temp.split(".")
            res = path[0]
            if (path[0] == "admin"):
                res = path[0] + "." + path[1]
            return res

    def get_list_replies(self):
        """
        Retrieve list of replies

        """
        return [
            'gateway', 'memory', 'admin.set', 'admin.get', 'admin.list',
            'admin.info', 'admin.status', 'admin.start', 'admin.stop',
            'admin.resume', 'admin.halt', 'check', 'ping'
        ]

    def get_list_pubs(self):
        """
        Retrieve list of pubs

        """
        return ['admin.list', 'admin.event', 'admin.status', 'enabled']

    def _publish_list(self, action, rdict):
        """
        Publish the list using channel list

        """
        path = "%s.%s" % (self.category, "admin.list")
        r_content = dict()
        r_content["action"] = action
        r_content["data"] = rdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_list : send list %s" %
                              j_content)
        print("Message sent : %s - %s" % (path, j_content))

    def _get_id(self, event, delay):
        """
        Return the id to use

        """
        return "%s.%s" % (event, delay)

    def publish_plugin_enabled(self, status):
        """
        Publish that the plugin is enabled

        """
        r_content = dict()
        r_content["status"] = status
        r_content["plugin"] = self.plugin_name
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        path = "%s.%s" % (self.category, "enabled")
        self._zmq_publish.send_event(path, j_content)
        path = "%s.%s" % ("plugin", "enabled")
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_plugin_enabled : data %s" %
                              j_content)
        print("Message sent : %s - %s" % (path, j_content))

    def publish_start(self, event, delay, eventdict, listdict):
        """
        An event wav created

        """
        path = "%s.%s" % (self.category, "admin.event")
        r_content = dict()
        r_content["action"] = "added"
        r_content["id"] = self._get_id(event, delay)
        r_content["data"] = eventdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_start : send list %s" %
                              j_content)
        print("Message sent : %s - %s" % (path, j_content))
        self._publish_list("event-added", eventdict)

    def publish_halt(self, event, delay, eventdict, listdict):
        """
        An event wav halted

        """
        path = "%s.%s" % (self.category, "admin.event")
        r_content = dict()
        r_content["action"] = "removed"
        r_content["id"] = self._get_id(event, delay)
        r_content["data"] = eventdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_halt : data %s" % j_content)
        print("Message sent : %s - %s" % (path, j_content))
        self._publish_list("event-removed", eventdict)

    def publish_stop(self, event, delay, eventdict):
        """
        An event was stopped

        """
        path = "%s.%s" % (self.category, "admin.event")
        r_content = dict()
        r_content["action"] = "stopped"
        r_content["id"] = self._get_id(event, delay)
        r_content["data"] = eventdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_stop : data %s" % j_content)
        print("Message sent : %s - %s" % (path, j_content))

    def publish_resume(self, event, delay, eventdict):
        """
       An event wav resumed

        """
        path = "%s.%s" % (self.category, "admin.event")
        r_content = dict()
        r_content["action"] = "resumed"
        r_content["id"] = self._get_id(event, delay)
        r_content["data"] = eventdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_resume : data %s" % j_content)
        print("Message sent : %s - %s" % (path, j_content))

    def publish_status(self, status, rdict):
        """
        A status is updated
        """
        path = "%s.%s" % (self.category, "event.status")
        r_content = dict()
        r_content["action"] = "status"
        r_content["id"] = status
        r_content["data"] = rdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_status : data %s" % j_content)
        print("Message sent : %s - %s" % (path, j_content))

    def reply(self):
        """
        Wait for ZMQ request and send reply.

        """
        self._earth.log.debug("EarthZmq.reply : Listen for messages ...")
        while not self.stop.isSet():
            self._earth.log.debug("EarthZmq.reply : Waiting for request...")
            try:
                j_request = self._zmq_reply.wait_for_request()
                self._earth.log.debug("EarthZmq.reply : Received request %s" %
                                      j_request)
                request = json.loads(j_request)
                action = self.get_reply_action(request['id'])
                repl = dict()
                print("YYYYYYYYYYYYYYMQ : Incoming request")
                if action == "check":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request check")
                    repl["check"] = "ok"
                elif action == "ping":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request ping")
                    repl["plugin"] = self.plugin_name
                elif action == "gateway":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request gateway")
                    repl = self._earth.gateway()
                elif action == "memory":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request memory")
                    repl = self._earth.memory()
                elif action == "admin.start":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request start")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else:
                        data = {}
                        if "args" in request['content']:
                            data["args"] = request['content']['args']
                        data["current"] = "started"
                        self._earth.events.add_event(etype, edelay, data)
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.stop":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request stop")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else:
                        self._earth.events.stop_event(etype, edelay)
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.resume":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request resume")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else:
                        self._earth.events.resume_event(etype, edelay)
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.halt":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request halt")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else:
                        self._earth.events.halt_event(etype, edelay)
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.info":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request info")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else:
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.simulate":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request simulate")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else:
                        repl["error"] = "Not implemented"
                elif action == "admin.set":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request set")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else:
                        repl["error"] = "Not implemented"
                elif action == "admin.get":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request get")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else:
                        repl["error"] = "Not implemented"
                elif action == "admin.status":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request status")
                    equery = None
                    if 'query' in request['content']:
                        equery = request['content']['query']
                    repl = self._earth.event_status(equery)
                elif action == "admin.list":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request list")
                    repl = self._earth.event_list()
                else:
                    repl["error"] = "action %s not found" % action
            except:
                repl["error"] = "exception %s" % traceback.format_exc()
            finally:
                self._zmq_reply.send_reply(repl)
Beispiel #4
0
class EarthZmq():
    """
    Interface to the ZMQ
    """
    plugin_name = "earth"
    category = "plugin.earth"

    def __init__(self, earth, stop):
        """
        Initialise the ZMQ interface.
        """
        self._earth = earth
        self.stop = stop
        self._zmq_reply = MessagingRep()
        self._zmq_publish = MessagingEventPub('plugin_earth')

    def __del__(self, earth, stop):
        """
        Delete zmq objects
        """
        del(self._zmq_reply)
        del(self._zmq_publish)

    def close(self):
        """
        Close the mq

        """
        self._zmq_reply.s_rep.close()
        self._zmq_publish.s_send.close()

    def get_reply_action(self, reqid):
        """
        Retrieve action from the id of request.

        """
        idxx = reqid.find(self.category)
        if idxx == -1:
            idxx = reqid.find("plugin.ping")
            if idxx == -1:
                return None
            else:
                return "ping"
        else :
            offset = len(self.category)
            temp = reqid[idxx+offset+1:]
            path = temp.split(".")
            res = path[0]
            if (path[0] == "admin") :
                res = path[0] + "." + path[1]
            return res

    def get_list_replies(self):
        """
        Retrieve list of replies

        """
        return ['gateway', 'memory', 'admin.set', 'admin.get', 'admin.list', 'admin.info',
            'admin.status', 'admin.start', 'admin.stop', 'admin.resume', 'admin.halt',
            'check', 'ping']

    def get_list_pubs(self):
        """
        Retrieve list of pubs

        """
        return ['admin.list', 'admin.event', 'admin.status', 'enabled']

    def _publish_list(self, action, rdict):
        """
        Publish the list using channel list

        """
        path = "%s.%s" % (self.category, "admin.list")
        r_content = dict()
        r_content["action"] = action
        r_content["data"] = rdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_list : send list %s" % j_content)
        print("Message sent : %s - %s"  % (path, j_content))

    def _get_id(self, event, delay):
        """
        Return the id to use

        """
        return "%s.%s" % (event, delay)

    def publish_plugin_enabled(self,status):
        """
        Publish that the plugin is enabled

        """
        r_content = dict()
        r_content["status"] = status
        r_content["plugin"] = self.plugin_name
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        path = "%s.%s" % (self.category, "enabled")
        self._zmq_publish.send_event(path, j_content)
        path = "%s.%s" % ("plugin", "enabled")
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_plugin_enabled : data %s" % j_content)
        print("Message sent : %s - %s"  % (path, j_content))

    def publish_start(self, event, delay, eventdict, listdict):
        """
        An event wav created

        """
        path = "%s.%s" % (self.category, "admin.event")
        r_content = dict()
        r_content["action"] = "added"
        r_content["id"] = self._get_id(event, delay)
        r_content["data"] = eventdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_start : send list %s" % j_content)
        print("Message sent : %s - %s"  % (path, j_content))
        self._publish_list("event-added",eventdict)

    def publish_halt(self, event, delay, eventdict, listdict):
        """
        An event wav halted

        """
        path = "%s.%s" % (self.category, "admin.event")
        r_content = dict()
        r_content["action"] = "removed"
        r_content["id"] = self._get_id(event, delay)
        r_content["data"] = eventdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_halt : data %s" % j_content)
        print("Message sent : %s - %s"  % (path, j_content))
        self._publish_list("event-removed",eventdict)

    def publish_stop(self, event, delay, eventdict):
        """
        An event was stopped

        """
        path = "%s.%s" % (self.category, "admin.event")
        r_content = dict()
        r_content["action"] = "stopped"
        r_content["id"] = self._get_id(event, delay)
        r_content["data"] = eventdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_stop : data %s" % j_content)
        print("Message sent : %s - %s"  % (path, j_content))

    def publish_resume(self, event, delay, eventdict):
        """
       An event wav resumed

        """
        path = "%s.%s" % (self.category, "admin.event")
        r_content = dict()
        r_content["action"] = "resumed"
        r_content["id"] = self._get_id(event, delay)
        r_content["data"] = eventdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_resume : data %s" % j_content)
        print("Message sent : %s - %s"  % (path, j_content))

    def publish_status(self, status, rdict):
        """
        A status is updated
        """
        path = "%s.%s" % (self.category, "event.status")
        r_content = dict()
        r_content["action"] = "status"
        r_content["id"] = status
        r_content["data"] = rdict
        j_content = json.dumps(r_content, cls=PythonObjectEncoder)
        self._zmq_publish.send_event(path, j_content)
        self._earth.log.debug("EarthZmq.publish_status : data %s" % j_content)
        print("Message sent : %s - %s"  % (path, j_content))

    def reply(self):
        """
        Wait for ZMQ request and send reply.

        """
        self._earth.log.debug("EarthZmq.reply : Listen for messages ...")
        while not self.stop.isSet():
            self._earth.log.debug("EarthZmq.reply : Waiting for request...")
            try:
                j_request = self._zmq_reply.wait_for_request()
                self._earth.log.debug("EarthZmq.reply : Received request %s" % j_request)
                request = json.loads(j_request)
                action = self.get_reply_action(request['id'])
                repl = dict()
                print("YYYYYYYYYYYYYYMQ : Incoming request")
                if action == "check":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request check")
                    repl["check"] = "ok"
                elif action == "ping":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request ping")
                    repl["plugin"] = self.plugin_name
                elif action == "gateway":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request gateway")
                    repl = self._earth.gateway()
                elif action == "memory":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request memory")
                    repl = self._earth.memory()
                elif action == "admin.start":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request start")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None :
                        repl["error"] = "no event type found in message."
                    else :
                        data = {}
                        if "args" in  request['content'] :
                            data["args"] = request['content']['args']
                        data["current"] = "started"
                        self._earth.events.add_event(etype, edelay, data)
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.stop":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request stop")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None :
                        repl["error"] = "no event type found in message."
                    else :
                        self._earth.events.stop_event(etype, edelay)
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.resume":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request resume")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None :
                        repl["error"] = "no event type found in message."
                    else :
                        self._earth.events.resume_event(etype, edelay)
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.halt":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request halt")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None :
                        repl["error"] = "no event type found in message."
                    else :
                        self._earth.events.halt_event(etype, edelay)
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.info":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request info")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None :
                        repl["error"] = "no event type found in message."
                    else :
                        repl = self._earth.event_info(etype, edelay)
                elif action == "admin.simulate":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request simulate")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None :
                        repl["error"] = "no event type found in message."
                    else :
                        repl["error"] = "Not implemented"
                elif action == "admin.set":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request set")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None:
                        repl["error"] = "no event type found in message."
                    else :
                        repl["error"] = "Not implemented"
                elif action == "admin.get":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request get")
                    etype = None
                    edelay = "0"
                    if 'type' in request['content']:
                        etype = request['content']['type']
                    if 'delay' in request['content']:
                        edelay = request['content']['delay']
                    if etype == None :
                        repl["error"] = "no event type found in message."
                    else :
                        repl["error"] = "Not implemented"
                elif action == "admin.status":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request status")
                    equery = None
                    if 'query' in request['content']:
                        equery = request['content']['query']
                    repl = self._earth.event_status(equery)
                elif action == "admin.list":
                    print("ZZZZZZZZZZZZZZZZMQ : Processing request list")
                    repl = self._earth.event_list()
                else:
                    repl["error"] = "action %s not found" % action
            except :
                repl["error"] = "exception %s" % traceback.format_exc()
            finally:
                self._zmq_reply.send_reply(repl)