예제 #1
0
  public void encode(IoSession session, PublishMessage message, ProtocolEncoderOutput out)
      throws Exception {
    if (message.getQos() == QOSType.RESERVED) {
      throw new IllegalArgumentException("Found a message with RESERVED Qos");
    }
    if (message.getTopicName() == null || message.getTopicName().isEmpty()) {
      throw new IllegalArgumentException("Found a message with empty or null topic name");
    }

    IoBuffer variableHeaderBuff = IoBuffer.allocate(2).setAutoExpand(true);
    variableHeaderBuff.put(Utils.encodeString(message.getTopicName()));
    if (message.getQos() == QOSType.LEAST_ONE || message.getQos() == QOSType.EXACTLY_ONCE) {
      if (message.getMessageID() == null) {
        throw new IllegalArgumentException(
            "Found a message with QOS 1 or 2 and not MessageID setted");
      }
      Utils.writeWord(variableHeaderBuff, message.getMessageID());
    }
    variableHeaderBuff.put(message.getPayload());
    variableHeaderBuff.flip();
    int variableHeaderSize = variableHeaderBuff.remaining();

    byte flags = Utils.encodeFlags(message);

    IoBuffer buff = IoBuffer.allocate(2 + variableHeaderSize).setAutoExpand(true);
    ;
    buff.put((byte) (AbstractMessage.PUBLISH << 4 | flags));
    buff.put(Utils.encodeRemainingLength(variableHeaderSize));
    buff.put(variableHeaderBuff).flip();

    out.write(buff);
  }
  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

  }