...
|
...
|
@@ -100,7 +100,7 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
100
|
100
|
private volatile DeviceSessionCtx deviceSessionCtx;
|
101
|
101
|
private volatile GatewaySessionHandler gatewaySessionHandler;
|
102
|
102
|
|
103
|
|
- MqttTransportHandler(MqttTransportContext context,SslHandler sslHandler) {
|
|
103
|
+ MqttTransportHandler(MqttTransportContext context, SslHandler sslHandler) {
|
104
|
104
|
this.sessionId = UUID.randomUUID();
|
105
|
105
|
this.context = context;
|
106
|
106
|
this.transportService = context.getTransportService();
|
...
|
...
|
@@ -138,32 +138,17 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
138
|
138
|
break;
|
139
|
139
|
case PUBLISH:
|
140
|
140
|
processPublish(ctx, (MqttPublishMessage) msg);
|
141
|
|
- transportService.reportActivity(sessionInfo);
|
142
|
|
- if (gatewaySessionHandler != null) {
|
143
|
|
- gatewaySessionHandler.reportActivity();
|
144
|
|
- }
|
145
|
141
|
break;
|
146
|
142
|
case SUBSCRIBE:
|
147
|
143
|
processSubscribe(ctx, (MqttSubscribeMessage) msg);
|
148
|
|
- transportService.reportActivity(sessionInfo);
|
149
|
|
- if (gatewaySessionHandler != null) {
|
150
|
|
- gatewaySessionHandler.reportActivity();
|
151
|
|
- }
|
152
|
144
|
break;
|
153
|
145
|
case UNSUBSCRIBE:
|
154
|
146
|
processUnsubscribe(ctx, (MqttUnsubscribeMessage) msg);
|
155
|
|
- transportService.reportActivity(sessionInfo);
|
156
|
|
- if (gatewaySessionHandler != null) {
|
157
|
|
- gatewaySessionHandler.reportActivity();
|
158
|
|
- }
|
159
|
147
|
break;
|
160
|
148
|
case PINGREQ:
|
161
|
149
|
if (checkConnected(ctx, msg)) {
|
162
|
150
|
ctx.writeAndFlush(new MqttMessage(new MqttFixedHeader(PINGRESP, false, AT_MOST_ONCE, false, 0)));
|
163
|
151
|
transportService.reportActivity(sessionInfo);
|
164
|
|
- if (gatewaySessionHandler != null) {
|
165
|
|
- gatewaySessionHandler.reportActivity();
|
166
|
|
- }
|
167
|
152
|
}
|
168
|
153
|
break;
|
169
|
154
|
case DISCONNECT:
|
...
|
...
|
@@ -174,7 +159,6 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
174
|
159
|
default:
|
175
|
160
|
break;
|
176
|
161
|
}
|
177
|
|
-
|
178
|
162
|
}
|
179
|
163
|
|
180
|
164
|
private void processPublish(ChannelHandlerContext ctx, MqttPublishMessage mqttMsg) {
|
...
|
...
|
@@ -188,6 +172,7 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
188
|
172
|
if (topicName.startsWith(MqttTopics.BASE_GATEWAY_API_TOPIC)) {
|
189
|
173
|
if (gatewaySessionHandler != null) {
|
190
|
174
|
handleGatewayPublishMsg(topicName, msgId, mqttMsg);
|
|
175
|
+ transportService.reportActivity(sessionInfo);
|
191
|
176
|
}
|
192
|
177
|
} else {
|
193
|
178
|
processDevicePublish(ctx, mqttMsg, topicName, msgId);
|
...
|
...
|
@@ -244,6 +229,8 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
244
|
229
|
} else if (topicName.equals(MqttTopics.DEVICE_CLAIM_TOPIC)) {
|
245
|
230
|
TransportProtos.ClaimDeviceMsg claimDeviceMsg = adaptor.convertToClaimDevice(deviceSessionCtx, mqttMsg);
|
246
|
231
|
transportService.process(sessionInfo, claimDeviceMsg, getPubAckCallback(ctx, msgId, claimDeviceMsg));
|
|
232
|
+ } else {
|
|
233
|
+ transportService.reportActivity(sessionInfo);
|
247
|
234
|
}
|
248
|
235
|
} catch (AdaptorException e) {
|
249
|
236
|
log.warn("[{}] Failed to process publish msg [{}][{}]", sessionId, topicName, msgId, e);
|
...
|
...
|
@@ -276,6 +263,7 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
276
|
263
|
}
|
277
|
264
|
log.trace("[{}] Processing subscription [{}]!", sessionId, mqttMsg.variableHeader().messageId());
|
278
|
265
|
List<Integer> grantedQoSList = new ArrayList<>();
|
|
266
|
+ boolean activityReported = false;
|
279
|
267
|
for (MqttTopicSubscription subscription : mqttMsg.payload().topicSubscriptions()) {
|
280
|
268
|
String topic = subscription.topicName();
|
281
|
269
|
MqttQoS reqQoS = subscription.qualityOfService();
|
...
|
...
|
@@ -284,11 +272,13 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
284
|
272
|
case MqttTopics.DEVICE_ATTRIBUTES_TOPIC: {
|
285
|
273
|
transportService.process(sessionInfo, TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null);
|
286
|
274
|
registerSubQoS(topic, grantedQoSList, reqQoS);
|
|
275
|
+ activityReported = true;
|
287
|
276
|
break;
|
288
|
277
|
}
|
289
|
278
|
case MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC: {
|
290
|
279
|
transportService.process(sessionInfo, TransportProtos.SubscribeToRPCMsg.newBuilder().build(), null);
|
291
|
280
|
registerSubQoS(topic, grantedQoSList, reqQoS);
|
|
281
|
+ activityReported = true;
|
292
|
282
|
break;
|
293
|
283
|
}
|
294
|
284
|
case MqttTopics.DEVICE_RPC_RESPONSE_SUB_TOPIC:
|
...
|
...
|
@@ -308,6 +298,9 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
308
|
298
|
grantedQoSList.add(FAILURE.value());
|
309
|
299
|
}
|
310
|
300
|
}
|
|
301
|
+ if (!activityReported) {
|
|
302
|
+ transportService.reportActivity(sessionInfo);
|
|
303
|
+ }
|
311
|
304
|
ctx.writeAndFlush(createSubAckMessage(mqttMsg.variableHeader().messageId(), grantedQoSList));
|
312
|
305
|
}
|
313
|
306
|
|
...
|
...
|
@@ -320,6 +313,7 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
320
|
313
|
if (!checkConnected(ctx, mqttMsg)) {
|
321
|
314
|
return;
|
322
|
315
|
}
|
|
316
|
+ boolean activityReported = false;
|
323
|
317
|
log.trace("[{}] Processing subscription [{}]!", sessionId, mqttMsg.variableHeader().messageId());
|
324
|
318
|
for (String topicName : mqttMsg.payload().topics()) {
|
325
|
319
|
mqttQoSMap.remove(new MqttTopicMatcher(topicName));
|
...
|
...
|
@@ -327,10 +321,12 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
327
|
321
|
switch (topicName) {
|
328
|
322
|
case MqttTopics.DEVICE_ATTRIBUTES_TOPIC: {
|
329
|
323
|
transportService.process(sessionInfo, TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().setUnsubscribe(true).build(), null);
|
|
324
|
+ activityReported = true;
|
330
|
325
|
break;
|
331
|
326
|
}
|
332
|
327
|
case MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC: {
|
333
|
328
|
transportService.process(sessionInfo, TransportProtos.SubscribeToRPCMsg.newBuilder().setUnsubscribe(true).build(), null);
|
|
329
|
+ activityReported = true;
|
334
|
330
|
break;
|
335
|
331
|
}
|
336
|
332
|
}
|
...
|
...
|
@@ -338,6 +334,9 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
338
|
334
|
log.warn("[{}] Failed to process unsubscription [{}] to [{}]", sessionId, mqttMsg.variableHeader().messageId(), topicName);
|
339
|
335
|
}
|
340
|
336
|
}
|
|
337
|
+ if (!activityReported) {
|
|
338
|
+ transportService.reportActivity(sessionInfo);
|
|
339
|
+ }
|
341
|
340
|
ctx.writeAndFlush(createUnSubAckMessage(mqttMsg.variableHeader().messageId()));
|
342
|
341
|
}
|
343
|
342
|
|
...
|
...
|
|