Skip to content

Commit

Permalink
Rename ServerIdentity in LwM2mServer at client side.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 3, 2023
1 parent f994bc1 commit 1186c01
Show file tree
Hide file tree
Showing 60 changed files with 567 additions and 575 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package org.eclipse.leshan.client.californium;

import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.client.servers.LwM2mServer;

public interface CaliforniumConnectionController {
void forceReconnection(Endpoint endpoint, ServerIdentity identity, boolean resume);
void forceReconnection(Endpoint endpoint, LwM2mServer server, boolean resume);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.californium.core.network.Exchange;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.leshan.client.californium.endpoint.ServerIdentityExtractor;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.core.californium.LwM2mCoapResource;
import org.eclipse.leshan.core.californium.identity.IdentityHandlerProvider;
import org.eclipse.leshan.core.peer.IpPeer;
Expand All @@ -40,12 +40,12 @@ public LwM2mClientCoapResource(String name, IdentityHandlerProvider identityHand
}

/**
* Extract the {@link ServerIdentity} for this exchange. If there is no corresponding server currently in
* communication with this client. Answer with an {@link ResponseCode#INTERNAL_SERVER_ERROR}.
* Extract the {@link LwM2mServer} for this exchange. If there is no corresponding server currently in communication
* with this client. Answer with an {@link ResponseCode#INTERNAL_SERVER_ERROR}.
*/
protected ServerIdentity getServerOrRejectRequest(CoapExchange exchange, Message receivedMessage) {
protected LwM2mServer getServerOrRejectRequest(CoapExchange exchange, Message receivedMessage) {
// search if we are in communication with this server.
ServerIdentity server = extractIdentity(exchange.advanced(), receivedMessage);
LwM2mServer server = extractIdentity(exchange.advanced(), receivedMessage);
if (server != null)
return server;

Expand All @@ -54,16 +54,16 @@ protected ServerIdentity getServerOrRejectRequest(CoapExchange exchange, Message
}

/**
* Get Leshan {@link ServerIdentity} from Californium {@link Exchange}.
* Get Leshan {@link LwM2mServer} from Californium {@link Exchange}.
*
* @param exchange The Californium {@link Exchange} containing the request for which we search sender identity.
* @return The corresponding Leshan {@link ServerIdentity}.
* @throws IllegalStateException if we are not able to extract {@link ServerIdentity}.
* @return The corresponding Leshan {@link LwM2mServer}.
* @throws IllegalStateException if we are not able to extract {@link LwM2mServer}.
*/
protected ServerIdentity extractIdentity(Exchange exchange, Message receivedMessage) {
IpPeer foreignPeerIdentity = getForeignPeerIdentity(exchange, receivedMessage);
if (foreignPeerIdentity == null)
protected LwM2mServer extractIdentity(Exchange exchange, Message receivedMessage) {
IpPeer foreignPeer = getForeignPeerIdentity(exchange, receivedMessage);
if (foreignPeer == null)
return null;
return serverIdentityExtractor.extractIdentity(exchange, foreignPeerIdentity);
return serverIdentityExtractor.extractIdentity(exchange, foreignPeer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.eclipse.leshan.client.californium.endpoint.ServerIdentityExtractor;
import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox;
import org.eclipse.leshan.client.request.DownlinkRequestReceiver;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.core.californium.ObserveUtil;
import org.eclipse.leshan.core.californium.identity.IdentityHandlerProvider;
import org.eclipse.leshan.core.node.LwM2mNode;
Expand Down Expand Up @@ -73,14 +73,14 @@ public RootResource(IdentityHandlerProvider identityHandlerProvider,
public void handleGET(CoapExchange exchange) {
// Manage Bootstrap Discover Request
Request coapRequest = exchange.advanced().getRequest();
ServerIdentity identity = getServerOrRejectRequest(exchange, coapRequest);
if (identity == null)
LwM2mServer server = getServerOrRejectRequest(exchange, coapRequest);
if (server == null)
return;

String URI = exchange.getRequestOptions().getUriPathString();

BootstrapDiscoverResponse response = requestReceiver
.requestReceived(identity, new BootstrapDiscoverRequest(URI, coapRequest)).getResponse();
.requestReceived(server, new BootstrapDiscoverRequest(URI, coapRequest)).getResponse();
if (response.getCode().isError()) {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
Expand All @@ -94,8 +94,8 @@ public void handleGET(CoapExchange exchange) {
@Override
public void handleFETCH(CoapExchange exchange) {
Request coapRequest = exchange.advanced().getRequest();
ServerIdentity identity = getServerOrRejectRequest(exchange, coapRequest);
if (identity == null)
LwM2mServer server = getServerOrRejectRequest(exchange, coapRequest);
if (server == null)
return;

// Handle content format for the response
Expand All @@ -121,7 +121,7 @@ public void handleFETCH(CoapExchange exchange) {
// Manage Observe Composite request
ObserveCompositeRequest observeRequest = new ObserveCompositeRequest(requestContentFormat,
responseContentFormat, paths, coapRequest);
ObserveCompositeResponse response = requestReceiver.requestReceived(identity, observeRequest).getResponse();
ObserveCompositeResponse response = requestReceiver.requestReceived(server, observeRequest).getResponse();

updateUserContextWithPaths(coapRequest, paths);

Expand All @@ -137,7 +137,7 @@ public void handleFETCH(CoapExchange exchange) {
} else {
// Manage Read Composite request
ReadCompositeResponse response = requestReceiver
.requestReceived(identity,
.requestReceived(server,
new ReadCompositeRequest(paths, requestContentFormat, responseContentFormat, coapRequest))
.getResponse();
if (response.getCode().isError()) {
Expand Down Expand Up @@ -166,8 +166,8 @@ private void updateUserContextWithPaths(Request coapRequest, List<LwM2mPath> pat
public void handleIPATCH(CoapExchange exchange) {
// Manage Read Composite request
Request coapRequest = exchange.advanced().getRequest();
ServerIdentity identity = getServerOrRejectRequest(exchange, coapRequest);
if (identity == null)
LwM2mServer server = getServerOrRejectRequest(exchange, coapRequest);
if (server == null)
return;

// Handle content format
Expand All @@ -181,7 +181,7 @@ public void handleIPATCH(CoapExchange exchange) {
null, toolbox.getModel());

WriteCompositeResponse response = requestReceiver
.requestReceived(identity, new WriteCompositeRequest(contentFormat, nodes, coapRequest)).getResponse();
.requestReceived(server, new WriteCompositeRequest(contentFormat, nodes, coapRequest)).getResponse();
if (response.getCode().isError()) {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
Expand All @@ -199,12 +199,12 @@ public void handleDELETE(CoapExchange exchange) {
}

Request coapRequest = exchange.advanced().getRequest();
ServerIdentity identity = getServerOrRejectRequest(exchange, coapRequest);
if (identity == null)
LwM2mServer server = getServerOrRejectRequest(exchange, coapRequest);
if (server == null)
return;

BootstrapDeleteResponse response = requestReceiver
.requestReceived(identity, new BootstrapDeleteRequest(URI, coapRequest)).getResponse();
.requestReceived(server, new BootstrapDeleteRequest(URI, coapRequest)).getResponse();
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.eclipse.leshan.client.californium.LwM2mClientCoapResource;
import org.eclipse.leshan.client.californium.endpoint.ServerIdentityExtractor;
import org.eclipse.leshan.client.request.DownlinkRequestReceiver;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.core.californium.identity.IdentityHandlerProvider;
import org.eclipse.leshan.core.request.BootstrapFinishRequest;
import org.eclipse.leshan.core.response.BootstrapFinishResponse;
Expand All @@ -51,13 +51,13 @@ public BootstrapResource(IdentityHandlerProvider identityHandlerProvider,
public void handlePOST(CoapExchange exchange) {
// Handle bootstrap request
Request coapRequest = exchange.advanced().getRequest();
ServerIdentity identity = getServerOrRejectRequest(exchange, coapRequest);
if (identity == null)
LwM2mServer server = getServerOrRejectRequest(exchange, coapRequest);
if (server == null)
return;

// Acknowledge bootstrap finished request
exchange.accept();
final SendableResponse<BootstrapFinishResponse> sendableResponse = requestReceiver.requestReceived(identity,
final SendableResponse<BootstrapFinishResponse> sendableResponse = requestReceiver.requestReceived(server,
new BootstrapFinishRequest(coapRequest));

// Create CoAP response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.eclipse.leshan.client.californium.CaliforniumConnectionController;
import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox;
import org.eclipse.leshan.client.endpoint.LwM2mClientEndpoint;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.core.californium.AsyncRequestObserver;
import org.eclipse.leshan.core.californium.ExceptionTranslator;
import org.eclipse.leshan.core.californium.SyncRequestObserver;
Expand Down Expand Up @@ -112,7 +112,7 @@ public long getMaxCommunicationPeriodFor(long lifetimeInMs) {
}

@Override
public <T extends LwM2mResponse> T send(ServerIdentity server, UplinkRequest<T> lwm2mRequest, long timeoutInMs)
public <T extends LwM2mResponse> T send(LwM2mServer server, UplinkRequest<T> lwm2mRequest, long timeoutInMs)
throws InterruptedException {
// Create the CoAP request from LwM2m request
final Request coapRequest = translator.createCoapRequest(server, lwm2mRequest, toolbox, model, identityHandler);
Expand Down Expand Up @@ -140,7 +140,7 @@ public T buildResponse(Response coapResponse) {
}

@Override
public <T extends LwM2mResponse> void send(ServerIdentity server, UplinkRequest<T> lwm2mRequest,
public <T extends LwM2mResponse> void send(LwM2mServer server, UplinkRequest<T> lwm2mRequest,
ResponseCallback<T> responseCallback, ErrorCallback errorCallback, long timeoutInMs) {
Validate.notNull(responseCallback);
Validate.notNull(errorCallback);
Expand All @@ -167,7 +167,7 @@ public T buildResponse(Response coapResponse) {
}

@Override
public void forceReconnection(ServerIdentity server, boolean resume) {
public void forceReconnection(LwM2mServer server, boolean resume) {
connectionController.forceReconnection(endpoint, server, resume);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.eclipse.leshan.client.endpoint.LwM2mClientEndpointsProvider;
import org.eclipse.leshan.client.request.DownlinkRequestReceiver;
import org.eclipse.leshan.client.resource.LwM2mObjectTree;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.client.servers.ServerInfo;
import org.eclipse.leshan.core.SecurityMode;
import org.eclipse.leshan.core.californium.identity.IdentityHandler;
Expand Down Expand Up @@ -77,7 +77,7 @@ public class CaliforniumClientEndpointsProvider implements LwM2mClientEndpointsP
private final InetAddress clientAddress;

// we support only 1 endpoint at a time by
private ServerIdentity currentServer;
private LwM2mServer currentServer;
private CaliforniumClientEndpoint endpoint;
private CoapServer coapServer;

Expand All @@ -97,7 +97,7 @@ protected CaliforniumClientEndpointsProvider(Builder builder) {
identityExtrator = new ServerIdentityExtractor() {

@Override
public ServerIdentity extractIdentity(Exchange exchange, IpPeer foreignPeerIdentity) {
public LwM2mServer extractIdentity(Exchange exchange, IpPeer foreignPeer) {
// TODO support multi server
Endpoint currentCoapEndpoint = endpoint.getCoapEndpoint();

Expand All @@ -115,7 +115,7 @@ public ServerIdentity extractIdentity(Exchange exchange, IpPeer foreignPeerIdent
if (endpoint.getProtocol().equals(Protocol.COAP)) {
if (currentServer.getTransportData() instanceof IpPeer) {
IpPeer currentIpServer = (IpPeer) currentServer.getTransportData();
if (!(currentIpServer.getSocketAddress().equals(foreignPeerIdentity.getSocketAddress()))) {
if (!(currentIpServer.getSocketAddress().equals(foreignPeer.getSocketAddress()))) {
return null;
}
} else {
Expand All @@ -126,14 +126,13 @@ public ServerIdentity extractIdentity(Exchange exchange, IpPeer foreignPeerIdent
}
// For OSCORE, be sure OSCORE is used.
if (currentServer.getTransportData().getIdentity() instanceof OscoreIdentity) {
if (!(foreignPeerIdentity.getIdentity() instanceof OscoreIdentity) //
if (!(foreignPeer.getIdentity() instanceof OscoreIdentity) //
// we also check OscoreIdentity but this is probably not useful
// because we are using static OSCOREstore which holds only 1 OscoreParameter,
// so if the request was successfully decrypted and OSCORE is used, this MUST be the
// right
// server.
|| !foreignPeerIdentity.getIdentity()
.equals(currentServer.getTransportData().getIdentity())) {
|| !foreignPeer.getIdentity().equals(currentServer.getTransportData().getIdentity())) {
return null;
}
}
Expand Down Expand Up @@ -165,8 +164,8 @@ protected Resource createRoot() {
}

@Override
public ServerIdentity createEndpoint(ServerInfo serverInfo, boolean clientInitiatedOnly,
List<Certificate> trustStore, ClientEndpointToolbox toolbox) {
public LwM2mServer createEndpoint(ServerInfo serverInfo, boolean clientInitiatedOnly, List<Certificate> trustStore,
ClientEndpointToolbox toolbox) {

// create endpoints
for (CaliforniumClientEndpointFactory endpointFactory : endpointsFactory) {
Expand Down Expand Up @@ -208,39 +207,39 @@ public ServerIdentity createEndpoint(ServerInfo serverInfo, boolean clientInitia
return null;
}

private ServerIdentity extractIdentity(ServerInfo serverInfo) {
IpPeer serverIdentity;
private LwM2mServer extractIdentity(ServerInfo serverInfo) {
IpPeer transportData;
if (serverInfo.isSecure()) {
// Support PSK
if (serverInfo.secureMode == SecurityMode.PSK) {
serverIdentity = new IpPeer(serverInfo.getAddress(), new PskIdentity(serverInfo.pskId));
transportData = new IpPeer(serverInfo.getAddress(), new PskIdentity(serverInfo.pskId));
} else if (serverInfo.secureMode == SecurityMode.RPK) {
serverIdentity = new IpPeer(serverInfo.getAddress(), new RpkIdentity(serverInfo.serverPublicKey));
transportData = new IpPeer(serverInfo.getAddress(), new RpkIdentity(serverInfo.serverPublicKey));
} else if (serverInfo.secureMode == SecurityMode.X509) {
// TODO We set CN with '*' as we are not able to know the CN for some certificate usage and so this is
// not used anymore to identify a server with x509.
// See : https://github.com/eclipse/leshan/issues/992
serverIdentity = new IpPeer(serverInfo.getAddress(), new X509Identity("*"));
transportData = new IpPeer(serverInfo.getAddress(), new X509Identity("*"));
} else {
throw new RuntimeException("Unable to create connector : unsupported security mode");
}
} else if (serverInfo.useOscore) {
// Build server identity for OSCORE
serverIdentity = new IpPeer(serverInfo.getAddress(),
transportData = new IpPeer(serverInfo.getAddress(),
new OscoreIdentity(serverInfo.oscoreSetting.getRecipientId()));
} else {
serverIdentity = new IpPeer((serverInfo.getAddress()));
transportData = new IpPeer((serverInfo.getAddress()));
}

if (serverInfo.bootstrap) {
return new ServerIdentity(serverIdentity, serverInfo.serverUri);
return new LwM2mServer(transportData, serverInfo.serverUri);
} else {
return new ServerIdentity(serverIdentity, serverInfo.serverId, serverInfo.serverUri);
return new LwM2mServer(transportData, serverInfo.serverId, serverInfo.serverUri);
}
}

@Override
public Collection<ServerIdentity> createEndpoints(Collection<? extends ServerInfo> serverInfo,
public Collection<LwM2mServer> createEndpoints(Collection<? extends ServerInfo> serverInfo,
boolean clientInitiatedOnly, List<Certificate> trustStore, ClientEndpointToolbox toolbox) {
// TODO TL : need to be implemented or removed ?
return null;
Expand All @@ -256,7 +255,7 @@ public void destroyEndpoints() {
}

@Override
public LwM2mClientEndpoint getEndpoint(ServerIdentity server) {
public LwM2mClientEndpoint getEndpoint(LwM2mServer server) {
if (currentServer.equals(server)) {
return endpoint;
}
Expand Down
Loading

0 comments on commit 1186c01

Please sign in to comment.