Example #1
0
  @Override
  public void postProcess(IOTMessage ioTMessage) {

    switch (ioTMessage.getMessageType()) {
      case ConnectAcknowledgeMessage.MESSAGE_TYPE:
        ConnectAcknowledgeMessage conMessage = (ConnectAcknowledgeMessage) ioTMessage;

        /** Use the connection acknowledgement message to store session id for persistance. */
        Channel channel = getChannel(ioTMessage.getConnectionId());
        if (Objects.nonNull(channel)) {

          if (MqttConnectReturnCode.CONNECTION_ACCEPTED.equals(conMessage.getReturnCode())) {

            channel.attr(ServerImpl.REQUEST_SESSION_ID).set(ioTMessage.getSessionId());
          } else {
            closeClient(ioTMessage.getConnectionId());
          }
        }

        break;
      default:
        super.postProcess(ioTMessage);
    }
  }
Example #2
0
  /**
   * 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);
              }
            });
  }