Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New CoapRequest / CoapResponse Builder API feedback #73

Closed
sbernard31 opened this issue Nov 14, 2023 · 4 comments
Closed

New CoapRequest / CoapResponse Builder API feedback #73

sbernard31 opened this issue Nov 14, 2023 · 4 comments

Comments

@sbernard31
Copy link
Collaborator

I updated Leshan code to use the new java-coap API.

I think the API is largely better now 👍

Only issue I faced was when I wanted to modify a CoapRequest to add element in TransportContext.

I didn't find better way than :

TransportContext extendedContext = observeRequest.getTransContext() //
                            .with(LwM2mKeys.LESHAN_OBSERVED_PATHS, paths);

CoapRequest modifiedObservedRequest = new CoapRequest(observeRequest.getMethod(),
        observeRequest.getToken(), observeRequest.options(), observeRequest.getPayload(),
        observeRequest.getPeerAddress(), extendedContext);

super.add(modifiedObservedRequest);

A solution to avoid the verbose coapRequest creation could be #68 (comment) and so here this could look like :

TransportContext extendedContext = observeRequest.getTransContext() //
                            .with(LwM2mKeys.LESHAN_OBSERVED_PATHS, paths);

super.add(coapRequest.modify().context(extendedContext).build());

Maybe we could also add a method to modify transportContext in the builder ? something like :

super.add(coapRequest.modify()
                     .context(ctx -> ctx.with(LwM2mKeys.LESHAN_OBSERVED_PATHS, paths))
                     .build());

Note that:

  • those new methods can be added without deprecating with*(...) methods a first time. (if you think this is too soon)
  • modify() method probably also makes sense for CoapResponse

(If you are curious the whole migration change are available at : eclipse-leshan/leshan@c2f8232)

@sbernard31
Copy link
Collaborator Author

sbernard31 commented Nov 14, 2023

Not directly linked but I'm think that some part of the README was not updated with the new API :

-        // (optional) set custom observation relation store, for example one that will use external storage
+        // (optional) set custom observations store, for example one that will use external storage
-       .observationRelationsStore(new HashMapObservationRelations())
+       .observationsStore(new HashMapObservationsStore())
-// define subscription manager for observable resources
+// define observers manager for observable resources
-InboundSubscriptionManager subscriptionManager = new InboundSubscriptionManager();
+ObserversManager observersManager = new ObserversManager();

server = CoapServer.builder()
        // configure with plain text UDP transport, listening on port 5683
        .transport(new DatagramSocketTransport(5683))
        // define routing
        // (note that each resource function is a `Service` type and can be decorated/transformed with `Filter`)
        .route(RouterService.builder()
                .get("/.well-known/core", req ->
                        CoapResponse.ok("</sensors/temperature>", MediaTypes.CT_APPLICATION_LINK__FORMAT).toFuture()
                )
                .post("/actuators/switch", req -> {
                    // ...
                    return coapResponse(Code.C204_CHANGED).toFuture();
                })
                // observable resource
-               .get("/sensors/temperature", subscriptionManager.then(req ->
+               .get("/sensors/temperature", observersManager.then(req ->
-                        completedFuture(CoapResponse.ok("21C"))
+                        CoapResponse.ok("21C").toFuture())
                ))
        )
        .build();

observersManager.init(server);

I didn't test/execute the code, so probably deserve a double check 😅

@szysas
Copy link
Collaborator

szysas commented Nov 15, 2023

Thanks for feedback. I agree than modifying transport context is really needed.
Thanks for spotting outdated readme. It is actually based on theses tests: https://github.com/open-coap/java-coap/blob/master/coap-core/src/test/java/protocolTests/UsageTest.java. Do you know any idea/tools how to verify if readme examples are up to date?

@sbernard31
Copy link
Collaborator Author

Do you know any idea/tools how to verify if readme examples are up to date?

I had some ideas but not sure they really works :

  • I was thinking about using checkstyle with regexp ? (but probably not work)
  • Using this github actions ? https://github.com/marketplace/actions/markdown-autodocs
  • Using another supported language than markdown (e.g. reStructuredText or asciidoctor) which have include directive but it seems that github doesn't support it ...
  • Using github code import but I understand you need to create permalink each time ... (and rendering is not so good)
    // define subscription manager for observable resources
    ObserversManager observersManager = new ObserversManager();
    server = CoapServer.builder()
    // configure with plain text UDP transport, listening on port 5683
    .transport(new DatagramSocketTransport(5683))
    // define routing
    // (note that each resource function is a `Service` type and can be decorated/transformed with `Filter`)
    .route(RouterService.builder()
    .get("/.well-known/core", req ->
    CoapResponse.ok("</sensors/temperature>", MediaTypes.CT_APPLICATION_LINK__FORMAT).toFuture()
    )
    .post("/actuators/switch", req -> {
    // ...
    return coapResponse(Code.C204_CHANGED).toFuture();
    })
    // observable resource
    .get("/sensors/temperature", observersManager.then(req ->
    CoapResponse.ok("21C").toFuture()
    ))
    )
    .build();
    observersManager.init(server);
    server.start();

So nothing great 😬

@szysas
Copy link
Collaborator

szysas commented Nov 20, 2023

I had some ideas but not sure they really works

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants