/
CaliforniumServerEndpoint.java
283 lines (244 loc) · 11.4 KB
/
CaliforniumServerEndpoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************************************
* Copyright (c) 2022 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.californium.endpoint;
import java.net.URI;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.californium.core.coap.MessageObserver;
import org.eclipse.californium.core.coap.MessageObserverAdapter;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.coap.Token;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.leshan.core.californium.AsyncRequestObserver;
import org.eclipse.leshan.core.californium.ExceptionTranslator;
import org.eclipse.leshan.core.californium.SyncRequestObserver;
import org.eclipse.leshan.core.californium.identity.IdentityHandler;
import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
import org.eclipse.leshan.core.endpoint.Protocol;
import org.eclipse.leshan.core.observation.Observation;
import org.eclipse.leshan.core.request.DownlinkRequest;
import org.eclipse.leshan.core.response.ErrorCallback;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.response.ObserveCompositeResponse;
import org.eclipse.leshan.core.response.ObserveResponse;
import org.eclipse.leshan.core.response.ResponseCallback;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.server.endpoint.LwM2mServerEndpoint;
import org.eclipse.leshan.server.endpoint.ServerEndpointToolbox;
import org.eclipse.leshan.server.observation.LwM2mNotificationReceiver;
import org.eclipse.leshan.server.profile.ClientProfile;
import org.eclipse.leshan.server.request.LowerLayerConfig;
public class CaliforniumServerEndpoint implements LwM2mServerEndpoint {
private final Protocol protocol;
private final String description;
private final ScheduledExecutorService executor;
private final CoapEndpoint endpoint;
private final ServerEndpointToolbox toolbox;
private final ServerCoapMessageTranslator translator;
private final LwM2mNotificationReceiver notificationReceiver;
private final IdentityHandler identityHandler;
private final ExceptionTranslator exceptionTranslator;
// A map which contains all ongoing CoAP requests
// This is used to be able to cancel request
private final ConcurrentNavigableMap<String/* sessionId#requestId */, Request /* ongoing coap Request */> ongoingRequests = new ConcurrentSkipListMap<>();
public CaliforniumServerEndpoint(Protocol protocol, String description, CoapEndpoint endpoint,
ServerCoapMessageTranslator translator, ServerEndpointToolbox toolbox,
LwM2mNotificationReceiver notificationReceiver, IdentityHandler identityHandler,
ExceptionTranslator exceptionTranslator, ScheduledExecutorService executor) {
this.protocol = protocol;
this.description = description;
this.translator = translator;
this.toolbox = toolbox;
this.endpoint = endpoint;
this.notificationReceiver = notificationReceiver;
this.identityHandler = identityHandler;
this.exceptionTranslator = exceptionTranslator;
this.executor = executor;
}
@Override
public Protocol getProtocol() {
return protocol;
}
@Override
public URI getURI() {
return EndpointUriUtil.createUri(protocol.getUriScheme(), endpoint.getAddress());
}
@Override
public String getDescription() {
return description;
}
public CoapEndpoint getCoapEndpoint() {
return endpoint;
}
@Override
public <T extends LwM2mResponse> T send(ClientProfile destination, DownlinkRequest<T> lwm2mRequest,
LowerLayerConfig lowerLayerConfig, long timeoutInMs) throws InterruptedException {
// Create the CoAP request from LwM2m request
final Request coapRequest = translator.createCoapRequest(destination, lwm2mRequest, toolbox, identityHandler);
// Apply customSetting
if (lowerLayerConfig != null) {
lowerLayerConfig.apply(coapRequest);
}
// Send CoAP request synchronously
SyncRequestObserver<T> syncMessageObserver = new SyncRequestObserver<T>(coapRequest, timeoutInMs,
exceptionTranslator) {
@Override
public T buildResponse(Response coapResponse) {
// Build LwM2m response
T lwM2mResponse = translator.createLwM2mResponse(destination, lwm2mRequest, coapRequest, coapResponse,
toolbox);
// Handle special observe case
if (lwM2mResponse != null && lwM2mResponse.isSuccess()) {
Observation observation = null;
if (lwM2mResponse instanceof ObserveResponse) {
observation = ((ObserveResponse) lwM2mResponse).getObservation();
} else if (lwM2mResponse instanceof ObserveCompositeResponse) {
observation = ((ObserveCompositeResponse) lwM2mResponse).getObservation();
}
if (observation != null) {
notificationReceiver.newObservation(observation, destination.getRegistration());
}
}
return lwM2mResponse;
}
};
coapRequest.addMessageObserver(syncMessageObserver);
// Store pending request to be able to cancel it later
addOngoingRequest(destination.getRegistrationId(), coapRequest);
// Send CoAP request asynchronously
endpoint.sendRequest(coapRequest);
// Wait for response, then return it
return syncMessageObserver.waitForResponse();
}
@Override
public <T extends LwM2mResponse> void send(ClientProfile destination, DownlinkRequest<T> lwm2mRequest,
ResponseCallback<T> responseCallback, ErrorCallback errorCallback, LowerLayerConfig lowerLayerConfig,
long timeoutInMs) {
Validate.notNull(responseCallback);
Validate.notNull(errorCallback);
// Create the CoAP request from LwM2m request
final Request coapRequest = translator.createCoapRequest(destination, lwm2mRequest, toolbox, identityHandler);
// Apply customSetting
if (lowerLayerConfig != null) {
lowerLayerConfig.apply(coapRequest);
}
// Add CoAP request callback
MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, timeoutInMs,
executor, exceptionTranslator) {
@Override
public T buildResponse(Response coapResponse) {
// Build LwM2m response
T lwM2mResponse = translator.createLwM2mResponse(destination, lwm2mRequest, coapRequest, coapResponse,
toolbox);
// Handle special observe case
if (lwM2mResponse != null && lwM2mResponse.isSuccess()) {
Observation observation = null;
if (lwM2mResponse instanceof ObserveResponse) {
observation = ((ObserveResponse) lwM2mResponse).getObservation();
} else if (lwM2mResponse instanceof ObserveCompositeResponse) {
observation = ((ObserveCompositeResponse) lwM2mResponse).getObservation();
}
if (observation != null) {
notificationReceiver.newObservation(observation, destination.getRegistration());
}
}
return lwM2mResponse;
}
};
coapRequest.addMessageObserver(obs);
// Store pending request to be able to cancel it later
addOngoingRequest(destination.getRegistrationId(), coapRequest);
// Send CoAP request asynchronously
endpoint.sendRequest(coapRequest);
}
@Override
public void cancelObservation(Observation observation) {
endpoint.cancelObservation(new Token(observation.getId().getBytes()));
}
/**
* Cancel all ongoing requests for the given sessionID.
*
* @param sessionID the Id associated to the ongoing requests you want to cancel.
*
* @see "All others send methods."
*/
@Override
public void cancelRequests(String sessionID) {
Validate.notNull(sessionID);
SortedMap<String, Request> requests = ongoingRequests.subMap(getFloorKey(sessionID), getCeilingKey(sessionID));
for (Request coapRequest : requests.values()) {
coapRequest.cancel();
}
requests.clear();
}
private static String getFloorKey(String sessionID) {
// The key format is sessionid#long, So we need a key which is always before this pattern (in natural order).
return sessionID + '#';
}
private static String getCeilingKey(String sessionID) {
// The key format is sessionid#long, So we need a key which is always after this pattern (in natural order).
return sessionID + "#A";
}
private static String getKey(String sessionID, long requestId) {
return sessionID + '#' + requestId;
}
private void addOngoingRequest(String sessionID, Request coapRequest) {
if (sessionID != null) {
CleanerMessageObserver observer = new CleanerMessageObserver(sessionID, coapRequest);
coapRequest.addMessageObserver(observer);
ongoingRequests.put(observer.getRequestKey(), coapRequest);
}
}
private void removeOngoingRequest(String key, Request coapRequest) {
Validate.notNull(key);
ongoingRequests.remove(key, coapRequest);
}
private final AtomicLong idGenerator = new AtomicLong(0l);
private class CleanerMessageObserver extends MessageObserverAdapter {
private final String requestKey;
private final Request coapRequest;
public CleanerMessageObserver(String sessionID, Request coapRequest) {
super();
requestKey = getKey(sessionID, idGenerator.incrementAndGet());
this.coapRequest = coapRequest;
}
public String getRequestKey() {
return requestKey;
}
@Override
public void onRetransmission() {
}
@Override
public void onResponse(Response response) {
removeOngoingRequest(requestKey, coapRequest);
}
@Override
public void onAcknowledgement() {
}
@Override
protected void failed() {
removeOngoingRequest(requestKey, coapRequest);
}
@Override
public void onCancel() {
removeOngoingRequest(requestKey, coapRequest);
}
}
}