Changeset 7124

Show
Ignore:
Timestamp:
02/25/08 18:27:46 (5 years ago)
Author:
frsyuki
Message:

public interface impl. 2

Location:
lang/actionscript/FxTerm/trunk
Files:
1 removed
3 modified
1 moved

Legend:

Unmodified
Added
Removed
  • lang/actionscript/FxTerm/trunk/FxTerm.as

    r7117 r7124  
    1515import flash.utils.IDataOutput; 
    1616 
    17 public class FxTerm extends Sprite implements IDataOutput { 
    18  
    19         private var telnetClient:FxTelnet; 
     17public class FxTerm extends Sprite implements IDataOutput 
     18{ 
    2019 
    2120        private var screen:Array;  // Array of Array of TextCell 
     
    4140        private var drawBuffer:ByteArray; 
    4241 
    43         private var decoderBuffer:ByteArray; 
     42        private var _decoderBuffer:ByteArray; 
    4443 
    4544        private var ansiForegroundColors:Array;    // Array of RGB, length must be 7 
     
    6261 
    6362        // IDataOutput interface 
    64         private var _objectEncoding:uint; 
    65         private var _endian:String; 
     63        private var _middleBuffer:ByteArray; 
    6664 
    6765        /** 
     
    121119                fontCalibration(); 
    122120 
    123                 resize(col, row); 
    124  
    125121                // FIXME 
    126122                characterWidthMeasure = new TextField(); 
     
    129125 
    130126                drawBuffer = new ByteArray(); 
    131                 decoderBuffer = new ByteArray(); 
    132  
    133                 _objectEncoding = decoderBuffer.objectEncoding; 
    134                 _endian = decoderBuffer.endian; 
    135          
    136                 telnetClient = new FxTelnet(); 
    137                 telnetClient.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler); 
    138                 telnetClient.connect("127.0.0.1", 5000); 
    139                 this.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler); 
     127                _decoderBuffer = new ByteArray(); 
     128                _middleBuffer = new ByteArray(); 
     129 
     130                resize(col, row); 
    140131        } 
    141132 
     
    150141                fontCalibration(); 
    151142                adjustDisplay(); 
    152                 for(var r:uint = 0; r < rows; ++r) { 
    153                         lineDirty[r] = true; 
    154                 } 
    155                 cursorDirty = true; 
    156                 refresh(); 
     143        } 
     144 
     145        /** 
     146         * Get font size. 
     147         */ 
     148        public function get fontSize():Object 
     149        { 
     150                return displayTextFormat.size; 
    157151        } 
    158152 
     
    205199 
    206200        /** 
     201         * Get number of columns 
     202         */ 
     203        public function get col():uint 
     204        { 
     205                return cols; 
     206        } 
     207 
     208        /** 
    207209         * Set number of columns 
    208210         */ 
     
    213215 
    214216        /** 
     217         * Get number of rows 
     218         */ 
     219        public function get row():uint 
     220        { 
     221                return rows; 
     222        } 
     223 
     224        /** 
    215225         * Set number of rows 
    216226         */ 
     
    218228        { 
    219229                resize(cols, n); 
    220         } 
    221  
    222         // FIXME 
    223         private function keyboardHandler(event:KeyboardEvent):void 
    224         { 
    225                 if( event.charCode == 0 ) { return; } 
    226                 telnetClient.writeByte(event.charCode); 
    227230        } 
    228231 
     
    234237        public function resize(newcols:uint, newrows:uint):void 
    235238        { 
     239                if( newcols <= 1 ) { newcols = 1; } 
     240                if( newrows <= 1 ) { newrows = 1; } 
     241 
    236242                screen.length = newrows; 
    237243                lineDirty.length = newrows; 
     
    256262                                tf.selectable = true; 
    257263                                tf.x = 0; 
    258                                 tf.y = lineHeight * r; 
     264                                // tf.y will be set at adjustDisplay() 
    259265                                addChild(tf); 
    260266                        } 
     
    287293                rows = newrows; 
    288294 
    289                 for(r = 0; r < rows; ++r) { 
    290                         lineDirty[r] = true; 
    291                 } 
    292  
    293                 adjustDisplay(); 
     295                if( ccol >= cols ) { ccol = cols - 1; } 
     296                if( crow >= rows ) { crow = rows - 1; } 
    294297 
    295298                // FIXME 
    296299                scrolltop = 0; 
    297300                scrollbottom = rows - 1; 
     301 
     302                adjustDisplay(); 
    298303        } 
    299304 
     
    314319        { 
    315320                var dwidth:Number  = calibrateFontWidth  * cols; 
    316                 var dheight:Number = calibrateFontHeight * rows; 
     321                var dheight:Number = lineHeight * rows + lineGapFilling; 
    317322                background.bitmapData = new BitmapData( 
    318323                                dwidth, 
     
    323328                //cursorCanvas.width = dwidth; 
    324329                //cursorCanvas.height = dheight; 
     330                //this.width = dwidth; 
     331                //this.height = dheight; 
     332                for(var r:uint = 0; r < rows; ++r) { 
     333                        fieldArray[r].y = lineHeight * r; 
     334                } 
     335                refresh(true);  // force refresh 
    325336        } 
    326337 
     
    328339        /** 
    329340         * refresh the console 
    330          */ 
    331         public function refresh():void 
    332         { 
     341         * @param force  force redraw whole screen 
     342         */ 
     343        public function refresh(force:Boolean = true):void 
     344        { 
     345                if(force) { 
     346                        for(var f:uint = 0; f < rows; ++f) { 
     347                                lineDirty[f] = true; 
     348                        } 
     349                } 
     350 
    333351                var field:TextField; 
    334352                var attr:uint = screen[0][0]; 
     
    359377 
    360378                // draw cursor 
    361                 if( cursorDirty ) { 
     379                if( cursorDirty || force ) { 
    362380                        cursorDirty = false; 
    363381                        cursorCanvas.graphics.clear(); 
    364                         cursorCanvas.graphics.beginFill(ansiForegroundColors[0], 1.0);   // FIXME  cursor color 
     382                        // FIXME  cursor color 
     383                        cursorCanvas.graphics.beginFill(ansiForegroundColors[0], 1.0); 
    365384                        cursorCanvas.graphics.drawRect( 
    366385                                        (this.width / cols - 0.05) * ccol + 2, 
     
    412431        public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void 
    413432        { 
    414                 var decoder:ByteArray = decoderBuffer; 
     433                var decoder:ByteArray = _decoderBuffer; 
    415434                var c:String; 
    416435                var len:uint = (length != 0 ? length : bytes.length); 
     
    454473        } 
    455474 
    456  
    457 // FIXME 
    458         private function dataHandler(event:ProgressEvent):void 
    459         { 
    460                 var buffer:ByteArray = telnetClient.buffer; 
    461                 var len:uint = buffer.length; 
    462                 buffer.position = 0; 
    463  
    464                 var decoder:ByteArray = decoderBuffer; 
    465                 var c:String; 
    466                 for(var n:uint = 0; n < len; ++n) { 
    467                         var b:uint = buffer[n]; 
    468                         if( escaped ) { 
    469                                 // escape sequence 
    470                                 escapeSequence.writeByte(b); 
    471                                 tryInterpretEscapeSequence(); 
    472                         } else if( decoder.length == 0 ) { 
    473                                 if( b >= 1 && b <= 31 ) { 
    474                                         // control character 
    475                                         handleControlChar(b); 
    476                                 } else if( b <= 0x7F ) { 
    477                                         // single byte character 
    478                                         if( b != 0 ) {  // ignore NULL 
    479                                                 c = String.fromCharCode(b); // FIXME 
    480                                                 // TODO: graphmode 
    481                                                 putNormalCharacter(c); 
    482                                         } 
    483                                 } else { 
    484                                         // first byte of multibyte character 
    485                                         decoder.writeByte(b); 
    486                                 } 
    487                         } else { 
    488                                 decoder.position = decoder.length; 
    489                                 decoder.writeByte(b); 
    490                                 try { 
    491                                         decoder.position = 0; 
    492                                         c = decoder.readMultiByte(decoder.length, charSet); 
    493                                         if( c.length > 0 ) { 
    494                                                 // complete multibyte character 
    495                                                 decoder.length = 0; 
    496                                                 putNormalCharacter(c); 
    497                                         } 
    498                                 } catch (e:Error) { 
    499                                         // insufficient data to complete multibyte character 
    500                                 } 
    501                         } 
    502                 } 
    503  
    504                 buffer.length = 0; 
    505                 refresh();  // FIXME 
    506         } 
    507  
    508475        private function cursorLineDown():void 
    509476        { 
     
    11331100 
    11341101        // FIXME  how ad-hoc! 
     1102        // FIXME  this method doesn't work when font size is very small 
    11351103        private function isWideCharacter(c:String):Boolean 
    11361104        { 
     
    12051173        public function set objectEncoding(value:uint):void 
    12061174        { 
    1207                 _objectEncoding = value; 
     1175                _middleBuffer.objectEncoding = value; 
    12081176        } 
    12091177        public function get objectEncoding():uint 
    12101178        { 
    1211                 return _objectEncoding; 
     1179                return _middleBuffer.objectEncoding; 
    12121180        } 
    12131181 
    12141182        public function set endian(value:String):void 
    12151183        { 
    1216                 _endian = value; 
     1184                _middleBuffer.endian = value; 
    12171185        } 
    12181186        public function get endian():String 
    12191187        { 
    1220                 return _endian; 
     1188                return _middleBuffer.endian; 
    12211189        } 
    12221190 
    12231191        public function writeBoolean(value:Boolean):void 
    12241192        { 
    1225                 var bytes:ByteArray = new ByteArray(); 
    1226                 bytes.writeBoolean(value); 
    1227                 writeBytes(bytes); 
     1193                var buffer:ByteArray = _middleBuffer; 
     1194                buffer.writeBoolean(value); 
     1195                writeBytes(buffer); 
     1196                buffer.length = 0; 
    12281197        } 
    12291198 
    12301199        public function writeByte(value:int):void 
    12311200        { 
    1232                 var bytes:ByteArray = new ByteArray(); 
    1233                 bytes.endian = _endian; 
    1234                 bytes.writeByte(value); 
    1235                 writeBytes(bytes); 
     1201                var buffer:ByteArray = _middleBuffer; 
     1202                buffer.writeByte(value); 
     1203                writeBytes(buffer); 
     1204                buffer.length = 0; 
    12361205        } 
    12371206 
    12381207        public function writeDouble(value:Number):void 
    12391208        { 
    1240                 var bytes:ByteArray = new ByteArray(); 
    1241                 bytes.endian = _endian; 
    1242                 bytes.writeDouble(value); 
    1243                 writeBytes(bytes); 
     1209                var buffer:ByteArray = _middleBuffer; 
     1210                buffer.writeDouble(value); 
     1211                writeBytes(buffer); 
     1212                buffer.length = 0; 
    12441213        } 
    12451214 
    12461215        public function writeFloat(value:Number):void 
    12471216        { 
    1248                 var bytes:ByteArray = new ByteArray(); 
    1249                 bytes.endian = _endian; 
    1250                 bytes.writeFloat(value); 
    1251                 writeBytes(bytes); 
     1217                var buffer:ByteArray = _middleBuffer; 
     1218                buffer.writeFloat(value); 
     1219                writeBytes(buffer); 
     1220                buffer.length = 0; 
    12521221        } 
    12531222 
    12541223        public function writeInt(value:int):void 
    12551224        { 
    1256                 var bytes:ByteArray = new ByteArray(); 
    1257                 bytes.endian = _endian; 
    1258                 bytes.writeInt(value); 
    1259                 writeBytes(bytes); 
     1225                var buffer:ByteArray = _middleBuffer; 
     1226                buffer.writeInt(value); 
     1227                writeBytes(buffer); 
     1228                buffer.length = 0; 
    12601229        } 
    12611230 
    12621231        public function writeMultiByte(value:String, charSet:String):void 
    12631232        { 
    1264                 var bytes:ByteArray = new ByteArray(); 
    1265                 bytes.endian = _endian; 
    1266                 bytes.writeMultiByte(value, charSet); 
    1267                 writeBytes(bytes); 
     1233                var buffer:ByteArray = _middleBuffer; 
     1234                buffer.writeMultiByte(value, charSet); 
     1235                writeBytes(buffer); 
     1236                buffer.length = 0; 
    12681237        } 
    12691238 
    12701239        public function writeObject(object:*):void 
    12711240        { 
    1272                 var bytes:ByteArray = new ByteArray(); 
    1273                 bytes.endian = _endian; 
    1274                 bytes.objectEncoding = _objectEncoding; 
    1275                 bytes.writeObject(object); 
    1276                 writeBytes(bytes); 
     1241                var buffer:ByteArray = _middleBuffer; 
     1242                buffer.writeObject(object); 
     1243                writeBytes(buffer); 
     1244                buffer.length = 0; 
    12771245        } 
    12781246 
    12791247        public function writeShort(value:int):void 
    12801248        { 
    1281                 var bytes:ByteArray = new ByteArray(); 
    1282                 bytes.endian = _endian; 
    1283                 bytes.writeShort(value); 
    1284                 writeBytes(bytes); 
     1249                var buffer:ByteArray = _middleBuffer; 
     1250                buffer.writeShort(value); 
     1251                writeBytes(buffer); 
     1252                buffer.length = 0; 
    12851253        } 
    12861254 
    12871255        public function writeUnsignedInt(value:uint):void 
    12881256        { 
    1289                 var bytes:ByteArray = new ByteArray(); 
    1290                 bytes.endian = _endian; 
    1291                 bytes.writeUnsignedInt(value); 
    1292                 writeBytes(bytes); 
     1257                var buffer:ByteArray = _middleBuffer; 
     1258                buffer.writeUnsignedInt(value); 
     1259                writeBytes(buffer); 
     1260                buffer.length = 0; 
    12931261        } 
    12941262 
    12951263        public function writeUTF(value:String):void 
    12961264        { 
    1297                 var bytes:ByteArray = new ByteArray(); 
    1298                 bytes.endian = _endian; 
    1299                 bytes.writeUTF(value); 
    1300                 writeBytes(bytes); 
     1265                var buffer:ByteArray = _middleBuffer; 
     1266                buffer.writeUTF(value); 
     1267                writeBytes(buffer); 
     1268                buffer.length = 0; 
    13011269        } 
    13021270 
    13031271        public function writeUTFBytes(value:String):void 
    13041272        { 
    1305                 var bytes:ByteArray = new ByteArray(); 
    1306                 bytes.endian = _endian; 
    1307                 bytes.writeUTFBytes(value); 
    1308                 writeBytes(bytes); 
     1273                var buffer:ByteArray = _middleBuffer; 
     1274                buffer.writeUTFBytes(value); 
     1275                writeBytes(buffer); 
     1276                buffer.length = 0; 
    13091277        } 
    13101278 
  • lang/actionscript/FxTerm/trunk/FxTermTest.mxml

    r7117 r7124  
    11<?xml version="1.0" encoding="utf-8"?> 
    2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="vertical" xmlns="*"> 
     2<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
     3                creationComplete="init()" layout="vertical" xmlns="*" 
     4        > 
     5        <mx:Style> 
     6                Application { 
     7                        backgroundColor: #999999; 
     8                } 
     9                .terminalPanel { 
     10                        color: #0f0f0f; 
     11                        backgroundColor: #000000; 
     12                } 
     13                .terminalControlLink { 
     14                        color: #606060; 
     15                        disabledColor: #0f0f0f; 
     16                        selectionColor: #000000; 
     17                        separatorColor: #CCCCC4; 
     18                } 
     19        </mx:Style> 
    320        <mx:Script> 
    4                 import FxTerm; 
    5                 import Terminal; 
     21        <![CDATA[ 
    622                import mx.controls.*; 
    723                import mx.containers.*; 
    8                 private var term:FxTerm; 
     24                import flash.events.*; 
     25                import mx.events.*; 
     26                import mx.core.*; 
    927                private function init():void 
    1028                { 
     29                        terminal.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler); 
     30                        terminal.addEventListener(ResizeEvent.RESIZE, terminalResizedHandler); 
     31                        commandLine.tabEnabled = false; 
     32                        for each(var c:UIComponent in terminalControlLink.getChildren()) { 
     33                                c.tabEnabled = false; 
     34                        } 
    1135                } 
     36 
     37                private function keyboardHandler(event:KeyboardEvent):void 
     38                { 
     39                        var code:uint = event.charCode; 
     40                        if(code == 0) { return; } 
     41                        telnet.writeByte(code); 
     42                } 
     43 
     44                private function terminalResizedHandler(event:ResizeEvent):void 
     45                { 
     46                        trace("resized"); 
     47                } 
     48 
     49                private var hasCommandLineInput:Boolean = false; 
     50                private function onCommandLineInput(event:TextEvent):void 
     51                { 
     52                        hasCommandLineInput = true;  // 漢字変換中は送信しない 
     53                        // http://aoi-project.com/pblog/article.php?id=11 
     54                } 
     55                private function onCommandLineKeyUp(event:KeyboardEvent):void { 
     56                        if (event.keyCode == Keyboard.ENTER && 
     57                                        hasCommandLineInput == false) { 
     58                                trace(commandLine.text); 
     59                                if(commandLine.text.length == 0) { 
     60                                        // send enter key 
     61                                        telnet.writeByte(Keyboard.ENTER); 
     62                                } else { 
     63                                        // send text 
     64                                        telnet.writeUTFBytes(commandLine.text); 
     65                                        commandLine.text = ""; 
     66                                } 
     67                        } 
     68                        hasCommandLineInput = false; 
     69                } 
     70        ]]> 
    1271        </mx:Script> 
    13         <!--<mx:Script source="FxTerm.as" />--> 
    14         <!--<mx:Style source="FxTerm.css" />--> 
    15         <Terminal id="kk" col="80" row="20"/> 
    16         <!--<mx:HBox id="pa" />--> 
    17         <!--<mx:VBox id="term" width="800" height="400"/>--> 
    18         <!--<mx:Text id="term" width="800" height="400"/>--> 
    19         <!--<mx:TextArea id="log" width="800" height="100%"/>--> 
     72        <TelnetService id="telnet" host="localhost" port="5000"/> 
     73        <mx:Panel id="terminalPanel" styleName="terminalPanel"> 
     74                <Terminal id="terminal" col="80" row="20" dataProvider="{telnet}"/> 
     75                <mx:ControlBar><mx:VBox width="100%" horizontalAlign="center"> 
     76                        <mx:LinkBar id="terminalControlLink" styleName="terminalControlLink" 
     77                                dataProvider="terminalControl"/> 
     78                        <mx:ViewStack id="terminalControl" width="100%" minWidth="400"> 
     79                                <mx:HBox label="Command" width="100%"> 
     80                                        <mx:TextInput width="100%" id="commandLine" 
     81                                                textInput="onCommandLineInput(event)" 
     82                                                keyUp="onCommandLineKeyUp(event)" 
     83                                                /> 
     84                                </mx:HBox> 
     85                                <mx:HBox label="Size" horizontalAlign="center"> 
     86                                        <mx:Label text="Colmuns" styleName="terminalControlLabel"/> 
     87                                        <mx:NumericStepper value="{terminal.col}" minimum="1" maximum="400" id="colmunStepper" 
     88                                                change="terminal.col=colmunStepper.value" 
     89                                                styleName="terminalControl"/> 
     90                                        <mx:Spacer width="10"/> 
     91                                        <mx:Label text="Rows" styleName="terminalControlLabel"/> 
     92                                        <mx:NumericStepper value="{terminal.row}" minimum="1" maximum="40" id="rowStepper" 
     93                                                change="terminal.row=rowStepper.value" 
     94                                                styleName="terminalControl"/> 
     95                                        <mx:Spacer width="10"/> 
     96                                        <mx:Label text="Font size" styleName="terminalControlLabel"/> 
     97                                        <mx:NumericStepper value="{terminal.fontSize}" minimum="1" maximum="40" id="fontSizeStepper" 
     98                                                change="terminal.fontSize=fontSizeStepper.value" 
     99                                                styleName="terminalControl"/> 
     100                                </mx:HBox> 
     101                                <mx:HBox label="Color" width="100%"> 
     102                                </mx:HBox> 
     103                        </mx:ViewStack> 
     104                </mx:VBox></mx:ControlBar> 
     105        </mx:Panel> 
    20106</mx:Application> 
    21107 
  • lang/actionscript/FxTerm/trunk/Telnet.as

    r7117 r7124  
    11package 
    22{ 
    3         import flash.events.*; 
    4         import flash.net.Socket; 
    5         import flash.utils.ByteArray; 
    6         import flash.system.Security; 
    7  
    8 //import mx.controls.*; 
    9  
    10         public class FxTelnet implements IEventDispatcher { 
    11                 private var socket:Socket; 
    12                 private var eventDispatcher:EventDispatcher; 
    13  
    14                 public var buffer:ByteArray; 
    15                 private var state:int = 0; 
    16                 private var is_active:Boolean = false; 
    17  
    18                 public function get bytesAvailable():uint { 
    19                         return buffer.length; 
    20                 } 
    21  
    22                 public function FxTelnet() { 
    23                         eventDispatcher = new EventDispatcher(this); 
    24                         buffer = new ByteArray(); 
    25                         socket = new Socket(); 
    26                         socket.addEventListener(Event.CONNECT, connectHandler); 
    27                         socket.addEventListener(Event.CLOSE, closeHandler); 
    28                         socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
    29                         socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 
    30                         socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
    31                 } 
    32  
    33                 public function connect(server:String, port:int):void { 
    34                         // Load policy file from remote server. 
    35                         //Security.loadPolicyFile("http://" + serverURL + "/crossdomain.xml"); 
    36                         //Security.loadPolicyFile("http://xcore.local:9200/crossdomain.xml"); 
    37                         try { 
    38                                 msg("Trying to connect to " + server + ":" + port + "\n"); 
    39                                 socket.connect(server, port); 
    40                                 is_active = true; 
    41                         } catch (error:Error) { 
    42                                 msg(error.message + "\n"); 
    43                                 socket.close(); 
    44                         } 
    45                 } 
    46  
    47                 public function readMultiByte(charSet:String):String { 
    48                         var buflen:uint = buffer.length; 
    49                         if( buflen == 0 ) { return ""; } 
    50                         buffer.position = 0; 
    51                         var value:String = buffer.readMultiByte(buflen, charSet); 
    52                         buffer.length = 0; 
    53                         return value; 
    54                 } 
    55                 public function readBytes(bytes:ByteArray, offset:uint = 0):void { 
    56                         buffer.position = 0; 
    57                         buffer.readBytes(bytes, offset, 0); 
    58                         buffer.length = 0; 
    59                 } 
    60                 public function writeByte(value:int):void 
    61                 { 
    62                         if( !is_active ) { return; } 
    63                         socket.writeByte(value); 
    64                 } 
    65                 public function writeMultibyte(value:String):void { 
    66                         if( !is_active ) { return; } 
    67                         //  
    68                 } 
    69                 public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void { 
    70                         if( !is_active ) { return; } 
    71                         //  
    72                 } 
    73  
    74                 private function connectHandler(event:Event):void { 
    75                         if (socket.connected) { 
    76                                 msg("connected...\n"); 
    77                         } else { 
    78                                 msg("unable to connect\n"); 
    79                         } 
    80                 } 
    81                 private function closeHandler(event:Event):void { 
    82                         msg("closed...\n"); 
    83                         is_active = false; 
    84                 } 
    85                 private function ioErrorHandler(event:IOErrorEvent):void { 
    86                         msg("Unable to connect: socket error.\n"); 
    87                         is_active = false; 
    88                 } 
    89                 private function securityErrorHandler(event:SecurityErrorEvent):void { 
    90                         //msg(event.text + "\n"); 
    91                         is_active = false; 
    92                         //Alert.show(event.text); 
    93                 } 
    94  
    95                 private const WILL:int = 0xFB; // 251 - WILL (option code) 
    96                 private const WONT:int = 0xFC; // 252 - WON'T (option code) 
    97                 private const DO:int   = 0xFD; // 253 - DO (option code) 
    98                 private const DONT:int = 0xFE; // 254 - DON'T (option code) 
    99                 private const IAC:int  = 0xFF; // 255 - Interpret as Command (IAC) 
    100                 private function socketDataHandler(ev:ProgressEvent):void { 
    101                         var n:int = socket.bytesAvailable; 
    102                         // Loop through each available byte returned from the socket connection. 
    103                         while (--n >= 0) { 
    104                                 // Read next available byte. 
    105                                 var b:int = socket.readUnsignedByte(); 
    106                                 switch (state) { 
    107                                         case 0: 
    108                                                 // If the current byte is the "Interpret as Command" code, set the state to 1. 
    109                                                 if (b == IAC) { 
    110                                                         state = 1; 
    111                                                         // Else display the character using the msg() method. 
    112                                                 } else { 
    113                                                         buffer.writeByte(b); 
    114                                                 } 
    115                                                 break; 
    116                                         case 1: 
    117                                                 // If the current byte is the "DO" code, set the state to 2. 
    118                                                 if (b == DO) { 
    119                                                         state = 2; 
    120                                                 } else { 
    121                                                         state = 0; 
    122                                                 } 
    123                                                 break; 
    124                                                 // Blindly reject the option. 
    125                                         case 2: 
    126                                                 /* 
    127                                                    Write the "Interpret as Command" code, "WONT" code,  
    128                                                    and current byte to the socket and send the contents  
    129                                                    to the server by calling the flush() method. 
    130                                                    */ 
    131                                                 socket.writeByte(IAC); 
    132                                                 socket.writeByte(WONT); 
    133                                                 socket.writeByte(b); 
    134                                                 socket.flush(); 
    135                                                 state = 0; 
    136                                                 break; 
    137                                 } 
    138                         } 
    139                         var event:Event = new ProgressEvent( 
    140                                         ProgressEvent.SOCKET_DATA, 
     3 
     4import flash.events.*; 
     5import flash.net.Socket; 
     6import flash.utils.ByteArray; 
     7import flash.utils.IDataOutput; 
     8 
     9public class Telnet extends EventDispatcher implements IDataOutput 
     10{ 
     11        private var _socket:Socket; 
     12        private var _buffer:ByteArray; 
     13        private var _state:int = 0; 
     14        private var _active:Boolean = false; 
     15 
     16        private static const WILL:int = 0xFB; // 251 - WILL (option code) 
     17        private static const WONT:int = 0xFC; // 252 - WON'T (option code) 
     18        private static const DO:int   = 0xFD; // 253 - DO (option code) 
     19        private static const DONT:int = 0xFE; // 254 - DON'T (option code) 
     20        private static const IAC:int  = 0xFF; // 255 - Interpret as Command (IAC) 
     21 
     22        // IDataOutput interface 
     23        private var _middleBuffer:ByteArray; 
     24 
     25        public function Telnet() 
     26        { 
     27                _buffer = new ByteArray(); 
     28                _middleBuffer = new ByteArray(); 
     29                var socket:Socket = _socket = new Socket(); 
     30                socket.addEventListener(Event.CONNECT, connectHandler); 
     31                socket.addEventListener(Event.CLOSE, closeHandler); 
     32                socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
     33                socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 
     34                socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
     35        } 
     36 
     37        public function connect(host:String, port:int):void 
     38        { 
     39                try { 
     40                        _socket.connect(host, port); 
     41                        _active = true; 
     42                } catch (error:Error) { 
     43                        _socket.close(); 
     44                        var event:IOErrorEvent = new IOErrorEvent( 
     45                                        IOErrorEvent.IO_ERROR, 
    14146                                        false, 
    14247                                        false, 
    143                                         buffer.length, 
    144                                         0 
     48                                        "failed connect to " + host + ":" + port 
    14549                                        ); 
    14650                        dispatchEvent(event); 
    14751                } 
    148  
    149                 private function msg(value:String):void { 
    150                         buffer.writeUTFBytes(value); 
    151                         var event:Event = new ProgressEvent( 
    152                                         ProgressEvent.SOCKET_DATA, 
    153                                         false, 
    154                                         false, 
    155                                         buffer.length, 
    156                                         0 
    157                                         ); 
    158                         dispatchEvent(event); 
     52        } 
     53 
     54        public function close():void 
     55        { 
     56                _socket.close(); 
     57                _active = false; 
     58        } 
     59 
     60        public function get bytesAvailable():uint 
     61        { 
     62                return _buffer.length; 
     63        } 
     64 
     65        public function readBytes(bytes:ByteArray, offset:uint = 0):void 
     66        { 
     67                var buffer:ByteArray = _buffer; 
     68                buffer.position = 0; 
     69                buffer.readBytes(bytes, offset, 0); 
     70                buffer.length = 0; 
     71        } 
     72 
     73 
     74        public function writeByte(value:int):void 
     75        { 
     76                if( !_active ) { return; } 
     77                if( value == IAC ) { 
     78                        _socket.writeByte(value); 
     79                        _socket.writeByte(value); 
     80                } else { 
     81                        _socket.writeByte(value); 
    15982                } 
    160  
    161  
    162  
    163                 // delegate EventDispatcher 
    164                 public function addEventListener(type:String, 
    165                                 listener:Function, 
    166                                 useCapture:Boolean = false, 
    167                                 priority:int = 0, 
    168                                 useWeakReference:Boolean = false):void 
    169                 { 
    170                         eventDispatcher.addEventListener(type, listener, useCapture, 
    171                                         priority, useWeakReference); 
     83        } 
     84 
     85        public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void { 
     86                if( !_active ) { return; } 
     87                _socket.writeBytes(bytes, offset, length); 
     88                // FIXME convert IAC to IAC IAC 
     89        } 
     90 
     91        private function connectHandler(event:Event):void 
     92        { 
     93                dispatchEvent(event); 
     94        } 
     95 
     96        private function closeHandler(event:Event):void 
     97        { 
     98                _active = false; 
     99                dispatchEvent(event); 
     100        } 
     101 
     102        private function ioErrorHandler(event:IOErrorEvent):void 
     103        { 
     104                _active = false; 
     105                dispatchEvent(event); 
     106        } 
     107 
     108        private function securityErrorHandler(event:SecurityErrorEvent):void 
     109        { 
     110                _active = false; 
     111                dispatchEvent(event); 
     112        } 
     113 
     114        private function socketDataHandler(ev:ProgressEvent):void 
     115        { 
     116                var socket:Socket = _socket; 
     117                var buffer:ByteArray = _buffer; 
     118                var beforeLength:uint = _buffer.length; 
     119                var state:int = _state; 
     120                var n:int = socket.bytesAvailable; 
     121                while (--n >= 0) { 
     122                        var b:int = socket.readUnsignedByte(); 
     123                        switch (state) { 
     124                                case 0: 
     125                                        if (b == IAC) { 
     126                                                state = 1; 
     127                                        } else { 
     128                                                buffer.writeByte(b); 
     129                                        } 
     130                                        break; 
     131                                case 1: 
     132                                        if (b == DO) { 
     133                                                state = 2; 
     134                                        } else { 
     135                                                state = 0; 
     136                                        } 
     137                                        break; 
     138                                case 2: 
     139                                        socket.writeByte(IAC); 
     140                                        socket.writeByte(WONT); 
     141                                        socket.writeByte(b); 
     142                                        socket.flush(); 
     143                                        state = 0; 
     144                                        break; 
     145                        } 
    172146                } 
    173  
    174                 public function removeEventListener(type:String, 
    175                                 listener:Function, 
    176                                 useCapture:Boolean = false):void 
    177                 { 
    178                         eventDispatcher.removeEventListener(type, listener, useCapture); 
    179                 } 
    180  
    181                 public function dispatchEvent(event:Event):Boolean 
    182                 { 
    183                         return eventDispatcher.dispatchEvent(event); 
    184                 } 
    185  
    186                 public function hasEventListener(type:String):Boolean 
    187                 { 
    188                         return eventDispatcher.hasEventListener(type); 
    189                 } 
    190  
    191  
    192                 public function willTrigger(type:String):Boolean 
    193                 { 
    194                         return eventDispatcher.willTrigger(type); 
    195                 } 
     147                _state = state; 
     148                var event:Event = new ProgressEvent( 
     149                                ProgressEvent.PROGRESS, 
     150                                false, 
     151                                false, 
     152                                buffer.length - beforeLength, 
     153                                buffer.length 
     154                                ); 
     155                dispatchEvent(event); 
     156        } 
     157 
     158 
     159        /** 
     160         * IDataOutput interface 
     161         */ 
     162        public function set objectEncoding(value:uint):void 
     163        { 
     164                _middleBuffer.objectEncoding = value; 
     165        } 
     166        public function get objectEncoding():uint 
     167        { 
     168                return _middleBuffer.objectEncoding; 
     169        } 
     170 
     171        public function set endian(value:String):void 
     172        { 
     173                _middleBuffer.endian = value; 
     174        } 
     175        public function get endian():String 
     176        { 
     177                return _middleBuffer.endian; 
     178        } 
     179 
     180        public function writeBoolean(value:Boolean):void 
     181        { 
     182                var buffer:ByteArray = _middleBuffer; 
     183                buffer.writeBoolean(value); 
     184                writeBytes(buffer); 
     185                buffer.length = 0; 
     186        } 
     187 
     188        public function writeDouble(value:Number):void 
     189        { 
     190                var buffer:ByteArray = _middleBuffer; 
     191                buffer.writeDouble(value); 
     192                writeBytes(buffer); 
     193                buffer.length = 0; 
     194        } 
     195 
     196        public function writeFloat(value:Number):void 
     197        { 
     198                var buffer:ByteArray = _middleBuffer; 
     199                buffer.writeFloat(value); 
     200                writeBytes(buffer); 
     201                buffer.length = 0; 
     202        } 
     203 
     204        public function writeInt(value:int):void 
     205        { 
     206                var buffer:ByteArray = _middleBuffer; 
     207                buffer.writeInt(value); 
     208                writeBytes(buffer); 
     209                buffer.length = 0; 
     210        } 
     211 
     212        public function writeMultiByte(value:String, charSet:String):void 
     213        { 
     214                var buffer:ByteArray = _middleBuffer; 
     215                buffer.writeMultiByte(value, charSet); 
     216                writeBytes(buffer); 
     217                buffer.length = 0; 
     218        } 
     219 
     220        public function writeObject(object:*):void 
     221        { 
     222                var buffer:ByteArray = _middleBuffer; 
     223                buffer.writeObject(object); 
     224                writeBytes(buffer); 
     225                buffer.length = 0; 
     226        } 
     227 
     228        public function writeShort(value:int):void 
     229        { 
     230                var buffer:ByteArray = _middleBuffer; 
     231                buffer.writeShort(value); 
     232                writeBytes(buffer); 
     233                buffer.length = 0; 
     234        } 
     235 
     236        public function writeUnsignedInt(value:uint):void 
     237        { 
     238                var buffer:ByteArray = _middleBuffer; 
     239                buffer.writeUnsignedInt(value); 
     240                writeBytes(buffer); 
     241                buffer.length = 0; 
     242        } 
     243 
     244        public function writeUTF(value:String):void 
     245        { 
     246                var buffer:ByteArray = _middleBuffer; 
     247                buffer.writeUTF(value); 
     248                writeBytes(buffer); 
     249                buffer.length = 0; 
     250        } 
     251 
     252        public function writeUTFBytes(value:String):void 
     253        { 
     254                var buffer:ByteArray = _middleBuffer; 
     255                buffer.writeUTFBytes(value); 
     256                writeBytes(buffer); 
     257                buffer.length = 0; 
    196258        } 
    197259} 
     260 
     261} 
  • lang/actionscript/FxTerm/trunk/Terminal.as

    r7117 r7124  
    33 
    44import flash.utils.ByteArray; 
    5 import mx.core.UIComponent; 
    65import flash.utils.IDataOutput; 
    76import flash.events.Event; 
    8 import TerminalEvent; 
     7import flash.events.ProgressEvent; 
     8import flash.text.TextFormat; 
     9 
     10import mx.core.UIComponent; 
     11import mx.events.*; 
    912 
    1013public class Terminal extends UIComponent implements IDataOutput 
    1114{ 
    12         private var term:FxTerm; 
     15        private var _term:FxTerm; 
    1316        private var _dataProvider:Object; 
    1417        private var _buffer:ByteArray; 
     
    1821                super(); 
    1922                _buffer = new ByteArray(); 
    20                 term = new FxTerm(); 
    21                 this.width = term.width; 
    22                 this.height = term.height; 
    23                 addChild(term); 
     23                _term = new FxTerm(); 
     24                this.width = _term.width; 
     25                this.height = _term.height; 
     26                addChild(_term); 
    2427        } 
    2528 
    2629        public function set dataProvider(object:Object):void 
    2730        { 
    28                 _dataProvider.addEventListener(TerminalEvent.TERMINAL_DATA, dataHandler); 
     31                if(_dataProvider) { 
     32                        _dataProvider.removeEventListner( 
     33                                        ProgressEvent.PROGRESS, dataHandler); 
     34                } 
     35                object.addEventListener(ProgressEvent.PROGRESS, dataHandler); 
    2936                _dataProvider = object; 
    30                 if(_dataProvider) { 
    31                         _dataProvider.removeEventListner(TerminalEvent.TERMINAL_DATA, dataHandler); 
    32                 } 
    3337        } 
    3438 
    35         private function dataHandler(event:TerminalEvent):void 
     39        private function dataHandler(event:ProgressEvent):void 
    3640        { 
    3741                var buffer:ByteArray = _buffer; 
     
    3943                writeBytes(buffer); 
    4044                refresh(); 
     45                buffer.length = 0; 
    4146        } 
    4247 
     
    4752        } 
    4853 
    49         public function set fontSize(value:Object):void 
    50                 { term.fontSize = value; } 
     54        private function resized():void 
     55        { 
     56                this.width = _term.width; 
     57                this.height = _term.height; 
     58        } 
     59 
     60        [Bindable] 
     61        public function get charSet():String 
     62                { return _term.charSet; } 
     63        public function set charSet(charset:String):void 
     64                { _term.charSet = charset; } 
     65 
     66        public function set textFormat(format:TextFormat):void 
     67                { _term.textFormat = format; resized(); } 
     68 
     69        [Bindable] 
     70        public function get fontSize():Number 
     71                { return Number(_term.fontSize); } 
     72        public function set fontSize(value:Number):void 
     73                { _term.fontSize = value as Object; resized(); } 
     74 
    5175        public function set foregroundColors(color8:Array):void 
    52                 { term.foregroundColors = color8; } 
     76                { _term.foregroundColors = color8; } 
    5377        public function set backgroundColors(color8:Array):void 
    54                 { term.backgroundColors = color8; } 
     78                { _term.backgroundColors = color8; } 
    5579        public function setForegroundColor(n:uint, color:uint):uint 
    56                 { return term.setForegroundColor(n, color); } 
     80                { return _term.setForegroundColor(n, color); } 
    5781        public function setBackgroundColor(n:uint, color:uint):uint 
    58                 { return term.setBackgroundColor(n, color); } 
     82                { return _term.setBackgroundColor(n, color); } 
    5983 
    6084        public function resize(cols:uint, rows:uint):void 
    61                 { term.resize(cols, rows); changed(); } 
     85                { _term.resize(cols, rows); changed(); resized(); } 
     86 
     87        [Bindable] 
     88        public function get col():uint 
     89                { return _term.col; } 
    6290        public function set col(n:uint):void 
    63                 { term.col = n; changed(); } 
     91                { _term.col = n; changed(); resized(); } 
     92 
     93        [Bindable] 
     94        public function get row():uint 
     95                { return _term.row; } 
    6496        public function set row(n:uint):void 
    65                 { term.row = n; changed(); } 
    66         public function refresh():void 
    67                 { term.refresh(); changed(); } 
     97                { _term.row = n; changed(); resized(); } 
     98 
     99        public function refresh(force:Boolean = true):void 
     100                { _term.refresh(force); changed(); } 
    68101 
    69102        public function set objectEncoding(value:uint):void 
    70                 { term.objectEncoding = value; } 
     103                { _term.objectEncoding = value; } 
    71104        public function get objectEncoding():uint 
    72                 { return term.objectEncoding } 
     105                { return _term.objectEncoding } 
     106 
    73107        public function set endian(value:String):void 
    74                 { term.endian = value; } 
     108                { _term.endian = value; } 
    75109        public function get endian():String 
    76                 { return term.endian; } 
     110                { return _term.endian; } 
     111 
    77112        public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void 
    78                 { term.writeBytes(bytes, offset, length); } 
     113                { _term.writeBytes(bytes, offset, length); } 
    79114        public function writeBoolean(value:Boolean):void 
    80                 { term.writeBoolean(value); } 
     115                { _term.writeBoolean(value); } 
    81116        public function writeByte(value:int):void 
    82                 { term.writeByte(value); } 
     117                { _term.writeByte(value); } 
    83118        public function writeDouble(value:Number):void 
    84                 { term.writeDouble(value); } 
     119                { _term.writeDouble(value); } 
    85120        public function writeFloat(value:Number):void 
    86                 { term.writeFloat(value); } 
     121                { _term.writeFloat(value); } 
    87122        public function writeInt(value:int):void 
    88                 { term.writeInt(value); } 
     123                { _term.writeInt(value); } 
    89124        public function writeMultiByte(value:String, charSet:String):void 
    90                 { term.writeMultiByte(value, charSet); } 
     125                { _term.writeMultiByte(value, charSet); } 
    91126        public function writeObject(object:*):void 
    92                 { term.writeObject(object); } 
     127                { _term.writeObject(object); } 
    93128        public function writeShort(value:int):void 
    94                 { term.writeShort(value); } 
     129                { _term.writeShort(value); } 
    95130        public function writeUnsignedInt(value:uint):void 
    96                 { term.writeUnsignedInt(value); } 
     131                { _term.writeUnsignedInt(value); } 
    97132        public function writeUTF(value:String):void 
    98                 { term.writeUTF(value); } 
     133                { _term.writeUTF(value); } 
    99134        public function writeUTFBytes(value:String):void 
    100                 { term.writeUTFBytes(value); } 
     135                { _term.writeUTFBytes(value); } 
    101136} 
    102137