예제 #1
0
    def handle_request(self, topic, message):
        if topic == self.subtopic:
            # event from proxy received
            try:
                data = etree.fromstring(message, PluginRegistry.getEventParser())
                event_type = stripNs(data.xpath('/g:Event/*', namespaces={'g': "http://www.gonicus.de/Events"})[0].tag)
                if event_type == "ClientLeave":
                    proxy_id = str(data.ClientLeave.Id)
                    registry = PluginRegistry.getInstance("BackendRegistry")
                    registry.unregisterBackend(proxy_id)

            except etree.XMLSyntaxError as e:
                self.log.error("Event parsing error: %s" % e)

        elif topic.startswith(self.subtopic):
            response_topic = "%s/response" % "/".join(topic.split("/")[0:4])

            try:
                id_, res = self.process(topic, message)
                if is_future(res):
                    res = yield res
                response = dumps({"result": res, "id": id_})
                self.log.debug("MQTT-RPC response: %s on topic %s" % (response, topic))

            except Exception as e:
                err = str(e)
                self.log.error("MQTT RPC call error: %s" % err)
                response = dumps({'id': topic.split("/")[-2], 'error': err})

            # Get rid of it...
            self.mqtt.send_message(response, topic=response_topic, qos=2)

        else:
            self.log.warning("unhandled topic request received: %s" % topic)
예제 #2
0
파일: main.py 프로젝트: GOsa3/gosa
 def __traverse_node(cls, node):
     res = {}
     for n in node:
         for child in n.iterchildren():
             tag = stripNs(child.tag)
             val = child.text if child.countchildren(
             ) == 0 else cls.__traverse_node(child)
             if tag in res:
                 if isinstance(res[tag], list):
                     res[tag].append(val)
             else:
                 res[tag] = val
     return res
예제 #3
0
파일: main.py 프로젝트: GOsa3/gosa
    def notify(cls, xml, channel='broadcast'):
        eventType = stripNs(
            xml.xpath('/g:Event/*',
                      namespaces={'g': "http://www.gonicus.de/Events"})[0].tag)
        func = getattr(cls, "_handle" +
                       eventType) if hasattr(cls, "_handle" +
                                             eventType) else None
        if func is not None:
            func(xml, channel)
        else:
            # default handling
            root = getattr(xml, eventType)
            message = cls.__traverse_node(root)

            SseHandler.send_message(message, topic=eventType, channel=channel)
예제 #4
0
 def __eventProcessor(self, topic, message):
     if message[0:1] == "{":
         # RPC response
         self.log.debug("RPC response received in channel %s: '%s'" % (topic, message))
     else:
         try:
             data = etree.fromstring(message, PluginRegistry.getEventParser())
             eventType = stripNs(data.xpath('/g:Event/*', namespaces={'g': "http://www.gonicus.de/Events"})[0].tag)
             self.log.debug("'%s' event received from local MQTT broker" % eventType)
             if hasattr(self, "_handle"+eventType):
                 func = getattr(self, "_handle" + eventType)
                 func(data)
             else:
                 self.log.debug("unhandled event %s" % eventType)
         except etree.XMLSyntaxError as e:
             self.log.error("XML parse error %s on message %s" % (e, message))
예제 #5
0
 def __eventProcessor(self, topic, message):
     if message[0:1] == "{":
         # RPC response
         self.log.debug("RPC response received in channel %s: '%s'" % (topic, message))
     else:
         try:
             data = etree.fromstring(message, PluginRegistry.getEventParser())
             eventType = stripNs(data.xpath('/g:Event/*', namespaces={'g': "http://www.gonicus.de/Events"})[0].tag)
             self.log.debug("Incoming MQTT event[%s]: '%s'" % (eventType, data))
             if hasattr(self, "_handle"+eventType):
                 func = getattr(self, "_handle" + eventType)
                 func(data)
             else:
                 self.log.debug("unhandled event %s" % eventType)
         except etree.XMLSyntaxError as e:
             self.log.error("XML parse error %s on message %s" % (e, message))
예제 #6
0
    def handle_request(self, topic, message):
        if topic == self.subtopic:
            # event from proxy received
            try:
                data = etree.fromstring(message,
                                        PluginRegistry.getEventParser())
                event_type = stripNs(
                    data.xpath(
                        '/g:Event/*',
                        namespaces={'g':
                                    "http://www.gonicus.de/Events"})[0].tag)
                if event_type == "ClientLeave":
                    proxy_id = str(data.ClientLeave.Id)
                    registry = PluginRegistry.getInstance("BackendRegistry")
                    registry.unregisterBackend(proxy_id)

            except etree.XMLSyntaxError as e:
                self.log.error("Event parsing error: %s" % e)

        elif topic.startswith(self.subtopic):
            response_topic = "%s/response" % "/".join(topic.split("/")[0:4])

            try:
                id_, res = self.process(topic, message)
                response = dumps({"result": res, "id": id_})

            except Exception as e:
                err = str(e)
                self.log.error("MQTT RPC call error: %s" % err)
                response = dumps({'id': topic.split("/")[-2], 'error': err})

            # Get rid of it...
            self.mqtt.send_message(response, topic=response_topic)

        else:
            self.log.warning("unhandled topic request received: %s" % topic)
예제 #7
0
 def test_ping(self):
     # just wait a second and test if the first ping has been sent
     time.sleep(1)
     args, kwargs = mocked_handler.send_event.call_args
     assert stripNs(args[0].xpath('/g:Event/*', namespaces={'g': "http://www.gonicus.de/Events"})[0].tag) == "ClientPing"
예제 #8
0
 def test_reAnnounce(self):
     self.mqtt.reAnnounce()
     print(mocked_handler.send_event.call_args)
     args, kwargs = mocked_handler.send_event.call_args
     assert stripNs(args[0].xpath('/g:Event/*', namespaces={'g': "http://www.gonicus.de/Events"})[0].tag) == "UserSession"
예제 #9
0
파일: events.py 프로젝트: peuter/gosa
 def _get_type(self, event):
     return stripNs(event.xpath('/g:Event/*', namespaces={'g': "http://www.gonicus.de/Events"})[0].tag)
예제 #10
0
 def __eventProcessor(self, data):
     eventType = stripNs(data.xpath('/g:Event/*', namespaces={'g': "http://www.gonicus.de/Events"})[0].tag)
     func = getattr(self, "_handle" + eventType)
     func(data)
예제 #11
0
파일: main.py 프로젝트: peuter/gosa
 def notify(cls, xml, channel='broadcast'):
     eventType = stripNs(xml.xpath('/g:Event/*', namespaces={'g': "http://www.gonicus.de/Events"})[0].tag)
     func = getattr(cls, "_handle" + eventType)
     func(xml, channel)
예제 #12
0
 def notify(cls, xml, channel='broadcast'):
     eventType = stripNs(
         xml.xpath('/g:Event/*',
                   namespaces={'g': "http://www.gonicus.de/Events"})[0].tag)
     func = getattr(cls, "_handle" + eventType)
     func(xml, channel)