-
Notifications
You must be signed in to change notification settings - Fork 402
/
Client.java
399 lines (329 loc) · 12.8 KB
/
Client.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*******************************************************************************
* Copyright (c) 2013-2015 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.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.client;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.leshan.LinkObject;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.util.Validate;
/**
* An immutable structure which represent a LW-M2M client registered on the server
*/
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
private static final long DEFAULT_LIFETIME_IN_SEC = 86400L;
private static final String DEFAULT_LWM2M_VERSION = "1.0";
private final Date registrationDate;
private final InetAddress address;
private final int port;
/*
* The address of the LWM2M Server's CoAP end point the client used to register.
*/
private final InetSocketAddress registrationEndpointAddress;
private final long lifeTimeInSec;
private final String smsNumber;
private final String lwM2mVersion;
private final BindingMode bindingMode;
/**
* The LWM2M Client's unique end point name.
*/
private final String endpoint;
private final String registrationId;
private final LinkObject[] objectLinks;
private final Map<String, String> additionalRegistrationAttributes;
/** The location where LWM2M objects are hosted on the device */
private final String rootPath;
private final Date lastUpdate;
protected Client(String registrationId, String endpoint, InetAddress address, int port, String lwM2mVersion,
Long lifetimeInSec, String smsNumber, BindingMode bindingMode, LinkObject[] objectLinks,
InetSocketAddress registrationEndpointAddress,
Date registrationDate, Date lastUpdate, Map<String, String> additionalRegistrationAttributes) {
Validate.notNull(registrationId);
Validate.notEmpty(endpoint);
Validate.notNull(address);
Validate.notNull(port);
Validate.notNull(registrationEndpointAddress);
this.registrationId = registrationId;
this.endpoint = endpoint;
this.address = address;
this.port = port;
this.smsNumber = smsNumber;
this.registrationEndpointAddress = registrationEndpointAddress;
this.objectLinks = objectLinks;
// extract the root objects path from the object links
String rootPath = "/";
if (objectLinks != null) {
for (LinkObject link : objectLinks) {
if (link != null && "oma.lwm2m".equals(link.getAttributes().get("rt"))) {
rootPath = link.getUrl();
break;
}
}
}
this.rootPath = rootPath;
this.lifeTimeInSec = lifetimeInSec == null ? DEFAULT_LIFETIME_IN_SEC : lifetimeInSec;
this.lwM2mVersion = lwM2mVersion == null ? DEFAULT_LWM2M_VERSION : lwM2mVersion;
this.bindingMode = bindingMode == null ? BindingMode.U : bindingMode;
this.registrationDate = registrationDate == null ? new Date() : registrationDate;
this.lastUpdate = lastUpdate == null ? new Date() : lastUpdate;
this.additionalRegistrationAttributes = additionalRegistrationAttributes == null
? Collections.unmodifiableMap(new HashMap<String, String>())
: Collections.unmodifiableMap(additionalRegistrationAttributes);
}
public String getRegistrationId() {
return registrationId;
}
public Date getRegistrationDate() {
return registrationDate;
}
/**
* Gets the client's network address.
*
* @return the source address from the client's most recent CoAP message.
*/
public InetAddress getAddress() {
return address;
}
/**
* Gets the client's network port number.
*
* @return the source port from the client's most recent CoAP message.
*/
public int getPort() {
return port;
}
/**
* Gets the network address and port number of LWM2M Server's CoAP endpoint the client originally registered at.
*
* A LWM2M Server may listen on multiple CoAP end points, e.g. a non-secure and a secure one. Clients are often
* behind a firewall which will only let incoming UDP packets pass if they originate from the same address:port that
* the client has initiated communication with, e.g. by means of registering with the LWM2M Server. It is therefore
* important to know, which of the server's CoAP end points the client contacted for registration.
*
* This information can be used to uniquely identify the CoAP endpoint that should be used to access resources on
* the client.
*
* @return the network address and port number
*/
public InetSocketAddress getRegistrationEndpointAddress() {
return registrationEndpointAddress;
}
public LinkObject[] getObjectLinks() {
return objectLinks;
}
public LinkObject[] getSortedObjectLinks() {
// sort the list of objects
if (objectLinks == null) {
return null;
}
LinkObject[] res = Arrays.copyOf(objectLinks, objectLinks.length);
Arrays.sort(res, new Comparator<LinkObject>() {
/* sort by objectid, object instance and ressource */
@Override
public int compare(LinkObject o1, LinkObject o2) {
if (o1 == null && o2 == null)
return 0;
if (o1 == null)
return -1;
if (o2 == null)
return 1;
// by object
Integer oi1 = o1.getObjectId();
Integer oi2 = o2.getObjectId();
if (oi1 == null && oi2 == null) {
return 0;
}
if (oi1 == null) {
return -1;
}
if (oi2 == null) {
return 1;
}
int oicomp = oi1.compareTo(oi2);
if (oicomp != 0) {
return oicomp;
}
Integer oii1 = o1.getObjectInstanceId();
Integer oii2 = o2.getObjectInstanceId();
if (oii1 == null && oii2 == null) {
return 0;
}
if (oii1 == null) {
return -1;
}
if (oii2 == null) {
return 1;
}
int oiicomp = oii1.compareTo(oii2);
if (oiicomp != 0) {
return oiicomp;
}
Integer or1 = o1.getResourceId();
Integer or2 = o2.getResourceId();
if (or1 == null && or2 == null) {
return 0;
}
if (or1 == null) {
return -1;
}
if (or2 == null) {
return 1;
}
return or1.compareTo(or2);
}
});
return res;
}
public synchronized Long getLifeTimeInSec() {
return lifeTimeInSec;
}
public String getSmsNumber() {
return smsNumber;
}
public String getLwM2mVersion() {
return lwM2mVersion;
}
public BindingMode getBindingMode() {
return bindingMode;
}
/**
* @return the path where the objects are hosted on the device
*/
public String getRootPath() {
return rootPath;
}
/**
* Gets the unique name the client has registered with.
*
* @return the name
*/
public String getEndpoint() {
return endpoint;
}
public Date getLastUpdate() {
return lastUpdate;
}
public boolean isAlive() {
return lastUpdate.getTime() + lifeTimeInSec * 1000 > System.currentTimeMillis();
}
public Map<String, String> getAdditionalRegistrationAttributes() {
return additionalRegistrationAttributes;
}
@Override
public String toString() {
return String.format(
"Client [registrationDate=%s, address=%s, port=%s, registrationEndpoint=%s, lifeTimeInSec=%s, smsNumber=%s, lwM2mVersion=%s, bindingMode=%s, endpoint=%s, registrationId=%s, objectLinks=%s, lastUpdate=%s]",
registrationDate, address, port, registrationEndpointAddress, lifeTimeInSec, smsNumber, lwM2mVersion,
bindingMode, endpoint, registrationId, Arrays.toString(objectLinks), lastUpdate);
}
/**
* Computes a hash code for this client.
*
* @return the hash code based on the <em>endpoint</em> property
*/
@Override
public int hashCode() {
return getEndpoint().hashCode();
}
/**
* Compares this Client to another object.
*
* @return <code>true</code> if the other object is a Client instance and its <em>endpoint</em> property has the
* same value as this Client
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof Client) {
Client other = (Client) obj;
return this.getEndpoint().equals(other.getEndpoint());
} else {
return false;
}
}
public static class Builder {
private final String registrationId;
private final String endpoint;
private final InetAddress address;
private final int port;
private final InetSocketAddress registrationEndpointAddress;
private Date registrationDate;
private Date lastUpdate;
private Long lifeTimeInSec;
private String smsNumber;
private BindingMode bindingMode;
private String lwM2mVersion;
private LinkObject[] objectLinks;
private Map<String, String> additionalRegistrationAttributes;
public Builder(String registrationId, String endpoint, InetAddress address, int port,
InetSocketAddress registrationEndpointAddress) {
Validate.notNull(registrationId);
Validate.notEmpty(endpoint);
Validate.notNull(address);
Validate.notNull(port);
Validate.notNull(registrationEndpointAddress);
this.registrationId = registrationId;
this.endpoint = endpoint;
this.address = address;
this.port = port;
this.registrationEndpointAddress = registrationEndpointAddress;
}
public Builder registrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
return this;
}
public Builder lastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
return this;
}
public Builder lifeTimeInSec(Long lifetimeInSec) {
this.lifeTimeInSec = lifetimeInSec;
return this;
}
public Builder smsNumber(String smsNumber) {
this.smsNumber = smsNumber;
return this;
}
public Builder bindingMode(BindingMode bindingMode) {
this.bindingMode = bindingMode;
return this;
}
public Builder lwM2mVersion(String lwM2mVersion) {
this.lwM2mVersion = lwM2mVersion;
return this;
}
public Builder objectLinks(LinkObject[] objectLinks) {
this.objectLinks = objectLinks;
return this;
}
public Builder additionalRegistrationAttributes(Map<String, String> additionalRegistrationAttributes) {
this.additionalRegistrationAttributes = additionalRegistrationAttributes;
return this;
}
public Client build() {
return new Client(Builder.this.registrationId, Builder.this.endpoint, Builder.this.address,
Builder.this.port, Builder.this.lwM2mVersion, Builder.this.lifeTimeInSec, Builder.this.smsNumber,
this.bindingMode, this.objectLinks, this.registrationEndpointAddress, this.registrationDate,
this.lastUpdate, this.additionalRegistrationAttributes);
}
}
}