Skip to content

Commit

Permalink
Use Identity as destination.
Browse files Browse the repository at this point in the history
Convert that Identity into a EndpointContext of a request.

Signed-off-by: Achim Kraus <achim.kraus@bosch-si.com>
Also-by: Simon Bernard <sbernard@sierrawireless.com>
  • Loading branch information
Achim Kraus authored and sbernard31 committed Jan 5, 2018
1 parent 83ed30e commit 6e84a99
Show file tree
Hide file tree
Showing 20 changed files with 296 additions and 148 deletions.
Expand Up @@ -16,6 +16,7 @@
*******************************************************************************/
package org.eclipse.leshan.core.request;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.security.PublicKey;

Expand Down Expand Up @@ -82,18 +83,34 @@ public static Identity unsecure(InetSocketAddress peerAddress) {
return new Identity(peerAddress, null, null, null);
}

public static Identity unsecure(InetAddress address, int port) {
return new Identity(new InetSocketAddress(address, port), null, null, null);
}

public static Identity psk(InetSocketAddress peerAddress, String identity) {
return new Identity(peerAddress, identity, null, null);
}

public static Identity psk(InetAddress address, int port, String identity) {
return new Identity(new InetSocketAddress(address, port), identity, null, null);
}

public static Identity rpk(InetSocketAddress peerAddress, PublicKey publicKey) {
return new Identity(peerAddress, null, publicKey, null);
}

public static Identity rpk(InetAddress address, int port, PublicKey publicKey) {
return new Identity(new InetSocketAddress(address, port), null, publicKey, null);
}

public static Identity x509(InetSocketAddress peerAddress, String commonName) {
return new Identity(peerAddress, null, null, commonName);
}

public static Identity x509(InetAddress address, int port, String commonName) {
return new Identity(new InetSocketAddress(address, port), null, null, commonName);
}

@Override
public String toString() {
if (pskIdentity != null)
Expand All @@ -105,4 +122,47 @@ else if (x509CommonName != null)
else
return String.format("Identity %s[unsecure]", peerAddress);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((peerAddress == null) ? 0 : peerAddress.hashCode());
result = prime * result + ((pskIdentity == null) ? 0 : pskIdentity.hashCode());
result = prime * result + ((rawPublicKey == null) ? 0 : rawPublicKey.hashCode());
result = prime * result + ((x509CommonName == null) ? 0 : x509CommonName.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Identity other = (Identity) obj;
if (peerAddress == null) {
if (other.peerAddress != null)
return false;
} else if (!peerAddress.equals(other.peerAddress))
return false;
if (pskIdentity == null) {
if (other.pskIdentity != null)
return false;
} else if (!pskIdentity.equals(other.pskIdentity))
return false;
if (rawPublicKey == null) {
if (other.rawPublicKey != null)
return false;
} else if (!rawPublicKey.equals(other.rawPublicKey))
return false;
if (x509CommonName == null) {
if (other.x509CommonName != null)
return false;
} else if (!x509CommonName.equals(other.x509CommonName))
return false;
return true;
}
}
Expand Up @@ -15,8 +15,6 @@
*******************************************************************************/
package org.eclipse.leshan.server.californium.impl;

import java.net.InetSocketAddress;

import org.eclipse.californium.core.coap.MessageObserver;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
Expand All @@ -29,6 +27,7 @@
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.request.DownlinkRequest;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.core.response.ErrorCallback;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.response.ResponseCallback;
Expand All @@ -54,10 +53,10 @@ public CaliforniumLwM2mBootstrapRequestSender(Endpoint secureEndpoint, Endpoint
}

@Override
public <T extends LwM2mResponse> T send(final String endpointName, final InetSocketAddress clientAddress,
final boolean secure, final DownlinkRequest<T> request, long timeout) throws InterruptedException {
// Create the CoAP request from LwM2m request
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(clientAddress, model, encoder);
public <T extends LwM2mResponse> T send(final String endpointName, final Identity destination,
final DownlinkRequest<T> request, long timeout) throws InterruptedException { // Create the CoAP request
// from LwM2m request
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(destination, model, encoder);
request.accept(coapClientRequestBuilder);

final Request coapRequest = coapClientRequestBuilder.getRequest();
Expand All @@ -68,9 +67,8 @@ public <T extends LwM2mResponse> T send(final String endpointName, final InetSoc
public T buildResponse(Response coapResponse) {
// TODO we need to fix that by removing the Client dependency from LwM2MResponseBuilder or by creating a
// LwM2mBootstrapResponseBuilder
Registration registration = new Registration.Builder("fakeregistrationid", endpointName,
clientAddress.getAddress(), clientAddress.getPort(),
secure ? secureEndpoint.getAddress() : nonSecureEndpoint.getAddress()).build();
Registration registration = new Registration.Builder("fakeregistrationid", endpointName, destination,
destination.isSecure() ? secureEndpoint.getAddress() : nonSecureEndpoint.getAddress()).build();
// Build LwM2m response
LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse,
registration, model, null, decoder);
Expand All @@ -81,7 +79,7 @@ public T buildResponse(Response coapResponse) {
coapRequest.addMessageObserver(syncMessageObserver);

// Send CoAP request asynchronously
if (secure)
if (destination.isSecure())
secureEndpoint.sendRequest(coapRequest);
else
nonSecureEndpoint.sendRequest(coapRequest);
Expand All @@ -91,11 +89,11 @@ public T buildResponse(Response coapResponse) {
}

@Override
public <T extends LwM2mResponse> void send(final String endpointName, final InetSocketAddress clientAddress,
final boolean secure, final DownlinkRequest<T> request, final long timeout,
ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
public <T extends LwM2mResponse> void send(final String endpointName, final Identity destination,
final DownlinkRequest<T> request, final long timeout, ResponseCallback<T> responseCallback,
ErrorCallback errorCallback) {
// Create the CoAP request from LwM2m request
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(clientAddress, model, encoder);
CoapRequestBuilder coapClientRequestBuilder = new CoapRequestBuilder(destination, model, encoder);
request.accept(coapClientRequestBuilder);
final Request coapRequest = coapClientRequestBuilder.getRequest();

Expand All @@ -105,9 +103,8 @@ public <T extends LwM2mResponse> void send(final String endpointName, final Inet
public T buildResponse(Response coapResponse) {
// TODO we need to fix that by removing the Client dependency from LwM2MResponseBuilder or by creating a
// LwM2mBootstrapResponseBuilder
Registration registration = new Registration.Builder("fakeregistrationid", endpointName,
clientAddress.getAddress(), clientAddress.getPort(),
secure ? secureEndpoint.getAddress() : nonSecureEndpoint.getAddress()).build();
Registration registration = new Registration.Builder("fakeregistrationid", endpointName, destination,
destination.isSecure() ? secureEndpoint.getAddress() : nonSecureEndpoint.getAddress()).build();

// Build LwM2m response
LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse,
Expand All @@ -119,7 +116,7 @@ public T buildResponse(Response coapResponse) {
coapRequest.addMessageObserver(obs);

// Send CoAP request asynchronously
if (secure)
if (destination.isSecure())
secureEndpoint.sendRequest(coapRequest);
else
nonSecureEndpoint.sendRequest(coapRequest);
Expand Down
Expand Up @@ -12,6 +12,7 @@
*
* Contributors:
* Sierra Wireless - initial API and implementation
* Achim Kraus (Bosch Software Innovations GmbH) - use Identity as destination
*******************************************************************************/
package org.eclipse.leshan.server.californium.impl;

Expand Down Expand Up @@ -76,8 +77,7 @@ public <T extends LwM2mResponse> T send(final Registration destination, final Do
final LwM2mModel model = modelProvider.getObjectModel(destination);

// Create the CoAP request from LwM2m request
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(
new InetSocketAddress(destination.getAddress(), destination.getPort()), destination.getRootPath(),
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(), destination.getRootPath(),
destination.getId(), destination.getEndpoint(), model, encoder);
request.accept(coapRequestBuilder);
final Request coapRequest = coapRequestBuilder.getRequest();
Expand Down Expand Up @@ -114,7 +114,7 @@ public <T extends LwM2mResponse> void send(final Registration destination, final

// Create the CoAP request from LwM2m request
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(
new InetSocketAddress(destination.getAddress(), destination.getPort()), destination.getRootPath(),
destination.getIdentity(), destination.getRootPath(),
destination.getId(), destination.getEndpoint(), model, encoder);
request.accept(coapRequestBuilder);
final Request coapRequest = coapRequestBuilder.getRequest();
Expand Down
Expand Up @@ -12,13 +12,17 @@
*
* Contributors:
* Sierra Wireless - initial API and implementation
* Achim Kraus (Bosch Software Innovations GmbH) - use Identity as destination
* and transform them to
* EndpointContext for requests
*******************************************************************************/
package org.eclipse.leshan.server.californium.impl;

import java.net.InetSocketAddress;

import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.elements.EndpointContext;
import org.eclipse.leshan.core.californium.ExchangeUtil;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mPath;
Expand All @@ -32,6 +36,7 @@
import org.eclipse.leshan.core.request.DiscoverRequest;
import org.eclipse.leshan.core.request.DownlinkRequestVisitor;
import org.eclipse.leshan.core.request.ExecuteRequest;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.core.request.ObserveRequest;
import org.eclipse.leshan.core.request.ReadRequest;
import org.eclipse.leshan.core.request.WriteAttributesRequest;
Expand All @@ -44,15 +49,15 @@ public class CoapRequestBuilder implements DownlinkRequestVisitor {
private Request coapRequest;

// client information
private final InetSocketAddress destination;
private final Identity destination;
private final String rootPath;
private final String registrationId;
private final String endpoint;

private final LwM2mModel model;
private final LwM2mNodeEncoder encoder;

public CoapRequestBuilder(InetSocketAddress destination, LwM2mModel model, LwM2mNodeEncoder encoder) {
public CoapRequestBuilder(Identity destination, LwM2mModel model, LwM2mNodeEncoder encoder) {
this.destination = destination;
this.rootPath = null;
this.registrationId = null;
Expand All @@ -61,9 +66,8 @@ public CoapRequestBuilder(InetSocketAddress destination, LwM2mModel model, LwM2m
this.encoder = encoder;
}

public CoapRequestBuilder(InetSocketAddress destination, String rootPath, String registrationId, String endpoint,
LwM2mModel model,
LwM2mNodeEncoder encoder) {
public CoapRequestBuilder(Identity destination, String rootPath, String registrationId, String endpoint,
LwM2mModel model, LwM2mNodeEncoder encoder) {
this.destination = destination;
this.rootPath = rootPath;
this.endpoint = endpoint;
Expand Down Expand Up @@ -155,16 +159,16 @@ public void visit(BootstrapWriteRequest request) {
public void visit(BootstrapDeleteRequest request) {
coapRequest = Request.newDelete();
coapRequest.setConfirmable(true);
coapRequest.setDestination(destination.getAddress());
coapRequest.setDestinationPort(destination.getPort());
EndpointContext context = ExchangeUtil.extractContext(destination);
coapRequest.setDestinationContext(context);
}

@Override
public void visit(BootstrapFinishRequest request) {
coapRequest = Request.newPost();
coapRequest.setConfirmable(true);
coapRequest.setDestination(destination.getAddress());
coapRequest.setDestinationPort(destination.getPort());
EndpointContext context = ExchangeUtil.extractContext(destination);
coapRequest.setDestinationContext(context);

// root path
if (rootPath != null) {
Expand All @@ -179,8 +183,8 @@ public void visit(BootstrapFinishRequest request) {
}

private final void setTarget(Request coapRequest, LwM2mPath path) {
coapRequest.setDestination(destination.getAddress());
coapRequest.setDestinationPort(destination.getPort());
EndpointContext context = ExchangeUtil.extractContext(destination);
coapRequest.setDestinationContext(context);

// root path
if (rootPath != null) {
Expand Down
Expand Up @@ -15,13 +15,15 @@
*******************************************************************************/
package org.eclipse.leshan.server.californium.impl;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import org.eclipse.leshan.LwM2m;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.server.registration.Registration;

public class CaliforniumTestSupport {
Expand All @@ -34,8 +36,8 @@ public class CaliforniumTestSupport {
public void givenASimpleClient() throws UnknownHostException {
registrationAddress = InetSocketAddress.createUnresolved("localhost", LwM2m.DEFAULT_COAP_PORT);

Registration.Builder builder = new Registration.Builder("ID", "urn:client", InetAddress.getLocalHost(), 10000,
registrationAddress);
Registration.Builder builder = new Registration.Builder("ID", "urn:client",
Identity.unsecure(Inet4Address.getLoopbackAddress(), 1000), registrationAddress);

registration = builder.build();
}
Expand Down

0 comments on commit 6e84a99

Please sign in to comment.