All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aesh.terminal.telnet.Option Maven / Gradle / Ivy

There is a newer version: 1.17
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2017 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.aesh.terminal.telnet;

/**
 * A telnet option.
 *
* @author Julien Viet
*/
public enum Option {

    /**
     * Telnet Binary Transmission (RFC856).
     */
    BINARY((byte) 0) {

        @Override
        void handleDo(TelnetConnection session) {
            session.sendBinary = true;
            session.handler.onSendBinary(true);
        }

        @Override
        void handleDont(TelnetConnection session) {
            session.sendBinary = false;
            session.handler.onSendBinary(false);
        }

        @Override
        void handleWill(TelnetConnection session) {
            session.receiveBinary = true;
            session.handler.onReceiveBinary(true);
        }

        @Override
        void handleWont(TelnetConnection session) {
            session.receiveBinary = false;
            session.handler.onReceiveBinary(false);
        }
    },

    /**
     * Telnet Echo Option (RFC857).
     */
    ECHO((byte) 1) {
        @Override
        void handleDo(TelnetConnection session) {
            session.handler.onEcho(true);
        }
        void handleDont(TelnetConnection session) {
            session.handler.onEcho(false);
        }
    },

    /**
     * Telnet Suppress Go Ahead Option (RFC858).
     */
    SGA((byte) 3) {
        void handleDo(TelnetConnection session) {
            session.handler.onSGA(true);
        }
        void handleDont(TelnetConnection session) {
            session.handler.onSGA(false);
        }
    },

    /**
     * Telnet Terminal Type Option (RFC884).
     */
    TERMINAL_TYPE((byte) 24) {

        final byte BYTE_IS = 0, BYTE_SEND = 1;

        @Override
        void handleWill(TelnetConnection session) {
            session.send(new byte[]{TelnetConnection.BYTE_IAC, TelnetConnection.BYTE_SB, code, BYTE_SEND, TelnetConnection.BYTE_IAC, TelnetConnection.BYTE_SE});
        }
        @Override
        void handleWont(TelnetConnection session) {
        }

        @Override
        void handleParameters(TelnetConnection session, byte[] parameters) {
            if (parameters.length > 0 && parameters[0] == BYTE_IS) {
                String terminalType = new String(parameters, 1, parameters.length - 1);
                session.handler.onTerminalType(terminalType);
            }
        }
    },

    /**
     * Telnet Window Size Option (RFC1073).
     */
    NAWS((byte) 31) {
        @Override
        void handleWill(TelnetConnection session) {
            session.handler.onNAWS(true);
        }
        @Override
        void handleWont(TelnetConnection session) {
            session.handler.onNAWS(false);
        }
        @Override
        void handleParameters(TelnetConnection session, byte[] parameters) {
            if (parameters.length == 4) {
                int width = ((parameters[0] & 0xff) << 8) + (parameters[1] & 0xff);
                int height = ((parameters[2] & 0xff) << 8) + (parameters[3] & 0xff);
                session.handler.onSize(width, height);
            }
        }
    }

    ;

    /**
     * The option code.
     */
    final byte code;

    Option(byte code) {
        this.code = code;
    }

    /**
     * Handle a DO message.
     *
     * @param session the session
     */
    void handleDo(TelnetConnection session) { }

    /**
     * Handle a DON'T message.
     *
     * @param session the session
     */
    void handleDont(TelnetConnection session) { }

    /**
     * Handle a WILL message.
     *
     * @param session the session
     */
    void handleWill(TelnetConnection session) { }

    /**
     * Handle a WON'T message.
     *
     * @param session the session
     */
    void handleWont(TelnetConnection session) { }

    /**
     * Handle a parameters message.
     *
     * @param session the session
     * @param parameters the parameters
     */
    void handleParameters(TelnetConnection session, byte[] parameters) { }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy