-
Notifications
You must be signed in to change notification settings - Fork 7
/
BenchClient.java
305 lines (264 loc) · 10.7 KB
/
BenchClient.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
package org.eclipse.leshan.benchmark.client;
import static org.eclipse.leshan.core.LwM2mId.SECURITY;
import static org.eclipse.leshan.core.LwM2mId.SERVER;
import static org.eclipse.leshan.client.object.Security.noSec;
import static org.eclipse.leshan.client.object.Security.noSecBootstap;
import static org.eclipse.leshan.client.object.Security.psk;
import static org.eclipse.leshan.client.object.Security.pskBootstrap;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.scandium.DTLSConnector;
import org.eclipse.leshan.client.californium.LeshanClient;
import org.eclipse.leshan.client.californium.LeshanClientBuilder;
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.observer.LwM2mClientObserverAdapter;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.core.request.BootstrapRequest;
import org.eclipse.leshan.core.request.DeregisterRequest;
import org.eclipse.leshan.core.request.RegisterRequest;
import org.eclipse.leshan.core.request.UpdateRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
public class BenchClient {
private Counter bootstrapSuccess;
private Counter bootstrapFailure;
private Counter bootstrapTimeout;
private Counter registrationSuccess;
private Counter registrationFailure;
private Counter registrationTimeout;
private Counter updateSuccess;
private Counter updateFailure;
private Counter updateTimeout;
private Counter deregistrationSuccess;
private Counter deregistrationFailure;
private Counter deregistrationTimeout;
private final boolean secure;
static {
List<ObjectModel> objectModels = ObjectLoader.loadDefault();
objectModels.addAll(ObjectLoader.loadDdfResources(new String[] { "/LWM2M_Software_Management-v1_0.xml" }));
model = new StaticModel(objectModels);
}
private static final Logger LOG = LoggerFactory.getLogger(BenchClient.class);
private final static LwM2mModel model;
private LeshanClient client;
public BenchClient(LeshanClientBuilder builder, String serverURI, boolean bootstrap, String pskId, byte[] pskKey,
long lifetimeInSec, MetricRegistry metricRegistry) {
// register metrics
bootstrapSuccess = registerIfNotExist(metricRegistry, "leshan.bench.client.bootstrap.success", new Counter());
bootstrapFailure = registerIfNotExist(metricRegistry, "leshan.bench.client.bootstrap.failure", new Counter());
bootstrapTimeout = registerIfNotExist(metricRegistry, "leshan.bench.client.bootstrap.timeout", new Counter());
registrationSuccess = registerIfNotExist(metricRegistry, "leshan.bench.client.registration.success",
new Counter());
registrationFailure = registerIfNotExist(metricRegistry, "leshan.bench.client.registration.failure",
new Counter());
registrationTimeout = registerIfNotExist(metricRegistry, "leshan.bench.client.registration.timeout",
new Counter());
updateSuccess = registerIfNotExist(metricRegistry, "leshan.bench.client.update.success", new Counter());
updateFailure = registerIfNotExist(metricRegistry, "leshan.bench.client.update.failure", new Counter());
updateTimeout = registerIfNotExist(metricRegistry, "leshan.bench.client.update.timeout", new Counter());
deregistrationSuccess = registerIfNotExist(metricRegistry, "leshan.bench.client.deregistration.success",
new Counter());
deregistrationFailure = registerIfNotExist(metricRegistry, "leshan.bench.client.deregistration.failure",
new Counter());
deregistrationTimeout = registerIfNotExist(metricRegistry, "leshan.bench.client.deregistration.timeout",
new Counter());
// Create objects
ObjectsInitializer initializer = new ObjectsInitializer(model);
// String endpoint;
secure = serverURI.startsWith("coaps");
if (secure) {
if (bootstrap) {
initializer.setInstancesForObject(SECURITY, pskBootstrap(serverURI, pskId.getBytes(), pskKey));
initializer.setClassForObject(SERVER, Server.class);
} else {
initializer.setInstancesForObject(SECURITY, psk(serverURI, 123, pskId.getBytes(), pskKey));
initializer.setInstancesForObject(SERVER, new Server(123, lifetimeInSec, BindingMode.U, false));
}
} else {
if (bootstrap) {
initializer.setInstancesForObject(SECURITY, noSecBootstap(serverURI));
initializer.setClassForObject(SERVER, Server.class);
} else {
initializer.setInstancesForObject(SECURITY, noSec(serverURI, 123));
initializer.setInstancesForObject(SERVER, new Server(123, lifetimeInSec, BindingMode.U, false));
}
}
initializer.setDummyInstancesForObject(5, 9);
List<LwM2mObjectEnabler> objects = initializer.createAll();
builder.setObjects(objects);
builder.setEncoder(new DefaultLwM2mNodeEncoder(true));
builder.setDecoder(new DefaultLwM2mNodeDecoder(true));
NetworkConfig networkConfig = LeshanClientBuilder.createDefaultNetworkConfig();
networkConfig.set(NetworkConfig.Keys.PREFERRED_BLOCK_SIZE, 1024);
client = builder.build();
client.addObserver(new LwM2mClientObserverAdapter() {
@Override
public void onUpdateTimeout(ServerIdentity server, UpdateRequest request) {
updateTimeout.inc();
}
@Override
public void onUpdateSuccess(ServerIdentity server, UpdateRequest request) {
updateSuccess.inc();
}
@Override
public void onUpdateFailure(ServerIdentity server, UpdateRequest request,
ResponseCode responseCode, String errorMessage, Exception e) {
updateFailure.inc();
if (e != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Update Failed : {}", extractMessage(e), e);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Update Failed : {}", extractMessage(e));
}
} else {
LOG.debug("Update Failed : {} {}", responseCode, errorMessage);
}
}
@Override
public void onRegistrationTimeout(ServerIdentity server,
RegisterRequest request) {
registrationTimeout.inc();
}
@Override
public void onRegistrationSuccess(ServerIdentity server, RegisterRequest request,
String registrationID) {
registrationSuccess.inc();
}
@Override
public void onRegistrationFailure(ServerIdentity server, RegisterRequest request,
ResponseCode responseCode, String errorMessage, Exception e) {
registrationFailure.inc();
if (e != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Registration Failed : {}", extractMessage(e), e);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Registration Failed : {}", extractMessage(e));
}
} else {
LOG.debug("Registration Failed : {} {}", responseCode, errorMessage);
}
}
@Override
public void onDeregistrationTimeout(ServerIdentity server,
DeregisterRequest request) {
deregistrationTimeout.inc();
}
@Override
public void onDeregistrationSuccess(ServerIdentity server,
DeregisterRequest request) {
deregistrationSuccess.inc();
}
@Override
public void onDeregistrationFailure(ServerIdentity server,
DeregisterRequest request, ResponseCode responseCode, String errorMessage, Exception e) {
deregistrationFailure.inc();
if (e != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Deregistration Failed : {}", extractMessage(e), e);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Deregistration Failed : {}", extractMessage(e));
}
} else {
LOG.debug("Deregistration Failed : {} {}", responseCode, errorMessage);
}
}
@Override
public void onBootstrapTimeout(ServerIdentity bsserver,
BootstrapRequest request) {
bootstrapTimeout.inc();
}
@Override
public void onBootstrapSuccess(ServerIdentity bsserver,
BootstrapRequest request) {
bootstrapSuccess.inc();
}
@Override
public void onBootstrapFailure(ServerIdentity bsserver, BootstrapRequest request,
ResponseCode responseCode, String errorMessage, Exception e) {
bootstrapFailure.inc();
if (e != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Bootstrap Failed : {}", extractMessage(e), e);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Bootstrap Failed : {}", extractMessage(e));
}
} else {
LOG.debug("Bootstrap Failed : {} {}", responseCode, errorMessage);
}
}
});
}
@SuppressWarnings("unchecked")
private <T extends Metric> T registerIfNotExist(MetricRegistry registry, String name, T metric) {
Metric prev = registry.getMetrics().get(name);
if (prev == null) {
registry.register(name, metric);
return metric;
} else if (prev.getClass().isAssignableFrom(metric.getClass())) {
return (T) prev;
} else {
throw new IllegalArgumentException("incompatible registry");
}
}
private String extractMessage(Exception e) {
if (e.getCause() != null && e.getCause().getMessage() != null) {
return e.getCause().getMessage();
} else if (e.getMessage() != null) {
return e.getMessage();
} else {
return e.getClass().getName();
}
}
public void start() {
client.start();
}
public void stop(boolean deregister) {
client.stop(deregister);
}
public InetSocketAddress getSocketAddress() {
return client.getAddress(getCurrentRegisteredServer());
}
public boolean triggerUpdate(boolean rehandshake, boolean abbreviated) {
if (getCurrentRegisteredServer() != null) {
if (rehandshake) {
CoapEndpoint endpoint = (CoapEndpoint) client.coap().getServer().getEndpoint(getSocketAddress());
if (endpoint.getConnector() instanceof DTLSConnector) {
DTLSConnector connector = (DTLSConnector) endpoint.getConnector();
if (abbreviated)
connector.forceResumeAllSessions();
else
connector.clearConnectionState();
}
}
client.triggerRegistrationUpdate();
return true;
} else {
return false;
}
}
public ServerIdentity getCurrentRegisteredServer() {
Map<String, ServerIdentity> registeredServers = client.getRegisteredServers();
if (registeredServers != null && !registeredServers.isEmpty())
return registeredServers.values().iterator().next();
return null;
}
public void destroy(boolean deregister) {
client.destroy(deregister);
}
}