001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.telnet;
019
020/**
021 * Implements the telnet window size option RFC 1073.
022 *
023 * @since 2.0
024 */
025public class WindowSizeOptionHandler extends TelnetOptionHandler {
026    /**
027     * Window size option
028     */
029    protected static final int WINDOW_SIZE = 31;
030
031    /**
032     * Horizontal Size
033     */
034    private int width = 80;
035
036    /**
037     * Vertical Size
038     */
039    private int height = 24;
040
041    /**
042     * Constructor for the WindowSizeOptionHandler. Initial and accept behavior flags are set to false
043     * <p>
044     *
045     * @param nWidth  - Window width.
046     * @param nHeight - Window Height
047     */
048    public WindowSizeOptionHandler(final int nWidth, final int nHeight) {
049        super(TelnetOption.WINDOW_SIZE, false, false, false, false);
050
051        width = nWidth;
052        height = nHeight;
053    }
054
055    /**
056     * Constructor for the WindowSizeOptionHandler. Allows defining desired initial setting for local/remote activation of this option and behavior in case a
057     * local/remote activation request for this option is received.
058     * <p>
059     *
060     * @param nWidth       - Window width.
061     * @param nHeight      - Window Height
062     * @param initlocal    - if set to true, a WILL is sent upon connection.
063     * @param initremote   - if set to true, a DO is sent upon connection.
064     * @param acceptlocal  - if set to true, any DO request is accepted.
065     * @param acceptremote - if set to true, any WILL request is accepted.
066     */
067    public WindowSizeOptionHandler(final int nWidth, final int nHeight, final boolean initlocal, final boolean initremote, final boolean acceptlocal,
068            final boolean acceptremote) {
069        super(TelnetOption.WINDOW_SIZE, initlocal, initremote, acceptlocal, acceptremote);
070
071        width = nWidth;
072        height = nHeight;
073    }
074
075    /**
076     * Implements the abstract method of TelnetOptionHandler. This will send the client Height and Width to the server.
077     * <p>
078     *
079     * @return array to send to remote system
080     */
081    @Override
082    public int[] startSubnegotiationLocal() {
083        final int nCompoundWindowSize = width * 0x10000 + height;
084        int nResponseSize = 5;
085        int nIndex;
086        int nShift;
087        int nTurnedOnBits;
088
089        if (width % 0x100 == 0xFF) {
090            nResponseSize += 1;
091        }
092
093        if (width / 0x100 == 0xFF) {
094            nResponseSize += 1;
095        }
096
097        if (height % 0x100 == 0xFF) {
098            nResponseSize += 1;
099        }
100
101        if (height / 0x100 == 0xFF) {
102            nResponseSize += 1;
103        }
104
105        //
106        // allocate response array
107        //
108        final int response[] = new int[nResponseSize];
109
110        //
111        // Build response array.
112        // ---------------------
113        // 1. put option name.
114        // 2. loop through Window size and fill the values,
115        // 3. duplicate 'ff' if needed.
116        //
117
118        response[0] = WINDOW_SIZE; // 1 //
119
120        for ( // 2 //
121                nIndex = 1, nShift = 24; nIndex < nResponseSize; nIndex++, nShift -= 8) {
122            nTurnedOnBits = 0xFF;
123            nTurnedOnBits <<= nShift;
124            response[nIndex] = (nCompoundWindowSize & nTurnedOnBits) >>> nShift;
125
126            if (response[nIndex] == 0xff) { // 3 //
127                nIndex++;
128                response[nIndex] = 0xff;
129            }
130        }
131
132        return response;
133    }
134
135}