public void sendMessageToClient(AbstractMessage message) { try { Container.logger().debug(">>> " + message); Buffer b1 = encoder.enc(message); sendMessageToClient(b1); } catch (Throwable e) { Container.logger().error(e.getMessage(), e); } }
@Override public void onError(Throwable e) { String clientInfo = getClientInfo(); Container.logger().error(clientInfo + ", " + e.getMessage(), e); // if(e instanceof CorruptedFrameException) { closeConnection(); // } }
@Override public void onToken(byte[] token, boolean timeout) throws Exception { try { Buffer buffer = Buffer.buffer(token); AbstractMessage message = decoder.dec(buffer); onMessageFromClient(message); } catch (Throwable ex) { String clientInfo = getClientInfo(); Container.logger().error(clientInfo + ", Bad error in processing the message", ex); closeConnection(); } }
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 }