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
58 stars 50 forks source link

combined multicast and unicast sockets to one socket #7

Closed hahahannes closed 6 years ago

hahahannes commented 6 years ago

Hi, I am encountering a problem when using a multicast server which should be requested with a unicast request. In the example below, I only get the first response after the multicast but not the second response from the unicast request. I am not sure, if my solution attempt is correct, but maybe you could have a look. Thanks!

To reproduce on localhost:

I first started the server.

from coapthon.server.coap import CoAP

from coapthon.resources.resource import Resource

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

    def render_GET(self, request):
        return self

    def render_PUT(self, request):
        self.payload = request.payload
        return self

    def render_POST(self, request):
        res = BasicResource()
        res.location_query = request.uri_query
        res.payload = request.payload
        return res

    def render_DELETE(self, request):
        return True

class CoAPServer(CoAP):
    def __init__(self, host, port):
        CoAP.__init__(self, (host, port), multicast=True)
        self.add_resource('basic/', BasicResource())

def main():
    server = CoAPServer("0.0.0.0", 5683)
    try:
        server.listen(10)
    except KeyboardInterrupt:
        server.close()

if __name__ == '__main__':
    main()

Then I started the client which discovers the server with a multicast request and then sends a unicast request with the informations from the first response.

from coapthon.client.helperclient import HelperClient

# Multicast 
host = "224.0.1.187"
port = 5683
path = "basic"

client = HelperClient(server=(host, port))
response = client.get(path)
print(response.pretty_print())

# Unicast
host = response.source[0]
port = response.source[1]

client = HelperClient(server=(host, port))
response = client.get(path)
print(response.pretty_print())