Tanganelli / CoAPthon3

CoAPthon3 is a porting to python3 of my CoAPthon library. CoAPthon3 is a python3 library to the CoAP protocol compliant with the RFC. Branch is available for the Twisted framework.
MIT License
59 stars 50 forks source link

Problem with nested endpoints #11

Closed hahahannes closed 5 years ago

hahahannes commented 5 years ago

I am encountering a problem with nested endpoints when the first part of the path is registered as an own resource, like in Case 2 below. I have a patch but have to prepare it for a PR if wished.

Case 1 (working):

from coapthon.server.coap import CoAP
from coapthon.resources.resource import Resource

class CoAPServer(CoAP):
    def __init__(self, host, port):
        CoAP.__init__(self, (host, port))
        self.add_resource('one/', OneResource())
        self.add_resource('one/two/', TwoResource())

class OneResource(Resource):
    def __init__(self, name="OneResource", coap_server=None):
        super(OneResource, self).__init__(name, coap_server, visible=True,
                                            observable=True, allow_children=True)

    def render_GET(self, request):
        print("level_1")
        self.payload = 'level_1'
        return self

class TwoResource(Resource):
    def __init__(self, name="TwoResource", coap_server=None):
        super(TwoResource, self).__init__(name, coap_server, visible=True,
                                            observable=True, allow_children=True)

    def render_GET(self, request):
        print("level_2")
        self.payload = 'level_2'
        return self

server = CoAPServer("0.0.0.0", 5683)
try:
    server.listen(10)
except KeyboardInterrupt:
    server.close()
from coapthon.client.helperclient import HelperClient
client = HelperClient(server=('127.0.0.1', 5683))
client.get('/one').pretty_print() # level_1
client.get('/one/two').pretty_print() # level_2

Case 2 (not working):

class CoAPServer(CoAP):
    def __init__(self, host, port):
        CoAP.__init__(self, (host, port))
        self.add_resource('one/', OneResource())
        self.add_resource('two/three/', TwoResource())

class OneResource(Resource):
    def __init__(self, name="OneResource", coap_server=None):
        super(OneResource, self).__init__(name, coap_server, visible=True,
                                            observable=True, allow_children=True)

    def render_GET(self, request):
        print("level_1")
        self.payload = 'level_1'
        return self

class TwoResource(Resource):
    def __init__(self, name="TwoResource", coap_server=None):
        super(TwoResource, self).__init__(name, coap_server, visible=True,
                                            observable=True, allow_children=True)

    def render_GET(self, request):
        print("level_2")
        self.payload = 'level_2'
        return self

server = CoAPServer("0.0.0.0", 5683)
try:
    server.listen(10)
except KeyboardInterrupt:
    server.close()
from coapthon.client.helperclient import HelperClient
client = HelperClient(server=('127.0.0.1', 5683))
client.get('/one').pretty_print() # level_1
client.get('/two/three').pretty_print() # not found