-
Notifications
You must be signed in to change notification settings - Fork 477
/
DefaultOperationsChannel.java
399 lines (363 loc) · 12.3 KB
/
DefaultOperationsChannel.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 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.client.channel.impl.channels;
import org.kaaproject.kaa.client.AbstractKaaClient;
import org.kaaproject.kaa.client.channel.ChannelDirection;
import org.kaaproject.kaa.client.channel.IpTransportInfo;
import org.kaaproject.kaa.client.channel.KaaDataChannel;
import org.kaaproject.kaa.client.channel.KaaDataDemultiplexer;
import org.kaaproject.kaa.client.channel.KaaDataMultiplexer;
import org.kaaproject.kaa.client.channel.ServerType;
import org.kaaproject.kaa.client.channel.TransportConnectionInfo;
import org.kaaproject.kaa.client.channel.TransportProtocolId;
import org.kaaproject.kaa.client.channel.TransportProtocolIdConstants;
import org.kaaproject.kaa.client.channel.connectivity.ConnectivityChecker;
import org.kaaproject.kaa.client.channel.failover.FailoverManager;
import org.kaaproject.kaa.client.channel.failover.FailoverStatus;
import org.kaaproject.kaa.client.channel.impl.channels.polling.CancelableCommandRunnable;
import org.kaaproject.kaa.client.channel.impl.channels.polling.CancelableRunnable;
import org.kaaproject.kaa.client.channel.impl.channels.polling.CancelableScheduledFuture;
import org.kaaproject.kaa.client.channel.impl.channels.polling.PollCommand;
import org.kaaproject.kaa.client.channel.impl.channels.polling.RawDataProcessor;
import org.kaaproject.kaa.client.persistence.KaaClientState;
import org.kaaproject.kaa.client.transport.AbstractHttpClient;
import org.kaaproject.kaa.common.TransportType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.RunnableScheduledFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
public class DefaultOperationsChannel implements KaaDataChannel, RawDataProcessor {
public static final Logger LOG = LoggerFactory // NOSONAR
.getLogger(DefaultOperationsChannel.class);
private static final Map<TransportType, ChannelDirection> SUPPORTED_TYPES = new HashMap<>();
private static final String CHANNEL_ID = "default_operations_long_poll_channel";
static {
SUPPORTED_TYPES.put(TransportType.PROFILE, ChannelDirection.BIDIRECTIONAL);
SUPPORTED_TYPES.put(TransportType.CONFIGURATION, ChannelDirection.BIDIRECTIONAL);
SUPPORTED_TYPES.put(TransportType.NOTIFICATION, ChannelDirection.BIDIRECTIONAL);
SUPPORTED_TYPES.put(TransportType.USER, ChannelDirection.BIDIRECTIONAL);
SUPPORTED_TYPES.put(TransportType.EVENT, ChannelDirection.DOWN);
}
private final Object httpClientLock = new Object();
private final Object httpClientSetLock = new Object();
private final AbstractKaaClient client;
private final KaaClientState state;
private final FailoverManager failoverManager;
private AbstractHttpClient httpClient;
private KaaDataDemultiplexer demultiplexer;
private KaaDataMultiplexer multiplexer;
private IpTransportInfo currentServer;
private ScheduledExecutorService scheduler;
private volatile Future<?> pollFuture;
private volatile boolean stopped = true;
private volatile boolean processingResponse = false;
private volatile boolean taskPosted = false;
private final CancelableCommandRunnable task = new CancelableCommandRunnable() {
@Override
protected void executeCommand() {
if (!stopped) {
taskPosted = false;
synchronized (httpClientSetLock) {
while (httpClient == null && !stopped && !Thread.currentThread().isInterrupted()) {
try {
httpClientSetLock.wait();
} catch (InterruptedException ex) {
break;
}
}
}
if (!stopped) {
currentCommand = new PollCommand(httpClient, DefaultOperationsChannel.this,
getSupportedTransportTypes(), currentServer);
if (!Thread.currentThread().isInterrupted()) {
currentCommand.execute();
}
currentCommand = null;
if (!taskPosted && !stopped && !Thread.currentThread().isInterrupted()) {
taskPosted = true;
pollFuture = scheduler.submit(task);
}
}
}
}
};
private volatile boolean isShutdown = false;
private volatile boolean isPaused = false;
/**
* All-args constructor.
*/
public DefaultOperationsChannel(AbstractKaaClient client, KaaClientState state,
FailoverManager failoverManager) {
this.client = client;
this.state = state;
this.failoverManager = failoverManager;
}
protected ScheduledExecutorService createExecutor() {
LOG.info("Creating a new executor for channel [{}]", getId());
return new ScheduledThreadPoolExecutor(1) {
@Override
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable,
RunnableScheduledFuture<V> task) {
if (runnable instanceof CancelableRunnable) {
return new CancelableScheduledFuture<V>((CancelableRunnable) runnable, task);
}
return super.decorateTask(runnable, task);
}
};
}
private void stopPollScheduler(boolean forced) {
if (!stopped) {
stopped = true;
if (!processingResponse && pollFuture != null) {
LOG.info("Stopping poll future..");
pollFuture.cancel(forced);
if (forced) {
task.waitUntilExecuted();
}
LOG.info("Poll scheduler stopped");
}
}
}
private void startPoll() {
if (!stopped) {
stopPollScheduler(true);
}
if (scheduler == null) {
scheduler = createExecutor();
}
stopped = false;
LOG.info("Starting poll scheduler..");
taskPosted = true;
pollFuture = scheduler.submit(task);
LOG.info("Poll scheduler started");
}
private void stopPoll() {
stopPollScheduler(true);
}
@Override
public LinkedHashMap<String, byte[]> createRequest(
Map<TransportType, ChannelDirection> types) { // NOSONAR
LinkedHashMap<String, byte[]> request = null;
try {
byte[] requestBodyRaw = multiplexer.compileRequest(types);
synchronized (httpClientLock) {
request = HttpRequestCreator.createOperationHttpRequest(requestBodyRaw,
httpClient.getEncoderDecoder());
}
} catch (Exception ex) {
LOG.error("Failed to create request {}", ex);
}
return request;
}
@Override
public void onResponse(byte[] response) {
LOG.debug("Response for channel [{}] received", getId());
byte[] decodedResponse;
try {
processingResponse = true;
synchronized (httpClientLock) {
decodedResponse = httpClient.getEncoderDecoder().decodeData(response);
}
demultiplexer.processResponse(decodedResponse);
processingResponse = false;
failoverManager.onServerConnected(currentServer);
} catch (Exception ex) {
LOG.error("Failed to process response {}", Arrays.toString(response));
LOG.error("Exception stack trace: ", ex);
}
}
@Override
public void onServerError(TransportConnectionInfo info) {
if (!stopped) {
LOG.debug("Channel [{}] connection failed", getId());
synchronized (this) {
stopPollScheduler(false);
}
failoverManager.onServerFailed(info, FailoverStatus.NO_CONNECTIVITY);
} else {
LOG.debug("Channel [{}] connection aborted", getId());
}
}
@Override
public synchronized void sync(TransportType type) {
sync(Collections.singleton(type));
}
@Override
public synchronized void sync(Set<TransportType> types) {
if (isShutdown) {
LOG.info("Can't sync. Channel [{}] is down", getId());
return;
}
if (isPaused) {
LOG.info("Can't sync. Channel [{}] is paused", getId());
return;
}
if (multiplexer == null) {
LOG.warn("Can't sync. Channel {} multiplexer is not set", getId());
return;
}
if (demultiplexer == null) {
LOG.warn("Can't sync. Channel {} demultiplexer is not set", getId());
return;
}
if (currentServer == null) {
LOG.warn("Can't sync. Server is null");
}
for (TransportType type : types) {
LOG.info("Processing sync {} for channel [{}]", type, getId());
if (getSupportedTransportTypes().get(type) == null) {
LOG.error("Unsupported type {} for channel [{}]", type, getId());
return;
}
}
stopPoll();
startPoll();
}
@Override
public synchronized void syncAll() {
if (isShutdown) {
LOG.info("Can't sync. Channel [{}] is down", getId());
return;
}
if (isPaused) {
LOG.info("Can't sync. Channel [{}] is paused", getId());
return;
}
LOG.info("Processing sync all for channel [{}]", getId());
if (multiplexer != null && demultiplexer != null) {
if (currentServer != null) {
stopPoll();
startPoll();
} else {
LOG.warn("Can't sync. Server is null");
}
}
}
@Override
public void syncAck(TransportType type) {
syncAck(Collections.singleton(type));
}
@Override
public void syncAck(Set<TransportType> types) {
LOG.info("Sync ack message is ignored for Channel {}", getId());
}
@Override
public String getId() {
return CHANNEL_ID;
}
@Override
public TransportProtocolId getTransportProtocolId() {
return TransportProtocolIdConstants.HTTP_TRANSPORT_ID;
}
@Override
public ServerType getServerType() {
return ServerType.OPERATIONS;
}
@Override
public synchronized void setDemultiplexer(KaaDataDemultiplexer demultiplexer) {
if (demultiplexer != null) {
this.demultiplexer = demultiplexer;
}
}
@Override
public synchronized void setMultiplexer(KaaDataMultiplexer multiplexer) {
if (multiplexer != null) {
this.multiplexer = multiplexer;
}
}
@Override
public TransportConnectionInfo getServer() {
return currentServer;
}
// TODO: refactor this as part of KAA-126
@Override
public synchronized void setServer(TransportConnectionInfo server) {
if (isShutdown) {
LOG.info("Can't set server. Channel [{}] is down", getId());
return;
}
if (server != null) {
if (!isPaused) {
stopPoll();
}
this.currentServer = new IpTransportInfo(server);
synchronized (httpClientLock) {
LOG.debug("Channel [{}]: creating HTTP client..", getId());
this.httpClient = client.createHttpClient(currentServer.getUrl() + "/EP/LongSync",
state.getPrivateKey(), state.getPublicKey(), currentServer.getPublicKey());
synchronized (httpClientSetLock) {
httpClientSetLock.notifyAll();
}
LOG.debug("Channel [{}]: HTTP client created", getId());
}
if (!isPaused) {
startPoll();
}
}
}
@Override
public void setConnectivityChecker(ConnectivityChecker checker) {
// Do nothing
}
@Override
public synchronized void shutdown() {
if (!isShutdown) {
isShutdown = true;
stopPoll();
if (scheduler != null) {
scheduler.shutdownNow();
}
}
}
@Override
public synchronized void pause() {
if (isShutdown) {
LOG.info("Can't pause channel. Channel [{}] is down", getId());
return;
}
if (!isPaused) {
isPaused = true;
stopPoll();
if (scheduler != null) {
scheduler.shutdownNow();
scheduler = null;
}
}
}
@Override
public synchronized void resume() {
if (isShutdown) {
LOG.info("Can't resume channel. Channel [{}] is down", getId());
return;
}
if (isPaused) {
isPaused = false;
startPoll();
}
}
@Override
public Map<TransportType, ChannelDirection> getSupportedTransportTypes() {
return SUPPORTED_TYPES;
}
}