Search in sources :

Example 31 with Registration

use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.

the class CoapRequestBuilderTest method build_read_request_with_non_default_object_path.

@Test
public void build_read_request_with_non_default_object_path() throws Exception {
    Registration reg = newRegistration("/lwm2m");
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    ReadRequest request = new ReadRequest(3, 0, 1);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals("coap://127.0.0.1:12354/lwm2m/3/0/1", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 32 with Registration

use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.

the class CoapRequestBuilderTest method build_execute_request.

@Test
public void build_execute_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    ExecuteRequest request = new ExecuteRequest(3, 0, 12, "params");
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.POST, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/3/0/12", coapRequest.getURI());
    assertEquals("params", coapRequest.getPayloadString());
}
Also used : ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Test(org.junit.Test)

Example 33 with Registration

use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.

the class CoapRequestBuilderTest method build_write_request_replace.

@Test
public void build_write_request_replace() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    WriteRequest request = new WriteRequest(3, 0, 14, "value");
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.PUT, coapRequest.getCode());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Test(org.junit.Test)

Example 34 with Registration

use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.

the class ObservationServiceTest method givenAnObservation.

private Observation givenAnObservation(String registrationId, LwM2mPath target) {
    Registration registration = store.getRegistration(registrationId);
    if (registration == null) {
        registration = givenASimpleClient(registrationId);
        store.addRegistration(registration);
    }
    coapRequest = Request.newGet();
    coapRequest.setToken(CaliforniumTestSupport.createToken());
    coapRequest.getOptions().addUriPath(String.valueOf(target.getObjectId()));
    coapRequest.getOptions().addUriPath(String.valueOf(target.getObjectInstanceId()));
    coapRequest.getOptions().addUriPath(String.valueOf(target.getResourceId()));
    coapRequest.setObserve();
    coapRequest.setDestinationContext(EndpointContextUtil.extractContext(support.registration.getIdentity()));
    Map<String, String> context = ObserveUtil.createCoapObserveRequestContext(registration.getEndpoint(), registrationId, new ObserveRequest(target.toString()));
    coapRequest.setUserContext(context);
    store.put(coapRequest.getToken(), new org.eclipse.californium.core.observe.Observation(coapRequest, null));
    Observation observation = new Observation(coapRequest.getToken().getBytes(), registrationId, target, null);
    observationService.addObservation(registration, observation);
    return observation;
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest)

Example 35 with Registration

use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.

the class RedisRequestResponseHandler method sendRequest.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void sendRequest(final String message) {
    // Parse JSON and extract ticket
    final String ticket;
    JsonObject jMessage;
    try {
        jMessage = (JsonObject) Json.parse(message);
        ticket = jMessage.getString("ticket", null);
    } catch (RuntimeException t) {
        LOG.error(String.format("Unexpected exception during request message handling. (%s)", message), t);
        return;
    }
    // Now if an error occurred we can prevent message sender
    try {
        // Check if we must handle this request
        String endpoint = jMessage.getString("ep", null);
        if (!isResponsibleFor(endpoint))
            return;
        // Get the registration for this endpoint
        final Registration destination = registrationService.getByEndpoint(endpoint);
        if (destination == null) {
            sendError(ticket, String.format("No registration for this endpoint %s.", endpoint));
        }
        // Deserialize Request
        DownlinkRequest<?> request = DownlinkRequestSerDes.deserialize((JsonObject) jMessage.get("req"));
        // Ack we will handle this request
        sendAck(ticket);
        // Send it
        server.send(destination, request, new ResponseCallback() {

            @Override
            public void onResponse(LwM2mResponse response) {
                handleResponse(destination.getEndpoint(), ticket, response);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                handlerError(destination.getEndpoint(), ticket, e);
            }
        });
    } catch (RuntimeException t) {
        String errorMessage = String.format("Unexpected exception during request message handling.(%s:%s)", t.toString(), t.getMessage());
        LOG.error(errorMessage, t);
        sendError(ticket, errorMessage);
    }
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) ResponseCallback(org.eclipse.leshan.core.response.ResponseCallback) JsonObject(com.eclipsesource.json.JsonObject) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse)

Aggregations

Registration (org.eclipse.leshan.server.registration.Registration)44 Test (org.junit.Test)19 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)15 Request (org.eclipse.californium.core.coap.Request)14 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)13 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)13 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)13 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)13 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)13 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)13 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)12 Observation (org.eclipse.leshan.core.observation.Observation)10 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)10 InetSocketAddress (java.net.InetSocketAddress)7 Jedis (redis.clients.jedis.Jedis)6 RegistrationUpdate (org.eclipse.leshan.server.registration.RegistrationUpdate)5 Deregistration (org.eclipse.leshan.server.registration.Deregistration)4 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)3 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3