Coap server resources discovery

585 views Asked by At

I am using the californium library for coap communication and it is being deployed on the Android platform. I have started coap server in one device and the client is on another device, both are in the same network.

Server code : Creating a server with below resource

class HelloWorldResource extends CoapResource {

    public HelloWorldResource() {

        // set resource identifier
        super("hello");

        // set display name
        getAttributes().setTitle("Hello-World Resource");
    }

    @Override
    public void handleGET(CoapExchange exchange) {

        // respond to the request
        exchange.respond("Hello Android!");
    }
}

Client code :

    CoapClient coapClient = new CoapClient("coap://localhost/.well-known/core");

    try {
        Set<WebLink> webLinks = coapClient.discover();
        System.out.println(webLinks.size());
    } catch (ConnectorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

no output from the above code. I don't know the IP address and I want to communicate with the server. is this the right way or I am missing something?

1

There are 1 answers

1
Achim Kraus On

A "discover" is a special coap request, to discover the resources of a known host.

To discover the host, a mutlicast request may be used, but must be supported by the server.

"localhost" is the address of the device itself. With that, the device will send the discover-request to itself.

I don't know the IP address and I want to communicate with the server.

If you dont't know the address, you might either use a "multicast" request first (requires also preparation on the server side, see MulticastServer Example. That example is more complex, than you will need. But demonstrates the principles as well). Or you need to lookup that address (android: settings -> Connections -> WLAN -> Settings-Icon : there you see your IP-address.

coapClient.discover()

Discovers a "known" server, not the local-network. Replace the localhost with "californium.eclipseprojects.io" and you will get:

</.well-known/core>
</large> Large resource
    rt: [block]
    sz: [1280]
</large-create> Large resource that can be created using POST method
    rt: [block]
</large-post> Handle POST with two-way blockwise transfer
    rt: [block]
</large-separate> Large resource
    rt: [block]
    sz: [1280]
</large-update> Large resource that can be updated using PUT method
    rt: [block]
    sz: [1280]
</link1> Link test resource
    rt: [Type1, Type2]
    if: [If1]
</link2> Link test resource
    rt: [Type2, Type3]
    if: [If2]
</link3> Link test resource
    rt: [Type1, Type3]
    if: [foo]
</location-query> Perform POST transaction with responses containing several Location-Query options (CON mode)
</multi-format> Resource that exists in different content formats (text/plain utf8 and application/xml)
    ct: [0, 41, 50, 60]
</obs> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-large> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-non> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-pumping> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-pumping-non> Observable resource which changes every 5 seconds
    rt: [observe]
    obs:    []
</obs-reset>
</path> Hierarchical link description entry
    ct: [40]
</path/sub1> Hierarchical link description sub-resource
</path/sub2> Hierarchical link description sub-resource
</path/sub3> Hierarchical link description sub-resource
</query> Resource accepting query parameters
</seg1> Long path resource
</seg1/seg2> Long path resource
</seg1/seg2/seg3> Long path resource
</separate> Resource which cannot be served immediately and which cannot be acknowledged in a piggy-backed way
</shutdown>
</test> Default test resource
</validate> Resource which varies
    ct: [0]
    sz: [20]

All the received links are relative to the server you send the discover to.