-
Notifications
You must be signed in to change notification settings - Fork 402
/
LwM2mObservationStore.java
145 lines (126 loc) · 6.04 KB
/
LwM2mObservationStore.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
/*******************************************************************************
* 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.observation;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.californium.core.coap.Token;
import org.eclipse.californium.core.observe.Observation;
import org.eclipse.californium.core.observe.ObservationStore;
import org.eclipse.californium.elements.EndpointContext;
import org.eclipse.leshan.core.californium.ObserveUtil;
import org.eclipse.leshan.core.observation.ObservationIdentifier;
import org.eclipse.leshan.server.observation.LwM2mNotificationReceiver;
import org.eclipse.leshan.server.registration.RegistrationStore;
public class LwM2mObservationStore implements ObservationStore {
private final RegistrationStore registrationStore;
private final LwM2mNotificationReceiver notificationListener;
private final ObservationSerDes observationSerDes;
public LwM2mObservationStore(RegistrationStore registrationStore, LwM2mNotificationReceiver notificationListener,
ObservationSerDes observationSerDes) {
this.registrationStore = registrationStore;
this.notificationListener = notificationListener;
this.observationSerDes = observationSerDes;
}
@Override
public Observation putIfAbsent(Token token, Observation obs) {
org.eclipse.leshan.core.observation.Observation lwm2mObservation = buildLwM2mObservation(obs);
Collection<org.eclipse.leshan.core.observation.Observation> removed = registrationStore
.addObservation(lwm2mObservation.getRegistrationId(), lwm2mObservation, true);
Observation previousObservation = null;
if (removed != null && !removed.isEmpty()) {
for (org.eclipse.leshan.core.observation.Observation observation : removed) {
if (Arrays.equals(observation.getId().getBytes(), token.getBytes())) {
previousObservation = buildCoapObservation(observation);
break;
}
}
}
for (org.eclipse.leshan.core.observation.Observation observation : removed) {
notificationListener.cancelled(observation);
}
return previousObservation;
}
@Override
public Observation put(Token token, Observation obs) {
org.eclipse.leshan.core.observation.Observation lwm2mObservation = buildLwM2mObservation(obs);
Collection<org.eclipse.leshan.core.observation.Observation> removed = registrationStore
.addObservation(lwm2mObservation.getRegistrationId(), buildLwM2mObservation(obs), false);
Observation previousObservation = null;
if (removed != null && !removed.isEmpty()) {
for (org.eclipse.leshan.core.observation.Observation observation : removed) {
if (Arrays.equals(observation.getId().getBytes(), token.getBytes())) {
previousObservation = buildCoapObservation(observation);
break;
}
}
}
for (org.eclipse.leshan.core.observation.Observation observation : removed) {
notificationListener.cancelled(observation);
}
return previousObservation;
}
@Override
public void remove(Token token) {
// try to find observation for given token
org.eclipse.leshan.core.observation.Observation observation = registrationStore
.getObservation(new ObservationIdentifier(token.getBytes()));
if (observation != null) {
// try to remove observation
org.eclipse.leshan.core.observation.Observation removedObservation = registrationStore
.removeObservation(observation.getRegistrationId(), new ObservationIdentifier(token.getBytes()));
if (removedObservation != null) {
notificationListener.cancelled(removedObservation);
}
}
}
@Override
public Observation get(Token token) {
org.eclipse.leshan.core.observation.Observation observation = registrationStore
.getObservation(new ObservationIdentifier(token.getBytes()));
if (observation == null) {
return null;
} else {
return buildCoapObservation(observation);
}
}
@Override
public void setContext(Token token, EndpointContext endpointContext) {
// In Leshan we always set context when we send the request, so this should not be needed to implement this.
}
@Override
public void setExecutor(ScheduledExecutorService executor) {
// registrationStore has its own executor.
}
@Override
public void start() {
// Internal RegistrationStore is started by Leshan.
}
@Override
public void stop() {
// Internal RegistrationStore is stopped by Leshan.
}
private org.eclipse.leshan.core.observation.Observation buildLwM2mObservation(Observation observation) {
String obs = observationSerDes.serialize(observation);
return ObserveUtil.createLwM2mObservation(observation, obs);
}
private Observation buildCoapObservation(org.eclipse.leshan.core.observation.Observation observation) {
String serializedObservation = ObserveUtil.extractSerializedObservation(observation);
if (serializedObservation == null)
return null;
return observationSerDes.deserialize(serializedObservation);
}
}