Search in sources :

Example 1 with RegisterRequest

use of org.eclipse.leshan.core.request.RegisterRequest in project leshan by eclipse.

the class FailingTest method sync_send_with_acknowleged_request_without_response.

@Test
public void sync_send_with_acknowleged_request_without_response() throws Exception {
    // Register client
    LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress());
    client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, Link.parse("</1>,</2>,</3>".getBytes()), null));
    client.expectResponse().go();
    helper.waitForRegistration(1);
    // Send read
    Future<ReadResponse> future = Executors.newSingleThreadExecutor().submit(new Callable<ReadResponse>() {

        @Override
        public ReadResponse call() throws Exception {
            // send a request with 3 seconds timeout
            return helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000);
        }
    });
    // Acknowledge the response
    client.expectRequest().storeMID("R").go();
    client.sendEmpty(Type.ACK).loadMID("R").go();
    // Request should timedout in ~3s as we send the ACK
    Thread.sleep(1500);
    Assert.assertFalse("we should still wait for response", future.isDone());
    ReadResponse response = future.get(2000, TimeUnit.MILLISECONDS);
    Assert.assertNull("we should timeout", response);
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) TimeoutException(org.eclipse.leshan.core.request.exception.TimeoutException) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 2 with RegisterRequest

use of org.eclipse.leshan.core.request.RegisterRequest in project leshan by eclipse.

the class FailingTest method async_send_with_acknowleged_request_without_response.

@Test
public void async_send_with_acknowleged_request_without_response() throws Exception {
    // register client
    LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress());
    client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, Link.parse("</1>,</2>,</3>".getBytes()), null));
    client.expectResponse().go();
    helper.waitForRegistration(1);
    // send read
    Callback<ReadResponse> callback = new Callback<ReadResponse>();
    helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000l, callback, callback);
    // Acknowledge the response
    client.expectRequest().storeMID("R").go();
    client.sendEmpty(Type.ACK).loadMID("R").go();
    // Request should timedout in ~3s as we send a ack
    Thread.sleep(1500);
    Assert.assertTrue("we should still wait for response", callback.getException() == null);
    callback.waitForResponse(2000);
    Assert.assertTrue("we should timeout", callback.getException() instanceof TimeoutException);
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) TimeoutException(org.eclipse.leshan.core.request.exception.TimeoutException) Test(org.junit.Test)

Example 3 with RegisterRequest

use of org.eclipse.leshan.core.request.RegisterRequest in project leshan by eclipse.

the class RegistrationEngine method register.

private boolean register() throws InterruptedException {
    ServersInfo serversInfo = ServersInfoExtractor.getInfo(objectEnablers);
    DmServerInfo dmInfo = serversInfo.deviceMangements.values().iterator().next();
    if (dmInfo == null) {
        LOG.error("Trying to register device but there is no LWM2M server config.");
        return false;
    }
    // send register request
    LOG.info("Trying to register to {} ...", dmInfo.getFullUri());
    RegisterRequest regRequest = new RegisterRequest(endpoint, dmInfo.lifetime, LwM2m.VERSION, dmInfo.binding, null, LinkFormatHelper.getClientDescription(objectEnablers.values(), null), additionalAttributes);
    RegisterResponse response = sender.send(dmInfo.getAddress(), dmInfo.isSecure(), regRequest, DEFAULT_TIMEOUT);
    if (response == null) {
        registrationID = null;
        LOG.error("Registration failed: Timeout.");
        if (observer != null) {
            observer.onRegistrationTimeout(dmInfo);
        }
    } else if (response.isSuccess()) {
        registrationID = response.getRegistrationID();
        LOG.info("Registered with location '{}'.", response.getRegistrationID());
        // update every lifetime period
        scheduleUpdate(dmInfo);
        if (observer != null) {
            observer.onRegistrationSuccess(dmInfo, response.getRegistrationID());
        }
    } else {
        registrationID = null;
        LOG.error("Registration failed: {} {}.", response.getCode(), response.getErrorMessage());
        if (observer != null) {
            observer.onRegistrationFailure(dmInfo, response.getCode(), response.getErrorMessage());
        }
    }
    return registrationID != null;
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) RegisterResponse(org.eclipse.leshan.core.response.RegisterResponse)

Example 4 with RegisterRequest

use of org.eclipse.leshan.core.request.RegisterRequest in project leshan by eclipse.

the class FailingTest method async_send_without_acknowleged.

@Test
public void async_send_without_acknowleged() throws Exception {
    // register client
    LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress());
    client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, Link.parse("</1>,</2>,</3>".getBytes()), null));
    client.expectResponse().go();
    helper.waitForRegistration(1);
    // send read
    Callback<ReadResponse> callback = new Callback<ReadResponse>();
    helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000l, callback, callback);
    // Request should timedout in ~1s we don't send ACK
    callback.waitForResponse(1500);
    Assert.assertTrue("we should timeout", callback.getException() instanceof TimeoutException);
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) TimeoutException(org.eclipse.leshan.core.request.exception.TimeoutException) Test(org.junit.Test)

Example 5 with RegisterRequest

use of org.eclipse.leshan.core.request.RegisterRequest in project leshan by eclipse.

the class FailingTest method sync_send_without_acknowleged.

@Test
public void sync_send_without_acknowleged() throws Exception {
    // Register client
    LockStepLwM2mClient client = new LockStepLwM2mClient(helper.server.getUnsecuredAddress());
    client.sendLwM2mRequest(new RegisterRequest(helper.getCurrentEndpoint(), 60l, null, BindingMode.U, null, Link.parse("</1>,</2>,</3>".getBytes()), null));
    client.expectResponse().go();
    helper.waitForRegistration(1);
    // Send read
    Future<ReadResponse> future = Executors.newSingleThreadExecutor().submit(new Callable<ReadResponse>() {

        @Override
        public ReadResponse call() throws Exception {
            // send a request with 3 seconds timeout
            return helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3), 3000);
        }
    });
    // Request should timedout in ~1s we don't send ACK
    ReadResponse response = future.get(1500, TimeUnit.MILLISECONDS);
    Assert.assertNull("we should timeout", response);
}
Also used : RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) TimeoutException(org.eclipse.leshan.core.request.exception.TimeoutException) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Aggregations

RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)7 Test (org.junit.Test)5 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)4 TimeoutException (org.eclipse.leshan.core.request.exception.TimeoutException)4 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)4 RegisterResponse (org.eclipse.leshan.core.response.RegisterResponse)3 HashMap (java.util.HashMap)2 Link (org.eclipse.leshan.Link)2 InetSocketAddress (java.net.InetSocketAddress)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 Endpoint (org.eclipse.californium.core.network.Endpoint)1 LeshanClient (org.eclipse.leshan.client.californium.LeshanClient)1 CaliforniumLwM2mRequestSender (org.eclipse.leshan.client.californium.impl.CaliforniumLwM2mRequestSender)1 EndpointContextUtil.extractIdentity (org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity)1 BindingMode (org.eclipse.leshan.core.request.BindingMode)1 DeregisterRequest (org.eclipse.leshan.core.request.DeregisterRequest)1 Identity (org.eclipse.leshan.core.request.Identity)1