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

Client is not registering on Server for Leshan version "2.0.0-M14" #1596

Closed
mhatrepratik11 opened this issue Mar 25, 2024 · 4 comments
Closed
Labels
question Any question about leshan

Comments

@mhatrepratik11
Copy link

Question

Hello Team.

I am new to Leshan and looking for help to start with Leshan. I am trying to implement Leshan Server and Client in my project.

I was following the Github documentation and have implemented Leshan Server and Client with version "1.5.0" which is working fine with each other.

When I tried implementation with version “2.0.0-M14”, my Leshan Client was not able to register onto the server. Whereas, my Leshan Server is working well with the Leshan-Client-Demo JAR file provided in the docs. I am able to register and able to send the data to server using command line.

Leshan Server Code is below:

public class LeshanServerApplication {

	public static void main(String[] args) throws Exception {

		LeshanServerBuilder builder = new LeshanServerBuilder();
		final ThrowingSupplier<URI> uriThrowingSupplier = () -> new URI("COAP", null, "localhost", 5683, null, null, null);

		URI uri = uriThrowingSupplier.get();
		final ServerProtocolProvider serverProtocolProvider = new CoapServerProtocolProvider();
		final CaliforniumServerEndpointFactory californiumServerEndpointFactory = serverProtocolProvider.createDefaultEndpointFactory(uri);
		final Configuration config = new Configuration();
		config.set(CoapConfig.PROTOCOL_STAGE_THREAD_COUNT, 4);
		final CaliforniumServerEndpointsProvider californiumServerEndpointsProvider = new CaliforniumServerEndpointsProvider.Builder(serverProtocolProvider)
			.setConfiguration(config)
			.addEndpoint(californiumServerEndpointFactory)
			.build();
		builder.setEndpointsProviders(californiumServerEndpointsProvider);

		builder.setRegistrationIdProvider(new RandomStringRegistrationIdProvider());
		LeshanServer server = builder.build();

		server.start();

		server.getRegistrationService().addListener(new RegistrationListener() {
			@Override
			public void registered(Registration registration, Registration previousReg, Collection<Observation> previousObservations) {
				System.out.println("Registered client: " + registration.getEndpoint());
			}

			// Other override  methods
		});


		// Starting jetty webserver for UI at http://localhost:8080
		InetSocketAddress jettyAddr = new InetSocketAddress("127.0.0.1", 8080);
		Server jettyServer = new Server(jettyAddr);
		jettyServer.start();
	}
}

There is only issue with server is, I am not able to access web-server to see the UI at http://localhost:8080. I fails with below error:

HTTP ERROR 404 Not Found

URI: http://localhost:8080/
STATUS: 404
MESSAGE: Not Found

Leshan Client Code is below:

public class LeshanClientApplication {

	public static void main(String[] args) throws UnknownHostException {

		CaliforniumClientEndpointsProvider.Builder endpointsBuilder = new CaliforniumClientEndpointsProvider.Builder();
		endpointsBuilder.setClientAddress(InetAddress.getLocalHost());

		final LeshanClientBuilder leshanClientBuilder = new LeshanClientBuilder("LwM2M Client");
		Server server = new Server(123, 5L * 60L, EnumSet.of(BindingMode.U), false, BindingMode.U);

		ObjectsInitializer initializer = new ObjectsInitializer();
		initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://127.0.0.1:5683", 123));
		initializer.setInstancesForObject(SERVER, server);
		initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("LwM2M Device 1", "model12345.7", "12345.7", EnumSet.of(BindingMode.U)));
		leshanClientBuilder.setObjects(initializer.createAll());

		final LeshanClient client = leshanClientBuilder
			.setEndpointsProviders(endpointsBuilder.build())
			.build();

		client.start();

		System.out.println("IsRegisteredServers map empty: " + client.getRegisteredServers().isEmpty());

		client.addObserver(new LwM2mClientObserver() {

			@Override
			public void onRegistrationSuccess(LwM2mServer server, RegisterRequest request, String registrationID) {
				System.out.println("onRegistrationSuccess called");
			}

			@Override
			public void onRegistrationTimeout(LwM2mServer server, RegisterRequest request) {
				System.out.println("onRegistrationTimeout called: " + server.getUri() + "    " + server.isLwm2mServer());
				System.out.println("onRegistrationTimeout called with request: " + request.getCoapRequest().toString());
			}

			// Others implemented methods
		});
	}
}

The Leshan Client with above code not able to register itself onto the server. It fails with below error:

01:35:31.511 [RegistrationEngine#0] INFO org.eclipse.leshan.client.engine.DefaultRegistrationEngine -- Trying to register to coap://127.0.0.1:5683 ...
01:35:31.495 [RegistrationEngine#0] INFO org.eclipse.leshan.client.engine.DefaultRegistrationEngine -- Registration failed: Timeout.
onRegistrationTimeout called: coap://127.0.0.1:5683    true
onRegistrationTimeout called with CoAP request: null

Also, "client.getRegisteredServers()" always returns empty map.

Thank you for your attention!

@mhatrepratik11 mhatrepratik11 added the question Any question about leshan label Mar 25, 2024
@mhatrepratik11
Copy link
Author

I was able to solve client registration issue with below code:

final LeshanClient client = leshanClientBuilder
			.setEndpointsProviders(new CaliforniumClientEndpointsProvider.Builder().build())
			.build();

Setting clientAddress to CaliforniumClientEndpointsProvider was my mistake: "endpointsBuilder.setClientAddress(InetAddress.getLocalHost());"
With this change the client is working fine now.

So now only issue persist is of Leshan WebServer UI is not working at http://localhost:8080
I have added below code for WebServer.

// Starting jetty webserver for UI at http://localhost:8080
InetSocketAddress jettyAddr = new InetSocketAddress("127.0.0.1", 8080);
Server jettyServer = new Server(jettyAddr);
jettyServer.start();

Any suggestions / help would be great.

Thank you for your attention!

@sbernard31
Copy link
Contributor

sbernard31 commented Apr 2, 2024

I looked at your server code and it should rather looks like :

        // create provider
        final CaliforniumServerEndpointsProvider californiumServerEndpointsProvider = new CaliforniumServerEndpointsProvider.Builder(
                new CoapServerProtocolProvider()) //
                        .setConfiguration(config -> config.set(CoapConfig.PROTOCOL_STAGE_THREAD_COUNT, 4))
                        .addEndpoint("coap://localhost:5683") //
                        // OR
                        // .addEndpoint(new InetSocketAddress("localhost", 5683), Protocol.COAP) //
                        .build();

        // create server
        LeshanServerBuilder builder = new LeshanServerBuilder();
        // builder.setRegistrationIdProvider(new RandomStringRegistrationIdProvider()); // not needed used by default
        builder.setEndpointsProviders(californiumServerEndpointsProvider);
        LeshanServer server = builder.build();

        // start it
        server.start();

        // listen for registration
        server.getRegistrationService().addListener(new RegistrationListener() {
            @Override
            public void registered(Registration registration, Registration previousReg,
                    Collection<Observation> previousObservations) {
                System.out.println("Registered client: " + registration.getEndpoint());
            }
            // Other override methods
        });

     // All the jetty code is useless OR it missed your own code to add `Servlet` to jetty.

So now only issue persist is of Leshan WebServer UI is not working at http://localhost:8080/

Leshan is a library, there is no web server UI.
But Leshan provide a standalone demo which contains a web UI.

See FAQ to better understand : https://github.com/eclipse-leshan/leshan/wiki/F.A.Q.#what-is-leshan-and-how-should-i-use-it-
Maybe I should change again the README + FAQ, as it seems not clear for everybody 🤔

@mhatrepratik11
Copy link
Author

Thank you very much @sbernard31. Now it's clear to me and thanks for the code. I will change my code accordingly. Satisfied with the answer and good to close this issue. Thanks again...

@sbernard31
Copy link
Contributor

You're welcome.

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

No branches or pull requests

2 participants