|
@@ -65,6 +65,7 @@ import java.util.concurrent.ConcurrentHashMap; |
|
@@ -65,6 +65,7 @@ import java.util.concurrent.ConcurrentHashMap; |
65
|
import java.util.concurrent.ConcurrentMap;
|
65
|
import java.util.concurrent.ConcurrentMap;
|
66
|
import java.util.concurrent.TimeUnit;
|
66
|
import java.util.concurrent.TimeUnit;
|
67
|
import java.util.concurrent.atomic.AtomicInteger;
|
67
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
68
|
+import java.util.stream.Collectors;
|
68
|
|
69
|
|
69
|
@Slf4j
|
70
|
@Slf4j
|
70
|
public class CoapTransportResource extends AbstractCoapTransportResource {
|
71
|
public class CoapTransportResource extends AbstractCoapTransportResource {
|
|
@@ -76,9 +77,8 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -76,9 +77,8 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
76
|
private static final int REQUEST_ID_POSITION_CERTIFICATE_REQUEST = 4;
|
77
|
private static final int REQUEST_ID_POSITION_CERTIFICATE_REQUEST = 4;
|
77
|
private static final String DTLS_SESSION_ID_KEY = "DTLS_SESSION_ID";
|
78
|
private static final String DTLS_SESSION_ID_KEY = "DTLS_SESSION_ID";
|
78
|
|
79
|
|
79
|
- private final ConcurrentMap<String, TransportProtos.SessionInfoProto> tokenToSessionInfoMap = new ConcurrentHashMap<>();
|
|
|
80
|
- private final ConcurrentMap<String, AtomicInteger> tokenToObserveNotificationSeqMap = new ConcurrentHashMap<>();
|
|
|
81
|
- private final ConcurrentMap<TransportProtos.SessionInfoProto, ObserveRelation> sessionInfoToObserveRelationMap = new ConcurrentHashMap<>();
|
80
|
+ private final ConcurrentMap<String, CoapObserveSessionInfo> tokenToCoapSessionInfoMap = new ConcurrentHashMap<>();
|
|
|
81
|
+ private final ConcurrentMap<CoapObserveSessionInfo, ObserveRelation> sessionInfoToObserveRelationMap = new ConcurrentHashMap<>();
|
82
|
private final Set<UUID> rpcSubscriptions = ConcurrentHashMap.newKeySet();
|
82
|
private final Set<UUID> rpcSubscriptions = ConcurrentHashMap.newKeySet();
|
83
|
private final Set<UUID> attributeSubscriptions = ConcurrentHashMap.newKeySet();
|
83
|
private final Set<UUID> attributeSubscriptions = ConcurrentHashMap.newKeySet();
|
84
|
|
84
|
|
|
@@ -94,7 +94,11 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -94,7 +94,11 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
94
|
this.timeout = coapServerService.getTimeout();
|
94
|
this.timeout = coapServerService.getTimeout();
|
95
|
this.sessionReportTimeout = ctx.getSessionReportTimeout();
|
95
|
this.sessionReportTimeout = ctx.getSessionReportTimeout();
|
96
|
ctx.getScheduler().scheduleAtFixedRate(() -> {
|
96
|
ctx.getScheduler().scheduleAtFixedRate(() -> {
|
97
|
- Set<TransportProtos.SessionInfoProto> observeSessions = sessionInfoToObserveRelationMap.keySet();
|
97
|
+ Set<CoapObserveSessionInfo> coapObserveSessionInfos = sessionInfoToObserveRelationMap.keySet();
|
|
|
98
|
+ Set<TransportProtos.SessionInfoProto> observeSessions = coapObserveSessionInfos
|
|
|
99
|
+ .stream()
|
|
|
100
|
+ .map(CoapObserveSessionInfo::getSessionInfoProto)
|
|
|
101
|
+ .collect(Collectors.toSet());
|
98
|
observeSessions.forEach(this::reportActivity);
|
102
|
observeSessions.forEach(this::reportActivity);
|
99
|
}, new Random().nextInt((int) sessionReportTimeout), sessionReportTimeout, TimeUnit.MILLISECONDS);
|
103
|
}, new Random().nextInt((int) sessionReportTimeout), sessionReportTimeout, TimeUnit.MILLISECONDS);
|
100
|
}
|
104
|
}
|
|
@@ -112,17 +116,17 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -112,17 +116,17 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
112
|
relation.setEstablished();
|
116
|
relation.setEstablished();
|
113
|
addObserveRelation(relation);
|
117
|
addObserveRelation(relation);
|
114
|
}
|
118
|
}
|
115
|
- AtomicInteger notificationCounter = tokenToObserveNotificationSeqMap.computeIfAbsent(token, s -> new AtomicInteger(0));
|
|
|
116
|
- response.getOptions().setObserve(notificationCounter.getAndIncrement());
|
119
|
+ AtomicInteger observeNotificationCounter = tokenToCoapSessionInfoMap.get(token).getObserveNotificationCounter();
|
|
|
120
|
+ response.getOptions().setObserve(observeNotificationCounter.getAndIncrement());
|
117
|
} // ObserveLayer takes care of the else case
|
121
|
} // ObserveLayer takes care of the else case
|
118
|
}
|
122
|
}
|
119
|
|
123
|
|
120
|
- public void clearAndNotifyObserveRelation(ObserveRelation relation, CoAP.ResponseCode code) {
|
124
|
+ private void clearAndNotifyObserveRelation(ObserveRelation relation, CoAP.ResponseCode code) {
|
121
|
relation.cancel();
|
125
|
relation.cancel();
|
122
|
relation.getExchange().sendResponse(new Response(code));
|
126
|
relation.getExchange().sendResponse(new Response(code));
|
123
|
}
|
127
|
}
|
124
|
|
128
|
|
125
|
- public Map<TransportProtos.SessionInfoProto, ObserveRelation> getSessionInfoToObserveRelationMap() {
|
129
|
+ private Map<CoapObserveSessionInfo, ObserveRelation> getCoapSessionInfoToObserveRelationMap() {
|
126
|
return sessionInfoToObserveRelationMap;
|
130
|
return sessionInfoToObserveRelationMap;
|
127
|
}
|
131
|
}
|
128
|
|
132
|
|
|
@@ -278,8 +282,8 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -278,8 +282,8 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
278
|
new CoapOkCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
|
282
|
new CoapOkCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
|
279
|
break;
|
283
|
break;
|
280
|
case SUBSCRIBE_ATTRIBUTES_REQUEST:
|
284
|
case SUBSCRIBE_ATTRIBUTES_REQUEST:
|
281
|
- TransportProtos.SessionInfoProto currentAttrSession = tokenToSessionInfoMap.get(getTokenFromRequest(request));
|
|
|
282
|
- if (currentAttrSession == null) {
|
285
|
+ CoapObserveSessionInfo currentCoapObserveAttrSessionInfo = tokenToCoapSessionInfoMap.get(getTokenFromRequest(request));
|
|
|
286
|
+ if (currentCoapObserveAttrSessionInfo == null) {
|
283
|
attributeSubscriptions.add(sessionId);
|
287
|
attributeSubscriptions.add(sessionId);
|
284
|
registerAsyncCoapSession(exchange, sessionInfo, coapTransportAdaptor,
|
288
|
registerAsyncCoapSession(exchange, sessionInfo, coapTransportAdaptor,
|
285
|
transportConfigurationContainer.getRpcRequestDynamicMessageBuilder(), getTokenFromRequest(request));
|
289
|
transportConfigurationContainer.getRpcRequestDynamicMessageBuilder(), getTokenFromRequest(request));
|
|
@@ -291,20 +295,20 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -291,20 +295,20 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
291
|
}
|
295
|
}
|
292
|
break;
|
296
|
break;
|
293
|
case UNSUBSCRIBE_ATTRIBUTES_REQUEST:
|
297
|
case UNSUBSCRIBE_ATTRIBUTES_REQUEST:
|
294
|
- TransportProtos.SessionInfoProto attrSession = lookupAsyncSessionInfo(getTokenFromRequest(request));
|
|
|
295
|
- if (attrSession != null) {
|
298
|
+ CoapObserveSessionInfo coapObserveAttrSessionInfo = lookupAsyncSessionInfo(getTokenFromRequest(request));
|
|
|
299
|
+ if (coapObserveAttrSessionInfo != null) {
|
|
|
300
|
+ TransportProtos.SessionInfoProto attrSession = coapObserveAttrSessionInfo.getSessionInfoProto();
|
296
|
UUID attrSessionId = toSessionId(attrSession);
|
301
|
UUID attrSessionId = toSessionId(attrSession);
|
297
|
attributeSubscriptions.remove(attrSessionId);
|
302
|
attributeSubscriptions.remove(attrSessionId);
|
298
|
- sessionInfoToObserveRelationMap.remove(attrSession);
|
|
|
299
|
transportService.process(attrSession,
|
303
|
transportService.process(attrSession,
|
300
|
TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().setUnsubscribe(true).build(),
|
304
|
TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().setUnsubscribe(true).build(),
|
301
|
- new CoapOkCallback(exchange, CoAP.ResponseCode.DELETED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
|
|
|
302
|
- closeAndDeregister(sessionInfo);
|
305
|
+ new CoapNoOpCallback(exchange));
|
303
|
}
|
306
|
}
|
|
|
307
|
+ closeAndDeregister(sessionInfo);
|
304
|
break;
|
308
|
break;
|
305
|
case SUBSCRIBE_RPC_COMMANDS_REQUEST:
|
309
|
case SUBSCRIBE_RPC_COMMANDS_REQUEST:
|
306
|
- TransportProtos.SessionInfoProto currentRpcSession = tokenToSessionInfoMap.get(getTokenFromRequest(request));
|
|
|
307
|
- if (currentRpcSession == null) {
|
310
|
+ CoapObserveSessionInfo currentCoapObserveRpcSessionInfo = tokenToCoapSessionInfoMap.get(getTokenFromRequest(request));
|
|
|
311
|
+ if (currentCoapObserveRpcSessionInfo == null) {
|
308
|
rpcSubscriptions.add(sessionId);
|
312
|
rpcSubscriptions.add(sessionId);
|
309
|
registerAsyncCoapSession(exchange, sessionInfo, coapTransportAdaptor,
|
313
|
registerAsyncCoapSession(exchange, sessionInfo, coapTransportAdaptor,
|
310
|
transportConfigurationContainer.getRpcRequestDynamicMessageBuilder(), getTokenFromRequest(request));
|
314
|
transportConfigurationContainer.getRpcRequestDynamicMessageBuilder(), getTokenFromRequest(request));
|
|
@@ -315,16 +319,16 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -315,16 +319,16 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
315
|
}
|
319
|
}
|
316
|
break;
|
320
|
break;
|
317
|
case UNSUBSCRIBE_RPC_COMMANDS_REQUEST:
|
321
|
case UNSUBSCRIBE_RPC_COMMANDS_REQUEST:
|
318
|
- TransportProtos.SessionInfoProto rpcSession = lookupAsyncSessionInfo(getTokenFromRequest(request));
|
|
|
319
|
- if (rpcSession != null) {
|
322
|
+ CoapObserveSessionInfo coapObserveRpcSessionInfo = lookupAsyncSessionInfo(getTokenFromRequest(request));
|
|
|
323
|
+ if (coapObserveRpcSessionInfo != null) {
|
|
|
324
|
+ TransportProtos.SessionInfoProto rpcSession = coapObserveRpcSessionInfo.getSessionInfoProto();
|
320
|
UUID rpcSessionId = toSessionId(rpcSession);
|
325
|
UUID rpcSessionId = toSessionId(rpcSession);
|
321
|
rpcSubscriptions.remove(rpcSessionId);
|
326
|
rpcSubscriptions.remove(rpcSessionId);
|
322
|
- sessionInfoToObserveRelationMap.remove(rpcSession);
|
|
|
323
|
transportService.process(rpcSession,
|
327
|
transportService.process(rpcSession,
|
324
|
TransportProtos.SubscribeToRPCMsg.newBuilder().setUnsubscribe(true).build(),
|
328
|
TransportProtos.SubscribeToRPCMsg.newBuilder().setUnsubscribe(true).build(),
|
325
|
new CoapOkCallback(exchange, CoAP.ResponseCode.DELETED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
|
329
|
new CoapOkCallback(exchange, CoAP.ResponseCode.DELETED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
|
326
|
- closeAndDeregister(sessionInfo);
|
|
|
327
|
}
|
330
|
}
|
|
|
331
|
+ closeAndDeregister(sessionInfo);
|
328
|
break;
|
332
|
break;
|
329
|
case TO_DEVICE_RPC_RESPONSE:
|
333
|
case TO_DEVICE_RPC_RESPONSE:
|
330
|
transportService.process(sessionInfo,
|
334
|
transportService.process(sessionInfo,
|
|
@@ -356,13 +360,12 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -356,13 +360,12 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
356
|
return new UUID(sessionInfoProto.getSessionIdMSB(), sessionInfoProto.getSessionIdLSB());
|
360
|
return new UUID(sessionInfoProto.getSessionIdMSB(), sessionInfoProto.getSessionIdLSB());
|
357
|
}
|
361
|
}
|
358
|
|
362
|
|
359
|
- private TransportProtos.SessionInfoProto lookupAsyncSessionInfo(String token) {
|
|
|
360
|
- tokenToObserveNotificationSeqMap.remove(token);
|
|
|
361
|
- return tokenToSessionInfoMap.remove(token);
|
363
|
+ private CoapObserveSessionInfo lookupAsyncSessionInfo(String token) {
|
|
|
364
|
+ return tokenToCoapSessionInfoMap.remove(token);
|
362
|
}
|
365
|
}
|
363
|
|
366
|
|
364
|
private void registerAsyncCoapSession(CoapExchange exchange, TransportProtos.SessionInfoProto sessionInfo, CoapTransportAdaptor coapTransportAdaptor, DynamicMessage.Builder rpcRequestDynamicMessageBuilder, String token) {
|
367
|
private void registerAsyncCoapSession(CoapExchange exchange, TransportProtos.SessionInfoProto sessionInfo, CoapTransportAdaptor coapTransportAdaptor, DynamicMessage.Builder rpcRequestDynamicMessageBuilder, String token) {
|
365
|
- tokenToSessionInfoMap.putIfAbsent(token, sessionInfo);
|
368
|
+ tokenToCoapSessionInfoMap.putIfAbsent(token, new CoapObserveSessionInfo(sessionInfo));
|
366
|
transportService.registerAsyncSession(sessionInfo, getCoapSessionListener(exchange, coapTransportAdaptor, rpcRequestDynamicMessageBuilder, sessionInfo));
|
369
|
transportService.registerAsyncSession(sessionInfo, getCoapSessionListener(exchange, coapTransportAdaptor, rpcRequestDynamicMessageBuilder, sessionInfo));
|
367
|
transportService.process(sessionInfo, getSessionEventMsg(TransportProtos.SessionEvent.OPEN), null);
|
370
|
transportService.process(sessionInfo, getSessionEventMsg(TransportProtos.SessionEvent.OPEN), null);
|
368
|
}
|
371
|
}
|
|
@@ -477,45 +480,36 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -477,45 +480,36 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
477
|
}
|
480
|
}
|
478
|
|
481
|
|
479
|
@Override
|
482
|
@Override
|
480
|
- public void onAttributeUpdate(TransportProtos.AttributeUpdateNotificationMsg msg) {
|
483
|
+ public void onAttributeUpdate(UUID sessionId, TransportProtos.AttributeUpdateNotificationMsg msg) {
|
|
|
484
|
+ log.trace("[{}] Received attributes update notification to device", sessionId);
|
481
|
try {
|
485
|
try {
|
482
|
exchange.respond(coapTransportAdaptor.convertToPublish(isConRequest(), msg));
|
486
|
exchange.respond(coapTransportAdaptor.convertToPublish(isConRequest(), msg));
|
483
|
} catch (AdaptorException e) {
|
487
|
} catch (AdaptorException e) {
|
484
|
log.trace("Failed to reply due to error", e);
|
488
|
log.trace("Failed to reply due to error", e);
|
485
|
- exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
|
489
|
+ closeObserveRelationAndNotify(sessionId, CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
|
|
|
490
|
+ closeAndDeregister();
|
486
|
}
|
491
|
}
|
487
|
}
|
492
|
}
|
488
|
|
493
|
|
489
|
@Override
|
494
|
@Override
|
490
|
public void onRemoteSessionCloseCommand(UUID sessionId, TransportProtos.SessionCloseNotificationProto sessionCloseNotification) {
|
495
|
public void onRemoteSessionCloseCommand(UUID sessionId, TransportProtos.SessionCloseNotificationProto sessionCloseNotification) {
|
491
|
log.trace("[{}] Received the remote command to close the session: {}", sessionId, sessionCloseNotification.getMessage());
|
496
|
log.trace("[{}] Received the remote command to close the session: {}", sessionId, sessionCloseNotification.getMessage());
|
492
|
- Map<TransportProtos.SessionInfoProto, ObserveRelation> sessionToObserveRelationMap = coapTransportResource.getSessionInfoToObserveRelationMap();
|
|
|
493
|
- if (coapTransportResource.getObserverCount() > 0 && !CollectionUtils.isEmpty(sessionToObserveRelationMap)) {
|
|
|
494
|
- Set<TransportProtos.SessionInfoProto> observeSessions = sessionToObserveRelationMap.keySet();
|
|
|
495
|
- Optional<TransportProtos.SessionInfoProto> observeSessionToClose = observeSessions.stream().filter(sessionInfoProto -> {
|
|
|
496
|
- UUID observeSessionId = new UUID(sessionInfoProto.getSessionIdMSB(), sessionInfoProto.getSessionIdLSB());
|
|
|
497
|
- return observeSessionId.equals(sessionId);
|
|
|
498
|
- }).findFirst();
|
|
|
499
|
- if (observeSessionToClose.isPresent()) {
|
|
|
500
|
- TransportProtos.SessionInfoProto sessionInfoProto = observeSessionToClose.get();
|
|
|
501
|
- ObserveRelation observeRelation = sessionToObserveRelationMap.get(sessionInfoProto);
|
|
|
502
|
- coapTransportResource.clearAndNotifyObserveRelation(observeRelation, CoAP.ResponseCode.SERVICE_UNAVAILABLE);
|
|
|
503
|
- }
|
|
|
504
|
- }
|
497
|
+ closeObserveRelationAndNotify(sessionId, CoAP.ResponseCode.SERVICE_UNAVAILABLE);
|
|
|
498
|
+ closeAndDeregister();
|
505
|
}
|
499
|
}
|
506
|
|
500
|
|
507
|
@Override
|
501
|
@Override
|
508
|
- public void onToDeviceRpcRequest(TransportProtos.ToDeviceRpcRequestMsg msg) {
|
|
|
509
|
- boolean successful;
|
502
|
+ public void onToDeviceRpcRequest(UUID sessionId, TransportProtos.ToDeviceRpcRequestMsg msg) {
|
|
|
503
|
+ log.trace("[{}] Received RPC command to device", sessionId);
|
|
|
504
|
+ boolean successful = true;
|
510
|
try {
|
505
|
try {
|
511
|
exchange.respond(coapTransportAdaptor.convertToPublish(isConRequest(), msg, rpcRequestDynamicMessageBuilder));
|
506
|
exchange.respond(coapTransportAdaptor.convertToPublish(isConRequest(), msg, rpcRequestDynamicMessageBuilder));
|
512
|
- successful = true;
|
|
|
513
|
} catch (AdaptorException e) {
|
507
|
} catch (AdaptorException e) {
|
514
|
log.trace("Failed to reply due to error", e);
|
508
|
log.trace("Failed to reply due to error", e);
|
515
|
- exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
|
509
|
+ closeObserveRelationAndNotify(sessionId, CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
|
516
|
successful = false;
|
510
|
successful = false;
|
517
|
- }
|
|
|
518
|
- if (msg.getPersisted()) {
|
511
|
+ } finally {
|
|
|
512
|
+ if (msg.getPersisted()) {
|
519
|
RpcStatus status;
|
513
|
RpcStatus status;
|
520
|
if (!successful) {
|
514
|
if (!successful) {
|
521
|
status = RpcStatus.FAILED;
|
515
|
status = RpcStatus.FAILED;
|
|
@@ -531,6 +525,10 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -531,6 +525,10 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
531
|
.setStatus(status.name())
|
525
|
.setStatus(status.name())
|
532
|
.build();
|
526
|
.build();
|
533
|
coapTransportResource.transportService.process(sessionInfo, responseMsg, TransportServiceCallback.EMPTY);
|
527
|
coapTransportResource.transportService.process(sessionInfo, responseMsg, TransportServiceCallback.EMPTY);
|
|
|
528
|
+ }
|
|
|
529
|
+ if (!successful) {
|
|
|
530
|
+ closeAndDeregister();
|
|
|
531
|
+ }
|
534
|
}
|
532
|
}
|
535
|
}
|
533
|
}
|
536
|
|
534
|
|
|
@@ -547,6 +545,30 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -547,6 +545,30 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
547
|
private boolean isConRequest() {
|
545
|
private boolean isConRequest() {
|
548
|
return exchange.advanced().getRequest().isConfirmable();
|
546
|
return exchange.advanced().getRequest().isConfirmable();
|
549
|
}
|
547
|
}
|
|
|
548
|
+
|
|
|
549
|
+ private void closeObserveRelationAndNotify(UUID sessionId, CoAP.ResponseCode responseCode) {
|
|
|
550
|
+ Map<CoapObserveSessionInfo, ObserveRelation> sessionToObserveRelationMap = coapTransportResource.getCoapSessionInfoToObserveRelationMap();
|
|
|
551
|
+ if (coapTransportResource.getObserverCount() > 0 && !CollectionUtils.isEmpty(sessionToObserveRelationMap)) {
|
|
|
552
|
+ Optional<CoapObserveSessionInfo> observeSessionToClose = sessionToObserveRelationMap.keySet().stream().filter(coapObserveSessionInfo -> {
|
|
|
553
|
+ TransportProtos.SessionInfoProto sessionToDelete = coapObserveSessionInfo.getSessionInfoProto();
|
|
|
554
|
+ UUID observeSessionId = new UUID(sessionToDelete.getSessionIdMSB(), sessionToDelete.getSessionIdLSB());
|
|
|
555
|
+ return observeSessionId.equals(sessionId);
|
|
|
556
|
+ }).findFirst();
|
|
|
557
|
+ if (observeSessionToClose.isPresent()) {
|
|
|
558
|
+ CoapObserveSessionInfo coapObserveSessionInfo = observeSessionToClose.get();
|
|
|
559
|
+ ObserveRelation observeRelation = sessionToObserveRelationMap.get(coapObserveSessionInfo);
|
|
|
560
|
+ coapTransportResource.clearAndNotifyObserveRelation(observeRelation, responseCode);
|
|
|
561
|
+ }
|
|
|
562
|
+ }
|
|
|
563
|
+ }
|
|
|
564
|
+
|
|
|
565
|
+ private void closeAndDeregister() {
|
|
|
566
|
+ Request request = exchange.advanced().getRequest();
|
|
|
567
|
+ String token = coapTransportResource.getTokenFromRequest(request);
|
|
|
568
|
+ CoapObserveSessionInfo deleted = coapTransportResource.lookupAsyncSessionInfo(token);
|
|
|
569
|
+ coapTransportResource.closeAndDeregister(deleted.getSessionInfoProto());
|
|
|
570
|
+ }
|
|
|
571
|
+
|
550
|
}
|
572
|
}
|
551
|
|
573
|
|
552
|
public class CoapResourceObserver implements ResourceObserver {
|
574
|
public class CoapResourceObserver implements ResourceObserver {
|
|
@@ -571,7 +593,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -571,7 +593,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
571
|
public void addedObserveRelation(ObserveRelation relation) {
|
593
|
public void addedObserveRelation(ObserveRelation relation) {
|
572
|
Request request = relation.getExchange().getRequest();
|
594
|
Request request = relation.getExchange().getRequest();
|
573
|
String token = getTokenFromRequest(request);
|
595
|
String token = getTokenFromRequest(request);
|
574
|
- sessionInfoToObserveRelationMap.putIfAbsent(tokenToSessionInfoMap.get(token), relation);
|
596
|
+ sessionInfoToObserveRelationMap.putIfAbsent(tokenToCoapSessionInfoMap.get(token), relation);
|
575
|
log.trace("Added Observe relation for token: {}", token);
|
597
|
log.trace("Added Observe relation for token: {}", token);
|
576
|
}
|
598
|
}
|
577
|
|
599
|
|
|
@@ -579,8 +601,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -579,8 +601,7 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
579
|
public void removedObserveRelation(ObserveRelation relation) {
|
601
|
public void removedObserveRelation(ObserveRelation relation) {
|
580
|
Request request = relation.getExchange().getRequest();
|
602
|
Request request = relation.getExchange().getRequest();
|
581
|
String token = getTokenFromRequest(request);
|
603
|
String token = getTokenFromRequest(request);
|
582
|
- TransportProtos.SessionInfoProto session = tokenToSessionInfoMap.get(token);
|
|
|
583
|
- sessionInfoToObserveRelationMap.remove(session);
|
604
|
+ sessionInfoToObserveRelationMap.remove(tokenToCoapSessionInfoMap.get(token));
|
584
|
log.trace("Relation removed for token: {}", token);
|
605
|
log.trace("Relation removed for token: {}", token);
|
585
|
}
|
606
|
}
|
586
|
}
|
607
|
}
|
|
@@ -591,7 +612,6 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -591,7 +612,6 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
591
|
transportService.deregisterSession(session);
|
612
|
transportService.deregisterSession(session);
|
592
|
rpcSubscriptions.remove(sessionId);
|
613
|
rpcSubscriptions.remove(sessionId);
|
593
|
attributeSubscriptions.remove(sessionId);
|
614
|
attributeSubscriptions.remove(sessionId);
|
594
|
- sessionInfoToObserveRelationMap.remove(session);
|
|
|
595
|
}
|
615
|
}
|
596
|
|
616
|
|
597
|
private TransportConfigurationContainer getTransportConfigurationContainer(DeviceProfile deviceProfile) throws AdaptorException {
|
617
|
private TransportConfigurationContainer getTransportConfigurationContainer(DeviceProfile deviceProfile) throws AdaptorException {
|
|
@@ -657,4 +677,17 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
|
@@ -657,4 +677,17 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
657
|
this.jsonPayload = jsonPayload;
|
677
|
this.jsonPayload = jsonPayload;
|
658
|
}
|
678
|
}
|
659
|
}
|
679
|
}
|
|
|
680
|
+
|
|
|
681
|
+ @Data
|
|
|
682
|
+ private static class CoapObserveSessionInfo {
|
|
|
683
|
+
|
|
|
684
|
+ private final TransportProtos.SessionInfoProto sessionInfoProto;
|
|
|
685
|
+ private final AtomicInteger observeNotificationCounter;
|
|
|
686
|
+
|
|
|
687
|
+ private CoapObserveSessionInfo(TransportProtos.SessionInfoProto sessionInfoProto) {
|
|
|
688
|
+ this.sessionInfoProto = sessionInfoProto;
|
|
|
689
|
+ this.observeNotificationCounter = new AtomicInteger(0);
|
|
|
690
|
+ }
|
|
|
691
|
+ }
|
|
|
692
|
+
|
660
|
} |
693
|
} |