Beispiel #1
0
def link_format_from_message(message):
    try:
        if message.opt.content_format == media_types_rev['application/link-format']:
            return parse(message.payload.decode('utf8'))
        elif message.opt.content_format == media_types_rev['application/link-format+json']:
            return LinkFormat.from_json_string(message.payload.decode('utf8'))
        elif message.opt.content_format == media_types_rev['application/link-format+cbor']:
            return LinkFormat.from_cbor_bytes(message.payload)
        else:
            raise error.UnsupportedMediaType()
    except (UnicodeDecodeError, link_header.ParseException):
        raise error.BadRequest()
Beispiel #2
0
def link_format_from_message(message):
    """Convert a response message into a LinkFormat object

    This expects an explicit media type set on the response (or was explicitly requested)
    """
    certain_format = message.opt.content_format
    if certain_format is None:
        certain_format = message.request.opt.accept
    try:
        if certain_format == ContentFormat.LINKFORMAT:
            return parse(message.payload.decode('utf8'))
        else:
            raise error.UnsupportedMediaType()
    except (UnicodeDecodeError, link_header.ParseException):
        raise error.BadRequest()
Beispiel #3
0
def link_format_from_message(message):
    """Convert a response message into a LinkFormat object

    This expects an explicit media type set on the response (or was explicitly requested)
    """
    certain_format = message.opt.content_format
    if certain_format is None:
        certain_format = message.request.opt.accept
    try:
        if certain_format == media_types_rev['application/link-format']:
            return parse(message.payload.decode('utf8'))
        elif certain_format == media_types_rev['application/link-format+json']:
            return LinkFormat.from_json_string(message.payload.decode('utf8'))
        elif certain_format == media_types_rev['application/link-format+cbor']:
            return LinkFormat.from_cbor_bytes(message.payload)
        else:
            raise error.UnsupportedMediaType()
    except (UnicodeDecodeError, link_header.ParseException):
        raise error.BadRequest()
    def incoming_observation(self, response):
        links = {}
        nodes = {}

        logging.info("Received response {} for {}".format(
            response, self.resource))
        if response.code.is_successful():
            cf = response.opt.content_format
            mime_type = media_types.get(cf, "type %s" % cf)
            mime_type, *parameters = mime_type.split(";")
            logging.info("Content format: {}".format(mime_type))
            if mime_type == "application/link-format":
                ls = linkformat.parse(response.payload.decode('utf-8'))
                for link in ls.links:
                    link = link.as_json_data()
                    pr = urllib.parse.urlparse(link["href"])
                    if pr.path.endswith("reboot"):
                        reboot_resources.add(link["href"])
                    link["addr"] = pr.netloc
                    link["path"] = pr.path
                    links[link["href"]] = link
        else:
            logging.error("Error: {}: {}".format(response, response.payload))
            return
        if self.websocket is not None and not self.websocket.is_closed():
            self.websocket.write_message(response.payload)
        elif self.websocket is None:
            for link in links.values():
                if link["addr"].startswith("[fe80::"):
                    logging.error("address {} link-local".format(link["addr"]))
                    continue
                path = link["path"]
                node, *rest = path.strip("/").split("/")
                if (len(rest) > 0) and (node not in nodes):
                    nodes[node] = {rest[0]: link}
                elif (len(rest) > 0):
                    nodes[node][rest[0]] = link
            if "btn" in nodes and "target" in nodes["btn"] and \
               "dsp" in nodes and "points" in nodes["dsp"]:
                asyncio.ensure_future(register(nodes["btn"], nodes["dsp"]))
            if "dsp" in nodes and "target" in nodes["dsp"] and \
               "dino" in nodes and "points" in nodes["dino"]:
                asyncio.ensure_future(register(nodes["dsp"], nodes["dino"]))