Skip to content

Commit 41689cd

Browse files
author
Manuel Sangoï
committed
Merge pull request #11 from sophokles73/improvements
Improved updating of Client registration
2 parents d504c0a + ac005fe commit 41689cd

3 files changed

Lines changed: 89 additions & 58 deletions

File tree

leshan-core/src/main/java/leshan/server/lwm2m/client/Client.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class Client {
6262

6363
private final String registrationId;
6464

65-
private final String[] objectLinks;
65+
private String[] objectLinks;
6666

6767
private Date lastUpdate;
6868

@@ -118,6 +118,10 @@ public String[] getObjectLinks() {
118118
return objectLinks;
119119
}
120120

121+
public void setObjectLinks(String[] objectLinks) {
122+
this.objectLinks = objectLinks;
123+
}
124+
121125
public long getLifeTimeInSec() {
122126
return lifeTimeInSec;
123127
}
@@ -167,10 +171,11 @@ public Date getLastUpdate() {
167171
}
168172

169173
public void setLastUpdate(Date lastUpdate) {
174+
// TODO should probably better be done "implicitly" as part of the other setters
170175
this.lastUpdate = lastUpdate;
171176
}
172177

173-
public void markLastRequestFaild() {
178+
public void markLastRequestFailed() {
174179
this.failedLastRequest = true;
175180
}
176181

leshan-core/src/main/java/leshan/server/lwm2m/client/ClientRegistryImpl.java

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package leshan.server.lwm2m.client;
3131

3232
import java.util.Collection;
33+
import java.util.Date;
3334
import java.util.List;
3435
import java.util.Map;
3536
import java.util.concurrent.ConcurrentHashMap;
@@ -49,7 +50,7 @@ public class ClientRegistryImpl implements ClientRegistry {
4950

5051
private static final Logger LOG = LoggerFactory.getLogger(ClientRegistryImpl.class);
5152

52-
private ConcurrentHashMap<String /* end-point */, Client> clientsByEp = new ConcurrentHashMap<>();
53+
private Map<String /* end-point */, Client> clientsByEp = new ConcurrentHashMap<>();
5354

5455
private List<RegistryListener> listeners = new CopyOnWriteArrayList<>();
5556

@@ -93,67 +94,73 @@ public Client registerClient(Client client) {
9394
@Override
9495
public Client updateClient(ClientUpdate clientUpdated) {
9596
LOG.debug("Updating registration for client: {}", clientUpdated);
96-
Validate.notNull(clientUpdated.getRegistrationId());
97-
for (Client client : clientsByEp.values()) {
98-
if (clientUpdated.getRegistrationId().equals(client.getRegistrationId())) {
99-
// update client
100-
if (clientUpdated.getAddress() != null) {
101-
client.setAddress(clientUpdated.getAddress());
102-
}
103-
104-
if (clientUpdated.getPort() > 0) {
105-
client.setPort(clientUpdated.getPort());
106-
}
97+
Client client = findByRegistrationId(clientUpdated.getRegistrationId());
98+
if (client == null) {
99+
return null;
100+
} else {
101+
// update client
102+
if (clientUpdated.getAddress() != null) {
103+
client.setAddress(clientUpdated.getAddress());
104+
}
107105

108-
if (clientUpdated.getLwM2mVersion() != null) {
109-
client.setLwM2mVersion(clientUpdated.getLwM2mVersion());
110-
}
106+
if (clientUpdated.getPort() > 0) {
107+
client.setPort(clientUpdated.getPort());
108+
}
111109

112-
if (clientUpdated.getBindingMode() != null) {
113-
client.setBindingMode(clientUpdated.getBindingMode());
114-
}
110+
if (clientUpdated.getObjectLinks() != null) {
111+
client.setObjectLinks(clientUpdated.getObjectLinks());
112+
}
113+
114+
client.setLifeTimeInSec(clientUpdated.getLifeTimeInSec());
115+
client.setLwM2mVersion(clientUpdated.getLwM2mVersion());
116+
client.setBindingMode(clientUpdated.getBindingMode());
115117

116-
if (clientUpdated.getSmsNumber() != null) {
117-
client.setSmsNumber(clientUpdated.getSmsNumber());
118-
}
119-
return client;
118+
if (clientUpdated.getSmsNumber() != null) {
119+
client.setSmsNumber(clientUpdated.getSmsNumber());
120120
}
121+
122+
client.setLastUpdate(new Date());
123+
return client;
121124
}
122-
return null;
123125
}
124126

125127
@Override
126128
public Client deregisterClient(String registrationId) {
127-
LOG.debug("Deregistering client with registrationId: {}", registrationId);
128129
Validate.notNull(registrationId);
130+
LOG.debug("Deregistering client with registrationId: {}", registrationId);
129131

130-
String endpoint = null;
131-
132-
for (Client client : clientsByEp.values()) {
133-
if (registrationId.equals(client.getRegistrationId())) {
134-
endpoint = client.getEndpoint();
135-
break;
136-
}
137-
}
138-
139-
Client unregistered = null;
140-
141-
if (endpoint != null) {
142-
unregistered = clientsByEp.remove(endpoint);
132+
Client toBeUnregistered = findByRegistrationId(registrationId);
133+
if (toBeUnregistered == null) {
134+
return null;
135+
} else {
136+
Client unregistered = clientsByEp.remove(toBeUnregistered.getEndpoint());
143137
for (RegistryListener l : listeners) {
144138
l.unregistered(unregistered);
145139
}
146-
LOG.debug("Unregistered client: {}", unregistered);
147-
}
148-
149-
return unregistered;
140+
LOG.debug("Deregistered client: {}", unregistered);
141+
return unregistered;
142+
}
150143
}
151144

145+
private Client findByRegistrationId(String id) {
146+
Client result = null;
147+
if (id != null) {
148+
for (Client client : clientsByEp.values()) {
149+
if (id.equals(client.getRegistrationId())) {
150+
result = client;
151+
break;
152+
}
153+
}
154+
}
155+
return result;
156+
}
157+
152158
/**
153159
* start the registration manager, will start regular cleanup of dead registrations.
154160
*/
155161
public void start() {
156162
// every 2 seconds clean the registration list
163+
// TODO re-consider clean-up interval: wouldn't 5 minutes do as well?
157164
schedExecutor.scheduleAtFixedRate(new Cleaner(), 2, 2, TimeUnit.SECONDS);
158165
}
159166

@@ -171,10 +178,10 @@ private class Cleaner implements Runnable {
171178

172179
@Override
173180
public void run() {
174-
for (Map.Entry<String, Client> e : clientsByEp.entrySet()) {
175-
if (!e.getValue().isAlive()) {
181+
for (Client client : clientsByEp.values()) {
182+
if (!client.isAlive()) {
176183
// force de-registration
177-
deregisterClient(e.getValue().getRegistrationId());
184+
deregisterClient(client.getRegistrationId());
178185
}
179186
}
180187
}

leshan-core/src/main/java/leshan/server/lwm2m/resource/RegisterResource.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@
5858
*/
5959
public class RegisterResource extends ResourceBase {
6060

61+
private static final String QUERY_PARAM_ENDPOINT = "ep=";
62+
63+
private static final String QUERY_PARAM_BINDING_MODE = "b=";
64+
65+
private static final String QUERY_PARAM_LWM2M_VERSION = "lwm2m=";
66+
67+
private static final String QUERY_PARAM_SMS = "sms=";
68+
69+
private static final String QUERY_PARAM_LIFETIME = "lt=";
70+
6171
private static final Logger LOG = LoggerFactory.getLogger(RegisterResource.class);
6272

6373
public static final String RESOURCE_NAME = "rd";
@@ -91,18 +101,19 @@ public void handlePOST(CoapExchange exchange) {
91101
String smsNumber = null;
92102
String lwVersion = null;
93103
BindingMode binding = null;
104+
String[] objectLinks = null;
94105
try {
95106

96107
for (String param : request.getOptions().getURIQueries()) {
97-
if (param.startsWith("ep=")) {
108+
if (param.startsWith(QUERY_PARAM_ENDPOINT)) {
98109
endpoint = param.substring(3);
99-
} else if (param.startsWith("lt=")) {
110+
} else if (param.startsWith(QUERY_PARAM_LIFETIME)) {
100111
lifetime = Long.valueOf(param.substring(3));
101-
} else if (param.startsWith("sms=")) {
112+
} else if (param.startsWith(QUERY_PARAM_SMS)) {
102113
smsNumber = param.substring(4);
103-
} else if (param.startsWith("lwm2m=")) {
114+
} else if (param.startsWith(QUERY_PARAM_LWM2M_VERSION)) {
104115
lwVersion = param.substring(6);
105-
} else if (param.startsWith("b=")) {
116+
} else if (param.startsWith(QUERY_PARAM_BINDING_MODE)) {
106117
binding = BindingMode.valueOf(param.substring(2));
107118
}
108119
}
@@ -112,8 +123,9 @@ public void handlePOST(CoapExchange exchange) {
112123
} else {
113124
// register
114125
String registrationId = RegisterResource.createRegistrationId();
115-
116-
String[] objectLinks = new String(request.getPayload(), Charsets.UTF_8).split(",");
126+
if (request.getPayload() != null) {
127+
objectLinks = new String(request.getPayload(), Charsets.UTF_8).split(",");
128+
}
117129

118130
Client client = new Client(registrationId, endpoint, request.getSource(), request.getSourcePort(),
119131
lwVersion, lifetime, smsNumber, binding, objectLinks);
@@ -154,19 +166,26 @@ public void handlePUT(CoapExchange exchange) {
154166
String smsNumber = null;
155167
String lwVersion = null;
156168
BindingMode binding = null;
169+
String [] objectLinks = null;
170+
157171
for (String param : request.getOptions().getURIQueries()) {
158-
if (param.startsWith("lt=")) {
172+
if (param.startsWith(QUERY_PARAM_LIFETIME)) {
159173
lifetime = Long.valueOf(param.substring(3));
160-
} else if (param.startsWith("sms=")) {
174+
} else if (param.startsWith(QUERY_PARAM_SMS)) {
161175
smsNumber = param.substring(4);
162-
} else if (param.startsWith("lwm2m=")) {
176+
} else if (param.startsWith(QUERY_PARAM_LWM2M_VERSION)) {
163177
lwVersion = param.substring(6);
164-
} else if (param.startsWith("b=")) {
178+
} else if (param.startsWith(QUERY_PARAM_BINDING_MODE)) {
165179
binding = BindingMode.valueOf(param.substring(2));
166180
}
167181
}
182+
183+
if (request.getPayload() != null) {
184+
objectLinks = new String(request.getPayload(), Charsets.UTF_8).split(",");
185+
}
186+
168187
ClientUpdate client = new ClientUpdate(registrationId, request.getSource(), request.getSourcePort(), lwVersion,
169-
lifetime, smsNumber, binding, null);
188+
lifetime, smsNumber, binding, objectLinks);
170189

171190
try {
172191
Client c = this.registry.updateClient(client);

0 commit comments

Comments
 (0)