protected void processMessage(AbstractMessage incomingMessage) { if (incomingMessage.isProcessed() && !(incomingMessage instanceof HaveMessage || incomingMessage instanceof BlockReplyMessage)) return; AbstractMessage replyMessage = null; // USM if (incomingMessage instanceof SubscribeMessage) { replyMessage = this.handleSubscribe((SubscribeMessage) incomingMessage); } else if (incomingMessage instanceof InterestedMessage) { replyMessage = this.handleInterested((InterestedMessage) incomingMessage); } else if (incomingMessage instanceof DisconnectMessage) { DisconnectMessage disconnectMessage = (DisconnectMessage) incomingMessage; if (disconnectMessage.stopUploading()) this.handleDisconnect(disconnectMessage); // TUNER if (disconnectMessage.stopDownloading()) this.handleDisconnect(disconnectMessage); } else if (incomingMessage instanceof HaveMessage) { this.handleHave((HaveMessage) incomingMessage); } else if (incomingMessage instanceof SubscribedMessage) { this.handleSubscribed((SubscribedMessage) incomingMessage); } else if (incomingMessage instanceof QueuedMessage) { this.handleQueued((QueuedMessage) incomingMessage); } else if (incomingMessage instanceof GrantedMessage) { this.handleGranted((GrantedMessage) incomingMessage); } else if (incomingMessage instanceof BlockReplyMessage) { this.handleBlockReply((BlockReplyMessage) incomingMessage); } else if (incomingMessage instanceof PeerSuggestionMessage) { this.handlePeerSuggestion((PeerSuggestionMessage) incomingMessage); } else { logger.warn("Unknown message received, not processing (" + incomingMessage + ")"); } incomingMessage.setReplyMessage(replyMessage, incomingMessage.getSender()); }
protected void handleDisconnect(final DisconnectMessage incomingMessage) { if (logger.isDebugEnabled()) logger.debug("processing message:" + incomingMessage.toString()); if (incomingMessage.stopUploading()) { // this peer won't upload anymore to the sender of the message (peer may be added again) this.videoSignaling.disconnectPeer( incomingMessage.getSender(), incomingMessage.getSegmentIdentifier()); } if (incomingMessage.stopDownloading()) { // this peer won't download anymore from the sender of the message (peer may be added again) this.tuner.disconnectPeer( incomingMessage.getSender(), incomingMessage.getSegmentIdentifier()); // this.tuner.getNeighborList().lookForCandidates(incomingMessage.getSegmentIdentifier()); } }
private void postSessionCleanUp(IOTClient iotClient, boolean isExpiry) { if (isExpiry) { log.debug( " postSessionCleanUp : ---------------- We are to publish a will man for {}", iotClient); publishWill(iotClient); } // Notify the server to remove this client from further sending in requests. DisconnectMessage disconnectMessage = DisconnectMessage.from(false); disconnectMessage = iotClient.copyTransmissionData(disconnectMessage); pushToServer(disconnectMessage); // Unsubscribe all if (iotClient.getIsCleanSession()) { Observable<IotSubscription> subscriptionObservable = getDatastore().getSubscriptions(iotClient); subscriptionObservable.subscribe( subscription -> getMessenger().unSubscribe(subscription), throwable -> log.error(" postSessionCleanUp : problems while unsubscribing", throwable), () -> { Observable<PublishMessage> publishMessageObservable = getDatastore().getMessages(iotClient); publishMessageObservable.subscribe( getDatastore()::removeMessage, throwable -> { log.error(" postSessionCleanUp : problems while unsubscribing", throwable); // any way still delete it from our db }, () -> { // and delete it from our db }); }); } }
/** * Provides the Observer with a new item to observe. * * <p>The {@link com.caricah.iotracah.core.modules.Server} may call this method 0 or more times. * * <p>The {@code Observable} will not call this method again after it calls either {@link * #onCompleted} or {@link #onError}. * * @param iotMessage the item emitted by the Observable */ @Override public void onNext(IOTMessage iotMessage) { getExecutorService() .submit( () -> { log.info(" onNext : received {}", iotMessage); try { IOTMessage response = null; switch (iotMessage.getMessageType()) { case ConnectMessage.MESSAGE_TYPE: ConnectMessage connectMessage = (ConnectMessage) iotMessage; response = ConnectAcknowledgeMessage.from( connectMessage.isDup(), connectMessage.getQos(), connectMessage.isRetain(), connectMessage.getKeepAliveTime(), MqttConnectReturnCode.CONNECTION_ACCEPTED); break; case SubscribeMessage.MESSAGE_TYPE: SubscribeMessage subscribeMessage = (SubscribeMessage) iotMessage; List<Integer> grantedQos = new ArrayList<>(); subscribeMessage .getTopicFilterList() .forEach( topic -> { String topicKey = quickCheckIdKey( "", Arrays.asList(topic.getKey().split(Constant.PATH_SEPARATOR))); Set<String> channelIds = subscriptions.get(topicKey); if (Objects.isNull(channelIds)) { channelIds = new HashSet<>(); } channelIds.add(subscribeMessage.getConnectionId()); subscriptions.put(topicKey, channelIds); grantedQos.add(topic.getValue()); }); response = SubscribeAcknowledgeMessage.from( subscribeMessage.getMessageId(), grantedQos); break; case UnSubscribeMessage.MESSAGE_TYPE: UnSubscribeMessage unSubscribeMessage = (UnSubscribeMessage) iotMessage; response = UnSubscribeAcknowledgeMessage.from(unSubscribeMessage.getMessageId()); break; case Ping.MESSAGE_TYPE: response = iotMessage; break; case PublishMessage.MESSAGE_TYPE: PublishMessage publishMessage = (PublishMessage) iotMessage; Set<String> matchingTopics = getMatchingSubscriptions("", publishMessage.getTopic()); for (String match : matchingTopics) { Set<String> channelIds = subscriptions.get(match); if (Objects.nonNull(channelIds)) { channelIds.forEach( id -> { PublishMessage clonePublishMessage = publishMessage.cloneMessage(); clonePublishMessage.copyTransmissionData(iotMessage); clonePublishMessage.setConnectionId(id); pushToServer(clonePublishMessage); }); } } if (MqttQoS.AT_MOST_ONCE.value() == publishMessage.getQos()) { break; } else if (MqttQoS.AT_LEAST_ONCE.value() == publishMessage.getQos()) { response = AcknowledgeMessage.from(publishMessage.getMessageId()); break; } case PublishReceivedMessage.MESSAGE_TYPE: case ReleaseMessage.MESSAGE_TYPE: case CompleteMessage.MESSAGE_TYPE: case DisconnectMessage.MESSAGE_TYPE: case AcknowledgeMessage.MESSAGE_TYPE: default: DisconnectMessage disconnectMessage = DisconnectMessage.from(true); disconnectMessage.copyTransmissionData(iotMessage); throw new ShutdownException(disconnectMessage); } if (Objects.nonNull(response)) { response.copyTransmissionData(iotMessage); pushToServer(response); } } catch (ShutdownException e) { IOTMessage response = e.getResponse(); if (Objects.nonNull(response)) { pushToServer(response); } } catch (Exception e) { log.error(" onNext : Serious error that requires attention ", e); } }); }