View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.telnet;
19  
20  /***
21   * Implements the telnet window size option RFC 1073.
22   * <p>
23   * @author Yuval Kashtan
24   * @version $Id: WindowSizeOptionHandler.java 1299238 2012-03-10 17:12:28Z sebb $
25   * @since 2.0
26   ***/
27  public class WindowSizeOptionHandler extends TelnetOptionHandler
28  {
29      /***
30       * Horizontal Size
31       ***/
32      private int m_nWidth = 80;
33  
34      /***
35       * Vertical Size
36       ***/
37      private int m_nHeight = 24;
38  
39      /***
40       * Window size option
41       ***/
42      protected static final int WINDOW_SIZE = 31;
43  
44      /***
45       * Constructor for the WindowSizeOptionHandler. Allows defining desired
46       * initial setting for local/remote activation of this option and
47       * behaviour in case a local/remote activation request for this
48       * option is received.
49       * <p>
50       * @param nWidth - Window width.
51       * @param nHeight - Window Height
52       * @param initlocal - if set to true, a WILL is sent upon connection.
53       * @param initremote - if set to true, a DO is sent upon connection.
54       * @param acceptlocal - if set to true, any DO request is accepted.
55       * @param acceptremote - if set to true, any WILL request is accepted.
56       ***/
57      public WindowSizeOptionHandler(
58          int nWidth,
59          int nHeight,
60          boolean initlocal,
61          boolean initremote,
62          boolean acceptlocal,
63          boolean acceptremote
64      ) {
65          super (
66              TelnetOption.WINDOW_SIZE,
67              initlocal,
68              initremote,
69              acceptlocal,
70              acceptremote
71          );
72  
73          m_nWidth = nWidth;
74          m_nHeight = nHeight;
75      }
76  
77      /***
78       * Constructor for the WindowSizeOptionHandler. Initial and accept
79       * behaviour flags are set to false
80       * <p>
81       * @param nWidth - Window width.
82       * @param nHeight - Window Height
83       ***/
84      public WindowSizeOptionHandler(
85          int nWidth,
86          int nHeight
87      ) {
88          super (
89              TelnetOption.WINDOW_SIZE,
90              false,
91              false,
92              false,
93              false
94          );
95  
96          m_nWidth = nWidth;
97          m_nHeight = nHeight;
98      }
99  
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 }