]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/as3/src/org/apache/thrift/transport/TSocket.as
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / as3 / src / org / apache / thrift / transport / TSocket.as
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.thrift.transport
21 {
22
23 import flash.events.EventDispatcher;
24 import flash.events.Event;
25 import flash.events.IOErrorEvent;
26 import flash.events.ProgressEvent;
27 import flash.events.SecurityErrorEvent;
28 import flash.errors.EOFError;
29 import flash.errors.IOError;
30 import flash.net.URLLoader;
31 import flash.net.URLLoaderDataFormat;
32 import flash.net.URLRequest;
33 import flash.net.URLRequestMethod;
34 import flash.utils.IDataInput;
35 import flash.utils.IDataOutput;
36 import flash.utils.ByteArray;
37 import flash.net.Socket;
38
39
40 /**
41 * Socket implementation of the TTransport interface. Used for working with a
42 * Thrift Socket Server based implementations.
43 */
44
45 public class TSocket extends TTransport
46 {
47 private var socket:Socket = null;
48
49 private var host:String;
50
51 private var port:int;
52
53 private var obuffer:ByteArray = new ByteArray();
54
55 private var input:IDataInput;
56
57 private var output:IDataOutput;
58
59 private var ioCallback:Function = null;
60
61 private var eventDispatcher:EventDispatcher = new EventDispatcher();
62
63 public function TSocket(host:String, port:int):void
64 {
65 this.host = host;
66 this.port = port;
67 }
68
69 public override function close():void
70 {
71 this.input = null;
72 this.output = null;
73 socket.close()
74 }
75
76 public override function peek():Boolean
77 {
78 if(socket.connected)
79 {
80 trace("Bytes remained:" + socket.bytesAvailable);
81 return socket.bytesAvailable>0;
82 }
83 return false;
84 }
85
86 public override function read(buf:ByteArray, off:int, len:int):int
87 {
88 var n1:int = 0, n2:int = 0, n3:int = 0, n4:int = 0, cidx:int = 2;
89 var chunkSize:ByteArray = new ByteArray();
90
91 try
92 {
93 input.readBytes(buf, off, len);
94 return len;
95 }
96 catch (e:EOFError)
97 {
98 trace(e);
99 throw new TTransportError(TTransportError.END_OF_FILE, "No more data available.");
100 }
101 catch (e:IOError)
102 {
103 trace(e);
104 if(isOpen())
105 {
106 throw new TTransportError(TTransportError.UNKNOWN, "IO error while reading: " + e);
107 }
108 else
109 {
110 throw new TTransportError(TTransportError.NOT_OPEN, "Socket seem not to be opened: " + e);
111 }
112 }
113 catch (e:Error)
114 {
115 trace(e);
116 throw new TTransportError(TTransportError.UNKNOWN, "Bad IO error: " + e);
117 }
118 return 0;
119 }
120
121 public override function write(buf:ByteArray, off:int, len:int):void
122 {
123 obuffer.writeBytes(buf, off, len);
124 }
125
126 public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
127 {
128 this.eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
129 }
130
131 public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
132 {
133 this.eventDispatcher.removeEventListener(type, listener, useCapture);
134 }
135
136 public override function open():void
137 {
138 this.socket = new Socket();
139 this.socket.addEventListener(Event.CONNECT, socketConnected);
140 this.socket.addEventListener(IOErrorEvent.IO_ERROR, socketError);
141 this.socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketSecurityError);
142 this.socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
143 this.socket.connect(host, port);
144 }
145
146 public function socketConnected(event:Event):void
147 {
148 this.output = this.socket;
149 this.input = this.socket;
150 this.eventDispatcher.dispatchEvent(event);
151 }
152
153 public function socketError(event:IOErrorEvent):void
154 {
155 trace("Error Connecting:" + event);
156 this.close();
157 if (ioCallback == null)
158 {
159 return;
160 }
161 ioCallback(new TTransportError(TTransportError.UNKNOWN, "IOError: " + event.text));
162 this.eventDispatcher.dispatchEvent(event);
163 }
164
165 public function socketSecurityError(event:SecurityErrorEvent):void
166 {
167 trace("Security Error Connecting:" + event);
168 this.close();
169 this.eventDispatcher.dispatchEvent(event);
170 }
171
172 public function socketDataHandler(event:ProgressEvent):void
173 {
174 if (ioCallback != null)
175 {
176 ioCallback(null);
177 }
178 this.eventDispatcher.dispatchEvent(event);
179 }
180
181 public override function flush(callback:Function = null):void
182 {
183 this.ioCallback = callback;
184 this.output.writeBytes(this.obuffer);
185 this.socket.flush();
186 this.obuffer.clear();
187 }
188
189 public override function isOpen():Boolean
190 {
191 return (this.socket == null ? false : this.socket.connected);
192 }
193 }
194 }