@Override public void handleMessage(final Channel channel, final MessageInputStream message) { try { ROOT_LOGGER.tracef("%s handling incoming data", this); final DataInput input = new DataInputStream(message); final ManagementProtocolHeader header = ManagementProtocolHeader.parse(input); final byte type = header.getType(); if (type == ManagementProtocol.TYPE_PING) { // Handle legacy ping/pong directly ROOT_LOGGER.tracef("Received ping on %s", this); handlePing(channel, header); } else if (type == ManagementProtocol.TYPE_BYE_BYE) { // Close the channel ROOT_LOGGER.tracef("Received bye bye on %s, closing", this); handleChannelReset(channel); } else { // Handle a message handleMessage(channel, input, header); } message.close(); } catch (IOException e) { handleError(channel, e); } catch (Exception e) { handleError(channel, new IOException(e)); } finally { StreamUtils.safeClose(message); ROOT_LOGGER.tracef("%s done handling incoming data", this); } final Channel.Receiver next = next(); if (next != null) { channel.receiveMessage(next); } }
/** * Handle a simple ping request. * * @param channel the channel * @param header the protocol header * @throws IOException for any error */ protected static void handlePing(final Channel channel, final ManagementProtocolHeader header) throws IOException { final ManagementProtocolHeader response = new ManagementPongHeader(header.getVersion()); final MessageOutputStream output = channel.writeMessage(); try { writeHeader(response, output); output.close(); } finally { StreamUtils.safeClose(output); } }
/** * Write the management protocol header. * * @param header the mgmt protocol header * @param os the output stream * @throws IOException */ protected static void writeHeader(final ManagementProtocolHeader header, final OutputStream os) throws IOException { final FlushableDataOutput output = FlushableDataOutputImpl.create(os); header.write(output); }