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

Some Idea to try to improve API to create CaliforniumServerEndpointsProvider #1395

Closed
sbernard31 opened this issue Feb 3, 2023 · 4 comments

Comments

@sbernard31
Copy link
Contributor

After discussion at #1383, I tried to think about how to improve API to create CaliforniumServerEndpointsProvider.
Here some ideas, where Coaps is used as example but could be generalized to all kind of protocol based on Californium :


A)

Being able to customize dtlsConfig on CoapsProtocolProvider. (See #1382 for more details)

e.g. proposed API :

LeshanServerBuilder serverBuilder = new LeshanServerBuilder();

CaliforniumServerEndpointsProvider.Builder endpointsBuilder = new CaliforniumServerEndpointsProvider.Builder(
        // add CoAP protocol Provider
        new CoapServerProtocolProvider(),

        // add CoAPS protocol Provider.
        new CoapsServerProtocolProvider(dtlsConfigBuilder -> {
            dtlsConfigBuilder.setSessionStore(new CustomSessionStore());
        }));

serverBuilder.setEndpointsProvider(endpointsBuilder.build());

B)

Currently the way to customize a Californium configuration looks like this :

LeshanServerBuilder serverBuilder = new LeshanServerBuilder();

CaliforniumServerEndpointsProvider.Builder endpointsBuilder = new CaliforniumServerEndpointsProvider.Builder(
        new CoapServerProtocolProvider(), new CoapsServerProtocolProvider());

// Change Californium configuration :
Configuration californiumConfig = endpointsBuilder.createDefaultConfiguration();
californiumConfig.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
californiumConfig.set(CoapConfig.ACK_TIMEOUT  , 1, TimeUnit.SECONDS);
endpointsBuilder.setConfiguration(californiumConfig);

serverBuilder.setEndpointsProvider(endpointsBuilder.build());

Maybe it would be better to propose this way instead (or in addition) :

LeshanServerBuilder serverBuilder = new LeshanServerBuilder();

CaliforniumServerEndpointsProvider.Builder endpointsBuilder = new CaliforniumServerEndpointsProvider.Builder(
        new CoapServerProtocolProvider(), new CoapsServerProtocolProvider());

// Change Californium configuration : 
// This configuration will be used for whole CoAP Server and so all endpoints.
endpointsBuilder.setConfiguration(c -> {
    c.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
    c.set(CoapConfig.ACK_TIMEOUT , 1, TimeUnit.SECONDS);
});

serverBuilder.setEndpointsProvider(endpointsBuilder.build());

C)

The "normal way" to create californium based endpoint is :

LeshanServerBuilder serverBuilder = new LeshanServerBuilder();

CaliforniumServerEndpointsProvider.Builder endpointsBuilder = new CaliforniumServerEndpointsProvider.Builder(
        new CoapServerProtocolProvider(), new CoapsServerProtocolProvider());

endpointsBuilder.addEndpoint(new InetSocketAddress(5784), Protocol.COAP);
endpointsBuilder.addEndpoint("coaps://localhost:5684");

serverBuilder.setEndpointsProvider(endpointsBuilder.build());

If default endpoint created by ProtocolProvider is not enough, you can :

endpointsBuilder.addEndpoint(new CoapsServerEndpointFactory(coapUri));

But currently there is no easy way to configure the CoapsServerEndpointFactory without creating a new class which inherits it.

An alternative could be to create a Builder which could like like :

endpointsBuilder.addEndpoint(new CoapsServerEndpointFactoryBuilder() //
        .uri(5784) //
        .loggingTagPrefix("Custom Coaps") //
        .dtlsConfig(c -> c.setSessionStore(new CustomSessionStore())) //
        .endpointConfig(c -> c.setTokenGenerator(new CustomTokenGenerator())) //
        // if used, endpoint will used a specific config instead of the CoAP server one. (see point B. 👆 )
        .configuration(c -> c.set(CoapConfig.ACK_TIMEOUT  , 1, TimeUnit.SECONDS)) //
        .build());
@cyril2maq
Copy link

The CoapsServerEndpointFactoryBuilder (C) proposal seems the most readable API to me.

It allows to set dedicated options without having to read, understand and override the whole CoapsServerEndpointFactory class.

@sbernard31
Copy link
Contributor Author

sbernard31 commented Feb 6, 2023

Reading your answer, I feel that maybe I was not clear enough.

So just to be sure, proposition A), B) and C) are not exclusive and doesn't target same goal.

About C), the full snippet of code would be :

LeshanServerBuilder serverBuilder = new LeshanServerBuilder();

CaliforniumServerEndpointsProvider.Builder endpointsBuilder = new CaliforniumServerEndpointsProvider.Builder(
        new CoapServerProtocolProvider(), new CoapsServerProtocolProvider());

// to add default endpoint for COAP and COAPS
endpointsBuilder.addEndpoint(Protocol.COAP);
endpointsBuilder.addEndpoint(Protocol.COAPS);

// to add another COAPS endpoint which custom config. 
endpointsBuilder.addEndpoint(new CoapsServerEndpointFactoryBuilder() //
        .uri(5784) //
        .loggingTagPrefix("Custom Coaps") //
        .dtlsConfig(c -> c.setSessionStore(new CustomSessionStore())) //
        .endpointConfig(c -> c.setTokenGenerator(new CustomTokenGenerator())) //
        .configuration(c -> c.set(CoapConfig.ACK_TIMEOUT  , 1, TimeUnit.SECONDS)) //
        .build());

// so the endpoints provider will provide 3 endpoints, 1 for coap and 2 for coaps.
serverBuilder.setEndpointsProvider(endpointsBuilder.build());

@sbernard31
Copy link
Contributor Author

This ideas are implemented at : #1407

@sbernard31
Copy link
Contributor Author

This is now integrated in master (See PR #1407)

Warmek pushed a commit to JaroslawLegierski/leshan that referenced this issue Mar 28, 2023
Warmek pushed a commit to JaroslawLegierski/leshan that referenced this issue Mar 28, 2023
Warmek pushed a commit to JaroslawLegierski/leshan that referenced this issue Mar 28, 2023
Warmek pushed a commit to JaroslawLegierski/leshan that referenced this issue Mar 28, 2023
Warmek pushed a commit to JaroslawLegierski/leshan that referenced this issue Mar 28, 2023
Warmek pushed a commit to JaroslawLegierski/leshan that referenced this issue Mar 28, 2023
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