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    
018    package org.apache.commons.net.telnet;
019    
020    /***
021     * Implements the telnet window size option RFC 1073.
022     * <p>
023     * @author Yuval Kashtan
024     * @version $Id: WindowSizeOptionHandler.java 1299238 2012-03-10 17:12:28Z sebb $
025     * @since 2.0
026     ***/
027    public class WindowSizeOptionHandler extends TelnetOptionHandler
028    {
029        /***
030         * Horizontal Size
031         ***/
032        private int m_nWidth = 80;
033    
034        /***
035         * Vertical Size
036         ***/
037        private int m_nHeight = 24;
038    
039        /***
040         * Window size option
041         ***/
042        protected static final int WINDOW_SIZE = 31;
043    
044        /***
045         * Constructor for the WindowSizeOptionHandler. Allows defining desired
046         * initial setting for local/remote activation of this option and
047         * behaviour in case a local/remote activation request for this
048         * option is received.
049         * <p>
050         * @param nWidth - Window width.
051         * @param nHeight - Window Height
052         * @param initlocal - if set to true, a WILL is sent upon connection.
053         * @param initremote - if set to true, a DO is sent upon connection.
054         * @param acceptlocal - if set to true, any DO request is accepted.
055         * @param acceptremote - if set to true, any WILL request is accepted.
056         ***/
057        public WindowSizeOptionHandler(
058            int nWidth,
059            int nHeight,
060            boolean initlocal,
061            boolean initremote,
062            boolean acceptlocal,
063            boolean acceptremote
064        ) {
065            super (
066                TelnetOption.WINDOW_SIZE,
067                initlocal,
068                initremote,
069                acceptlocal,
070                acceptremote
071            );
072    
073            m_nWidth = nWidth;
074            m_nHeight = nHeight;
075        }
076    
077        /***
078         * Constructor for the WindowSizeOptionHandler. Initial and accept
079         * behaviour flags are set to false
080         * <p>
081         * @param nWidth - Window width.
082         * @param nHeight - Window Height
083         ***/
084        public WindowSizeOptionHandler(
085            int nWidth,
086            int nHeight
087        ) {
088            super (
089                TelnetOption.WINDOW_SIZE,
090                false,
091                false,
092                false,
093                false
094            );
095    
096            m_nWidth = nWidth;
097            m_nHeight = nHeight;
098        }
099    
100        /***
101         * Implements the abstract method of TelnetOptionHandler.
102         * <p>
103         * @param suboptionData - the sequence received, whithout IAC SB & IAC SE
104         * @param suboptionLength - the length of data in suboption_data
105         * <p>
106         * @return terminal type information
107         ***/
108        @Override
109        public int[] answerSubnegotiation(int suboptionData[], int suboptionLength)
110        {
111            return null;
112        }
113    
114        /***
115         * Implements the abstract method of TelnetOptionHandler.
116         * This will send the client Height and Width to the server.
117         * <p>
118         * @return array to send to remote system
119         ***/
120        @Override
121        public int[] startSubnegotiationLocal()
122        {
123            int nCompoundWindowSize = m_nWidth * 0x10000 + m_nHeight;
124            int nResponseSize = 5;
125            int nIndex;
126            int nShift;
127            int nTurnedOnBits;
128    
129            if ((m_nWidth % 0x100) == 0xFF) {
130                nResponseSize += 1;
131            }
132    
133            if ((m_nWidth / 0x100) == 0xFF) {
134                nResponseSize += 1;
135            }
136    
137            if ((m_nHeight % 0x100) == 0xFF) {
138                nResponseSize += 1;
139            }
140    
141            if ((m_nHeight / 0x100) == 0xFF) {
142                nResponseSize += 1;
143            }
144    
145            //
146            // allocate response array
147            //
148            int response[] = new int[nResponseSize];
149    
150            //
151            // Build response array.
152            // ---------------------
153            // 1. put option name.
154            // 2. loop through Window size and fill the values,
155            // 3.    duplicate 'ff' if needed.
156            //
157    
158            response[0] = WINDOW_SIZE;                          // 1 //
159    
160            for (                                               // 2 //
161                nIndex=1, nShift = 24;
162                nIndex < nResponseSize;
163                nIndex++, nShift -=8
164            ) {
165                nTurnedOnBits = 0xFF;
166                nTurnedOnBits <<= nShift;
167                response[nIndex] = (nCompoundWindowSize & nTurnedOnBits) >>> nShift;
168    
169                if (response[nIndex] == 0xff) {                 // 3 //
170                    nIndex++;
171                    response[nIndex] = 0xff;
172                }
173            }
174    
175            return response;
176        }
177    
178        /***
179         * Implements the abstract method of TelnetOptionHandler.
180         * <p>
181         * @return always null (no response to subnegotiation)
182         ***/
183        @Override
184        public int[] startSubnegotiationRemote()
185        {
186            return null;
187        }
188    }