/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.protocol.mgmt;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.jboss.as.protocol.StreamUtils;
import org.jboss.as.protocol.logging.ProtocolLogger;
import org.jboss.remoting3.Channel;
import org.jboss.remoting3.MessageInputStream;
import org.jboss.remoting3.MessageOutputStream;

/**
 * Base receiver class for the management protocol support.
 *
 * @author Emanuel Muckenhuber
 */
public final class ManagementChannelReceiver implements Channel.Receiver {

    /**
     * Create a {@code ManagementChannelReceiver} which delegates protocol messages to
     * a {@code ManagementMessageHandler}.
     *
     * @param handler the handler
     * @return the receiver
     */
    public static ManagementChannelReceiver createDelegating(final ManagementMessageHandler handler) {
        assert handler != null;
        return new ManagementChannelReceiver(handler);
    }

    private final ManagementMessageHandler handler;
    private volatile long lastMessageTime;

    private ManagementChannelReceiver(final ManagementMessageHandler handler) {
        this.handler = handler;
    }

    @Override
    public void handleMessage(final Channel channel, final MessageInputStream message) {
        try {
            ProtocolLogger.ROOT_LOGGER.tracef("%s handling incoming data", this);
            lastMessageTime = System.currentTimeMillis();
            final DataInput input = new DataInputStream(message);
            final ManagementProtocolHeader header = ManagementProtocolHeader.parse(input);
            final byte type = header.getType();
            try {
                if (type == ManagementProtocol.TYPE_PING) {
                    // Handle legacy ping/pong directly
                    ProtocolLogger.ROOT_LOGGER.tracef("Received ping on %s", this);
                    handlePing(channel, header);
                } else if (type == ManagementProtocol.TYPE_PONG) {
                    // Nothing to do here
                    ProtocolLogger.ROOT_LOGGER.tracef("Received on on %s", this);
                } else if (type == ManagementProtocol.TYPE_BYE_BYE) {
                    // Close the channel
                    ProtocolLogger.ROOT_LOGGER.tracef("Received bye bye on %s, closing", this);
                    handleChannelReset(channel);
                } else {
                    // Handle a message
                    handler.handleMessage(channel, input, header);
                }
            } finally {
                try {
                    //noinspection StatementWithEmptyBody
                    while (message.read() != -1) {
                        // drain the message to workaround a potential remoting buffer leak
                    }
                } catch (IOException ignore) {
                    //
                }
            }
            message.close();
        } catch(IOException e) {
            handleError(channel, e);
        } catch (Exception e) {
            handleError(channel, new IOException(e));
        } finally {
            StreamUtils.safeClose(message);
            ProtocolLogger.ROOT_LOGGER.tracef("%s done handling incoming data", this);
        }
        channel.receiveMessage(this);
    }

    /**
     * @return the time of the most recent invocation of {@link #handleMessage(Channel, MessageInputStream)}
     */
    public long getLastMessageTime() {
        return lastMessageTime;
    }

    @Override
    public void handleError(final Channel channel, final IOException error) {
        ProtocolLogger.ROOT_LOGGER.tracef(error, "%s error handling incoming data", this);
        try {
            channel.close();
        } catch (IOException e) {
            ProtocolLogger.ROOT_LOGGER.errorClosingChannel(e.getMessage());
        }
    }

    @Override
    public void handleEnd(final Channel channel) {
        try {
            channel.close();
        } catch (IOException e) {
            ProtocolLogger.ROOT_LOGGER.errorClosingChannel(e.getMessage());
        }
    }

    /**
     * Handle the legacy bye-bye notification.
     *
     * @param channel the channel the bye-bye message was received
     */
    protected void handleChannelReset(Channel channel) {
        //
    }

    /**
     * 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);
    }

}

@@ -3104,18 +3104,20 @@
 eceived 
+p
 on
+g
  on %25s%22,
@@ -3221,23 +3221,225 @@
  // 
-Close the chann
+This signal has been a no-op for years and years, maybe since AS 7.0.0.Final!%0A                    // JBoss Remoting deals with channel close itself; we don't do it at the%0A                    // management protocol lev
 el%0A 
@@ -3516,20 +3516,21 @@
  on %25s, 
-clos
+ignor
 ing%22, th
@@ -3538,57 +3538,8 @@
 s);%0A
-                    handleChannelReset(channel);%0A
     
@@ -5241,220 +5241,8 @@
  %7D%0A%0A
-    /**%0A     * Handle the legacy bye-bye notification.%0A     *%0A     * @param channel the channel the bye-bye message was received%0A     */%0A    protected void handleChannelReset(Channel channel) %7B%0A        //%0A    %7D%0A%0A
     

Drop no-op protected method for cruft bye-bye handling