smeshlink / CoAP.NET

A C# implementation of the CoAP protocol
open.smeshlink.com/CoAP.NET/
Other
159 stars 81 forks source link

Discover not Multicast Ready #66

Open Alv3s opened 4 years ago

Alv3s commented 4 years ago

I have been playing with this library to discover CoAP devices using my own library on the microcontroller (ESP8266): https://github.com/Alv3s/Thing.CoAP

It seems CoAP.NET is not ready for this feature. Microcontroller is configured to respond to CoAP multicast address (224.0.1.187), I am opening a client to this coap endpoint "coap://224.0.1.187/" and I was expecting to discover all the resources of all available CoAP devices on the network. But it seems I am only getting the first one.

I tried to do as follows:

CoapClient client = new CoapClient(new Uri("coap://224.0.1.187/"));
            client.Respond += Request_Respond;
            var result = client.Discover().ToList();

            for (; ; )
            {
                Thread.Sleep(1000);
            }

The code above only returns the first device replying to the request. I also tried as follows:

        static void CoAPMulticast()
        {
            // new a GET request
            Request request = new Request(Method.GET, false);
            request.Multicast = true;
            request.URI = new Uri("coap://224.0.1.187/.well-known/core");
            request.Respond += Request_Respond;

            request.Send();

            for (; ; )
            {
                Thread.Sleep(1000);
            }
        }

private static void Request_Respond(object sender, ResponseEventArgs e)
        {
            Console.WriteLine("Here");
        }

I was expecting function "Request_Respond" to print twice the "Here", but I can see on the logs that the second response is being ignored.

Any tips? Maybe I could help you with this one.

mfalkvidd commented 4 years ago

Does the request reach all the clients? Should be easy to verify by checking the logs for multiple devices.

Do all the clients' responses reach the computer where the client is running? Easiest way to verify is probably to run wireshark on the computer.

Alv3s commented 4 years ago

@mfalkvidd Thank your for the reply. I did verify and checked the multicast request reached both devices and both devices response reach CoAP.NET. I could verify this with Wireshark and also via logs generated on the console by CoAP.NET.

The issue seems to be the Discover method is only expecting one response (which usually it is fine). I guess a method called "DiscoverAll" could be created and return an IEnumerable of Devices with weblinks. If that seems fine to you, I could implement it and open a merge request for you.

mfalkvidd commented 4 years ago

I don't have control over this repository, and I don't use multicast myself, but your suggestion sounds good.

I'm not sure anyone is maintaining this repo anymore. I submitted a pull request quite a while ago: https://github.com/smeshlink/CoAP.NET/pull/64

I would prefer that this repo remained the "true" source for the project, but you are welcome to submit a pull request to my fork if you have no luck here.

There are a few other forks as well: https://github.com/smeshlink/CoAP.NET/network