@Override
 public Optional<Message> generate(Connection connection) {
   if (connection.getState().equals(ConnectionState.NOT_CONNECTED)) {
     ConnectMessage connectMessage = generateConnectMessage();
     connection.setState(ConnectionState.PENDING_CONNECTION);
     connection.setClientId(connectMessage.getId());
     return Optional.of(connectMessage);
   } else if (connection.getState().equals(ConnectionState.PENDING_CONNECTION)) {
     // Connect message is still being processed
     return Optional.empty();
   } else {
     PublishMessage publishMessage = generatePublishMessage();
     return Optional.of(publishMessage);
   }
 }
 private ConnectMessage generateConnectMessage() {
   ConnectMessage connectMessage =
       (ConnectMessage) MessageFactory.getMessageInstance(MessageType.CONNECT);
   connectMessage.setId(Math.abs(new Random().nextInt()));
   return connectMessage;
 }
Exemple #3
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);
              }
            });
  }
  private void onMessageFromClient(AbstractMessage msg) throws Exception {
    Container.logger().debug("<<< " + msg);
    switch (msg.getMessageType()) {
      case CONNECT:
        ConnectMessage connect = (ConnectMessage) msg;
        ConnAckMessage connAck = new ConnAckMessage();
        if (session == null) {
          session = new MQTTSession(vertx, config);
          session.setPublishMessageHandler(this::sendMessageToClient);
          session.setKeepaliveErrorHandler(
              clientID -> {
                String cinfo = clientID;
                if (session != null) {
                  cinfo = session.getClientInfo();
                }
                Container.logger()
                    .info("keep alive exausted! closing connection for client[" + cinfo + "] ...");
                closeConnection();
              });
          connAck.setSessionPresent(false);
        } else {
          Container.logger().warn("Session alredy allocated ...");
          /*
          The Server MUST process a second CONNECT Packet sent from a Client as a protocol violation and disconnect the Client
           */
          //                    connAck.setSessionPresent(true);// TODO implement cleanSession=false
          closeConnection();
          break;
        }
        session.handleConnectMessage(
            connect,
            authenticated -> {
              if (authenticated) {
                connAck.setReturnCode(ConnAckMessage.CONNECTION_ACCEPTED);
                sendMessageToClient(connAck);
              } else {
                Container.logger()
                    .error(
                        "Authentication failed! clientID= "
                            + connect.getClientID()
                            + " username="******"type of message not known: " + msg.getClass().getSimpleName());
        break;
    }

    // TODO: forward mqtt message to backup server

  }