diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/jaegertracing/thrift/lib/haxe | |
parent | Initial commit. (diff) | |
download | ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/jaegertracing/thrift/lib/haxe')
82 files changed, 8179 insertions, 0 deletions
diff --git a/src/jaegertracing/thrift/lib/haxe/README.md b/src/jaegertracing/thrift/lib/haxe/README.md new file mode 100644 index 000000000..c9f74b578 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/README.md @@ -0,0 +1,162 @@ +Thrift Haxe Software Library + +License +======= + +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. + +Using Thrift with Haxe +======================== + +Haxe setup +--------------- + +Thrift requires Haxe 3.1.3. Installers for Windows and OSX +platforms are available at `http://haxe.org/download`. + +Depending on the desired targets, you may have to install the appropriate HaxeLibs +after installing Haxe itself. For example, if you plan to target C#, Java and C++, +enter the following commands after installing Haxe: + + haxelib install hxcpp + haxelib install hxjava + haxelib install hxcs + +For other targets, please consult the Haxe documentation whether or not any additional +target libraries need to be installed and how to achieve this. + + +Haxe on Linux +--------------- + +For Linux platforms it is recommended to use the distro-specific package +manager, where possible. More detailed information can be found at the +Haxe Linux download section: http://haxe.org/download/linux + +If you run into the error message + + Uncaught exception - load.c(237) : Failed to load library : /usr/lib/neko/regexp.ndll + (libpcre.so.3: cannot open shared object file: No such file or directory) + +this can be solved depending on your OSes bitness by either + + sudo ln -sf /usr/lib/libpcre.so.1 /usr/lib/libpcre.so.3 + sudo ldconfig + +or + + sudo ln -sf /usr/lib64/libpcre.so.1 /usr/lib64/libpcre.so.3 + sudo ldconfig + +Thrift Haxe bindings +------------------- + +Thrift Haxe bindings can be set up via the `haxelib` tool +either from the official ASF repo, or via the github mirror. + +- To set up any **stable version**, choose the appropriate branch (e.g. `0.12.0`): + + - `haxelib git thrift https://github.com/apache/thrift.git 0.12.0 lib/haxe` + +- To set up the current **development version**, use the `master` branch: + + - `haxelib git thrift https://github.com/apache/thrift.git master lib/haxe` + +As usual, the installed library can be updated using `haxelib upgrade` +or `haxelib update thrift`. + +In order to work with Thrift, you will need to install the Thrift compiler +or build from source, depending on your operating system. Appropriate +downloads and more information can be found at http://thrift.apache.org + +To get started, visit the /tutorial/haxe and /test/haxe dirs for examples. +If you are using HIDE or the FlashDevelop IDE, you'll find appropriate +project files in these folders. + + +Current status +======================== +- tested with Haxe C++ target +- tested with Haxe PHP target (console/web server, binary protocols) +- transports: Socket, HTTP (servers run inside PHP server/PHP target only), Stream +- protocols: Binary, JSON, Multiplex, Compact +- tutorial client and server available +- cross-test client and server available + + +Further developments +======================== +- improve to work with C#, Java and JavaScript Haxe/OpenFL targets +- improve to work with more (ideally all) Haxe/OpenFL targets +- add HTTP server, update tutorial and tests accordingly + + +Known restrictions +======================== + +Although designed with maximum portability in mind, for technical reasons some platforms +may only support parts of the library, or not be compatible at all. + +Javascript: +- tutorial fails to build because of unsupported Sys.args + +PHP HTTP Server notes +======================== + +- you have to import PHP files generated by haxe into PHP +```php +require_once dirname(__FILE__) . '/bin/php-web-server/Main-debug.php'; +``` + +- trace() by default outputs into stdout (http response), so you have to redirect it to stderr or you own logs, something like +```haxe +//remap trace to error log +haxe.Log.trace = function(v:Dynamic, ?infos:haxe.PosInfos) +{ + //simulate normal trace https://github.com/HaxeFoundation/haxe/blob/development/std/haxe/Log.hx + var newValue : Dynamic; + if (infos != null && infos.customParams!=null) { + var extra:String = ""; + for( v in infos.customParams ) + extra += "," + v; + newValue = v + extra; + } + else { + newValue = v; + } + var msg = infos != null ? infos.fileName + ':' + infos.lineNumber + ': ' : ''; + Sys.stderr().writeString('${msg}${newValue}\n'); +} +``` + +- to allow thrift server to read/write HTTP request/response, it should be pointed out to php streams +```haxe +transport = new TWrappingServerTransport( + new TStreamTransport( + new TFileStream("php://input", Read), + new TFileStream("php://output", Append) + ) + ); +``` + +- TSimpleServer doesn't stop after first call, so processor.process() should be called instead, or use runOnce property +```haxe +var server = new TSimpleServer( processor, transport, transfactory, protfactory); +server.runOnce = true; +``` + diff --git a/src/jaegertracing/thrift/lib/haxe/coding_standards.md b/src/jaegertracing/thrift/lib/haxe/coding_standards.md new file mode 100644 index 000000000..fa0390bb5 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/coding_standards.md @@ -0,0 +1 @@ +Please follow [General Coding Standards](/doc/coding_standards.md) diff --git a/src/jaegertracing/thrift/lib/haxe/haxelib.json b/src/jaegertracing/thrift/lib/haxe/haxelib.json new file mode 100644 index 000000000..b3c15308d --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/haxelib.json @@ -0,0 +1,12 @@ +{ + "name": "thrift", + "url" : "http://thrift.apache.org", + "license": "Apache", + "tags": ["thrift", "rpc", "serialization", "cross", "framework"], + "description": "Haxe bindings for the Apache Thrift RPC and serialization framework", + "version": "0.13.0", + "releasenote": "Licensed under Apache License, Version 2.0. The Apache Thrift compiler needs to be installed separately.", + "contributors": ["Apache Software Foundation (ASF)"], + "dependencies": { }, + "classPath": "src" +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/AbstractMethodError.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/AbstractMethodError.hx new file mode 100644 index 000000000..54b81534a --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/AbstractMethodError.hx @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift; + +#if flash +import flash.errors.IllegalOperationError; +#else +import org.apache.thrift.TException; +#end + +class AbstractMethodError +#if flash +extends IllegalOperationError +#else +extends TException +#end +{ + + public function new(message : String="") { + super("Attempt to call an abstract method"); + } + +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/ArgumentError.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/ArgumentError.hx new file mode 100644 index 000000000..3ca04fdc4 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/ArgumentError.hx @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift; + +#if ! flash +// predefined for flash only +class ArgumentError extends TException { + public function new(msg : String = "") { + super(msg); + } +} +#end diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/Limits.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/Limits.hx new file mode 100644 index 000000000..44eec3a61 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/Limits.hx @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift; + +class Limits { + + // Haxe limits are not fixed values, they depend on the target platform + // For example, neko limits an int to 31 bits instead of 32. So we detect + // the values once during intialisation in order to + // (a) get the right values for the current platform, and + // (b) prevent us from dependecies to a bunch of defines + + public static var I32_MAX = { + var last : Int = 0; + var next : Int = 0; + for(bit in 0 ... 32) { + last = next; + next = last | (1 << bit); + if(next < 0) { + break; + } + } + last; // final value + } + + // add whatever you need +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TApplicationException.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TApplicationException.hx new file mode 100644 index 000000000..7fe844f28 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TApplicationException.hx @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift; + +import org.apache.thrift.protocol.TField; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.protocol.TProtocolUtil; +import org.apache.thrift.protocol.TStruct; +import org.apache.thrift.protocol.TType; + + /** + * Application level exception + */ +class TApplicationException extends TException { + + private static var TAPPLICATION_EXCEPTION_STRUCT = { new TStruct("TApplicationException"); }; + private static var MESSAGE_FIELD = { new TField("message", TType.STRING, 1); }; + private static var TYPE_FIELD = { new TField("type", TType.I32, 2); }; + + // WARNING: These are subject to be extended in the future, so we can't use enums + // with Haxe 3.1.3 because of https://github.com/HaxeFoundation/haxe/issues/3649 + public static inline var UNKNOWN : Int = 0; + public static inline var UNKNOWN_METHOD : Int = 1; + public static inline var INVALID_MESSAGE_TYPE : Int = 2; + public static inline var WRONG_METHOD_NAME : Int = 3; + public static inline var BAD_SEQUENCE_ID : Int = 4; + public static inline var MISSING_RESULT : Int = 5; + public static inline var INTERNAL_ERROR : Int = 6; + public static inline var PROTOCOL_ERROR : Int = 7; + public static inline var INVALID_TRANSFORM : Int = 8; + public static inline var INVALID_PROTOCOL : Int = 9; + public static inline var UNSUPPORTED_CLIENT_TYPE : Int = 10; + + public function new(type : Int = UNKNOWN, message : String = "") { + super(message, type); + } + + public static function read(iprot:TProtocol) : TApplicationException { + var field:TField; + iprot.readStructBegin(); + + var message : String = null; + var type : Int = UNKNOWN; + + while (true) { + field = iprot.readFieldBegin(); + if (field.type == TType.STOP) { + break; + } + switch (field.id) { + case 1: + if (field.type == TType.STRING) { + message = iprot.readString(); + } + else { + TProtocolUtil.skip(iprot, field.type); + } + case 2: + if (field.type == TType.I32) { + type = iprot.readI32(); + } + else { + TProtocolUtil.skip(iprot, field.type); + } + default: + TProtocolUtil.skip(iprot, field.type); + } + iprot.readFieldEnd(); + } + iprot.readStructEnd(); + return new TApplicationException(type, message); + } + + public function write(oprot:TProtocol) : Void { + oprot.writeStructBegin(TAPPLICATION_EXCEPTION_STRUCT); + if (errorMsg != null) { + oprot.writeFieldBegin(MESSAGE_FIELD); + oprot.writeString(errorMsg); + oprot.writeFieldEnd(); + } + oprot.writeFieldBegin(TYPE_FIELD); + oprot.writeI32(errorID); + oprot.writeFieldEnd(); + oprot.writeFieldStop(); + oprot.writeStructEnd(); + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TBase.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TBase.hx new file mode 100644 index 000000000..ede0beb4a --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TBase.hx @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift; + +// Make sure we use at least 3.1.3 +// Some Linux platforms have waaaay too old packages in their repos +// Pro Tip: Look at http://openfl.com for a good Linux install script +#if( haxe_ver < 3.103) +#error Haxe 3.1.3 or newer required, sorry! +#end + +import org.apache.thrift.protocol.TProtocol; + + /** + * Generic base interface for generated Thrift objects. + * + */ +interface TBase { + + /** + * Reads the TObject from the given input protocol. + * + * @param iprot Input protocol + */ + function read(iprot:TProtocol) : Void; + + /** + * Writes the objects out to the protocol + * + * @param oprot Output protocol + */ + function write(oprot:TProtocol) : Void; + + /** + * Check if a field is currently set or unset. + * + * @param fieldId The field's id tag as found in the IDL. + */ + function isSet(fieldId : Int) : Bool; + + /** + * Get a field's value by id. Primitive types will be wrapped in the + * appropriate "boxed" types. + * + * @param fieldId The field's id tag as found in the IDL. + */ + function getFieldValue(fieldId : Int) : Dynamic; + + /** + * Set a field's value by id. Primitive types must be "boxed" in the + * appropriate object wrapper type. + * + * @param fieldId The field's id tag as found in the IDL. + */ + function setFieldValue(fieldId : Int, value : Dynamic) : Void; +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TException.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TException.hx new file mode 100644 index 000000000..54fa1ffef --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TException.hx @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift; + +class TException { + + @:isVar + public var errorID(default,null) : Int; + @:isVar + public var errorMsg(default,null) : String; + + + public function new(msg : String = "", id : Int = 0) { + errorID = id; + errorMsg = msg; + } + +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TFieldRequirementType.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TFieldRequirementType.hx new file mode 100644 index 000000000..039a2cf3a --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TFieldRequirementType.hx @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift; + + /** + * Requirement type constants. + * + */ +@:enum +abstract TFieldRequirementType(Int) from Int to Int { + public static inline var REQUIRED : Int = 1; + public static inline var OPTIONAL : Int = 2; + public static inline var DEFAULT : Int = 3; +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TProcessor.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TProcessor.hx new file mode 100644 index 000000000..0cb6f7d46 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/TProcessor.hx @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift; + +import org.apache.thrift.protocol.TProtocol; + +/** + * A processor is a generic object which operates upon an input stream and + * writes to some output stream. + */ +interface TProcessor { + function process(input:TProtocol, output:TProtocol) : Bool; +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/BitConverter.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/BitConverter.hx new file mode 100644 index 000000000..ee0aaa8e9 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/BitConverter.hx @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.helper; + +import haxe.Int64; +import haxe.io.Bytes; +import haxe.io.BytesBuffer; + +class BitConverter { + + public static function DoubleToInt64Bits( db : Float) : Int64 { + var buf = new BytesBuffer(); + buf.addDouble( db); + return bytesToLong( buf.getBytes()); + } + + + public static function Int64BitsToDouble( i64 : Int64) : Float { + var buf = new BytesBuffer(); + buf.add( fixedLongToBytes( i64)); + return buf.getBytes().getDouble(0); + } + + + + /** + * Convert a long into little-endian bytes in buf starting at off and going + * until off+7. + */ + public static function fixedLongToBytes( n : Int64) : Bytes { + var buf = Bytes.alloc(8); + #if( haxe_ver < 3.2) + buf.set( 0, Int64.getLow( Int64.and( n, Int64.make(0, 0xff)))); + buf.set( 1, Int64.getLow( Int64.and( Int64.shr( n, 8), Int64.make(0, 0xff)))); + buf.set( 2, Int64.getLow( Int64.and( Int64.shr( n, 16), Int64.make(0, 0xff)))); + buf.set( 3, Int64.getLow( Int64.and( Int64.shr( n, 24), Int64.make(0, 0xff)))); + buf.set( 4, Int64.getLow( Int64.and( Int64.shr( n, 32), Int64.make(0, 0xff)))); + buf.set( 5, Int64.getLow( Int64.and( Int64.shr( n, 40), Int64.make(0, 0xff)))); + buf.set( 6, Int64.getLow( Int64.and( Int64.shr( n, 48), Int64.make(0, 0xff)))); + buf.set( 7, Int64.getLow( Int64.and( Int64.shr( n, 56), Int64.make(0, 0xff)))); + #else + buf.set( 0, Int64.and( n, Int64.make(0, 0xff)).low); + buf.set( 1, Int64.and( Int64.shr( n, 8), Int64.make(0, 0xff)).low); + buf.set( 2, Int64.and( Int64.shr( n, 16), Int64.make(0, 0xff)).low); + buf.set( 3, Int64.and( Int64.shr( n, 24), Int64.make(0, 0xff)).low); + buf.set( 4, Int64.and( Int64.shr( n, 32), Int64.make(0, 0xff)).low); + buf.set( 5, Int64.and( Int64.shr( n, 40), Int64.make(0, 0xff)).low); + buf.set( 6, Int64.and( Int64.shr( n, 48), Int64.make(0, 0xff)).low); + buf.set( 7, Int64.and( Int64.shr( n, 56), Int64.make(0, 0xff)).low); + #end + return buf; + } + + /** + * Note that it's important that the mask bytes are long literals, + * otherwise they'll default to ints, and when you shift an int left 56 bits, + * you just get a messed up int. + */ + public static function bytesToLong( bytes : Bytes) : Int64 { + var result : Int64 = Int64.make(0, 0); + result = Int64.or( Int64.shl( result, 8), Int64.make( 0, bytes.get(7))); + result = Int64.or( Int64.shl( result, 8), Int64.make( 0, bytes.get(6))); + result = Int64.or( Int64.shl( result, 8), Int64.make( 0, bytes.get(5))); + result = Int64.or( Int64.shl( result, 8), Int64.make( 0, bytes.get(4))); + result = Int64.or( Int64.shl( result, 8), Int64.make( 0, bytes.get(3))); + result = Int64.or( Int64.shl( result, 8), Int64.make( 0, bytes.get(2))); + result = Int64.or( Int64.shl( result, 8), Int64.make( 0, bytes.get(1))); + result = Int64.or( Int64.shl( result, 8), Int64.make( 0, bytes.get(0))); + return result; + } + + + #if debug + private static function TestBTL( test : Int64) : Void { + var buf : Bytes = fixedLongToBytes( test); + var erg = bytesToLong(buf); + if ( Int64.compare( erg, test) != 0) + throw 'BitConverter.bytesToLongTest($test) failed: $erg'; + } + #end + + + #if debug + private static function TestPair( a : Float, b : Int64) : Void { + var bx = DoubleToInt64Bits(a); + if ( Int64.compare( bx, b) != 0) + throw 'BitConverter.TestPair: DoubleToInt64Bits($a): expected $b, got $bx'; + var ax = Int64BitsToDouble(b); + if( ax != a) + throw 'BitConverter.TestPair: Int64BitsToDouble($b: expected $a, got $ax'; + } + #end + + + #if debug + public static function UnitTest() : Void { + + // bytesToLong() + var i : Int; + TestBTL( Int64.make(0,0)); + for ( i in 0 ... 62) { + TestBTL( Int64.shl( Int64.make(0,1), i)); + TestBTL( Int64.sub( Int64.make(0,0), Int64.shl( Int64.make(0,1), i))); + } + TestBTL( Int64.make(0x7FFFFFFF,0xFFFFFFFF)); + TestBTL( Int64.make(cast(0x80000000,Int),0x00000000)); + + // DoubleToInt64Bits; + TestPair( 1.0000000000000000E+000, Int64.make(cast(0x3FF00000,Int),cast(0x00000000,Int))); + TestPair( 1.5000000000000000E+001, Int64.make(cast(0x402E0000,Int),cast(0x00000000,Int))); + TestPair( 2.5500000000000000E+002, Int64.make(cast(0x406FE000,Int),cast(0x00000000,Int))); + TestPair( 4.2949672950000000E+009, Int64.make(cast(0x41EFFFFF,Int),cast(0xFFE00000,Int))); + TestPair( 3.9062500000000000E-003, Int64.make(cast(0x3F700000,Int),cast(0x00000000,Int))); + TestPair( 2.3283064365386963E-010, Int64.make(cast(0x3DF00000,Int),cast(0x00000000,Int))); + TestPair( 1.2345678901230000E-300, Int64.make(cast(0x01AA74FE,Int),cast(0x1C1E7E45,Int))); + TestPair( 1.2345678901234500E-150, Int64.make(cast(0x20D02A36,Int),cast(0x586DB4BB,Int))); + TestPair( 1.2345678901234565E+000, Int64.make(cast(0x3FF3C0CA,Int),cast(0x428C59FA,Int))); + TestPair( 1.2345678901234567E+000, Int64.make(cast(0x3FF3C0CA,Int),cast(0x428C59FB,Int))); + TestPair( 1.2345678901234569E+000, Int64.make(cast(0x3FF3C0CA,Int),cast(0x428C59FC,Int))); + TestPair( 1.2345678901234569E+150, Int64.make(cast(0x5F182344,Int),cast(0xCD3CDF9F,Int))); + TestPair( 1.2345678901234569E+300, Int64.make(cast(0x7E3D7EE8,Int),cast(0xBCBBD352,Int))); + TestPair( -1.7976931348623157E+308, Int64.make(cast(0xFFEFFFFF,Int),cast(0xFFFFFFFF,Int))); + TestPair( 1.7976931348623157E+308, Int64.make(cast(0x7FEFFFFF,Int),cast(0xFFFFFFFF,Int))); + TestPair( 4.9406564584124654E-324, Int64.make(cast(0x00000000,Int),cast(0x00000001,Int))); + TestPair( 0.0000000000000000E+000, Int64.make(cast(0x00000000,Int),cast(0x00000000,Int))); + TestPair( 4.94065645841247E-324, Int64.make(cast(0x00000000,Int),cast(0x00000001,Int))); + TestPair( 3.2378592100206092E-319, Int64.make(cast(0x00000000,Int),cast(0x0000FFFF,Int))); + TestPair( 1.3906711615669959E-309, Int64.make(cast(0x0000FFFF,Int),cast(0xFFFFFFFF,Int))); + TestPair( Math.NEGATIVE_INFINITY, Int64.make(cast(0xFFF00000,Int),cast(0x00000000,Int))); + TestPair( Math.POSITIVE_INFINITY, Int64.make(cast(0x7FF00000,Int),cast(0x00000000,Int))); + + // NaN is special + var i64nan = DoubleToInt64Bits( Math.NaN); + var i64cmp = Int64.make(cast(0xFFF80000, Int), cast(0x00000000, Int)); + if ( ! Math.isNaN( Int64BitsToDouble( i64cmp))) + throw 'BitConverter NaN-Test #1: expected NaN'; + + // For doubles, a quiet NaN is a bit pattern + // between 7FF8000000000000 and 7FFFFFFFFFFFFFFF + // or FFF8000000000000 and FFFFFFFFFFFFFFFF + var min1 = Int64.make( cast(0x7FF80000, Int), cast(0x00000000, Int)); + var max1 = Int64.make( cast(0x7FFFFFFF, Int), cast(0xFFFFFFFF, Int)); + var min2 = Int64.make( cast(0xFFF80000, Int), cast(0x00000000, Int)); + var max2 = Int64.make( cast(0xFFFFFFFF, Int), cast(0xFFFFFFFF, Int)); + var ok1 = (Int64.compare( min1, i64nan) <= 0) && (Int64.compare( i64nan, max1) <= 0); + var ok2 = (Int64.compare( min2, i64nan) <= 0) && (Int64.compare( i64nan, max2) <= 0); + if( ! (ok1 || ok2)) + throw 'BitConverter NaN-Test #2: failed'; + } + #end + +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/Int64Map.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/Int64Map.hx new file mode 100644 index 000000000..8845fd095 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/Int64Map.hx @@ -0,0 +1,295 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.helper; + +import Map; +import haxe.Int64; +import haxe.ds.IntMap; + + +// Int64Map allows mapping of Int64 keys to arbitrary values. +// ObjectMap<> cannot be used, since we want to compare by value, not address + +class Int64Map<T> implements IMap< Int64, T> { + + private var SubMaps : IntMap< IntMap< T>>; // Hi -> Lo -> Value + + public function new() : Void { + SubMaps = new IntMap< IntMap< T>>(); + }; + + private function GetSubMap( hi : haxe.Int32, canCreate : Bool) : IntMap< T> { + if( SubMaps.exists(hi)) { + return SubMaps.get(hi); + } + + if( ! canCreate) { + return null; + } + + var lomap = new IntMap< T>(); + SubMaps.set( hi, lomap); + return lomap; + } + + + private function GetLowMap( key : haxe.Int64, canCreate : Bool) : IntMap< T> { + #if( haxe_ver < 3.2) + return GetSubMap( Int64.getHigh(key), canCreate); + #else + return GetSubMap( key.high, canCreate); + #end + } + + + private function GetLowIndex( key : haxe.Int64) : haxe.Int32 { + #if( haxe_ver < 3.2) + return Int64.getLow(key); + #else + return key.low; + #end + } + + + private function NullCheck( key : haxe.Int64) : Bool { + #if( haxe_ver < 3.2) + return (key != null); + #else + return true; // Int64 is not nullable anymore (it never really was) + #end + }; + + + + /** + Maps `key` to `value`. + If `key` already has a mapping, the previous value disappears. + If `key` is null, the result is unspecified. + **/ + public function set( key : Int64, value : T ) : Void { + if( ! NullCheck(key)) { + return; + } + + var lomap = GetLowMap( key, true); + lomap.set( GetLowIndex(key), value); + } + + + /** + Returns the current mapping of `key`. + If no such mapping exists, null is returned. + If `key` is null, the result is unspecified. + + Note that a check like `map.get(key) == null` can hold for two reasons: + + 1. the map has no mapping for `key` + 2. the map has a mapping with a value of `null` + + If it is important to distinguish these cases, `exists()` should be + used. + + **/ + public function get( key : Int64) : Null<T> { + if( ! NullCheck(key)) { + return null; + } + + var lomap = GetLowMap( key, true); + if( lomap == null) { + return null; + } + + return lomap.get( GetLowIndex(key)); + } + + /** + Returns true if `key` has a mapping, false otherwise. + If `key` is null, the result is unspecified. + **/ + public function exists( key : Int64) : Bool { + if( ! NullCheck(key)) { + return false; + } + + var lomap = GetLowMap( key, true); + if( lomap == null) { + return false; + } + + return lomap.exists( GetLowIndex(key)); + } + + /** + Removes the mapping of `key` and returns true if such a mapping existed, + false otherwise. If `key` is null, the result is unspecified. + **/ + public function remove( key : Int64) : Bool { + if( ! NullCheck(key)) { + return false; + } + + var lomap = GetLowMap( key, true); + if( lomap == null) { + return false; + } + + return lomap.remove( GetLowIndex(key)); + } + + + /** + Returns an Iterator over the keys of `this` Map. + The order of keys is undefined. + **/ + public function keys() : Iterator<Int64> { + return new Int64KeyIterator<T>(SubMaps); + } + + /** + Returns an Iterator over the values of `this` Map. + The order of values is undefined. + **/ + public function iterator() : Iterator<T> { + return new Int64ValueIterator<T>(SubMaps); + } + + /** + Returns a String representation of `this` Map. + The exact representation depends on the platform and key-type. + **/ + public function toString() : String { + var result : String = "{"; + + var first = true; + for( key in this.keys()) { + if( first) { + first = false; + } else { + result += ","; + } + + result += " "; + var value = this.get(key); + result += Int64.toStr(key) + ' => $value'; + } + + return result + "}"; + } + +} + + +// internal helper class for Int64Map<T> +// all class with matching methods can be used as iterator (duck typing) +private class Int64MapIteratorBase<T> { + + private var SubMaps : IntMap< IntMap< T>>; // Hi -> Lo -> Value + + private var HiIterator : Iterator< Int> = null; + private var LoIterator : Iterator< Int> = null; + private var CurrentHi : Int = 0; + + public function new( data : IntMap< IntMap< T>>) : Void { + SubMaps = data; + HiIterator = SubMaps.keys(); + LoIterator = null; + CurrentHi = 0; + }; + + /** + Returns false if the iteration is complete, true otherwise. + + Usually iteration is considered to be complete if all elements of the + underlying data structure were handled through calls to next(). However, + in custom iterators any logic may be used to determine the completion + state. + **/ + public function hasNext() : Bool { + + if( (LoIterator != null) && LoIterator.hasNext()) { + return true; + } + + while( (HiIterator != null) && HiIterator.hasNext()) { + CurrentHi = HiIterator.next(); + LoIterator = SubMaps.get(CurrentHi).keys(); + if( (LoIterator != null) && LoIterator.hasNext()) { + return true; + } + } + + HiIterator = null; + LoIterator = null; + return false; + } + +} + + +// internal helper class for Int64Map<T> +// all class with matching methods can be used as iterator (duck typing) +private class Int64KeyIterator<T>extends Int64MapIteratorBase<T> { + + public function new( data : IntMap< IntMap< T>>) : Void { + super(data); + }; + + /** + Returns the current item of the Iterator and advances to the next one. + + This method is not required to check hasNext() first. A call to this + method while hasNext() is false yields unspecified behavior. + **/ + public function next() : Int64 { + if( hasNext()) { + return Int64.make( CurrentHi, LoIterator.next()); + } else { + throw "no more elements"; + } + } +} + + +// internal helper class for Int64Map<T> +// all class with matching methods can be used as iterator (duck typing) +private class Int64ValueIterator<T> extends Int64MapIteratorBase<T> { + + public function new( data : IntMap< IntMap< T>>) : Void { + super(data); + }; + + /** + Returns the current item of the Iterator and advances to the next one. + + This method is not required to check hasNext() first. A call to this + method while hasNext() is false yields unspecified behavior. + **/ + public function next() : T { + if( hasNext()) { + return SubMaps.get(CurrentHi).get(LoIterator.next()); + } else { + throw "no more elements"; + } + } +} + + +// EOF diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/IntSet.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/IntSet.hx new file mode 100644 index 000000000..91e5d8e97 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/IntSet.hx @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.helper; + +import Map; + + +class IntSet { + + private var _elements = new haxe.ds.IntMap<Int>(); + private var _size : Int = 0; + public var size(get,never) : Int; + + public function new( values : Array<Int> = null) { + if ( values != null) { + for ( value in values) { + add(value); + } + } + } + + public function iterator():Iterator<Int> { + return _elements.keys(); + } + + public function traceAll() : Void { + trace('$_size entries'); + for(entry in this) { + var yes = contains(entry); + trace('- $entry, contains() = $yes'); + } + } + + public function add(o : Int) : Bool { + if( _elements.exists(o)) { + return false; + } + _size++; + _elements.set(o,_size); + return true; + } + + public function clear() : Void { + while( _size > 0) { + remove( _elements.keys().next()); + } + } + + public function contains(o : Int) : Bool { + return _elements.exists(o); + } + + public function isEmpty() : Bool { + return _size == 0; + } + + public function remove(o : Int) : Bool { + if (contains(o)) { + _elements.remove(o); + _size--; + return true; + } else { + return false; + } + } + + public function toArray() : Array<Int> { + var ret : Array<Int> = new Array<Int>(); + for (key in _elements.keys()) { + ret.push(key); + } + return ret; + } + + public function get_size() : Int { + return _size; + } +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/ObjectSet.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/ObjectSet.hx new file mode 100644 index 000000000..bcf72fb7f --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/ObjectSet.hx @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.helper; + +import Map; + + +class ObjectSet<K> { + + private var _elements = new haxe.ds.ObjectMap<K,Int>(); + private var _size : Int = 0; + public var size(get,never) : Int; + + public function new( values : Array<K> = null) { + if ( values != null) { + for ( value in values) { + add(value); + } + } + } + + public function iterator():Iterator<K> { + return _elements.keys(); + } + + public function traceAll() : Void { + trace('$_size entries'); + for(entry in this) { + var yes = contains(entry); + trace('- $entry, contains() = $yes'); + } + } + + public function add(o : K) : Bool { + if( _elements.exists(o)) { + return false; + } + _size++; + _elements.set(o,_size); + return true; + } + + public function clear() : Void { + while( _size > 0) { + remove( _elements.keys().next()); + } + } + + public function contains(o : K) : Bool { + return _elements.exists(o); + } + + public function isEmpty() : Bool { + return _size == 0; + } + + public function remove(o : K) : Bool { + if (contains(o)) { + _elements.remove(o); + _size--; + return true; + } else { + return false; + } + } + + public function toArray() : Array<K> { + var ret : Array<K> = new Array<K>(); + for (key in _elements.keys()) { + ret.push(key); + } + return ret; + } + + public function get_size() : Int { + return _size; + } +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/StringSet.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/StringSet.hx new file mode 100644 index 000000000..d8c0d90af --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/StringSet.hx @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.helper; + +import Map; + + +class StringSet { + + private var _elements = new haxe.ds.StringMap<Int>(); + private var _size : Int = 0; + public var size(get,never) : Int; + + public function new( values : Array<String> = null) { + if ( values != null) { + for ( value in values) { + add(value); + } + } + } + + public function iterator():Iterator<String> { + return _elements.keys(); + } + + public function traceAll() : Void { + trace('$_size entries'); + for(entry in this) { + var yes = contains(entry); + trace('- $entry, contains() = $yes'); + } + } + + public function add(o : String) : Bool { + if( _elements.exists(o)) { + return false; + } + _size++; + _elements.set(o,_size); + return true; + } + + public function clear() : Void { + while( _size > 0) { + remove( _elements.keys().next()); + } + } + + public function contains(o : String) : Bool { + return _elements.exists(o); + } + + public function isEmpty() : Bool { + return _size == 0; + } + + public function remove(o : String) : Bool { + if (contains(o)) { + _elements.remove(o); + _size--; + return true; + } else { + return false; + } + } + + public function toArray() : Array<String> { + var ret : Array<String> = new Array<String>(); + for (key in _elements.keys()) { + ret.push(key); + } + return ret; + } + + public function get_size() : String { + return _size; + } +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/ZigZag.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/ZigZag.hx new file mode 100644 index 000000000..3f8601beb --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/helper/ZigZag.hx @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.helper; + +import haxe.Int64; +import haxe.Int32; + +class ZigZag { + + /** + * Convert n into a zigzag int. This allows negative numbers to be + * represented compactly as a varint. + */ + public static function FromInt( n : Int) : UInt { + #if php + + return cast(cast(cast(n,Int32) << 1,Int32) ^ cast(cast(n,Int32) >> 31,Int32),UInt); + + #else + + return cast(n << 1,UInt) ^ cast(n >> 31,UInt); + + #end + } + + + /** + * Convert from zigzag int to int. + */ + public static function ToInt( n : UInt) : Int { + #if php + + var a = (0x7FFFFFFF & cast(n >> 1,Int)); + var b = (cast(n & 1,Int)); + b = -b; // workaround for https://github.com/HaxeFoundation/haxe/issues/5288 + return a ^ b; + + #else + + return (0x7FFFFFFF & cast(n >> 1,Int)) ^ (-cast(n & 1,Int)); + + #end + } + + + /** + * Convert l into a zigzag long. This allows negative numbers to be + * represented compactly as a varint. + */ + public static function FromLong( n : Int64) : Int64 { + return Int64.xor( Int64.shl(n, 1), Int64.shr(n, 63)); + } + + + /** + * Convert from zigzag long to long. + */ + public static function ToLong( n : Int64) : Int64 { + return Int64.xor( + Int64.and( + Int64.shr(n, 1), + Int64.make(0x7FFFFFFF, 0xFFFFFFFF)), + Int64.sub( + Int64.make(0, 0), + Int64.and(n, Int64.make(0,1)))); + } + + + #if debug + private static function Test32( test : Int) : Void { + var a : UInt = ZigZag.FromInt( test); + var b : Int = ZigZag.ToInt(a); + #if php + test = test & 0xFFFFFFFF; // workaround for https://github.com/HaxeFoundation/haxe/issues/5289 + #end + if( test != b) + throw 'ZigZag.Test32($test) failed: a = $a, b = $b'; + } + #end + + + + #if debug + private static function Test64( test : haxe.Int64) : Void { + var a : Int64 = ZigZag.FromLong( test); + var b : Int64 = ZigZag.ToLong(a); + if( Int64.compare( test, b) != 0) + throw 'ZigZag.Test64($test) failed: a = $a, b = $b'; + } + #end + + + #if debug + public static function UnitTest() : Void { + var u1 : UInt = 0xFFFFFFFE; + var u2 : UInt = 0xFFFFFFFF; + var i1 : Int = 2147483647; + var i2 : Int = -2147483648; + + #if php + i2 = i2 & 0xFFFFFFFF; // workaround for https://github.com/HaxeFoundation/haxe/issues/5289 + #end + + // protobuf testcases + if( FromInt(0) != 0) throw 'pb #1 to ZigZag'; + if( FromInt(-1) != 1) throw 'pb #2 to ZigZag'; + if( FromInt(1) != 2) throw 'pb #3 to ZigZag'; + if( FromInt(-2) != 3) throw 'pb #4 to ZigZag'; + if( FromInt(i1) != u1) throw 'pb #5 to ZigZag'; + if( FromInt(i2) != u2) throw 'pb #6 to ZigZag'; + + // protobuf testcases + if( ToInt(0) != 0) throw 'pb #1 from ZigZag'; + if( ToInt(1) != -1) throw 'pb #2 from ZigZag'; + if( ToInt(2) != 1) throw 'pb #3 from ZigZag'; + if( ToInt(3) != -2) throw 'pb #4 from ZigZag'; + if( ToInt(u1) != i1) throw 'pb #5 from ZigZag, got ${ToInt(u1)} expected $i1'; + if( ToInt(u2) != i2) throw 'pb #6 from ZigZag, got ${ToInt(u2)} expected $i2'; + + // back and forth 32 + Test32( 0); + for( i in 0 ... 30) { + Test32( 1 << i); + Test32( -(1 << i)); + } + Test32( 0x7FFFFFFF); + Test32( cast(0x80000000,Int)); + + // back and forth 64 + Test64( Int64.make(0,0)); + for( i in 0 ... 62) { + Test64( Int64.shl( Int64.make(0,1), i)); + Test64( Int64.sub( Int64.make(0,0), Int64.shl( Int64.make(0,1), i))); + } + Test64( Int64.make(0x7FFFFFFF,0xFFFFFFFF)); + Test64( Int64.make(cast(0x80000000,Int),0x00000000)); + } + #end +} + +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/FieldMetaData.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/FieldMetaData.hx new file mode 100644 index 000000000..26db1134e --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/FieldMetaData.hx @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.meta_data; + +import flash.utils.Dictionary; + +/** +* This class is used to store meta data about thrift fields. Every field in a +* a struct should have a corresponding instance of this class describing it. +* +*/ +class FieldMetaData { + + public var fieldName : String; + public var requirementType : Int; + public var valueMetaData:FieldValueMetaData; + + private static var structMap:Dictionary = new Dictionary(); + + public function FieldMetaData(name : String, req : Int, vMetaData:FieldValueMetaData) { + this.fieldName = name; + this.requirementType = req; + this.valueMetaData = vMetaData; + } + + public static function addStructMetaDataMap(sClass:Class, map:Dictionary) : Void{ + structMap[sClass] = map; + } + + /** + * Returns a map with metadata (i.e. instances of FieldMetaData) that + * describe the fields of the given class. + * + * @param sClass The TBase class for which the metadata map is requested + */ + public static function getStructMetaDataMap(sClass:Class):Dictionary { + return structMap[sClass]; + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/FieldValueMetaData.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/FieldValueMetaData.hx new file mode 100644 index 000000000..8879d9156 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/FieldValueMetaData.hx @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.meta_data; + +import org.apache.thrift.protocol.TType; + +/** + * FieldValueMetaData and collection of subclasses to store metadata about + * the value(s) of a field + */ +class FieldValueMetaData { + + public var type : Int; + + public function FieldValueMetaData(type : Int) { + this.type = type; + } + + public function isStruct() : Bool { + return type == TType.STRUCT; + } + + public function isContainer() : Bool { + return type == TType.LIST || type == TType.MAP || type == TType.SET; + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/ListMetaData.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/ListMetaData.hx new file mode 100644 index 000000000..40ca31bad --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/ListMetaData.hx @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.meta_data; + +class ListMetaData extends FieldValueMetaData { + + public var elemMetaData:FieldValueMetaData; + + public function ListMetaData(type : Int, eMetaData:FieldValueMetaData) { + super(type); + this.elemMetaData = eMetaData; + } +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/MapMetaData.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/MapMetaData.hx new file mode 100644 index 000000000..5463e6276 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/MapMetaData.hx @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.meta_data; + +class MapMetaData extends FieldValueMetaData { + + public var keyMetaData:FieldValueMetaData; + public var valueMetaData:FieldValueMetaData; + + public function MapMetaData(type : Int, kMetaData:FieldValueMetaData, vMetaData:FieldValueMetaData) { + super(type); + this.keyMetaData = kMetaData; + this.valueMetaData = vMetaData; + } +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/SetMetaData.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/SetMetaData.hx new file mode 100644 index 000000000..a3367f449 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/SetMetaData.hx @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.meta_data; + +class SetMetaData extends FieldValueMetaData { + + public var elemMetaData:FieldValueMetaData; + + public function SetMetaData(type : Int, eMetaData:FieldValueMetaData) { + super(type); + this.elemMetaData = eMetaData; + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/StructMetaData.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/StructMetaData.hx new file mode 100644 index 000000000..1822dd37f --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/meta_data/StructMetaData.hx @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.meta_data; + +class StructMetaData extends FieldValueMetaData { + + public var structClass:Class; + + public function StructMetaData(type : Int, sClass:Class) { + super(type); + this.structClass = sClass; + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TBinaryProtocol.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TBinaryProtocol.hx new file mode 100644 index 000000000..7ef291c0e --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TBinaryProtocol.hx @@ -0,0 +1,301 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import haxe.io.Bytes; +import haxe.io.BytesInput; +import haxe.io.BytesOutput; +import haxe.io.BytesBuffer; +import haxe.Int64; + +import org.apache.thrift.TException; +import org.apache.thrift.transport.TTransport; + +/** +* Binary protocol implementation for thrift. +*/ +class TBinaryProtocol extends TRecursionTracker implements TProtocol { + + private static var ANONYMOUS_STRUCT:TStruct = new TStruct(); + + private static inline var VERSION_MASK : haxe.Int32 = 0xffff0000; + private static inline var VERSION_1 : haxe.Int32 = 0x80010000; + + private var strictRead_ : Bool = false; + private var strictWrite_ : Bool = true; + private var trans_ : TTransport; + + /** + * Constructor + */ + public function new(trans:TTransport, strictRead : Bool=false, strictWrite : Bool=true) { + trans_ = trans; + strictRead_ = strictRead; + strictWrite_ = strictWrite; + } + + public function getTransport():TTransport { + return trans_; + } + + public function writeMessageBegin(message:TMessage) : Void { + if (strictWrite_) { + var version : Int = VERSION_1 | message.type; + writeI32(version); + writeString(message.name); + writeI32(message.seqid); + } else { + writeString(message.name); + writeByte(message.type); + writeI32(message.seqid); + } + } + + public function writeMessageEnd() : Void {} + + public function writeStructBegin(struct:TStruct) : Void {} + + public function writeStructEnd() : Void {} + + public function writeFieldBegin(field:TField) : Void { + writeByte(field.type); + writeI16(field.id); + } + + public function writeFieldEnd() : Void {} + + public function writeFieldStop() : Void { + writeByte(TType.STOP); + } + + public function writeMapBegin(map:TMap) : Void { + writeByte(map.keyType); + writeByte(map.valueType); + writeI32(map.size); + } + + public function writeMapEnd() : Void {} + + public function writeListBegin(list:TList) : Void { + writeByte(list.elemType); + writeI32(list.size); + } + + public function writeListEnd() : Void {} + + public function writeSetBegin(set:TSet) : Void { + writeByte(set.elemType); + writeI32(set.size); + } + + public function writeSetEnd() : Void {} + + public function writeBool(b : Bool) : Void { + writeByte(b ? 1 : 0); + } + + + public function writeByte(b : Int) : Void { + var out = new BytesOutput(); + out.bigEndian = true; + out.writeByte(b); + trans_.write(out.getBytes(), 0, 1); + } + + public function writeI16(i16 : Int) : Void { + var out = new BytesOutput(); + out.bigEndian = true; + out.writeInt16(i16); + trans_.write(out.getBytes(), 0, 2); + } + + public function writeI32(i32 : Int) : Void { + var out = new BytesOutput(); + out.bigEndian = true; + out.writeInt32(i32); + trans_.write(out.getBytes(), 0, 4); + } + + public function writeI64(i64 : haxe.Int64) : Void { + var out = new BytesOutput(); + out.bigEndian = true; + #if( haxe_ver < 3.2) + var hi = Int64.getHigh(i64); + var lo = Int64.getLow(i64); + out.writeInt32(hi); + out.writeInt32(lo); + #else + out.writeInt32(i64.high); + out.writeInt32(i64.low); + #end + trans_.write(out.getBytes(), 0, 8); + } + + public function writeDouble(dub:Float) : Void { + var out = new BytesOutput(); + out.bigEndian = true; + out.writeDouble(dub); + trans_.write(out.getBytes(), 0, 8); + } + + public function writeString(str : String) : Void { + var out = new BytesOutput(); + out.bigEndian = true; + out.writeString(str); + var bytes = out.getBytes(); + writeI32( bytes.length); + trans_.write( bytes, 0, bytes.length); + } + + public function writeBinary(bin:Bytes) : Void { + writeI32(bin.length); + trans_.write(bin, 0, bin.length); + } + + /** + * Reading methods. + */ + + public function readMessageBegin():TMessage { + var size : Int = readI32(); + if (size < 0) { + var version : Int = size & VERSION_MASK; + if (version != VERSION_1) { + throw new TProtocolException(TProtocolException.BAD_VERSION, "Bad version in readMessageBegin"); + } + return new TMessage(readString(), size & 0x000000ff, readI32()); + } else { + if (strictRead_) { + throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?"); + } + return new TMessage(readStringBody(size), readByte(), readI32()); + } + } + + public function readMessageEnd() : Void {} + + public function readStructBegin():TStruct { + return ANONYMOUS_STRUCT; + } + + public function readStructEnd() : Void {} + + public function readFieldBegin() : TField { + var type : Int = readByte(); + var id : Int = 0; + if (type != TType.STOP) + { + id = readI16(); + } + return new TField("", type, id); + } + + public function readFieldEnd() : Void {} + + public function readMapBegin() : TMap { + return new TMap(readByte(), readByte(), readI32()); + } + + public function readMapEnd() : Void {} + + public function readListBegin():TList { + return new TList(readByte(), readI32()); + } + + public function readListEnd() : Void {} + + public function readSetBegin() : TSet { + return new TSet(readByte(), readI32()); + } + + public function readSetEnd() : Void {} + + public function readBool() : Bool { + return (readByte() == 1); + } + + + public function readByte() : Int { + var buffer = new BytesBuffer(); + var len = trans_.readAll( buffer, 0, 1); + var inp = new BytesInput( buffer.getBytes(), 0, 1); + inp.bigEndian = true; + return inp.readByte(); + } + + public function readI16() : Int { + var buffer = new BytesBuffer(); + var len = trans_.readAll( buffer, 0, 2); + var inp = new BytesInput( buffer.getBytes(), 0, 2); + inp.bigEndian = true; + return inp.readInt16(); + } + + public function readI32() : Int { + var buffer = new BytesBuffer(); + var len = trans_.readAll( buffer, 0, 4); + var inp = new BytesInput( buffer.getBytes(), 0, 4); + inp.bigEndian = true; + return inp.readInt32(); + } + + public function readI64() : haxe.Int64 { + var buffer = new BytesBuffer(); + var len = trans_.readAll( buffer, 0, 8); + var inp = new BytesInput( buffer.getBytes(), 0, 8); + inp.bigEndian = true; + var hi = inp.readInt32(); + var lo = inp.readInt32(); + return Int64.make(hi,lo); + } + + public function readDouble():Float { + var buffer = new BytesBuffer(); + var len = trans_.readAll( buffer, 0, 8); + var inp = new BytesInput( buffer.getBytes(), 0, 8); + inp.bigEndian = true; + return inp.readDouble(); + } + + public function readString() : String { + return readStringBody( readI32()); + } + + public function readStringBody(len : Int) : String { + if( len > 0) { + var buffer = new BytesBuffer(); + trans_.readAll( buffer, 0, len); + var inp = new BytesInput( buffer.getBytes(), 0, len); + inp.bigEndian = true; + return inp.readString(len); + } else { + return ""; + } + } + + public function readBinary() : Bytes { + var len : Int = readI32(); + var buffer = new BytesBuffer(); + trans_.readAll( buffer, 0, len); + return buffer.getBytes(); + } + +} + diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TBinaryProtocolFactory.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TBinaryProtocolFactory.hx new file mode 100644 index 000000000..f4a9becef --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TBinaryProtocolFactory.hx @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import org.apache.thrift.transport.TTransport; + + +/** +* Binary Protocol Factory +*/ +class TBinaryProtocolFactory implements TProtocolFactory { + + private var strictRead_ : Bool = false; + private var strictWrite_ : Bool = true; + + public function new( strictRead : Bool = false, strictWrite : Bool = true) { + strictRead_ = strictRead; + strictWrite_ = strictWrite; + } + + public function getProtocol( trans : TTransport) : TProtocol { + return new TBinaryProtocol( trans, strictRead_, strictWrite_); + } +} + + + +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactProtocol.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactProtocol.hx new file mode 100644 index 000000000..03b13e2f6 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactProtocol.hx @@ -0,0 +1,718 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import haxe.io.Bytes; +import haxe.io.BytesInput; +import haxe.io.BytesOutput; +import haxe.io.BytesBuffer; +import haxe.ds.GenericStack; +import haxe.Int32; +import haxe.Int64; +import haxe.Utf8; + +import org.apache.thrift.TException; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.helper.ZigZag; +import org.apache.thrift.helper.BitConverter; + + +/** +* Compact protocol implementation for thrift. +*/ +class TCompactProtocol extends TRecursionTracker implements TProtocol { + + private static var ANONYMOUS_STRUCT : TStruct = new TStruct(""); + private static var TSTOP : TField = new TField("", TType.STOP, 0); + + private static inline var PROTOCOL_ID : Int = 0x82; + private static inline var VERSION : Int = 1; + private static inline var VERSION_MASK : Int = 0x1f; // 0001 1111 + private static inline var TYPE_MASK : Int = 0xE0; // 1110 0000 + private static inline var TYPE_BITS : Int = 0x07; // 0000 0111 + private static inline var TYPE_SHIFT_AMOUNT : Int = 5; + + + private static var ttypeToCompactType = [ + TType.STOP => TCompactTypes.STOP, + TType.BOOL => TCompactTypes.BOOLEAN_TRUE, + TType.BYTE => TCompactTypes.BYTE, + TType.DOUBLE => TCompactTypes.DOUBLE, + TType.I16 => TCompactTypes.I16, + TType.I32 => TCompactTypes.I32, + TType.I64 => TCompactTypes.I64, + TType.STRING => TCompactTypes.BINARY, + TType.STRUCT => TCompactTypes.STRUCT, + TType.MAP => TCompactTypes.MAP, + TType.SET => TCompactTypes.SET, + TType.LIST => TCompactTypes.LIST + ]; + + private static var tcompactTypeToType = [ + TCompactTypes.STOP => TType.STOP, + TCompactTypes.BOOLEAN_TRUE => TType.BOOL, + TCompactTypes.BOOLEAN_FALSE => TType.BOOL, + TCompactTypes.BYTE => TType.BYTE, + TCompactTypes.I16 => TType.I16, + TCompactTypes.I32 => TType.I32, + TCompactTypes.I64 => TType.I64, + TCompactTypes.DOUBLE => TType.DOUBLE, + TCompactTypes.BINARY => TType.STRING, + TCompactTypes.LIST => TType.LIST, + TCompactTypes.SET => TType.SET, + TCompactTypes.MAP => TType.MAP, + TCompactTypes.STRUCT => TType.STRUCT + ]; + + + /** + * Used to keep track of the last field for the current and previous structs, + * so we can do the delta stuff. + */ + private var lastField_ : GenericStack<Int> = new GenericStack<Int>(); + private var lastFieldId_ : Int = 0; + + /** + * If we encounter a boolean field begin, save the TField here so it can + * have the value incorporated. + */ + private var booleanField_ : Null<TField>; + + /** + * If we Read a field header, and it's a boolean field, save the boolean + * value here so that ReadBool can use it. + */ + private var boolValue_ : Null<Bool>; + + + // whether the underlying system holds Strings as UTF-8 + // http://old.haxe.org/manual/encoding + private static var utf8Strings = haxe.Utf8.validate("Ç-ß-Æ-Ю-Ш"); + + // the transport used + public var trans(default,null) : TTransport; + + + // TCompactProtocol Constructor + public function new( trans : TTransport) { + this.trans = trans; + } + + public function getTransport() : TTransport { + return trans; + } + + + public function Reset() : Void{ + while ( ! lastField_.isEmpty()) { + lastField_.pop(); + } + lastFieldId_ = 0; + } + + + /** + * Writes a byte without any possibility of all that field header nonsense. + * Used internally by other writing methods that know they need to Write a byte. + */ + private function WriteByteDirect( b : Int) : Void { + var buf = Bytes.alloc(1); + buf.set( 0, b); + trans.write( buf, 0, 1); + } + + /** + * Write an i32 as a varint. Results in 1-5 bytes on the wire. + */ + private function WriteVarint32( n : UInt) : Void { + var i32buf = new BytesBuffer(); + while (true) + { + if ((n & ~0x7F) == 0) + { + i32buf.addByte( n & 0xFF); + break; + } + else + { + i32buf.addByte( (n & 0x7F) | 0x80); + n >>= 7; + } + } + + var tmp = i32buf.getBytes(); + trans.write( tmp, 0, tmp.length); + } + + /** + * Write a message header to the wire. Compact Protocol messages contain the + * protocol version so we can migrate forwards in the future if need be. + */ + public function writeMessageBegin( message : TMessage) : Void { + Reset(); + + var versionAndType : Int = (VERSION & VERSION_MASK) | ((message.type << TYPE_SHIFT_AMOUNT) & TYPE_MASK); + WriteByteDirect( PROTOCOL_ID); + WriteByteDirect( versionAndType); + WriteVarint32( cast( message.seqid, UInt)); + writeString( message.name); + } + + /** + * Write a struct begin. This doesn't actually put anything on the wire. We + * use it as an opportunity to put special placeholder markers on the field + * stack so we can get the field id deltas correct. + */ + public function writeStructBegin(struct:TStruct) : Void { + lastField_.add( lastFieldId_); + lastFieldId_ = 0; + } + + /** + * Write a struct end. This doesn't actually put anything on the wire. We use + * this as an opportunity to pop the last field from the current struct off + * of the field stack. + */ + public function writeStructEnd() : Void { + lastFieldId_ = lastField_.pop(); + } + + /** + * Write a field header containing the field id and field type. If the + * difference between the current field id and the last one is small (< 15), + * then the field id will be encoded in the 4 MSB as a delta. Otherwise, the + * field id will follow the type header as a zigzag varint. + */ + public function writeFieldBegin(field:TField) : Void { + if (field.type == TType.BOOL) + booleanField_ = field; // we want to possibly include the value, so we'll wait. + else + WriteFieldBeginInternal(field, 0xFF); + } + + /** + * The workhorse of WriteFieldBegin. It has the option of doing a + * 'type override' of the type header. This is used specifically in the + * boolean field case. + */ + private function WriteFieldBeginInternal( field : TField, typeOverride : Int) : Void { + // if there's a type override, use that. + var typeToWrite : Int; + if ( typeOverride == 0xFF) + typeToWrite = getCompactType( field.type); + else + typeToWrite = typeOverride; + + // check if we can use delta encoding for the field id + if (field.id > lastFieldId_ && field.id - lastFieldId_ <= 15) + { + // Write them together + WriteByteDirect((field.id - lastFieldId_) << 4 | typeToWrite); + } + else + { + // Write them separate + WriteByteDirect(typeToWrite); + writeI16(field.id); + } + + lastFieldId_ = field.id; + } + + /** + * Write the STOP symbol so we know there are no more fields in this struct. + */ + public function writeFieldStop() : Void { + WriteByteDirect( cast(TCompactTypes.STOP, Int)); + } + + /** + * Write a map header. If the map is empty, omit the key and value type + * headers, as we don't need any additional information to skip it. + */ + public function writeMapBegin(map:TMap) : Void { + if (map.size == 0) + { + WriteByteDirect(0); + } + else + { + var kvtype = (getCompactType(map.keyType) << 4) | getCompactType(map.valueType); + WriteVarint32( cast( map.size, UInt)); + WriteByteDirect( kvtype); + } + } + + /** + * Write a list header. + */ + public function writeListBegin( list : TList) : Void { + WriteCollectionBegin( list.elemType, list.size); + } + + /** + * Write a set header. + */ + public function writeSetBegin( set : TSet) : Void { + WriteCollectionBegin( set.elemType, set.size); + } + + /** + * Write a boolean value. Potentially, this could be a boolean field, in + * which case the field header info isn't written yet. If so, decide what the + * right type header is for the value and then Write the field header. + * Otherwise, Write a single byte. + */ + public function writeBool(b : Bool) : Void { + var bct : Int = b ? TCompactTypes.BOOLEAN_TRUE : TCompactTypes.BOOLEAN_FALSE; + + if (booleanField_ != null) + { + // we haven't written the field header yet + WriteFieldBeginInternal( booleanField_, bct); + booleanField_ = null; + } + else + { + // we're not part of a field, so just Write the value. + WriteByteDirect( bct); + } + } + + /** + * Write a byte. Nothing to see here! + */ + public function writeByte( b : Int) : Void { + WriteByteDirect( b); + } + + /** + * Write an I16 as a zigzag varint. + */ + public function writeI16( i16 : Int) : Void { + WriteVarint32( ZigZag.FromInt( i16)); + } + + /** + * Write an i32 as a zigzag varint. + */ + public function writeI32( i32 : Int) : Void { + WriteVarint32( ZigZag.FromInt( i32)); + } + + /** + * Write an i64 as a zigzag varint. + */ + public function writeI64( i64 : haxe.Int64) : Void { + WriteVarint64( ZigZag.FromLong( i64)); + } + + /** + * Write a double to the wire as 8 bytes. + */ + public function writeDouble( dub : Float) : Void { + var data = BitConverter.fixedLongToBytes( BitConverter.DoubleToInt64Bits(dub)); + trans.write( data, 0, data.length); + } + + /** + * Write a string to the wire with a varint size preceding. + */ + public function writeString(str : String) : Void { + var buf = new BytesBuffer(); + if( utf8Strings) + buf.addString( str); // no need to encode on UTF8 targets, the string is just fine + else + buf.addString( Utf8.encode( str)); + var tmp = buf.getBytes(); + writeBinary( tmp); + } + + /** + * Write a byte array, using a varint for the size. + */ + public function writeBinary( bin : Bytes) : Void { + WriteVarint32( cast(bin.length,UInt)); + trans.write( bin, 0, bin.length); + } + + + // These methods are called by structs, but don't actually have any wire + // output or purpose. + public function writeMessageEnd() : Void { } + public function writeMapEnd() : Void { } + public function writeListEnd() : Void { } + public function writeSetEnd() : Void { } + public function writeFieldEnd() : Void { } + + // + // Internal writing methods + // + + /** + * Abstract method for writing the start of lists and sets. List and sets on + * the wire differ only by the type indicator. + */ + private function WriteCollectionBegin( elemType : Int, size : Int) : Void { + if (size <= 14) { + WriteByteDirect( size << 4 | getCompactType(elemType)); + } + else { + WriteByteDirect( 0xf0 | getCompactType(elemType)); + WriteVarint32( cast(size, UInt)); + } + } + + /** + * Write an i64 as a varint. Results in 1-10 bytes on the wire. + */ + private function WriteVarint64(n : haxe.Int64) : Void { + var varint64out = new BytesBuffer(); + while (true) + { + if( Int64.isZero( Int64.and( n, Int64.neg(Int64.make(0,0x7F))))) + { + #if( haxe_ver < 3.2) + varint64out.addByte( Int64.getLow(n)); + #else + varint64out.addByte( n.low); + #end + break; + } + else + { + #if ( haxe_ver < 3.2) + varint64out.addByte( (Int64.getLow(n) & 0x7F) | 0x80); + #else + varint64out.addByte( (n.low & 0x7F) | 0x80); + #end + n = Int64.shr( n, 7); + n = Int64.and( n, Int64.make(0x01FFFFFF,0xFFFFFFFF)); // clean out the shifted 7 bits + } + } + var tmp = varint64out.getBytes(); + trans.write( tmp, 0, tmp.length); + } + + + /** + * Read a message header. + */ + public function readMessageBegin():TMessage { + Reset(); + + var protocolId : Int = readByte(); + if (protocolId != PROTOCOL_ID) { + throw new TProtocolException( TProtocolException.INVALID_DATA, "Expected protocol id " + StringTools.hex(PROTOCOL_ID,2) + " but got " + StringTools.hex(protocolId)); + } + + var versionAndType : Int = readByte(); + var version : Int = (versionAndType & VERSION_MASK); + if (version != VERSION) { + throw new TProtocolException( TProtocolException.INVALID_DATA, "Expected version " + VERSION + " but got " + version); + } + + var type : Int = ((versionAndType >> TYPE_SHIFT_AMOUNT) & TYPE_BITS); + var seqid : Int = cast( ReadVarint32(), Int); + var msgNm : String = readString(); + return new TMessage( msgNm, type, seqid); + } + + /** + * Read a struct begin. There's nothing on the wire for this, but it is our + * opportunity to push a new struct begin marker onto the field stack. + */ + public function readStructBegin():TStruct { + lastField_.add(lastFieldId_); + lastFieldId_ = 0; + return ANONYMOUS_STRUCT; + } + + /** + * Doesn't actually consume any wire data, just removes the last field for + * this struct from the field stack. + */ + public function readStructEnd() : Void { + // consume the last field we Read off the wire. + lastFieldId_ = lastField_.pop(); + } + + /** + * Read a field header off the wire. + */ + public function readFieldBegin() : TField { + var type : Int = readByte(); + + // if it's a stop, then we can return immediately, as the struct is over. + if (type == cast(TCompactTypes.STOP,Int)) { + return TSTOP; + } + + var fieldId : Int; + + // mask off the 4 MSB of the type header. it could contain a field id delta. + var modifier : Int = ((type & 0xf0) >> 4); + if (modifier == 0) + fieldId = readI16(); // not a delta. look ahead for the zigzag varint field id. + else + fieldId = lastFieldId_ + modifier; // add the delta to the last Read field id. + + var field : TField = new TField( "", cast(getTType(type & 0x0f),Int), fieldId); + + // if this happens to be a boolean field, the value is encoded in the type + if (isBoolType(type)) { + // save the boolean value in a special instance variable. + boolValue_ = ((type & 0x0f) == cast(TCompactTypes.BOOLEAN_TRUE,Int)); + } + + // push the new field onto the field stack so we can keep the deltas going. + lastFieldId_ = field.id; + return field; + } + + /** + * Read a map header off the wire. If the size is zero, skip Reading the key + * and value type. This means that 0-length maps will yield TMaps without the + * "correct" types. + */ + public function readMapBegin() : TMap { + var size : Int = cast( ReadVarint32(), Int); + var keyAndValueType : Int = ((size == 0) ? 0 : readByte()); + var key : Int = cast( getTType( (keyAndValueType & 0xF0) >> 4), Int); + var val : Int = cast( getTType( keyAndValueType & 0x0F), Int); + return new TMap( key, val, size); + } + + /** + * Read a list header off the wire. If the list size is 0-14, the size will + * be packed into the element type header. If it's a longer list, the 4 MSB + * of the element type header will be 0xF, and a varint will follow with the + * true size. + */ + public function readListBegin():TList { + var size_and_type : Int = readByte(); + + var size : Int = ((size_and_type & 0xF0) >> 4) & 0x0F; + if (size == 15) { + size = cast( ReadVarint32(), Int); + } + + var type = getTType(size_and_type); + return new TList( type, size); + } + + /** + * Read a set header off the wire. If the set size is 0-14, the size will + * be packed into the element type header. If it's a longer set, the 4 MSB + * of the element type header will be 0xF, and a varint will follow with the + * true size. + */ + public function readSetBegin() : TSet { + var size_and_type : Int = readByte(); + + var size : Int = ((size_and_type & 0xF0) >> 4) & 0x0F; + if (size == 15) { + size = cast( ReadVarint32(), Int); + } + + var type = getTType(size_and_type); + return new TSet( type, size); + } + + /** + * Read a boolean off the wire. If this is a boolean field, the value should + * already have been Read during ReadFieldBegin, so we'll just consume the + * pre-stored value. Otherwise, Read a byte. + */ + public function readBool() : Bool { + if (boolValue_ != null) { + var result : Bool = boolValue_; + boolValue_ = null; + return result; + } + + return (readByte() == cast(TCompactTypes.BOOLEAN_TRUE,Int)); + } + + /** + * Read a single byte off the wire. Nothing interesting here. + */ + public function readByte() : Int { + var byteRawBuf = new BytesBuffer(); + trans.readAll( byteRawBuf, 0, 1); + return byteRawBuf.getBytes().get(0); + } + + /** + * Read an i16 from the wire as a zigzag varint. + */ + public function readI16() : Int { + return ZigZag.ToInt( ReadVarint32()); + } + + /** + * Read an i32 from the wire as a zigzag varint. + */ + public function readI32() : Int { + return ZigZag.ToInt( ReadVarint32()); + } + + /** + * Read an i64 from the wire as a zigzag varint. + */ + public function readI64() : haxe.Int64 { + return ZigZag.ToLong( ReadVarint64()); + } + + /** + * No magic here - just Read a double off the wire. + */ + public function readDouble():Float { + var longBits = new BytesBuffer(); + trans.readAll( longBits, 0, 8); + return BitConverter.Int64BitsToDouble( BitConverter.bytesToLong( longBits.getBytes())); + } + + /** + * Reads a byte[] (via ReadBinary), and then UTF-8 decodes it. + */ + public function readString() : String { + var length : Int = cast( ReadVarint32(), Int); + + if (length == 0) { + return ""; + } + + var buf = new BytesBuffer(); + trans.readAll( buf, 0, length); + + length = buf.length; + var inp = new BytesInput( buf.getBytes()); + var str = inp.readString( length); + if( utf8Strings) + return str; // no need to decode on UTF8 targets, the string is just fine + else + return Utf8.decode( str); + } + + /** + * Read a byte[] from the wire. + */ + public function readBinary() : Bytes { + var length : Int = cast( ReadVarint32(), Int); + if (length == 0) { + return Bytes.alloc(0); + } + + var buf = new BytesBuffer(); + trans.readAll( buf, 0, length); + return buf.getBytes(); + } + + + // These methods are here for the struct to call, but don't have any wire + // encoding. + public function readMessageEnd() : Void { } + public function readFieldEnd() : Void { } + public function readMapEnd() : Void { } + public function readListEnd() : Void { } + public function readSetEnd() : Void { } + + // + // Internal Reading methods + // + + /** + * Read an i32 from the wire as a varint. The MSB of each byte is set + * if there is another byte to follow. This can Read up to 5 bytes. + */ + private function ReadVarint32() : UInt { + var result : UInt = 0; + var shift : Int = 0; + while (true) { + var b : Int = readByte(); + result |= cast((b & 0x7f) << shift, UInt); + if ((b & 0x80) != 0x80) { + break; + } + shift += 7; + } + return result; + } + + /** + * Read an i64 from the wire as a proper varint. The MSB of each byte is set + * if there is another byte to follow. This can Read up to 10 bytes. + */ + private function ReadVarint64() : Int64 { + var shift : Int = 0; + var result : Int64 = Int64.make(0,0); + while (true) { + var b : Int = readByte(); + result = Int64.or( result, Int64.shl( Int64.make(0,b & 0x7f), shift)); + if ((b & 0x80) != 0x80) { + break; + } + shift += 7; + } + + return result; + } + + + // + // type testing and converting + // + + private function isBoolType( b : Int) : Bool { + var lowerNibble : Int = b & 0x0f; + switch(lowerNibble) + { + case TCompactTypes.BOOLEAN_TRUE: return true; + case TCompactTypes.BOOLEAN_FALSE: return true; + default: return false; + } + } + + + /** + * Given a TCompactProtocol.TCompactTypes constant, convert it to its corresponding + * TType value. + */ + private function getTType( type : Int) : Int { + try + { + return tcompactTypeToType[type]; + } + catch ( e : Dynamic) + { + var tt : Int = (type & 0x0f); + throw new TProtocolException( TProtocolException.UNKNOWN, 'don\'t know what type: $tt ($e)'); + } + } + + /** + * Given a TType value, find the appropriate TCompactProtocol.TCompactTypes constant. + */ + private function getCompactType( ttype : Int) : Int + { + return cast( ttypeToCompactType[ttype], Int); + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactProtocolFactory.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactProtocolFactory.hx new file mode 100644 index 000000000..c5673b40c --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactProtocolFactory.hx @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import org.apache.thrift.transport.TTransport; + + +/** +* Compact Protocol Factory +*/ +class TCompactProtocolFactory implements TProtocolFactory { + + public function new() { + } + + public function getProtocol( trans : TTransport) : TProtocol { + return new TCompactProtocol( trans); + } +} + + + +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactTypes.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactTypes.hx new file mode 100644 index 000000000..cdd3d874f --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TCompactTypes.hx @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +/** + * All of the on-wire type codes. + */ +@:enum +abstract TCompactTypes(Int) from Int to Int { + public static inline var STOP = 0x00; + public static inline var BOOLEAN_TRUE = 0x01; + public static inline var BOOLEAN_FALSE = 0x02; + public static inline var BYTE = 0x03; + public static inline var I16 = 0x04; + public static inline var I32 = 0x05; + public static inline var I64 = 0x06; + public static inline var DOUBLE = 0x07; + public static inline var BINARY = 0x08; + public static inline var LIST = 0x09; + public static inline var SET = 0x0A; + public static inline var MAP = 0x0B; + public static inline var STRUCT = 0x0C; +} + diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TField.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TField.hx new file mode 100644 index 000000000..3f5498d82 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TField.hx @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +class TField { + + public var name : String; + public var type : Int; + public var id : Int; + + public function new(n : String = "", t : Int = 0, i : Int = 0) { + name = n; + type = t; + id = i; + } + + public function toString() : String { + return '<TField name:"$name" type:"$type" field-id:"$id">'; + } + + public function equals( otherField : TField) : Bool { + return (type == otherField.type) + && (id == otherField.id); + } + +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TJSONProtocol.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TJSONProtocol.hx new file mode 100644 index 000000000..e20ff33c5 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TJSONProtocol.hx @@ -0,0 +1,1073 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import haxe.io.Bytes; +import haxe.io.BytesInput; +import haxe.io.BytesOutput; +import haxe.io.BytesBuffer; +import haxe.ds.GenericStack; +import haxe.Utf8; +import haxe.crypto.Base64; +import haxe.Int64; + +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TMessage; +import org.apache.thrift.protocol.TField; +import org.apache.thrift.protocol.TMap; +import org.apache.thrift.protocol.TSet; +import org.apache.thrift.protocol.TList; +import org.apache.thrift.transport.TTransport; + + + +/* JSON protocol implementation for thrift. +* This is a full-featured protocol supporting Write and Read. +* +* Please see the C++ class header for a detailed description of the wire format. +* +* Adapted from the Java version. +*/ +class TJSONProtocol extends TRecursionTracker implements TProtocol { + + public var trans(default,null) : TTransport; + + // Stack of nested contexts that we may be in + private var contextStack : GenericStack<JSONBaseContext> = new GenericStack<JSONBaseContext>(); + + // Current context that we are in + private var context : JSONBaseContext; + + // Reader that manages a 1-byte buffer + private var reader : LookaheadReader; + + // whether the underlying system holds Strings as UTF-8 + // http://old.haxe.org/manual/encoding + private static var utf8Strings = haxe.Utf8.validate("Ç-ß-Æ-Ю-Ш"); + + // TJSONProtocol Constructor + public function new( trans : TTransport) + { + this.trans = trans; + this.context = new JSONBaseContext(this); + this.reader = new LookaheadReader(this); + } + + public function getTransport() : TTransport { + return trans; + } + + public function writeMessageBegin(message:TMessage) : Void { + WriteJSONArrayStart(); + WriteJSONInteger( JSONConstants.VERSION); + WriteJSONString( BytesFromString(message.name)); + WriteJSONInteger( message.type); + WriteJSONInteger( message.seqid); + } + + public function writeMessageEnd() : Void { + WriteJSONArrayEnd(); + } + + public function writeStructBegin(struct:TStruct) : Void { + WriteJSONObjectStart(); + } + + public function writeStructEnd() : Void { + WriteJSONObjectEnd(); + } + + public function writeFieldBegin(field:TField) : Void { + WriteJSONInteger( field.id ); + WriteJSONObjectStart(); + WriteJSONString( BytesFromString( JSONConstants.GetTypeNameForTypeID( field.type))); + } + + public function writeFieldEnd() : Void { + WriteJSONObjectEnd(); + } + + public function writeFieldStop() : Void { } + + public function writeMapBegin(map:TMap) : Void { + WriteJSONArrayStart(); + WriteJSONString( BytesFromString( JSONConstants.GetTypeNameForTypeID( map.keyType))); + WriteJSONString( BytesFromString( JSONConstants.GetTypeNameForTypeID( map.valueType))); + WriteJSONInteger( map.size); + WriteJSONObjectStart(); + } + + public function writeMapEnd() : Void { + WriteJSONObjectEnd(); + WriteJSONArrayEnd(); + } + + public function writeListBegin(list:TList) : Void { + WriteJSONArrayStart(); + WriteJSONString( BytesFromString( JSONConstants.GetTypeNameForTypeID( list.elemType ))); + WriteJSONInteger( list.size); + } + + public function writeListEnd() : Void { + WriteJSONArrayEnd(); + } + + public function writeSetBegin(set:TSet) : Void { + WriteJSONArrayStart(); + WriteJSONString( BytesFromString( JSONConstants.GetTypeNameForTypeID( set.elemType))); + WriteJSONInteger( set.size); + } + + public function writeSetEnd() : Void { + WriteJSONArrayEnd(); + } + + public function writeBool(b : Bool) : Void { + if( b) + WriteJSONInteger( 1); + else + WriteJSONInteger( 0); + } + + public function writeByte(b : Int) : Void { + WriteJSONInteger( b); + } + + public function writeI16(i16 : Int) : Void { + WriteJSONInteger( i16); + } + + public function writeI32(i32 : Int) : Void { + WriteJSONInteger( i32); + } + + public function writeI64(i64 : haxe.Int64) : Void { + WriteJSONInt64( i64); + } + + public function writeDouble(dub:Float) : Void { + WriteJSONDouble(dub); + } + + public function writeString(str : String) : Void { + WriteJSONString( BytesFromString(str)); + } + + public function writeBinary(bin:Bytes) : Void { + WriteJSONBase64(bin); + } + + public function readMessageBegin():TMessage { + var message : TMessage = new TMessage(); + ReadJSONArrayStart(); + if (ReadJSONInteger() != JSONConstants.VERSION) + { + throw new TProtocolException(TProtocolException.BAD_VERSION, + "Message contained bad version."); + } + + message.name = ReadJSONString(false); + message.type = ReadJSONInteger(); + message.seqid = ReadJSONInteger(); + return message; + } + + public function readMessageEnd() : Void { + ReadJSONArrayEnd(); + } + + public function readStructBegin():TStruct { + ReadJSONObjectStart(); + return new TStruct(); + } + + public function readStructEnd() : Void { + ReadJSONObjectEnd(); + } + + public function readFieldBegin() : TField { + var field : TField = new TField(); + var ch = reader.Peek(); + if (StringFromBytes(ch) == JSONConstants.RBRACE) + { + field.type = TType.STOP; + } + else + { + field.id = ReadJSONInteger(); + ReadJSONObjectStart(); + field.type = JSONConstants.GetTypeIDForTypeName( ReadJSONString(false)); + } + return field; + } + + public function readFieldEnd() : Void { + ReadJSONObjectEnd(); + } + + public function readMapBegin() : TMap { + ReadJSONArrayStart(); + var KeyType = JSONConstants.GetTypeIDForTypeName( ReadJSONString(false)); + var ValueType = JSONConstants.GetTypeIDForTypeName( ReadJSONString(false)); + var Count : Int = ReadJSONInteger(); + ReadJSONObjectStart(); + + var map = new TMap( KeyType, ValueType, Count); + return map; + } + + public function readMapEnd() : Void { + ReadJSONObjectEnd(); + ReadJSONArrayEnd(); + } + + public function readListBegin():TList { + ReadJSONArrayStart(); + var ElementType = JSONConstants.GetTypeIDForTypeName( ReadJSONString(false)); + var Count : Int = ReadJSONInteger(); + + var list = new TList( ElementType, Count); + return list; + } + + public function readListEnd() : Void { + ReadJSONArrayEnd(); + } + + public function readSetBegin() : TSet { + ReadJSONArrayStart(); + var ElementType = JSONConstants.GetTypeIDForTypeName( ReadJSONString(false)); + var Count : Int = ReadJSONInteger(); + + var set = new TSet( ElementType, Count); + return set; + } + + public function readSetEnd() : Void { + ReadJSONArrayEnd(); + } + + public function readBool() : Bool { + return (ReadJSONInteger() != 0); + } + + public function readByte() : Int { + return ReadJSONInteger(); + } + + public function readI16() : Int { + return ReadJSONInteger(); + } + + public function readI32() : Int { + return ReadJSONInteger(); + } + + public function readI64() : haxe.Int64 { + return ReadJSONInt64(); + } + + public function readDouble():Float { + return ReadJSONDouble(); + } + + public function readString() : String { + return ReadJSONString(false); + } + + public function readBinary() : Bytes { + return ReadJSONBase64(); + } + + // Push a new JSON context onto the stack. + private function PushContext(c : JSONBaseContext) : Void { + contextStack.add(context); + context = c; + } + + // Pop the last JSON context off the stack + private function PopContext() : Void { + context = contextStack.pop(); + } + + + // Write the bytes in array buf as a JSON characters, escaping as needed + private function WriteJSONString( b : Bytes) : Void { + context.Write(); + + var tmp = BytesFromString( JSONConstants.QUOTE); + trans.write( tmp, 0, tmp.length); + + for (i in 0 ... b.length) { + var value = b.get(i); + + if ((value & 0x00FF) >= 0x30) + { + if (String.fromCharCode(value) == JSONConstants.BACKSLASH.charAt(0)) + { + tmp = BytesFromString( JSONConstants.BACKSLASH + JSONConstants.BACKSLASH); + trans.write( tmp, 0, tmp.length); + } + else + { + trans.write( b, i, 1); + } + } + else + { + var num = JSONConstants.JSON_CHAR_TABLE[value]; + if (num == 1) + { + trans.write( b, i, 1); + } + else if (num > 1) + { + var buf = new BytesBuffer(); + buf.addString( JSONConstants.BACKSLASH); + buf.addByte( num); + tmp = buf.getBytes(); + trans.write( tmp, 0, tmp.length); + } + else + { + var buf = new BytesBuffer(); + buf.addString( JSONConstants.ESCSEQ); + buf.addString( HexChar( (value & 0xFF000000) >> 12)); + buf.addString( HexChar( (value & 0x00FF0000) >> 8)); + buf.addString( HexChar( (value & 0x0000FF00) >> 4)); + buf.addString( HexChar( value & 0x000000FF)); + tmp = buf.getBytes(); + trans.write( tmp, 0, tmp.length); + } + } + } + + tmp = BytesFromString( JSONConstants.QUOTE); + trans.write( tmp, 0, tmp.length); + } + + // Write out number as a JSON value. If the context dictates so, + // it will be wrapped in quotes to output as a JSON string. + private function WriteJSONInteger( num : Int) : Void { + context.Write(); + + var str : String = ""; + var escapeNum : Bool = context.EscapeNumbers(); + + if (escapeNum) { + str += JSONConstants.QUOTE; + } + + str += Std.string(num); + + if (escapeNum) { + str += JSONConstants.QUOTE; + } + + var tmp = BytesFromString( str); + trans.write( tmp, 0, tmp.length); + } + + // Write out number as a JSON value. If the context dictates so, + // it will be wrapped in quotes to output as a JSON string. + private function WriteJSONInt64( num : Int64) : Void { + context.Write(); + + var str : String = ""; + var escapeNum : Bool = context.EscapeNumbers(); + + if (escapeNum) { + str += JSONConstants.QUOTE; + } + + str += Std.string(num); + + if (escapeNum) { + str += JSONConstants.QUOTE; + } + + var tmp = BytesFromString( str); + trans.write( tmp, 0, tmp.length); + } + + // Write out a double as a JSON value. If it is NaN or infinity or if the + // context dictates escaping, Write out as JSON string. + private function WriteJSONDouble(num : Float) : Void { + context.Write(); + + + var special : Bool = false; + var rendered : String = ""; + if( Math.isNaN(num)) { + special = true; + rendered = JSONConstants.FLOAT_IS_NAN; + } else if (! Math.isFinite(num)) { + special = true; + if( num > 0) { + rendered = JSONConstants.FLOAT_IS_POS_INF; + } else { + rendered = JSONConstants.FLOAT_IS_NEG_INF; + } + } else { + rendered = Std.string(num); // plain and simple float number + } + + // compose output + var escapeNum : Bool = special || context.EscapeNumbers(); + var str : String = ""; + if (escapeNum) { + str += JSONConstants.QUOTE; + } + str += rendered; + if (escapeNum) { + str += JSONConstants.QUOTE; + } + + var tmp = BytesFromString( str); + trans.write( tmp, 0, tmp.length); + } + + // Write out contents of byte array b as a JSON string with base-64 encoded data + private function WriteJSONBase64( b : Bytes) : Void { + context.Write(); + + var buf = new BytesBuffer(); + buf.addString( JSONConstants.QUOTE); + buf.addString( Base64.encode(b)); + buf.addString( JSONConstants.QUOTE); + + var tmp = buf.getBytes(); + trans.write( tmp, 0, tmp.length); + } + + private function WriteJSONObjectStart() : Void { + context.Write(); + var tmp = BytesFromString( JSONConstants.LBRACE); + trans.write( tmp, 0, tmp.length); + PushContext( new JSONPairContext(this)); + } + + private function WriteJSONObjectEnd() : Void { + PopContext(); + var tmp = BytesFromString( JSONConstants.RBRACE); + trans.write( tmp, 0, tmp.length); + } + + private function WriteJSONArrayStart() : Void { + context.Write(); + var tmp = BytesFromString( JSONConstants.LBRACKET); + trans.write( tmp, 0, tmp.length); + PushContext( new JSONListContext(this)); + } + + private function WriteJSONArrayEnd() : Void { + PopContext(); + var tmp = BytesFromString( JSONConstants.RBRACKET); + trans.write( tmp, 0, tmp.length); + } + + + /** + * Reading methods. + */ + + // Read a byte that must match char, otherwise an exception is thrown. + public function ReadJSONSyntaxChar( char : String) : Void { + var b = BytesFromString( char); + + var ch = reader.Read(); + if (ch.get(0) != b.get(0)) + { + throw new TProtocolException(TProtocolException.INVALID_DATA, + 'Unexpected character: $ch'); + } + } + + // Read in a JSON string, unescaping as appropriate. + // Skip Reading from the context if skipContext is true. + private function ReadJSONString(skipContext : Bool) : String + { + if (!skipContext) + { + context.Read(); + } + + var buffer : BytesBuffer = new BytesBuffer(); + + ReadJSONSyntaxChar( JSONConstants.QUOTE); + while (true) + { + var ch = reader.Read(); + + // end of string? + if (StringFromBytes(ch) == JSONConstants.QUOTE) + { + break; + } + + // escaped? + if (StringFromBytes(ch) != JSONConstants.ESCSEQ.charAt(0)) + { + buffer.addByte( ch.get(0)); + continue; + } + + // distinguish between \uXXXX (hex unicode) and \X (control chars) + ch = reader.Read(); + if (StringFromBytes(ch) != JSONConstants.ESCSEQ.charAt(1)) + { + var value = JSONConstants.ESCAPE_CHARS_TO_VALUES[ch.get(0)]; + if( value == null) + { + throw new TProtocolException( TProtocolException.INVALID_DATA, "Expected control char"); + } + buffer.addByte( value); + continue; + } + + + // it's \uXXXX + var hexbuf = new BytesBuffer(); + var hexlen = trans.readAll( hexbuf, 0, 4); + if( hexlen != 4) + { + throw new TProtocolException( TProtocolException.INVALID_DATA, "Not enough data for \\uNNNN sequence"); + } + + var hexdigits = hexbuf.getBytes(); + var charcode = 0; + charcode = (charcode << 4) + HexVal( String.fromCharCode(hexdigits.get(0))); + charcode = (charcode << 4) + HexVal( String.fromCharCode(hexdigits.get(1))); + charcode = (charcode << 4) + HexVal( String.fromCharCode(hexdigits.get(2))); + charcode = (charcode << 4) + HexVal( String.fromCharCode(hexdigits.get(3))); + buffer.addString( String.fromCharCode(charcode)); + } + + return StringFromBytes( buffer.getBytes()); + } + + // Return true if the given byte could be a valid part of a JSON number. + private function IsJSONNumeric(b : Int) : Bool { + switch (b) + { + case "+".code: return true; + case "-".code: return true; + case ".".code: return true; + case "0".code: return true; + case "1".code: return true; + case "2".code: return true; + case "3".code: return true; + case "4".code: return true; + case "5".code: return true; + case "6".code: return true; + case "7".code: return true; + case "8".code: return true; + case "9".code: return true; + case "E".code: return true; + case "e".code: return true; + } + return false; + } + + // Read in a sequence of characters that are all valid in JSON numbers. Does + // not do a complete regex check to validate that this is actually a number. + private function ReadJSONNumericChars() : String + { + var buffer : BytesBuffer = new BytesBuffer(); + while (true) + { + var ch = reader.Peek(); + if( ! IsJSONNumeric( ch.get(0))) + { + break; + } + buffer.addByte( reader.Read().get(0)); + } + return StringFromBytes( buffer.getBytes()); + } + + // Read in a JSON number. If the context dictates, Read in enclosing quotes. + private function ReadJSONInteger() : Int { + context.Read(); + + if (context.EscapeNumbers()) { + ReadJSONSyntaxChar( JSONConstants.QUOTE); + } + + var str : String = ReadJSONNumericChars(); + + if (context.EscapeNumbers()) { + ReadJSONSyntaxChar( JSONConstants.QUOTE); + } + + var value = Std.parseInt(str); + if( value == null) { + throw new TProtocolException(TProtocolException.INVALID_DATA, 'Bad numeric data: $str'); + } + + return value; + } + + // Read in a JSON number. If the context dictates, Read in enclosing quotes. + private function ReadJSONInt64() : haxe.Int64 { + context.Read(); + + if (context.EscapeNumbers()) { + ReadJSONSyntaxChar( JSONConstants.QUOTE); + } + + var str : String = ReadJSONNumericChars(); + if( str.length == 0) { + throw new TProtocolException(TProtocolException.INVALID_DATA, 'Bad numeric data: $str'); + } + + if (context.EscapeNumbers()) { + ReadJSONSyntaxChar( JSONConstants.QUOTE); + } + + // process sign + var bMinus = false; + var startAt = 0; + if( (str.charAt(0) == "+") || (str.charAt(0) == "-")) { + bMinus = (str.charAt(0) == "-"); + startAt++; + } + + // process digits + var value : Int64 = Int64.make(0,0); + var bGotDigits = false; + for( i in startAt ... str.length) { + var ch = str.charAt(i); + var digit = JSONConstants.DECIMAL_DIGITS[ch]; + if( digit == null) { + throw new TProtocolException(TProtocolException.INVALID_DATA, 'Bad numeric data: $str'); + } + bGotDigits = true; + + // these are decimal digits + value = Int64.mul( value, Int64.make(0,10)); + value = Int64.add( value, Int64.make(0,digit)); + } + + // process pending minus sign, if applicable + // this should also handle the edge case MIN_INT64 correctly + if( bMinus && (Int64.compare(value,Int64.make(0,0)) > 0)) { + value = Int64.neg( value); + bMinus = false; + } + + if( ! bGotDigits) { + throw new TProtocolException(TProtocolException.INVALID_DATA, 'Bad numeric data: $str'); + } + + return value; + } + + // Read in a JSON double value. Throw if the value is not wrapped in quotes + // when expected or if wrapped in quotes when not expected. + private function ReadJSONDouble() : Float { + context.Read(); + + var str : String = ""; + if (StringFromBytes(reader.Peek()) == JSONConstants.QUOTE) { + str = ReadJSONString(true); + + // special cases + if( str == JSONConstants.FLOAT_IS_NAN) { + return Math.NaN; + } + if( str == JSONConstants.FLOAT_IS_POS_INF) { + return Math.POSITIVE_INFINITY; + } + if( str == JSONConstants.FLOAT_IS_NEG_INF) { + return Math.NEGATIVE_INFINITY; + } + + if( ! context.EscapeNumbers()) { + // throw - we should not be in a string in this case + throw new TProtocolException(TProtocolException.INVALID_DATA, "Numeric data unexpectedly quoted"); + } + } + else + { + if( context.EscapeNumbers()) { + // This will throw - we should have had a quote if EscapeNumbers() == true + ReadJSONSyntaxChar( JSONConstants.QUOTE); + } + + str = ReadJSONNumericChars(); + } + + // parse and check - we should have at least one valid digit + var dub = Std.parseFloat( str); + if( (str.length == 0) || Math.isNaN(dub)) { + throw new TProtocolException(TProtocolException.INVALID_DATA, 'Bad numeric data: $str'); + } + + return dub; + } + + // Read in a JSON string containing base-64 encoded data and decode it. + private function ReadJSONBase64() : Bytes + { + var str = ReadJSONString(false); + return Base64.decode( str); + } + + private function ReadJSONObjectStart() : Void { + context.Read(); + ReadJSONSyntaxChar( JSONConstants.LBRACE); + PushContext(new JSONPairContext(this)); + } + + private function ReadJSONObjectEnd() : Void { + ReadJSONSyntaxChar( JSONConstants.RBRACE); + PopContext(); + } + + private function ReadJSONArrayStart() : Void { + context.Read(); + ReadJSONSyntaxChar( JSONConstants.LBRACKET); + PushContext(new JSONListContext(this)); + } + + private function ReadJSONArrayEnd() : Void { + ReadJSONSyntaxChar( JSONConstants.RBRACKET); + PopContext(); + } + + + public static function BytesFromString( str : String) : Bytes { + var buf = new BytesBuffer(); + if( utf8Strings) + buf.addString( str); // no need to encode on UTF8 targets, the string is just fine + else + buf.addString( Utf8.encode( str)); + return buf.getBytes(); + } + + public static function StringFromBytes( buf : Bytes) : String { + var inp = new BytesInput( buf); + if( buf.length == 0) + return ""; // readString() would return null in that case, which is wrong + var str = inp.readString( buf.length); + if( utf8Strings) + return str; // no need to decode on UTF8 targets, the string is just fine + else + return Utf8.decode( str); + } + + // Convert a byte containing a hex char ('0'-'9' or 'a'-'f') into its corresponding hex value + private static function HexVal(char : String) : Int { + var value = JSONConstants.HEX_DIGITS[char]; + if( value == null) { + throw new TProtocolException(TProtocolException.INVALID_DATA, 'Expected hex character: $char'); + } + return value; + } + + // Convert a byte containing a hex nibble to its corresponding hex character + private static function HexChar(nibble : Int) : String + { + return "0123456789abcdef".charAt(nibble & 0x0F); + } + + +} + + +@:allow(TJSONProtocol) +class JSONConstants { + public static var COMMA = ","; + public static var COLON = ":"; + public static var LBRACE = "{"; + public static var RBRACE = "}"; + public static var LBRACKET = "["; + public static var RBRACKET = "]"; + public static var QUOTE = "\""; + public static var BACKSLASH = "\\"; + + public static var ESCSEQ = "\\u"; + + public static var FLOAT_IS_NAN = "NaN"; + public static var FLOAT_IS_POS_INF = "Infinity"; + public static var FLOAT_IS_NEG_INF = "-Infinity"; + + public static var VERSION = 1; + public static var JSON_CHAR_TABLE = [ + 0, 0, 0, 0, 0, 0, 0, 0, + "b".code, "t".code, "n".code, 0, "f".code, "r".code, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, "\"".code, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + ]; + + public static var ESCAPE_CHARS = ['"','\\','/','b','f','n','r','t']; + public static var ESCAPE_CHARS_TO_VALUES = [ + "\"".code => 0x22, + "\\".code => 0x5C, + "/".code => 0x2F, + "b".code => 0x08, + "f".code => 0x0C, + "n".code => 0x0A, + "r".code => 0x0D, + "t".code => 0x09 + ]; + + public static var DECIMAL_DIGITS = [ + "0" => 0, + "1" => 1, + "2" => 2, + "3" => 3, + "4" => 4, + "5" => 5, + "6" => 6, + "7" => 7, + "8" => 8, + "9" => 9 + ]; + + public static var HEX_DIGITS = [ + "0" => 0, + "1" => 1, + "2" => 2, + "3" => 3, + "4" => 4, + "5" => 5, + "6" => 6, + "7" => 7, + "8" => 8, + "9" => 9, + "A" => 10, + "a" => 10, + "B" => 11, + "b" => 11, + "C" => 12, + "c" => 12, + "D" => 13, + "d" => 13, + "E" => 14, + "e" => 14, + "F" => 15, + "f" => 15 + ]; + + + public static var DEF_STRING_SIZE = 16; + + public static var NAME_BOOL = 'tf'; + public static var NAME_BYTE = 'i8'; + public static var NAME_I16 = 'i16'; + public static var NAME_I32 = 'i32'; + public static var NAME_I64 = 'i64'; + public static var NAME_DOUBLE = 'dbl'; + public static var NAME_STRUCT = 'rec'; + public static var NAME_STRING = 'str'; + public static var NAME_MAP = 'map'; + public static var NAME_LIST = 'lst'; + public static var NAME_SET = 'set'; + + public static function GetTypeNameForTypeID(typeID : Int) : String { + switch (typeID) + { + case TType.BOOL: return NAME_BOOL; + case TType.BYTE: return NAME_BYTE; + case TType.I16: return NAME_I16; + case TType.I32: return NAME_I32; + case TType.I64: return NAME_I64; + case TType.DOUBLE: return NAME_DOUBLE; + case TType.STRING: return NAME_STRING; + case TType.STRUCT: return NAME_STRUCT; + case TType.MAP: return NAME_MAP; + case TType.SET: return NAME_SET; + case TType.LIST: return NAME_LIST; + } + throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized type"); + } + + private static var NAMES_TO_TYPES = [ + NAME_BOOL => TType.BOOL, + NAME_BYTE => TType.BYTE, + NAME_I16 => TType.I16, + NAME_I32 => TType.I32, + NAME_I64 => TType.I64, + NAME_DOUBLE => TType.DOUBLE, + NAME_STRING => TType.STRING, + NAME_STRUCT => TType.STRUCT, + NAME_MAP => TType.MAP, + NAME_SET => TType.SET, + NAME_LIST => TType.LIST + ]; + + public static function GetTypeIDForTypeName(name : String) : Int + { + var type = NAMES_TO_TYPES[name]; + if( null != type) { + return type; + } + throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "Unrecognized type"); + } + +} + + +// Base class for tracking JSON contexts that may require inserting/Reading +// additional JSON syntax characters. This base context does nothing. +@:allow(TJSONProtocol) +class JSONBaseContext +{ + private var proto : TJSONProtocol; + + public function new(proto : TJSONProtocol ) + { + this.proto = proto; + } + + public function Write() : Void { } + public function Read() : Void { } + + public function EscapeNumbers() : Bool { + return false; + } +} + + +// Context for JSON lists. +// Will insert/Read commas before each item except for the first one +@:allow(TJSONProtocol) +class JSONListContext extends JSONBaseContext +{ + public function new( proto : TJSONProtocol) { + super(proto); + } + + private var first : Bool = true; + + public override function Write() : Void { + if (first) + { + first = false; + } + else + { + var buf = new BytesBuffer(); + buf.addString( JSONConstants.COMMA); + var tmp = buf.getBytes(); + proto.trans.write( tmp, 0, tmp.length); + } + } + + public override function Read() : Void { + if (first) + { + first = false; + } + else + { + proto.ReadJSONSyntaxChar( JSONConstants.COMMA); + } + } +} + + +// Context for JSON records. +// Will insert/Read colons before the value portion of each record +// pair, and commas before each key except the first. In addition, +// will indicate that numbers in the key position need to be escaped +// in quotes (since JSON keys must be strings). +@:allow(TJSONProtocol) +class JSONPairContext extends JSONBaseContext +{ + public function new( proto : TJSONProtocol ) { + super( proto); + } + + private var first : Bool = true; + private var colon : Bool = true; + + public override function Write() : Void { + if (first) + { + first = false; + colon = true; + } + else + { + var buf = new BytesBuffer(); + buf.addString( colon ? JSONConstants.COLON : JSONConstants.COMMA); + var tmp = buf.getBytes(); + proto.trans.write( tmp, 0, tmp.length); + colon = !colon; + } + } + + public override function Read() : Void { + if (first) + { + first = false; + colon = true; + } + else + { + proto.ReadJSONSyntaxChar( colon ? JSONConstants.COLON : JSONConstants.COMMA); + colon = !colon; + } + } + + public override function EscapeNumbers() : Bool + { + return colon; + } +} + +// Holds up to one byte from the transport +@:allow(TJSONProtocol) +class LookaheadReader { + + private var proto : TJSONProtocol; + private var data : Bytes; + + public function new( proto : TJSONProtocol ) { + this.proto = proto; + data = null; + } + + + // Return and consume the next byte to be Read, either taking it from the + // data buffer if present or getting it from the transport otherwise. + public function Read() : Bytes { + var retval = Peek(); + data = null; + return retval; + } + + // Return the next byte to be Read without consuming, filling the data + // buffer if it has not been filled alReady. + public function Peek() : Bytes { + if (data == null) { + var buf = new BytesBuffer(); + proto.trans.readAll(buf, 0, 1); + data = buf.getBytes(); + } + return data; + } +} + diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TJSONProtocolFactory.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TJSONProtocolFactory.hx new file mode 100644 index 000000000..363558a1f --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TJSONProtocolFactory.hx @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import org.apache.thrift.transport.TTransport; + + +/** +* JSON Protocol Factory +*/ +class TJSONProtocolFactory implements TProtocolFactory { + + public function new() { + } + + public function getProtocol( trans : TTransport) : TProtocol { + return new TJSONProtocol( trans); + } +} + + + +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TList.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TList.hx new file mode 100644 index 000000000..5a1fb5500 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TList.hx @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +class TList { + + public var elemType : Int; + public var size : Int; + + public function new(t : Int = 0, s : Int = 0) { + elemType = t; + size = s; + } + +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMap.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMap.hx new file mode 100644 index 000000000..f4e6288e2 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMap.hx @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +class TMap { + + public var keyType : Int; + public var valueType : Int; + public var size : Int; + + public function new(k : Int = 0, v : Int = 0, s : Int = 0) { + keyType = k; + valueType = v; + size = s; + } + +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMessage.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMessage.hx new file mode 100644 index 000000000..d99264a56 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMessage.hx @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +class TMessage { + + public var name : String; + public var type : Int; + public var seqid : Int; + + public function new(n : String = "", t : Int = 0, s : Int = 0) { + name = n; + type = t; + seqid = s; + } + + public function toString() : String { + return "<TMessage name:'" + name + "' type: " + type + " seqid:" + seqid + ">"; + } + + public function equals(other:TMessage) : Bool { + return name == other.name && type == other.type && seqid == other.seqid; + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMessageType.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMessageType.hx new file mode 100644 index 000000000..706d3298b --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMessageType.hx @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +@:enum +abstract TMessageType(Int) from Int to Int { + public static inline var CALL : Int = 1; + public static inline var REPLY : Int = 2; + public static inline var EXCEPTION : Int = 3; + public static inline var ONEWAY : Int = 4; +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMultiplexedProcessor.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMultiplexedProcessor.hx new file mode 100644 index 000000000..50aa3cdf0 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMultiplexedProcessor.hx @@ -0,0 +1,177 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import haxe.ds.StringMap; +import org.apache.thrift.TApplicationException; +import org.apache.thrift.TProcessor; + +import org.apache.thrift.transport.TTransport; + + +/** + * TMultiplexedProcessor is a TProcessor allowing a single TServer to provide multiple services. + * To do so, you instantiate the processor and then register additional processors with it, + * as shown in the following example: + * + * TMultiplexedProcessor processor = new TMultiplexedProcessor(); + * + * processor.registerProcessor( + * "Calculator", + * new Calculator.Processor(new CalculatorHandler())); + * + * processor.registerProcessor( + * "WeatherReport", + * new WeatherReport.Processor(new WeatherReportHandler())); + * + * TServerTransport t = new TServerSocket(9090); + * TSimpleServer server = new TSimpleServer(processor, t); + * + * server.serve(); + */ +class TMultiplexedProcessor implements TProcessor +{ + private var serviceProcessorMap : StringMap<TProcessor> = new StringMap<TProcessor>(); + private var defaultProcessor : TProcessor = null; + + public function new() { + } + + /** + * 'Register' a service with this TMultiplexedProcessor. This allows us to broker + * requests to individual services by using the service name to select them at request time. + * + * Args: + * - serviceName Name of a service, has to be identical to the name + * declared in the Thrift IDL, e.g. "WeatherReport". + * - processor Implementation of a service, usually referred to as "handlers", + * e.g. WeatherReportHandler implementing WeatherReport.Iface. + */ + public function RegisterProcessor(serviceName : String, processor : TProcessor, asDefault : Bool = false) : Void { + serviceProcessorMap.set(serviceName, processor); + if ( asDefault) { + if( defaultProcessor != null) { + throw new TApplicationException( TApplicationException.UNKNOWN, "Can't have multiple default processors"); + } else { + defaultProcessor = processor; + } + } + } + + + private function Fail( oprot : TProtocol, message : TMessage, extype : Int, etxt : String) : Void { + var appex = new TApplicationException( extype, etxt); + + var newMessage = new TMessage(message.name, TMessageType.EXCEPTION, message.seqid); + + oprot.writeMessageBegin(newMessage); + appex.write( oprot); + oprot.writeMessageEnd(); + oprot.getTransport().flush(); + } + + + /** + * This implementation of process performs the following steps: + * + * - Read the beginning of the message. + * - Extract the service name from the message. + * - Using the service name to locate the appropriate processor. + * - Dispatch to the processor, with a decorated instance of TProtocol + * that allows readMessageBegin() to return the original TMessage. + * + * Throws an exception if + * - the message type is not CALL or ONEWAY, + * - the service name was not found in the message, or + * - the service name has not been RegisterProcessor()ed. + */ + public function process( iprot : TProtocol, oprot : TProtocol) : Bool { + /* Use the actual underlying protocol (e.g. TBinaryProtocol) to read the + message header. This pulls the message "off the wire", which we'll + deal with at the end of this method. */ + + var message : TMessage = iprot.readMessageBegin(); + var methodName : String = ""; + + if ((message.type != TMessageType.CALL) && (message.type != TMessageType.ONEWAY)) + { + Fail(oprot, message, + TApplicationException.INVALID_MESSAGE_TYPE, + "Message type CALL or ONEWAY expected"); + return false; + } + + // Extract the service name + var actualProcessor : TProcessor = null; + var index = message.name.indexOf(TMultiplexedProtocol.SEPARATOR); + if (index < 0) { + // fallback to default processor + methodName = message.name; + actualProcessor = defaultProcessor; + if( actualProcessor == null) { + Fail(oprot, message, + TApplicationException.INVALID_PROTOCOL, + "Service name not found in message name: " + message.name + " and no default processor defined. " + + "Did you forget to use a TMultiplexProtocol in your client?"); + return false; + } + + } else { + // service name given + var serviceName = message.name.substring(0, index); + methodName = message.name.substring( serviceName.length + TMultiplexedProtocol.SEPARATOR.length); + actualProcessor = serviceProcessorMap.get( serviceName); + if( actualProcessor == null) { + Fail(oprot, message, + TApplicationException.INTERNAL_ERROR, + "Service name not found: " + serviceName + ". " + + "Did you forget to call RegisterProcessor()?"); + return false; + } + } + + // Create a new TMessage, removing the service name + // Dispatch processing to the stored processor + var newMessage = new TMessage( methodName, message.type, message.seqid); + var storedMsg = new StoredMessageProtocol( iprot, newMessage); + return actualProcessor.process( storedMsg, oprot); + } +} + + +/** + * Our goal was to work with any protocol. In order to do that, we needed + * to allow them to call readMessageBegin() and get a TMessage in exactly + * the standard format, without the service name prepended to TMessage.name. + */ +class StoredMessageProtocol extends TProtocolDecorator +{ + private var messageBegin : TMessage; + + public function new( protocol : TProtocol, messageBegin : TMessage) { + super( protocol); + this.messageBegin = messageBegin; + } + + public override function readMessageBegin() : TMessage { + return messageBegin; + } +} + diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMultiplexedProtocol.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMultiplexedProtocol.hx new file mode 100644 index 000000000..cacd1d782 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TMultiplexedProtocol.hx @@ -0,0 +1,97 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import org.apache.thrift.transport.TTransport; + + +/** + * TMultiplexedProtocol is a protocol-independent concrete decorator that allows a Thrift + * client to communicate with a multiplexing Thrift server, by prepending the service name + * to the function name during function calls. + * + * NOTE: THIS IS NOT TO BE USED BY SERVERS. + * On the server, use TMultiplexedProcessor to handle requests from a multiplexing client. + * + * This example uses a single socket transport to invoke two services: + * + * TSocket transport = new TSocket("localhost", 9090); + * transport.open(); + * + * TBinaryProtocol protocol = new TBinaryProtocol(transport); + * + * TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator"); + * Calculator.Client service = new Calculator.Client(mp); + * + * TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport"); + * WeatherReport.Client service2 = new WeatherReport.Client(mp2); + * + * System.out.println(service.add(2,2)); + * System.out.println(service2.getTemperature()); + * + */ +class TMultiplexedProtocol extends TProtocolDecorator { + + /** Used to delimit the service name from the function name */ + public static inline var SEPARATOR : String = ":"; + + private var service : String; + + /** + * Wrap the specified protocol, allowing it to be used to communicate with a + * multiplexing server. The <code>serviceName</code> is required as it is + * prepended to the message header so that the multiplexing server can broker + * the function call to the proper service. + * + * Args: + * protocol Your communication protocol of choice, e.g. TBinaryProtocol + * serviceName The service name of the service communicating via this protocol. + */ + public function new( protocol : TProtocol, serviceName : String) { + super( protocol); + service = serviceName; + } + + /** + * Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR. + * Args: + * tMessage The original message. + */ + public override function writeMessageBegin( message : TMessage) : Void { + switch( message.type) + { + case TMessageType.CALL: + super.writeMessageBegin(new TMessage( + service + SEPARATOR + message.name, + message.type, + message.seqid)); + + case TMessageType.ONEWAY: + super.writeMessageBegin(new TMessage( + service + SEPARATOR + message.name, + message.type, + message.seqid)); + + default: + super.writeMessageBegin(message); + } + } +} + diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocol.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocol.hx new file mode 100644 index 000000000..b7f3842d0 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocol.hx @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import haxe.io.Bytes; +import org.apache.thrift.TException; +import org.apache.thrift.transport.TTransport; + +/** +* Protocol interface definition +*/ +interface TProtocol { + + function getTransport() : TTransport; + + /** + * Writing methods. + */ + function writeMessageBegin(message:TMessage) : Void; + function writeMessageEnd() : Void; + function writeStructBegin(struct:TStruct) : Void; + function writeStructEnd() : Void; + function writeFieldBegin(field:TField) : Void; + function writeFieldEnd() : Void; + function writeFieldStop() : Void; + function writeMapBegin(map:TMap) : Void; + function writeMapEnd() : Void; + function writeListBegin(list:TList) : Void; + function writeListEnd() : Void; + function writeSetBegin(set:TSet) : Void; + function writeSetEnd() : Void; + function writeBool(b : Bool) : Void; + function writeByte(b : Int) : Void; + function writeI16(i16 : Int) : Void; + function writeI32(i32 : Int) : Void; + function writeI64(i64 : haxe.Int64) : Void; + function writeDouble(dub : Float) : Void; + function writeString(str : String) : Void; + function writeBinary(bin : Bytes) : Void; + + /** + * Reading methods. + */ + function readMessageBegin():TMessage; + function readMessageEnd() : Void; + function readStructBegin():TStruct; + function readStructEnd() : Void; + function readFieldBegin():TField; + function readFieldEnd() : Void; + function readMapBegin():TMap; + function readMapEnd() : Void; + function readListBegin():TList; + function readListEnd() : Void; + function readSetBegin():TSet; + function readSetEnd() : Void; + function readBool() : Bool; + function readByte() : Int; + function readI16() : Int; + function readI32() : Int; + function readI64() : haxe.Int64; + function readDouble() : Float; + function readString() : String; + function readBinary() : Bytes; + + // recursion tracking + function IncrementRecursionDepth() : Void; + function DecrementRecursionDepth() : Void; +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolDecorator.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolDecorator.hx new file mode 100644 index 000000000..769e93cc5 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolDecorator.hx @@ -0,0 +1,226 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import haxe.io.Bytes; +import haxe.Int64; + +import org.apache.thrift.transport.TTransport; + + +/** + * TProtocolDecorator forwards all requests to an enclosed TProtocol instance, + * providing a way to author concise concrete decorator subclasses. While it has + * no abstract methods, it is marked abstract as a reminder that by itself, + * it does not modify the behaviour of the enclosed TProtocol. + * + * See p.175 of Design Patterns (by Gamma et al.) + * See TMultiplexedProtocol + */ +class TProtocolDecorator implements TProtocol +{ + private var wrapped : TProtocol; + + /** + * Encloses the specified protocol. + * @param protocol All operations will be forward to this protocol. Must be non-null. + */ + private function new( protocol : TProtocol) // not to be instantiated, must derive a class + { + wrapped = protocol; + } + + public function getTransport() : TTransport { + return wrapped.getTransport(); + } + + public function writeMessageBegin( value : TMessage) : Void { + wrapped.writeMessageBegin( value); + } + + public function writeMessageEnd() : Void { + wrapped.writeMessageEnd(); + } + + public function writeStructBegin(value : TStruct) : Void { + wrapped.writeStructBegin( value); + } + + public function writeStructEnd() : Void { + wrapped.writeStructEnd(); + } + + public function writeFieldBegin(value : TField) : Void { + wrapped.writeFieldBegin( value); + } + + public function writeFieldEnd() : Void { + wrapped.writeFieldEnd(); + } + + public function writeFieldStop() : Void { + wrapped.writeFieldStop(); + } + + public function writeMapBegin( value : TMap) : Void { + wrapped.writeMapBegin( value); + } + + public function writeMapEnd() : Void { + wrapped.writeMapEnd(); + } + + public function writeListBegin( value : TList) : Void { + wrapped.writeListBegin( value); + } + + public function writeListEnd() : Void { + wrapped.writeListEnd(); + } + + public function writeSetBegin( value : TSet) : Void { + wrapped.writeSetBegin( value); + } + + public function writeSetEnd() : Void { + wrapped.writeSetEnd(); + } + + public function writeBool(value : Bool) : Void { + wrapped.writeBool( value); + } + + public function writeByte(value : Int) : Void { + wrapped.writeByte( value); + } + + public function writeI16(value : Int) : Void { + wrapped.writeI16( value); + } + + public function writeI32(value : Int) : Void { + wrapped.writeI32( value); + } + + public function writeI64(value : haxe.Int64) : Void { + wrapped.writeI64( value); + } + + public function writeDouble(value : Float) : Void { + wrapped.writeDouble( value); + } + + public function writeString(value : String) : Void { + wrapped.writeString( value); + } + + public function writeBinary(value : Bytes ) : Void { + wrapped.writeBinary( value); + } + + public function readMessageBegin() : TMessage { + return wrapped.readMessageBegin(); + } + + public function readMessageEnd() : Void { + wrapped.readMessageEnd(); + } + + public function readStructBegin() : TStruct { + return wrapped.readStructBegin(); + } + + public function readStructEnd() : Void { + wrapped.readStructEnd(); + } + + public function readFieldBegin() : TField { + return wrapped.readFieldBegin(); + } + + public function readFieldEnd() : Void { + wrapped.readFieldEnd(); + } + + public function readMapBegin() : TMap { + return wrapped.readMapBegin(); + } + + public function readMapEnd() : Void { + wrapped.readMapEnd(); + } + + public function readListBegin() : TList { + return wrapped.readListBegin(); + } + + public function readListEnd() : Void { + wrapped.readListEnd(); + } + + public function readSetBegin() : TSet { + return wrapped.readSetBegin(); + } + + public function readSetEnd() : Void { + wrapped.readSetEnd(); + } + + public function readBool() : Bool + { + return wrapped.readBool(); + } + + public function readByte() : Int { + return wrapped.readByte(); + } + + public function readI16() : Int { + return wrapped.readI16(); + } + + public function readI32() : Int { + return wrapped.readI32(); + } + + public function readI64() : haxe.Int64 { + return wrapped.readI64(); + } + + public function readDouble() : Float { + return wrapped.readDouble(); + } + + public function readString() : String { + return wrapped.readString(); + } + + public function readBinary() : Bytes { + return wrapped.readBinary(); + } + + public function IncrementRecursionDepth() : Void { + return wrapped.IncrementRecursionDepth(); + } + + public function DecrementRecursionDepth() : Void { + return wrapped.DecrementRecursionDepth(); + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolException.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolException.hx new file mode 100644 index 000000000..a3b37a570 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolException.hx @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import org.apache.thrift.TException; + +class TProtocolException extends TException { + + // WARNING: These are subject to be extended in the future, so we can't use enums + // with Haxe 3.1.3 because of https://github.com/HaxeFoundation/haxe/issues/3649 + public static inline var UNKNOWN : Int = 0; + public static inline var INVALID_DATA : Int = 1; + public static inline var NEGATIVE_SIZE : Int = 2; + public static inline var SIZE_LIMIT : Int = 3; + public static inline var BAD_VERSION : Int = 4; + public static inline var NOT_IMPLEMENTED : Int = 5; + public static inline var DEPTH_LIMIT : Int = 6; + + public function new(error : Int = UNKNOWN, message : String = "") { + super(message, error); + } + + +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolFactory.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolFactory.hx new file mode 100644 index 000000000..1c2d62e2d --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolFactory.hx @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import org.apache.thrift.transport.TTransport; + +interface TProtocolFactory { + function getProtocol(trans:TTransport):TProtocol; +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolUtil.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolUtil.hx new file mode 100644 index 000000000..001e40564 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TProtocolUtil.hx @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import org.apache.thrift.*; + + /** + * Utility class with static methods for interacting with protocol data + * streams. + * + */ +class TProtocolUtil { + + /** + * Skips over the next data element from the provided input TProtocol object. + * + * @param prot the protocol object to read from + * @param type the next value will be intepreted as this TType value. + */ + public static function skip(prot:TProtocol, type : Int) : Void { + prot.IncrementRecursionDepth(); + try + { + switch (type) { + case TType.BOOL: + prot.readBool(); + + case TType.BYTE: + prot.readByte(); + + case TType.I16: + prot.readI16(); + + case TType.I32: + prot.readI32(); + + case TType.I64: + prot.readI64(); + + case TType.DOUBLE: + prot.readDouble(); + + case TType.STRING: + prot.readBinary(); + + case TType.STRUCT: + prot.readStructBegin(); + while (true) { + var field:TField = prot.readFieldBegin(); + if (field.type == TType.STOP) { + break; + } + skip(prot, field.type); + prot.readFieldEnd(); + } + prot.readStructEnd(); + + case TType.MAP: + var map:TMap = prot.readMapBegin(); + for (i in 0 ... map.size) { + skip(prot, map.keyType); + skip(prot, map.valueType); + } + prot.readMapEnd(); + + case TType.SET: + var set:TSet = prot.readSetBegin(); + for (j in 0 ... set.size) { + skip(prot, set.elemType); + } + prot.readSetEnd(); + + case TType.LIST: + var list:TList = prot.readListBegin(); + for (k in 0 ... list.size) { + skip(prot, list.elemType); + } + prot.readListEnd(); + + default: + throw new TProtocolException(TProtocolException.UNKNOWN, "Unknown field type ${type}"); + } + + prot.DecrementRecursionDepth(); + } + catch(e:Dynamic) + { + prot.DecrementRecursionDepth(); + throw e; + } + } + +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TRecursionTracker.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TRecursionTracker.hx new file mode 100644 index 000000000..cf0211b39 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TRecursionTracker.hx @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +import org.apache.thrift.*; + + +class TRecursionTracker { + + // default + private static inline var DEFAULT_RECURSION_DEPTH : Int = 64; + + // limit and actual value + public var recursionLimit : Int = DEFAULT_RECURSION_DEPTH; + private var recursionDepth : Int = 0; + + public function IncrementRecursionDepth() : Void + { + if (recursionDepth < recursionLimit) + ++recursionDepth; + else + throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded"); + } + + public function DecrementRecursionDepth() : Void + { + --recursionDepth; + } + + +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TSet.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TSet.hx new file mode 100644 index 000000000..44eab36ca --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TSet.hx @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +class TSet { + + public var elemType : Int; + public var size : Int; + + public function new(t : Int = 0, s : Int = 0) { + elemType = t; + size = s; + } + +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TStruct.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TStruct.hx new file mode 100644 index 000000000..9e0b7ddba --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TStruct.hx @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +class TStruct { + + public var name : String; + + public function new(n : String = "") { + name = n; + } + +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TType.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TType.hx new file mode 100644 index 000000000..6abbc9685 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/protocol/TType.hx @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.protocol; + +@:enum +abstract TType(Int) from Int to Int { + public static inline var STOP : Int = 0; + public static inline var VOID : Int = 1; + public static inline var BOOL : Int = 2; + public static inline var BYTE : Int = 3; + public static inline var DOUBLE : Int = 4; + public static inline var I16 : Int = 6; + public static inline var I32 : Int = 8; + public static inline var I64 : Int = 10; + public static inline var STRING : Int = 11; + public static inline var STRUCT : Int = 12; + public static inline var MAP : Int = 13; + public static inline var SET : Int = 14; + public static inline var LIST : Int = 15; +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TServer.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TServer.hx new file mode 100644 index 000000000..56eee0add --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TServer.hx @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.server; + +import org.apache.thrift.*; +import org.apache.thrift.protocol.*; +import org.apache.thrift.transport.*; +import org.apache.thrift.meta_data.*; + +class TServer +{ + private var processor : TProcessor = null; + private var serverTransport : TServerTransport = null; + private var inputTransportFactory : TTransportFactory = null; + private var outputTransportFactory : TTransportFactory = null; + private var inputProtocolFactory : TProtocolFactory = null; + private var outputProtocolFactory : TProtocolFactory = null; + + // server events + public var serverEventHandler : TServerEventHandler = null; + + // Log delegation + private var _logDelegate : Dynamic->Void = null; + public var logDelegate(get,set) : Dynamic->Void; + + public function new( processor : TProcessor, + serverTransport : TServerTransport, + inputTransportFactory : TTransportFactory = null, + outputTransportFactory : TTransportFactory = null, + inputProtocolFactory : TProtocolFactory = null, + outputProtocolFactory : TProtocolFactory = null, + logDelegate : Dynamic->Void = null) + { + this.processor = processor; + this.serverTransport = serverTransport; + this.inputTransportFactory = inputTransportFactory; + this.outputTransportFactory = outputTransportFactory; + this.inputProtocolFactory = inputProtocolFactory; + this.outputProtocolFactory = outputProtocolFactory; + this.logDelegate = logDelegate; + + ApplyMissingDefaults(); + } + + private function ApplyMissingDefaults() { + if( processor == null) + throw "Invalid server configuration: processor missing"; + if( serverTransport == null) + throw "Invalid server configuration: serverTransport missing"; + if( inputTransportFactory == null) + inputTransportFactory = new TTransportFactory(); + if( outputTransportFactory == null) + outputTransportFactory = new TTransportFactory(); + if( inputProtocolFactory == null) + inputProtocolFactory = new TBinaryProtocolFactory(); + if( outputProtocolFactory == null) + outputProtocolFactory= new TBinaryProtocolFactory(); + if( logDelegate == null) + logDelegate = DefaultLogDelegate; + } + + + private function set_logDelegate(value : Dynamic->Void) : Dynamic->Void { + if(value != null) { + _logDelegate = value; + } else { + _logDelegate = DefaultLogDelegate; + } + return _logDelegate; + } + + + private function get_logDelegate() : Dynamic->Void { + return _logDelegate; + } + + + private function DefaultLogDelegate(value : Dynamic) : Void { + trace( value); + } + + + + public function Serve() : Void { + throw new AbstractMethodError(); + } + + + public function Stop() : Void { + throw new AbstractMethodError(); + } + +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TServerEventHandler.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TServerEventHandler.hx new file mode 100644 index 000000000..9bc992755 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TServerEventHandler.hx @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.server; + +import org.apache.thrift.*; +import org.apache.thrift.transport.*; +import org.apache.thrift.protocol.*; + + +// Interface implemented by server users to handle events from the server +interface TServerEventHandler { + + // Called before the server begins + function preServe() : Void; + + // Called when a new client has connected and is about to being processing + function createContext( input : TProtocol, output : TProtocol) : Dynamic; + + // Called when a client has finished request-handling to delete server context + function deleteContext( serverContext : Dynamic, input : TProtocol, output : TProtocol) : Void; + + // Called when a client is about to call the processor + function processContext( serverContext : Dynamic, transport : TTransport) : Void; +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TSimpleServer.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TSimpleServer.hx new file mode 100644 index 000000000..0600744d3 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/server/TSimpleServer.hx @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.server; + +import org.apache.thrift.*; +import org.apache.thrift.protocol.*; +import org.apache.thrift.transport.*; +import org.apache.thrift.meta_data.*; + +// Simple single-threaded server for testing +class TSimpleServer extends TServer { + + private var stop : Bool = false; + + //stops just after input transport returns EOF + //useful for limited scenarios, like embeding into php server + public var runOnce : Bool = false; + + public function new( processor : TProcessor, + serverTransport : TServerTransport, + transportFactory : TTransportFactory = null, + protocolFactory : TProtocolFactory = null, + logger : Dynamic->Void = null) { + super( processor, serverTransport, + transportFactory, transportFactory, + protocolFactory, protocolFactory, + logger); + } + + + public override function Serve() : Void + { + try + { + serverTransport.Listen(); + } + catch (ttx : TTransportException) + { + logDelegate(ttx); + return; + } + + // Fire the preServe server event when server is up, + // but before any client connections + if (serverEventHandler != null) { + serverEventHandler.preServe(); + } + + while( ! stop) + { + var client : TTransport = null; + var inputTransport : TTransport = null; + var outputTransport : TTransport = null; + var inputProtocol : TProtocol = null; + var outputProtocol : TProtocol = null; + var connectionContext : Dynamic = null; + try + { + client = serverTransport.Accept(); + if (client != null) { + inputTransport = inputTransportFactory.getTransport( client); + outputTransport = outputTransportFactory.getTransport( client); + inputProtocol = inputProtocolFactory.getProtocol( inputTransport); + outputProtocol = outputProtocolFactory.getProtocol( outputTransport); + + // Recover event handler (if any) and fire createContext + // server event when a client connects + if (serverEventHandler != null) { + connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); + } + + // Process client requests until client disconnects + while( true) { + // Fire processContext server event + // N.B. This is the pattern implemented in C++ and the event fires provisionally. + // That is to say it may be many minutes between the event firing and the client request + // actually arriving or the client may hang up without ever makeing a request. + if (serverEventHandler != null) { + serverEventHandler.processContext(connectionContext, inputTransport); + } + + //Process client request (blocks until transport is readable) + if( ! processor.process( inputProtocol, outputProtocol)) { + break; + } + } + } + } + catch( ttx : TTransportException) + { + // Usually a client disconnect, expected + if(runOnce && ttx.errorID == TTransportException.END_OF_FILE) { + //input returns eof, exit + //follows lib/cpp/src/thrift/server/TServerFramework.cpp + Stop(); + } + } + catch( pex : TProtocolException) + { + logDelegate('$pex ${pex.errorID} ${pex.errorMsg}'); // Unexpected + } + catch( e : Dynamic) + { + logDelegate(e); // Unexpected + } + + if(client != null && !runOnce) + { + client.close(); + } + + // Fire deleteContext server event after client disconnects + if (serverEventHandler != null) { + serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); + } + } + } + + public override function Stop() : Void + { + stop = true; + serverTransport.Close(); + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TBufferedTransport.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TBufferedTransport.hx new file mode 100644 index 000000000..4b33fcf86 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TBufferedTransport.hx @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import org.apache.thrift.transport.*; + +import haxe.io.Eof; +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import haxe.io.BytesOutput; +import haxe.io.BytesInput; + + +class TBufferedTransport extends TTransport +{ + // constants + public static inline var DEFAULT_BUFSIZE : Int = 0x1000; // 4096 Bytes + public static inline var MIN_BUFSIZE : Int = 0x100; // 256 Bytes + public static inline var MAX_BUFSIZE : Int = 0x100000; // 1 MB + + // Underlying transport + public var transport(default,null) : TTransport = null; + + // Buffer for input/output + private var readBuffer_ : BytesInput = null; + private var writeBuffer_ : BytesOutput = null; + private var bufSize : Int; + + // Constructor wraps around another transport + public function new( transport : TTransport, bufSize : Int = DEFAULT_BUFSIZE) { + + // ensure buffer size is in the range + if ( bufSize < MIN_BUFSIZE) + bufSize = MIN_BUFSIZE; + else if( bufSize > MAX_BUFSIZE) + bufSize = MAX_BUFSIZE; + + this.transport = transport; + this.bufSize = bufSize; + this.writeBuffer_ = new BytesOutput(); + this.writeBuffer_.bigEndian = true; + } + + public override function open() : Void { + transport.open(); + } + + public override function isOpen() : Bool { + return transport.isOpen(); + } + + public override function close() : Void { + transport.close(); + } + + public override function read(buf : BytesBuffer, off : Int, len : Int) : Int { + try { + var data = Bytes.alloc(len); + + while( true) { + if ((readBuffer_ != null) && (readBuffer_.position < readBuffer_.length)) { + var got = readBuffer_.readBytes(data, 0, len); + if (got > 0) { + buf.addBytes(data, 0, got); + return got; + } + } + + // there is no point in buffering whenever the + // remaining length exceeds the buffer size + if ( len >= bufSize) { + var got = transport.read( buf, off, len); + if (got > 0) { + buf.addBytes(data, 0, got); + return got; + } + } + + // fill the buffer + if ( readChunk() <= 0) + break; + } + + throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read $len bytes!'); + } + catch (eof : Eof) { + throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read $len bytes!'); + } + } + + function readChunk() : Int { + var size = bufSize; + try { + var buffer = new BytesBuffer(); + size = transport.read( buffer, 0, size); + readBuffer_ = new BytesInput( buffer.getBytes(), 0, size); + readBuffer_.bigEndian = true; + return size; + } + catch(eof : Eof) { + throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read $size bytes!'); + } + } + + private function writeChunk(forceWrite : Bool) : Void { + if( writeBuffer_.length > 0) { + if ( forceWrite || (writeBuffer_.length >= bufSize)) { + var buf = writeBuffer_.getBytes(); + writeBuffer_ = new BytesOutput(); + writeBuffer_.bigEndian = true; + transport.write(buf, 0, buf.length); + } + } + } + + public override function write(buf : Bytes, off : Int, len : Int) : Void { + var halfSize : Int = Std.int(bufSize / 2); + + // No point in buffering if len exceeds the buffer size. + // However, if the buffer is less than half full we should still consider + // squashing all into one write, except when the actual write len is very large. + var huge_write : Bool = (len >= (2 * bufSize)); + var exceeds_buf : Bool = huge_write || (len >= bufSize); + var write_thru : Bool = exceeds_buf && (writeBuffer_.length >= halfSize); + if ( write_thru) { + writeChunk(true); // force send whatever we have in there + transport.write(buf, off, len); // write thru + } else { + writeBuffer_.writeBytes(buf, off, len); + writeChunk(false); + } + } + + public override function flush( callback : Dynamic->Void =null) : Void { + writeChunk(true); + transport.flush(callback); + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TBufferedTransportFactory.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TBufferedTransportFactory.hx new file mode 100644 index 000000000..539e72033 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TBufferedTransportFactory.hx @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import org.apache.thrift.transport.*; + + +class TBufferedTransportFactory extends TTransportFactory { + + private var bufSize : Int; + + public function new(bufSize : Int = TBufferedTransport.DEFAULT_BUFSIZE) { + super(); + this.bufSize = bufSize; + } + + public override function getTransport(base : TTransport) : TTransport { + return new TBufferedTransport(base, bufSize); + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFileStream.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFileStream.hx new file mode 100644 index 000000000..cd8ad1776 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFileStream.hx @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import haxe.io.Input; +import haxe.io.Output; + + +enum TFileMode { + CreateNew; + Append; + Read; +} + + +class TFileStream implements TStream { + + public var FileName(default,null) : String; + + private var Input : sys.io.FileInput; + private var Output : sys.io.FileOutput; + + + public function new( fname : String, mode : TFileMode) { + FileName = fname; + switch ( mode) + { + case TFileMode.CreateNew: + Output = sys.io.File.write( fname, true); + + case TFileMode.Append: + Output = sys.io.File.append( fname, true); + + case TFileMode.Read: + Input = sys.io.File.read( fname, true); + + default: + throw new TTransportException( TTransportException.UNKNOWN, + "Unsupported mode"); + } + + } + + public function Close() : Void { + if( Input != null) { + Input.close(); + Input = null; + } + if( Output != null) { + Output.close(); + Output = null; + } + } + + public function Peek() : Bool { + if( Input == null) + throw new TTransportException( TTransportException.NOT_OPEN, "File not open for input"); + + return (! Input.eof()); + } + + public function Read( buf : Bytes, offset : Int, count : Int) : Int { + if( Input == null) + throw new TTransportException( TTransportException.NOT_OPEN, "File not open for input"); + + return Input.readBytes( buf, offset, count); + } + + public function Write( buf : Bytes, offset : Int, count : Int) : Void { + if( Output == null) + throw new TTransportException( TTransportException.NOT_OPEN, "File not open for output"); + + Output.writeBytes( buf, offset, count); + } + + public function Flush() : Void { + if( Output != null) + Output.flush(); + } + +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFramedTransport.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFramedTransport.hx new file mode 100644 index 000000000..cef82ef61 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFramedTransport.hx @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import org.apache.thrift.transport.*; + +import haxe.io.Eof; +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import haxe.io.BytesOutput; +import haxe.io.BytesInput; + + +/** + * TFramedTransport is a buffered TTransport that ensures a fully read message + * every time by preceding messages with a 4-byte frame size. + */ +class TFramedTransport extends TTransport +{ + public static inline var DEFAULT_MAX_LENGTH = 16384000; + + var maxLength_ : Int; + + /** + * Underlying transport + */ + var transport_ : TTransport = null; + + /** + * Buffer for output + */ + var writeBuffer_ : BytesOutput = new BytesOutput(); + + /** + * Buffer for input + */ + var readBuffer_ : BytesInput = null; + + /** + * Constructor wraps around another transport + */ + public function new( transport : TTransport, maxLength : Int = DEFAULT_MAX_LENGTH) { + transport_ = transport; + maxLength_ = maxLength; + } + + public override function open() : Void { + transport_.open(); + } + + public override function isOpen() : Bool { + return transport_.isOpen(); + } + + public override function close() : Void { + transport_.close(); + } + + public override function read(buf : BytesBuffer, off : Int, len : Int) : Int { + try { + var data = Bytes.alloc(len); + + if ((readBuffer_ != null) && (readBuffer_.position < readBuffer_.length)) { + var got : Int = readBuffer_.readBytes(data, off, len); + if (got > 0) { + buf.addBytes(data,0,got); + return got; + }; + }; + + // Read another frame of data + readFrame(); + + var got = readBuffer_.readBytes(data, off, len); + buf.addBytes(data,0,got); + return got; + } + catch (eof : Eof) { + throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read $len bytes!'); + } + } + + + function readFrameSize() : Int { + try { + var buffer = new BytesBuffer(); + var len = transport_.readAll( buffer, 0, 4); + var inp = new BytesInput( buffer.getBytes(), 0, 4); + inp.bigEndian = true; + return inp.readInt32(); + } + catch(eof : Eof) { + throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read 4 bytes!'); + } + } + + + function readFrame() : Void { + var size : Int = readFrameSize(); + + if (size < 0) { + throw new TTransportException(TTransportException.UNKNOWN, 'Read a negative frame size ($size)!'); + }; + if (size > maxLength_) { + throw new TTransportException(TTransportException.UNKNOWN, 'Frame size ($size) larger than max length ($maxLength_)!'); + }; + + try { + var buffer = new BytesBuffer(); + size = transport_.readAll( buffer, 0, size); + readBuffer_ = new BytesInput( buffer.getBytes(), 0, size); + readBuffer_.bigEndian = true; + } + catch(eof : Eof) { + throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read $size bytes!'); + } + } + + public override function write(buf : Bytes, off : Int, len : Int) : Void { + writeBuffer_.writeBytes(buf, off, len); + } + + function writeFrameSize(len : Int) : Void { + var out = new BytesOutput(); + out.bigEndian = true; + out.writeInt32(len); + transport_.write(out.getBytes(), 0, 4); + } + + public override function flush( callback : Dynamic->Void =null) : Void { + var buf : Bytes = writeBuffer_.getBytes(); + var len : Int = buf.length; + writeBuffer_ = new BytesOutput(); + + writeFrameSize(len); + transport_.write(buf, 0, len); + transport_.flush(callback); + } +} + + diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFramedTransportFactory.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFramedTransportFactory.hx new file mode 100644 index 000000000..8d45a641a --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFramedTransportFactory.hx @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import org.apache.thrift.transport.*; + + +class TFramedTransportFactory extends TTransportFactory { + + var maxLength_ : Int; + + public function new(maxLength : Int = TFramedTransport.DEFAULT_MAX_LENGTH) { + super(); + maxLength_ = maxLength; + } + + public override function getTransport(base : TTransport) : TTransport { + return new TFramedTransport(base, maxLength_); + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFullDuplexHttpClient.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFullDuplexHttpClient.hx new file mode 100644 index 000000000..1972853ef --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TFullDuplexHttpClient.hx @@ -0,0 +1,253 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import flash.errors.EOFError; +import flash.events.Event; +import flash.events.IOErrorEvent; +import flash.events.ProgressEvent; +import flash.events.SecurityErrorEvent; +import flash.net.URLLoader; +import flash.net.URLLoaderDataFormat; +import flash.net.URLRequest; +import flash.net.URLRequestMethod; +import flash.utils.IDataInput; +import flash.utils.IDataOutput; +import haxe.io.Bytes; +import flash.net.Socket; +import flash.events.EventDispatcher; + + + /** + * HTTP implementation of the TTransport interface. Used for working with a + * Thrift web services implementation. + * Unlike Http Client, it uses a single POST, and chunk-encoding to transfer all messages. + */ + + public class TFullDuplexHttpClient extends TTransport + { + private var socket : Socket = null; + private var host : String; + private var port : Int; + private var resource : String; + private var stripped : Bool = false; + private var obuffer : Bytes = new Bytes(); + private var input : IDataInput; + private var output : IDataOutput; + private var bytesInChunk : Int = 0; + private var CRLF : Bytes = new Bytes(); + private var ioCallback : TException->Void = null; + private var eventDispatcher : EventDispatcher = new EventDispatcher(); + + public function new(host : String, port : Int, resource : String) : Void + { + CRLF.writeByte(13); + CRLF.writeByte(10); + this.host = host; + this.port = port; + this.resource = resource; + } + + public override function close() : Void + { + this.input = null; + this.output = null; + this.stripped = false; + socket.close() + } + + public override function peek() : Bool + { + if(socket.connected) + { + trace("Bytes remained:" + socket.bytesAvailable); + return socket.bytesAvailable>0; + } + return false; + } + + public override function read(buf : Bytes, off : Int, len : Int) : Int + { + var n1 : Int = 0, n2 : Int = 0, n3 : Int = 0, n4 : Int = 0, cidx : Int = 2; + var chunkSize : Bytes = new Bytes(); + + try + { + while (!stripped) + { + n1 = n2; + n2 = n3; + n3 = n4; + n4 = input.readByte(); + if ((n1 == 13) && (n2 == 10) && (n3 == 13) && (n4 == 10)) + { + stripped = true; + } + } + + // read chunk size + if (bytesInChunk == 0) + { + n1 = input.readByte(); + n2 = input.readByte(); + + chunkSize.writeByte(n1); + chunkSize.writeByte(n2); + + while (!((n1 == 13) && (n2 == 10))) + { + n1 = n2; + n2 = input.readByte(); + chunkSize.writeByte(n2); + } + + bytesInChunk = parseInt(chunkSize.toString(), 16); + } + + input.readBytes(buf, off, len); + debugBuffer(buf); + bytesInChunk -= len; + + if (bytesInChunk == 0) + { + // advance the : "\r\n" + input.readUTFBytes(2); + } + return len; + } + catch (e : EOFError) + { + trace(e); + throw new TTransportException(TTransportException.UNKNOWN, "No more data available."); + } + catch (e : TException) + { + trace('TException $e'); + throw e; + } + catch (e : Error) + { + trace(e); + throw new TTransportException(TTransportException.UNKNOWN, 'Bad IO error: $e'); + } + catch (e : Dynamic) + { + trace(e); + throw new TTransportException(TTransportException.UNKNOWN, 'Bad IO error: $e'); + } + return 0; + } + + public function debugBuffer(buf : Bytes) : Void + { + var debug : String = "BUFFER >>"; + var i : Int; + for (i = 0; i < buf.length; i++) + { + debug += buf[i] as int; + debug += " "; + } + + trace(debug + "<<"); + } + + public override function write(buf : Bytes, off : Int, len : Int) : Void + { + obuffer.writeBytes(buf, off, len); + } + + public function addEventListener(type : String, listener : Function, useCapture : Bool = false, priority : Int = 0, useWeakReference : Bool = false) : Void + { + this.eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); + } + + public override function open() : Void + { + this.socket = new Socket(); + this.socket.addEventListener(Event.CONNECT, socketConnected); + this.socket.addEventListener(IOErrorEvent.IO_ERROR, socketError); + this.socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketSecurityError); + this.socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); + this.socket.connect(host, port); + } + + public function socketConnected(event : Event) : Void + { + this.output = this.socket; + this.input = this.socket; + this.output.writeUTF( "CONNECT " + resource + " HTTP/1.1\n" + + "Host : " + host + ":" + port + "\r\n" + + "User-Agent : Thrift/Haxe\r\n" + + "Transfer-Encoding : chunked\r\n" + + "content-type : application/x-thrift\r\n" + + "Accept : */*\r\n" + + "\r\n"); + this.eventDispatcher.dispatchEvent(event); + } + + public function socketError(event : IOErrorEvent) : Void + { + trace("Error Connecting:" + event); + this.close(); + if (ioCallback == null) + { + return; + } + ioCallback(new TTransportException(TTransportException.UNKNOWN, "IOError : " + event.text)); + this.eventDispatcher.dispatchEvent(event); + } + + public function socketSecurityError(event : SecurityErrorEvent) : Void + { + trace("Security Error Connecting:" + event); + this.close(); + this.eventDispatcher.dispatchEvent(event); + } + + public function socketDataHandler(event : ProgressEvent) : Void + { + trace("Got Data call:" +ioCallback); + if (ioCallback != null) + { + ioCallback(null); + }; + this.eventDispatcher.dispatchEvent(event); + } + + public override function flush(callback : Error->Void = null) : Void + { + trace("set callback:" + callback); + this.ioCallback = callback; + this.output.writeUTF(this.obuffer.length.toString(16)); + this.output.writeBytes(CRLF); + this.output.writeBytes(this.obuffer); + this.output.writeBytes(CRLF); + this.socket.flush(); + // waiting for new Flex sdk 3.5 + //this.obuffer.clear(); + this.obuffer = new Bytes(); + } + + public override function isOpen() : Bool + { + return (this.socket == null ? false : this.socket.connected); + } + +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/THttpClient.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/THttpClient.hx new file mode 100644 index 000000000..79f86610d --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/THttpClient.hx @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + + +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import haxe.io.BytesOutput; +import haxe.io.BytesInput; + +import haxe.Http; + + + +/** +* HTTP implementation of the TTransport interface. Used for working with a +* Thrift web services implementation. +*/ + +class THttpClient extends TTransport { + + private var requestBuffer_ : BytesOutput = new BytesOutput(); + private var responseBuffer_ : BytesInput = null; + + private var request_ : Http = null; + + + public function new( requestUrl : String) : Void { + request_ = new Http(requestUrl); + request_.addHeader( "contentType", "application/x-thrift"); + } + + + public override function open() : Void { + } + + public override function close() : Void { + } + + public override function isOpen() : Bool { + return true; + } + + public override function read(buf:BytesBuffer, off : Int, len : Int) : Int { + if (responseBuffer_ == null) { + throw new TTransportException(TTransportException.UNKNOWN, "Response buffer is empty, no request."); + } + + var data =Bytes.alloc(len); + len = responseBuffer_.readBytes(data, off, len); + buf.addBytes(data,0,len); + return len; + } + + public override function write(buf:Bytes, off : Int, len : Int) : Void { + requestBuffer_.writeBytes(buf, off, len); + } + + + public override function flush(callback:Dynamic->Void = null) : Void { + var buffer = requestBuffer_; + requestBuffer_ = new BytesOutput(); + responseBuffer_ = null; + + request_.onData = function(data : String) { + var tmp = new BytesBuffer(); + tmp.addString(data); + responseBuffer_ = new BytesInput(tmp.getBytes()); + if( callback != null) { + callback(null); + } + }; + + request_.onError = function(msg : String) { + if( callback != null) { + callback(new TTransportException(TTransportException.UNKNOWN, "IOError: " + msg)); + } + }; + + request_.setPostData(buffer.getBytes().toString()); + request_.request(true/*POST*/); + } + +} + +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx new file mode 100644 index 000000000..4badb2a1f --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import haxe.remoting.SocketProtocol; +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import haxe.io.BytesInput; +import haxe.io.BytesOutput; +import haxe.io.Input; +import haxe.io.Output; +import haxe.io.Eof; + +//import flash.net.ServerSocket; - not yet available on Haxe 3.1.3 +#if ! (flash || html5) + +import sys.net.Host; + + +class TServerSocket extends TServerTransport { + + // Underlying server with socket + private var _socket : Socket= null; + + // Timeout for client sockets from accept + private var _clientTimeout : Float = 5; + + // Whether or not to wrap new TSocket connections in buffers + private var _useBufferedSockets : Bool = false; + + + public function new(?address : String = 'localhost', port : Int, clientTimeout : Float = 5, useBufferedSockets : Bool = false) + { + _clientTimeout = clientTimeout; + _useBufferedSockets = useBufferedSockets; + + try + { + _socket = new Socket(); + _socket.bind( new Host(address), port); + } + catch (e : Dynamic) + { + _socket = null; + throw new TTransportException( TTransportException.UNKNOWN, 'Could not create ServerSocket on port $port: $e'); + } + } + + + public override function Listen() : Void + { + // Make sure not to block on accept + if (_socket != null) { + try + { + #if !php + _socket.listen(1); + #end + } + catch (e : Dynamic) + { + trace('Error $e'); + throw new TTransportException( TTransportException.UNKNOWN, 'Could not accept on listening socket: $e'); + } + } + } + + private override function AcceptImpl() : TTransport + { + if (_socket == null) { + throw new TTransportException( TTransportException.NOT_OPEN, "No underlying server socket."); + } + + try + { + var accepted = _socket.accept(); + var result = TSocket.fromSocket(accepted); + result.setTimeout( _clientTimeout); + + if( _useBufferedSockets) + { + throw "buffered transport not yet supported"; // TODO + //result = new TBufferedTransport(result); + } + + return result; + } + catch (e : Dynamic) + { + trace('Error $e'); + throw new TTransportException( TTransportException.UNKNOWN, '$e'); + } + } + + public override function Close() : Void + { + if (_socket != null) + { + try + { + _socket.close(); + } + catch (e : Dynamic) + { + trace('Error $e'); + throw new TTransportException( TTransportException.UNKNOWN, 'WARNING: Could not close server socket: $e'); + } + _socket = null; + } + } +} + +#end diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TServerTransport.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TServerTransport.hx new file mode 100644 index 000000000..21899819e --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TServerTransport.hx @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +class TServerTransport { + + public function Accept() : TTransport { + var transport = AcceptImpl(); + if (transport == null) { + throw new TTransportException( TTransportException.UNKNOWN, "accept() may not return NULL"); + } + return transport; + } + + public function Listen() : Void { + throw new AbstractMethodError(); + } + + public function Close() : Void { + throw new AbstractMethodError(); + } + + private function AcceptImpl() : TTransport { + throw new AbstractMethodError(); + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TSocket.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TSocket.hx new file mode 100644 index 000000000..7941ab9fe --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TSocket.hx @@ -0,0 +1,318 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +#if flash +import flash.net.Socket; +#elseif js +import js.html.WebSocket; +#else +import haxe.remoting.SocketProtocol; +#end + +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import haxe.io.BytesInput; +import haxe.io.BytesOutput; +import haxe.io.Input; +import haxe.io.Output; +import haxe.io.Eof; + + +#if ! (flash || js) +import sys.net.Host; +#end + + + /** + * Socket implementation of the TTransport interface. Used for working with a + * Thrift Socket Server based implementations. + */ + +class TSocket extends TTransport { + + #if (flash || js) + private var host : String; + #else + private var host : Host; + #end + + private var port : Int; + + #if js + private var socket : WebSocket = null; + #else + private var socket : Socket = null; + #end + + #if js + private var input : Dynamic = null; + private var output : WebSocket = null; + #elseif flash + private var input : Socket = null; + private var output : Socket = null; + #else + private var input : Input = null; + private var output : Output = null; + #end + + private var timeout : Float = 30; + + private var obuffer : BytesOutput = new BytesOutput(); + private var ioCallback : TException->Void = null; + private var readCount : Int = 0; + + public function new(host : String, port : Int) : Void { + #if (flash || js) + this.host = host; + #else + this.host = new Host(host); + #end + + this.port = port; + } + + #if ! (flash || js) + // used by TSocketServer + public static function fromSocket( socket : Socket) : TSocket { + var socketHost = socket.host(); + var result = new TSocket(socketHost.host.toString(), socketHost.port); + result.assignSocket(socket); + return result; + } + #end + + public override function close() : Void { + input = null; + output = null; + socket.close(); + } + + public override function peek() : Bool { + if( (input == null) || (socket == null)) { + return false; + } else { + #if flash + return (input.bytesAvailable > 0); + #elseif js + return true; + #else + var ready = Socket.select( [socket], null, null, 0); + return (ready.read.length > 0); + #end + } + } + + // Reads up to len bytes into buffer buf, starting att offset off. + // May return less bytes than len required + public override function read( buf : BytesBuffer, off : Int, len : Int) : Int { + try + { + #if flash + + var remaining = len; + while( remaining > 0) { + buf.addByte( input.readByte()); + --remaining; + } + return len; + + #elseif js + + if( input == null) { + throw new TTransportException(TTransportException.UNKNOWN, "Still no data "); // don't block + } + var nr = len; + while( nr < len) { + buf.addByte( input.get(off+nr)); + ++nr; + } + return len; + + #else + + //socket.waitForRead(); - no, this ignores timeout and blocks infinitely + if(readCount < off) { + input.read(off-readCount); + readCount = off; + } + + var data = Bytes.alloc(len); + var got = input.readBytes(data, 0, len); + buf.addBytes( data, 0, got); + readCount += got; + return got; + + #end + } + catch (e : Eof) + { + trace('Eof $e'); + throw new TTransportException(TTransportException.END_OF_FILE, "No more data available."); + } + catch (e : TException) + { + trace('TException $e'); + throw e; + } + catch (e : Dynamic) + { + trace('Error $e'); + throw new TTransportException(TTransportException.UNKNOWN, 'Bad IO error : $e'); + } + } + + + public override function write(buf : Bytes, off : Int, len : Int) : Void + { + obuffer.writeBytes(buf, off, len); + } + + + + public override function flush(callback : Dynamic->Void = null) : Void + { + if( ! isOpen()) + { + throw new TTransportException(TTransportException.NOT_OPEN, "Transport not open"); + } + + #if flash + + var bytes = new flash.utils.ByteArray(); + var data = obuffer.getBytes(); + var len = 0; + while( len < data.length) { + bytes.writeByte(data.get(len)); + ++len; + } + + #elseif js + + var data = obuffer.getBytes(); + var outbuf = new js.html.Int8Array(data.length); + var len = 0; + while( len < data.length) { + outbuf.set( [data.get(len)], len); + ++len; + } + var bytes = outbuf.buffer; + + #else + + var bytes = obuffer.getBytes(); + var len = bytes.length; + + #end + + obuffer = new BytesOutput(); + + + ioCallback = callback; + try { + readCount = 0; + + #if js + output.send( bytes); + #else + output.writeBytes( bytes, 0, bytes.length); + #end + + if(ioCallback != null) { + ioCallback(null); // success call + } + } + catch (e : TException) + { + trace('TException $e, message : ${e.errorMsg}'); + if(ioCallback != null) { + ioCallback(e); + } + } + catch (e : Dynamic) { + trace(e); + if(ioCallback != null) { + ioCallback(new TTransportException(TTransportException.UNKNOWN, 'Bad IO error : $e')); + } + } + } + + public override function isOpen() : Bool + { + return (socket != null); + } + + public override function open() : Void + { + #if js + var socket = new WebSocket(); + socket.onmessage = function( event : js.html.MessageEvent) { + this.input = event.data; + } + + #elseif flash + var socket = new Socket(); + socket.connect(host, port); + + #elseif php + var socket = new Socket(); + socket.connect(host, port); + socket.setBlocking(true); + socket.setTimeout(timeout); + + #else + var socket = new Socket(); + socket.setBlocking(true); + socket.setFastSend(true); + socket.setTimeout(timeout); + socket.connect(host, port); + + #end + + assignSocket( socket); + } + + #if js + private function assignSocket( socket : WebSocket) : Void + #else + private function assignSocket( socket : Socket) : Void + #end + { + this.socket = socket; + + #if (flash || js) + output = socket; + input = socket; + + #else + output = socket.output; + input = socket.input; + + #end + } + + public function setTimeout( timeout : Float ) : Void { + if(isOpen()) { + socket.setTimeout(timeout); + } + this.timeout = timeout; + } + +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TStream.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TStream.hx new file mode 100644 index 000000000..50b3ed3cc --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TStream.hx @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import haxe.io.Bytes; +import haxe.io.BytesBuffer; + + +interface TStream { + function Close() : Void; + function Peek() : Bool; + function Read( buf : Bytes, offset : Int, count : Int) : Int; + function Write( buf : Bytes, offset : Int, count : Int) : Void; + function Flush() : Void; +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TStreamTransport.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TStreamTransport.hx new file mode 100644 index 000000000..31a7c141e --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TStreamTransport.hx @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import org.apache.thrift.transport.*; +import org.apache.thrift.helper.*; + +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import haxe.io.BytesOutput; +import haxe.io.BytesInput; + + +class TStreamTransport extends TTransport { + + public var InputStream(default,null) : TStream; + public var OutputStream(default,null) : TStream; + + + public function new( input : TStream, output : TStream) { + this.InputStream = input; + this.OutputStream = output; + } + + public override function isOpen() : Bool { + return true; + } + + public override function peek() : Bool { + return (InputStream != null); + } + + public override function open() : Void { + } + + public override function close() : Void { + if (InputStream != null) + { + InputStream.Close(); + InputStream = null; + } + if (OutputStream != null) + { + OutputStream.Close(); + OutputStream = null; + } + } + + public override function read( buf : BytesBuffer, off : Int, len : Int) : Int { + if (InputStream == null) + { + throw new TTransportException( TTransportException.NOT_OPEN, + "Cannot read from null InputStream"); + } + + var data : Bytes = Bytes.alloc(len); + var size = InputStream.Read( data, off, len); + buf.addBytes( data, 0, size); + return size; + } + + public override function write(buf:Bytes, off : Int, len : Int) : Void { + if (OutputStream == null) + { + throw new TTransportException( TTransportException.NOT_OPEN, + "Cannot write to null OutputStream"); + } + + OutputStream.Write(buf, off, len); + } + + public override function flush(callback:Dynamic->Void =null) : Void { + if (OutputStream == null) + { + var err = new TTransportException( TTransportException.NOT_OPEN, + "Cannot flush null OutputStream"); + if(callback != null) + callback(err); + else + throw err; + } + + OutputStream.Flush(); + } + +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransport.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransport.hx new file mode 100644 index 000000000..e6b31791f --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransport.hx @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import haxe.io.Eof; +import haxe.io.Bytes; +import haxe.io.BytesBuffer; +import org.apache.thrift.AbstractMethodError; + +class TTransport { + + /** + * Queries whether the transport is open. + * + * @return True if the transport is open. + */ + public function isOpen() : Bool { + throw new AbstractMethodError(); + } + + /** + * Is there more data to be read? + * + * @return True if the remote side is still alive and feeding us + */ + public function peek() : Bool { + return isOpen(); + } + + /** + * Opens the transport for reading/writing. + * + * @throws TTransportException if the transport could not be opened + */ + public function open() : Void { + throw new AbstractMethodError(); + } + + /** + * Closes the transport. + */ + public function close() : Void { + throw new AbstractMethodError(); + }; + + /** + * Reads up to len bytes into buffer buf, starting att offset off. + * + * @param buf Array to read into + * @param off Index to start reading at + * @param len Maximum number of bytes to read + * @return The bytes count actually read + * @throws TTransportException if there was an error reading data + */ + public function read( buf : BytesBuffer, off : Int, len : Int) : Int { + throw new AbstractMethodError(); + } + + /** + * Guarantees that all of len bytes are actually read off the transport. + * + * @param buf Array to read into + * @param off Index to start reading at + * @param len Maximum number of bytes to read + * @return The number of bytes actually read, which must be equal to len + * @throws TTransportException if there was an error reading data + */ + public function readAll(buf : BytesBuffer, off : Int, len : Int) : Int { + var got : Int = 0; + var ret : Int = 0; + while (got < len) { + try { + ret = read(buf, off+got, len-got); + if (ret <= 0) { + throw new TTransportException(TTransportException.UNKNOWN, + "Cannot read. Remote side has closed. Tried to read " + + len + " bytes, but only got " + got + " bytes."); + } + } + catch (eof : Eof) { + throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read $len bytes!'); + } + got += ret; + } + return got; + } + + /** + * Writes the buffer to the output + * + * @param buf The output data buffer + * @throws TTransportException if an error occurs writing data + */ + public function writeAll(buf:Bytes) : Void { + write(buf, 0, buf.length); + } + + /** + * Writes up to len bytes from the buffer. + * + * @param buf The output data buffer + * @param off The offset to start writing from + * @param len The number of bytes to write + * @throws TTransportException if there was an error writing data + */ + public function write(buf:Bytes, off : Int, len : Int) : Void { + throw new AbstractMethodError(); + } + + /** + * Flush any pending data out of a transport buffer. + * + * @throws TTransportException if there was an error writing out data. + */ + public function flush(callback:Dynamic->Void =null) : Void { + if(callback != null) + callback(new AbstractMethodError()); + else + throw new AbstractMethodError(); + } + +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransportException.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransportException.hx new file mode 100644 index 000000000..ad028dd86 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransportException.hx @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +import org.apache.thrift.TException; + +class TTransportException extends TException { + + // WARNING: These are subject to be extended in the future, so we can't use enums + // with Haxe 3.1.3 because of https://github.com/HaxeFoundation/haxe/issues/3649 + public static inline var UNKNOWN : Int = 0; + public static inline var NOT_OPEN : Int = 1; + public static inline var ALREADY_OPEN : Int = 2; + public static inline var TIMED_OUT : Int = 3; + public static inline var END_OF_FILE : Int = 4; + + public function new(error : Int = UNKNOWN, message : String = "") { + super(message, error); + } + +} +
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransportFactory.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransportFactory.hx new file mode 100644 index 000000000..361f35f10 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TTransportFactory.hx @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + +/** + * Factory class used to create wrapped instance of Transports. + * This is used primarily in servers, which get Transports from + * a ServerTransport and then may want to mutate them (i.e. create + * a BufferedTransport from the underlying base transport) + * + */ +class TTransportFactory { + + public function new() { + } + + /** + * Return a wrapped instance of the base Transport. + * + * @param trans The base transport + * @return Wrapped Transport + */ + public function getTransport( trans : TTransport) : TTransport { + return trans; + } + +} diff --git a/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TWrappingServerTransport.hx b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TWrappingServerTransport.hx new file mode 100644 index 000000000..b2272f387 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/src/org/apache/thrift/transport/TWrappingServerTransport.hx @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.thrift.transport; + + +/* + wraps real transport, provided by constructor +*/ +class TWrappingServerTransport extends TServerTransport { + + private var transport(default,null) : TTransport; + + public function new(transport : TTransport) { + this.transport = transport; + } + + public override function Listen() : Void + { + } + + private override function AcceptImpl() : TTransport + { + return transport; + } + + public override function Close() : Void + { + + } +} diff --git a/src/jaegertracing/thrift/lib/haxe/test/HaxeTests.hxproj b/src/jaegertracing/thrift/lib/haxe/test/HaxeTests.hxproj new file mode 100644 index 000000000..3beed8244 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/HaxeTests.hxproj @@ -0,0 +1,69 @@ +<?xml version="1.0" encoding="utf-8"?> +<project version="2"> + <!-- Output SWF options --> + <output> + <movie outputType="Application" /> + <movie input="" /> + <movie path="bin/HaxeTests" /> + <movie fps="30" /> + <movie width="800" /> + <movie height="600" /> + <movie version="1" /> + <movie minorVersion="0" /> + <movie platform="C++" /> + <movie background="#FFFFFF" /> + </output> + <!-- Other classes to be compiled into your SWF --> + <classpaths> + <class path="src" /> + <class path="gen-haxe" /> + <class path="../src" /> + </classpaths> + <!-- Build options --> + <build> + <option directives="" /> + <option flashStrict="False" /> + <option noInlineOnDebug="False" /> + <option mainClass="Main" /> + <option enabledebug="False" /> + <option additional="" /> + </build> + <!-- haxelib libraries --> + <haxelib> + <!-- example: <library name="..." /> --> + </haxelib> + <!-- Class files to compile (other referenced classes will automatically be included) --> + <compileTargets> + <!-- example: <compile path="..." /> --> + </compileTargets> + <!-- Paths to exclude from the Project Explorer tree --> + <hiddenPaths> + <hidden path="obj" /> + <hidden path="cpp.hxml" /> + <hidden path="csharp.hxml" /> + <hidden path="flash.hxml" /> + <hidden path="java.hxml" /> + <hidden path="javascript.hxml" /> + <hidden path="make_all.bat" /> + <hidden path="make_all.sh" /> + <hidden path="Makefile.am" /> + <hidden path="neko.hxml" /> + <hidden path="php.hxml" /> + <hidden path="project.hide" /> + <hidden path="python.hxml" /> + </hiddenPaths> + <!-- Executed before build --> + <preBuildCommand>thrift -r -gen haxe ../../../test/ThriftTest.thrift +thrift -r -gen haxe ../../../contrib/async-test/aggr.thrift +thrift -r -gen haxe ../../../lib/rb/benchmark/Benchmark.thrift</preBuildCommand> + <!-- Executed after build --> + <postBuildCommand alwaysRun="False" /> + <!-- Other project options --> + <options> + <option showHiddenPaths="False" /> + <option testMovie="Custom" /> + <option testMovieCommand="bin/HaxeTests/Main.exe server multiplex" /> + </options> + <!-- Plugin storage --> + <storage /> +</project>
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/Makefile.am b/src/jaegertracing/thrift/lib/haxe/test/Makefile.am new file mode 100644 index 000000000..2b8b24524 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/Makefile.am @@ -0,0 +1,85 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +THRIFTCMD = $(THRIFT) --gen haxe -r +THRIFTTEST = $(top_srcdir)/test/ThriftTest.thrift +AGGR = $(top_srcdir)/contrib/async-test/aggr.thrift +BENCHMARK = $(top_srcdir)/lib/rb/benchmark/Benchmark.thrift + +BIN_CPP = bin/Main-debug +BIN_PHP = bin/php/Main-debug.php + +gen-haxe/thrift/test/ThriftTest.hx: $(THRIFTTEST) + $(THRIFTCMD) $(THRIFTTEST) + +gen-haxe/thrift/test/Aggr.hx: $(AGGR) + $(THRIFTCMD) $(AGGR) + +gen-haxe/thrift/test/BenchmarkService.hx: $(BENCHMARK) + $(THRIFTCMD) $(BENCHMARK) + +all-local: $(BIN_CPP) $(BIN_PHP) + +$(BIN_CPP): \ + src/*.hx \ + ../src/org/apache/thrift/**/*.hx \ + gen-haxe/thrift/test/ThriftTest.hx \ + gen-haxe/thrift/test/Aggr.hx \ + gen-haxe/thrift/test/BenchmarkService.hx + $(HAXE) --cwd . cpp.hxml + +$(BIN_PHP): \ + src/*.hx \ + ../src/org/apache/thrift/**/*.hx \ + gen-haxe/thrift/test/ThriftTest.hx \ + gen-haxe/thrift/test/Aggr.hx \ + gen-haxe/thrift/test/BenchmarkService.hx + $(HAXE) --cwd . php.hxml + + +#TODO: other haxe targets +# $(HAXE) --cwd . csharp +# $(HAXE) --cwd . flash +# $(HAXE) --cwd . java +# $(HAXE) --cwd . javascript +# $(HAXE) --cwd . neko +# $(HAXE) --cwd . python # needs Haxe 3.2.0 + + +clean-local: + $(RM) -r gen-haxe bin + +check: $(BIN_CPP) $(BIN_PHP) + $(BIN_CPP) + php -f $(BIN_PHP) + +EXTRA_DIST = \ + src \ + cpp.hxml \ + csharp.hxml \ + flash.hxml \ + java.hxml \ + javascript.hxml \ + neko.hxml \ + php.hxml \ + python.hxml \ + project.hide \ + HaxeTests.hxproj \ + make_all.bat \ + make_all.sh diff --git a/src/jaegertracing/thrift/lib/haxe/test/cpp.hxml b/src/jaegertracing/thrift/lib/haxe/test/cpp.hxml new file mode 100644 index 000000000..73848a8bc --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/cpp.hxml @@ -0,0 +1,41 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#integrate files to classpath +-cp src +-cp ../src +-cp gen-haxe + +#this class wil be used as entry point for your app. +-main Main + +#CPP target +-cpp bin + +#To produce 64 bit binaries the file should define the HXCPP_M64 compile variable: +#-D HXCPP_M64 + +#Add debug information +-debug + +#dead code elimination : remove unused code +#"-dce no" : do not remove unused code +#"-dce std" : remove unused code in the std lib (default) +#"-dce full" : remove all unused code +-dce full
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/csharp.hxml b/src/jaegertracing/thrift/lib/haxe/test/csharp.hxml new file mode 100644 index 000000000..4c34b0d94 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/csharp.hxml @@ -0,0 +1,38 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#integrate files to classpath +-cp src +-cp ../src +-cp gen-haxe + +#this class wil be used as entry point for your app. +-main Main + +#CSHARP target +-cs bin/Test.exe + +#Add debug information +-debug + +#dead code elimination : remove unused code +#"-dce no" : do not remove unused code +#"-dce std" : remove unused code in the std lib (default) +#"-dce full" : remove all unused code +-dce full
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/flash.hxml b/src/jaegertracing/thrift/lib/haxe/test/flash.hxml new file mode 100644 index 000000000..8b1763190 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/flash.hxml @@ -0,0 +1,38 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#integrate files to classpath +-cp src +-cp ../src +-cp gen-haxe + +#this class wil be used as entry point for your app. +-main Main + +#Flash target +-swf bin/Test.swf + +#Add debug information +-debug + +#dead code elimination : remove unused code +#"-dce no" : do not remove unused code +#"-dce std" : remove unused code in the std lib (default) +#"-dce full" : remove all unused code +-dce full
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/java.hxml b/src/jaegertracing/thrift/lib/haxe/test/java.hxml new file mode 100644 index 000000000..c9471597c --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/java.hxml @@ -0,0 +1,38 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#integrate files to classpath +-cp src +-cp ../src +-cp gen-haxe + +#this class wil be used as entry point for your app. +-main Main + +#Java target +-java bin/Test.jar + +#Add debug information +-debug + +#dead code elimination : remove unused code +#"-dce no" : do not remove unused code +#"-dce std" : remove unused code in the std lib (default) +#"-dce full" : remove all unused code +-dce full
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/javascript.hxml b/src/jaegertracing/thrift/lib/haxe/test/javascript.hxml new file mode 100644 index 000000000..18d9964c2 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/javascript.hxml @@ -0,0 +1,44 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#integrate files to classpath +-cp src +-cp ../src +-cp gen-haxe + +#this class wil be used as entry point for your app. +-main Main + +#JavaScript target +-js bin/Test.js + +#You can use -D source-map-content (requires Haxe 3.1+) to have the .hx +#files directly embedded into the map file, this way you only have to +#upload it, and it will be always in sync with the compiled .js even if +#you modify your .hx files. +-D source-map-content + +#Generate source map and add debug information +-debug + +#dead code elimination : remove unused code +#"-dce no" : do not remove unused code +#"-dce std" : remove unused code in the std lib (default) +#"-dce full" : remove all unused code +-dce full
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/make_all.bat b/src/jaegertracing/thrift/lib/haxe/test/make_all.bat new file mode 100644 index 000000000..0314e18a3 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/make_all.bat @@ -0,0 +1,70 @@ +@echo off +rem /* +rem * Licensed to the Apache Software Foundation (ASF) under one +rem * or more contributor license agreements. See the NOTICE file +rem * distributed with this work for additional information +rem * regarding copyright ownership. The ASF licenses this file +rem * to you under the Apache License, Version 2.0 (the +rem * "License"); you may not use this file except in compliance +rem * with the License. You may obtain a copy of the License at +rem * +rem * http://www.apache.org/licenses/LICENSE-2.0 +rem * +rem * Unless required by applicable law or agreed to in writing, +rem * software distributed under the License is distributed on an +rem * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +rem * KIND, either express or implied. See the License for the +rem * specific language governing permissions and limitations +rem * under the License. +rem */ + +setlocal +if "%HOMEDRIVE%"=="" goto MISSINGVARS +if "%HOMEPATH%"=="" goto MISSINGVARS +if "%HAXEPATH%"=="" goto NOTINSTALLED + +set path=%HAXEPATH%;%HAXEPATH%\..\neko;%path% + +rem # invoke Thrift comnpiler +thrift -r -gen haxe ..\..\..\test\ThriftTest.thrift +thrift -r -gen haxe ..\..\..\contrib\async-test\aggr.thrift +thrift -r -gen haxe ..\..\..\lib\rb\benchmark\Benchmark.thrift +if errorlevel 1 goto STOP + +rem # invoke Haxe compiler for all targets +for %%a in (*.hxml) do ( + rem * filter Python, as it is not supported by Haxe 3.1.3 (but will be in 3.1.4) + if not "%%a"=="python.hxml" ( + echo -------------------------- + echo Building %%a ... + echo -------------------------- + haxe --cwd . %%a + ) +) + + +echo. +echo done. +pause +goto eof + +:NOTINSTALLED +echo FATAL: Either Haxe is not installed, or the HAXEPATH variable is not set. +pause +goto eof + +:MISSINGVARS +echo FATAL: Unable to locate home folder. +echo. +echo Both HOMEDRIVE and HOMEPATH need to be set to point to your Home folder. +echo The current values are: +echo HOMEDRIVE=%HOMEDRIVE% +echo HOMEPATH=%HOMEPATH% +pause +goto eof + +:STOP +pause +goto eof + +:eof diff --git a/src/jaegertracing/thrift/lib/haxe/test/make_all.sh b/src/jaegertracing/thrift/lib/haxe/test/make_all.sh new file mode 100644 index 000000000..512f5ec03 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/make_all.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +# invoke Thrift comnpiler +thrift -r -gen haxe ../../../test/ThriftTest.thrift +thrift -r -gen haxe ../../../contrib/async-test/aggr.thrift +thrift -r -gen haxe ../../../lib/rb/benchmark/Benchmark.thrift + +# output folder +if [ ! -d bin ]; then + mkdir bin +fi + +# invoke Haxe compiler +for target in *.hxml; do + echo -------------------------- + echo Building ${target} ... + echo -------------------------- + if [ ! -d bin/${target} ]; then + mkdir bin/${target} + fi + haxe --cwd . ${target} +done + + +#eof diff --git a/src/jaegertracing/thrift/lib/haxe/test/neko.hxml b/src/jaegertracing/thrift/lib/haxe/test/neko.hxml new file mode 100644 index 000000000..2db70c8e6 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/neko.hxml @@ -0,0 +1,38 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#integrate files to classpath +-cp src +-cp ../src +-cp gen-haxe + +#this class wil be used as entry point for your app. +-main Main + +#neko target +-neko bin/Test.n + +#Add debug information +-debug + +#dead code elimination : remove unused code +#"-dce no" : do not remove unused code +#"-dce std" : remove unused code in the std lib (default) +#"-dce full" : remove all unused code +-dce full
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/php.hxml b/src/jaegertracing/thrift/lib/haxe/test/php.hxml new file mode 100644 index 000000000..14f2b2d01 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/php.hxml @@ -0,0 +1,39 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#integrate files to classpath +-cp src +-cp ../src +-cp gen-haxe + +#this class wil be used as entry point for your app. +-main Main + +#PHP target +-php bin/php/ +--php-front Main-debug.php + +#Add debug information +-debug + +#dead code elimination : remove unused code +#"-dce no" : do not remove unused code +#"-dce std" : remove unused code in the std lib (default) +#"-dce full" : remove all unused code +-dce full diff --git a/src/jaegertracing/thrift/lib/haxe/test/project.hide b/src/jaegertracing/thrift/lib/haxe/test/project.hide new file mode 100644 index 000000000..16ef98e98 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/project.hide @@ -0,0 +1,67 @@ +{ + "type" : 0 + ,"target" : 4 + ,"name" : "Test" + ,"main" : null + ,"projectPackage" : "" + ,"company" : "" + ,"license" : "" + ,"url" : "" + ,"targetData" : [ + { + "pathToHxml" : "flash.hxml" + ,"runActionType" : 1 + ,"runActionText" : "bin/Test.swf" + } + ,{ + "pathToHxml" : "javascript.hxml" + ,"runActionType" : 1 + ,"runActionText" : "bin\\index.html" + } + ,{ + "pathToHxml" : "neko.hxml" + ,"runActionType" : 2 + ,"runActionText" : "neko bin/Test.n" + } + ,{ + "pathToHxml" : "php.hxml" + } + ,{ + "pathToHxml" : "cpp.hxml" + ,"runActionType" : 2 + ,"runActionText" : "bin/Main-debug.exe" + } + ,{ + "pathToHxml" : "java.hxml" + } + ,{ + "pathToHxml" : "csharp.hxml" + } + ,{ + "pathToHxml" : "python.hxml" + ,"runActionType" : 2 + ,"runActionText" : "python bin/Test.py" + } + ] + ,"files" : [ + { + "path" : "src\\Main.hx" + ,"useTabs" : true + ,"indentSize" : 4 + ,"foldedRegions" : [ + + ] + ,"activeLine" : 13 + } + ] + ,"activeFile" : "src\\Main.hx" + ,"openFLTarget" : null + ,"openFLBuildMode" : "Debug" + ,"runActionType" : null + ,"runActionText" : null + ,"buildActionCommand" : null + ,"hiddenItems" : [ + + ] + ,"showHiddenItems" : false +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/python.hxml b/src/jaegertracing/thrift/lib/haxe/test/python.hxml new file mode 100644 index 000000000..4d6a133d6 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/python.hxml @@ -0,0 +1,38 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +#integrate files to classpath +-cp src +-cp ../src +-cp gen-haxe + +#this class wil be used as entry point for your app. +-main Main + +#Python target +-python bin/Test.py + +#Add debug information +-debug + +#dead code elimination : remove unused code +#"-dce no" : do not remove unused code +#"-dce std" : remove unused code in the std lib (default) +#"-dce full" : remove all unused code +-dce full
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/src/Main.hx b/src/jaegertracing/thrift/lib/haxe/test/src/Main.hx new file mode 100644 index 000000000..6c262d78f --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/src/Main.hx @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package; + +import org.apache.thrift.*; +import org.apache.thrift.protocol.*; +import org.apache.thrift.transport.*; +import org.apache.thrift.server.*; +import org.apache.thrift.meta_data.*; + +import thrift.test.*; // generated code + + +enum WhatTests { + Normal; + Multiplex; +} + +class Main +{ + static private var tests : WhatTests = Normal; + static private var server : Bool = false; + + static private inline var CMDLINEHELP : String + = "\nHaxeTests [client|server] [multiplex]\n" + + " client|server ... determines run mode for some tests, default is client\n" + + " multiplex ........ run multiplex test server or client\n"; + + static private function ParseArgs() { + #if sys + + var args = Sys.args(); + if ( args != null) { + for ( arg in args) { + switch(arg.toLowerCase()) { + case "client": + server = false; + case "server" : + server = true; + case "multiplex" : + tests = Multiplex; + default: + throw 'Invalid argument "$arg"\n'+CMDLINEHELP; + } + } + } + + #end + } + + static public function main() + { + try + { + ParseArgs(); + + switch( tests) { + case Normal: + StreamTest.Run(server); + case Multiplex: + MultiplexTest.Run(server); + default: + throw "Unhandled test mode $tests"; + } + + trace("All tests completed."); + } + catch( e: Dynamic) + { + trace('$e'); + #if sys + Sys.exit(1); // indicate error + #end + } + } +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/haxe/test/src/MultiplexTest.hx b/src/jaegertracing/thrift/lib/haxe/test/src/MultiplexTest.hx new file mode 100644 index 000000000..3818b6609 --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/src/MultiplexTest.hx @@ -0,0 +1,224 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package; + +import haxe.Int64; +import haxe.Int32; + +import org.apache.thrift.*; +import org.apache.thrift.protocol.*; +import org.apache.thrift.transport.*; +import org.apache.thrift.server.*; +import org.apache.thrift.meta_data.*; + +// debug only +import org.apache.thrift.protocol.TProtocolDecorator; +import org.apache.thrift.protocol.TMultiplexedProtocol; +import org.apache.thrift.protocol.TMultiplexedProcessor; + +// generated code imports +import Aggr; +import AggrImpl; +import AggrProcessor; +import BenchmarkService; +import BenchmarkServiceImpl; +import BenchmarkServiceProcessor; +import Error; + + +class BenchmarkServiceHandler implements BenchmarkService +{ + public function new() { + } + + public function fibonacci(n : haxe.Int32) : haxe.Int32 { + trace('Benchmark.fibonacci($n)'); + var next : Int; + var prev = 0; + var result = 1; + while( n > 0) + { + next = result + prev; + prev = result; + result = next; + --n; + } + return result; + } +} + + +class AggrServiceHandler implements Aggr +{ + private var values : List<haxe.Int32> = new List<haxe.Int32>(); + + public function new() { + } + + public function addValue(value : haxe.Int32) : Void { + trace('Aggr.addValue($value)'); + values.add( value); + } + + public function getValues() : List< haxe.Int32> { + trace('Aggr.getValues()'); + return values; + } +} + + + +class MultiplexTest extends TestBase { + + private inline static var NAME_BENCHMARKSERVICE : String = "BenchmarkService"; + private inline static var NAME_AGGR : String = "Aggr"; + + + public static override function Run(server : Bool) : Void { + if ( server) { + RunMultiplexServer(); + } else { + RunMultiplexClient(); + RunDefaultClient(); + } + } + + + // run the multiplex server + public static override function RunMultiplexServer() : Void { + try + { + var benchHandler : BenchmarkService = new BenchmarkServiceHandler(); + var benchProcessor : TProcessor = new BenchmarkServiceProcessor( benchHandler); + + var aggrHandler : Aggr = new AggrServiceHandler(); + var aggrProcessor : TProcessor = new AggrProcessor( aggrHandler); + + var multiplex : TMultiplexedProcessor = new TMultiplexedProcessor(); + multiplex.RegisterProcessor( NAME_BENCHMARKSERVICE, benchProcessor, true); // default + multiplex.RegisterProcessor( NAME_AGGR, aggrProcessor); + + // protocol+transport stack + var protfact : TProtocolFactory = new TBinaryProtocolFactory(true,true); + var servertrans : TServerTransport = new TServerSocket( 9090, 5, false); + var transfact : TTransportFactory = new TFramedTransportFactory(); + + var server : TServer = new TSimpleServer( multiplex, servertrans, transfact, protfact); + + trace("Starting the server ..."); + server.Serve(); + } + catch( e : TApplicationException) + { + TestBase.Expect(false,'${e.errorID} ${e.errorMsg}'); + } + catch( e : TException) + { + TestBase.Expect(false,'$e'); + } + } + + + // run multiplex client against multiplex server + public static override function RunMultiplexClient() : Void { + try + { + var trans : TTransport; + trans = new TSocket("localhost", 9090); + trans = new TFramedTransport(trans); + trans.open(); + + var protocol : TProtocol = new TBinaryProtocol(trans,true,true); + var multiplex : TMultiplexedProtocol; + + multiplex = new TMultiplexedProtocol( protocol, NAME_BENCHMARKSERVICE); + var bench = new BenchmarkServiceImpl( multiplex); + + multiplex = new TMultiplexedProtocol( protocol, NAME_AGGR); + var aggr = new AggrImpl( multiplex); + + trace('calling aggr.add( bench.fibo())...'); + for( i in 1 ... 10) + { + trace('$i'); + aggr.addValue( bench.fibonacci(i)); + } + + trace('calling aggr ...'); + var i = 1; + var values = aggr.getValues(); + TestBase.Expect(values != null,'aggr.getValues() == null'); + for( k in values) + { + trace('fib($i) = $k'); + ++i; + } + + trans.close(); + trace('done.'); + + } + catch( e : TApplicationException) + { + TestBase.Expect(false,'${e.errorID} ${e.errorMsg}'); + } + catch( e : TException) + { + TestBase.Expect(false,'$e'); + } + } + + + // run non-multiplex client against multiplex server to test default fallback + public static override function RunDefaultClient() : Void { + try + { + var trans : TTransport; + trans = new TSocket("localhost", 9090); + trans = new TFramedTransport(trans); + trans.open(); + + var protocol : TProtocol = new TBinaryProtocol(trans,true,true); + + var bench = new BenchmarkServiceImpl( protocol); + + trace('calling bench (via default) ...'); + for( i in 1 ... 10) + { + var k = bench.fibonacci(i); + trace('fib($i) = $k'); + } + + trans.close(); + trace('done.'); + } + catch( e : TApplicationException) + { + TestBase.Expect(false,'${e.errorID} ${e.errorMsg}'); + } + catch( e : TException) + { + TestBase.Expect(false,'$e'); + } + } + +} + + diff --git a/src/jaegertracing/thrift/lib/haxe/test/src/StreamTest.hx b/src/jaegertracing/thrift/lib/haxe/test/src/StreamTest.hx new file mode 100644 index 000000000..244f1ea9d --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/src/StreamTest.hx @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package; + +import haxe.Int64; +import sys.FileSystem; + +import org.apache.thrift.*; +import org.apache.thrift.protocol.*; +import org.apache.thrift.transport.*; +import org.apache.thrift.server.*; +import org.apache.thrift.meta_data.*; + +import thrift.test.*; // generated code + + +class StreamTest extends TestBase { + + + private inline static var tmpfile : String = "data.tmp"; + + + private static function MakeTestData() : Xtruct { + var data : Xtruct = new Xtruct(); + data.string_thing = "Streamtest"; + data.byte_thing = -128; + data.i32_thing = 4711; + data.i64_thing = Int64.make(0x12345678,0x9ABCDEF0); + return data; + } + + public static function WriteData() : Xtruct + { + var stream : TStream = new TFileStream( tmpfile, CreateNew); + var trans : TTransport = new TStreamTransport( null, stream); + var prot = new TJSONProtocol( trans); + + var data = MakeTestData(); + data.write(prot); + trans.close(); + + return data; + } + + public static function ReadData() : Xtruct + { + var stream : TStream = new TFileStream( tmpfile, Read); + var trans : TTransport = new TStreamTransport( stream, null); + var prot = new TJSONProtocol( trans); + + var data : Xtruct = new Xtruct(); + data.read(prot); + trans.close(); + + return data; + } + + public static override function Run(server : Bool) : Void + { + try { + var written = WriteData(); + var read = ReadData(); + FileSystem.deleteFile(tmpfile); + + TestBase.Expect( read.string_thing == written.string_thing, "string data"); + TestBase.Expect( read.byte_thing == written.byte_thing, "byte data"); + TestBase.Expect( read.i32_thing == written.i32_thing, "i32 data"); + TestBase.Expect( Int64.compare( read.i64_thing, written.i64_thing) == 0, "i64 data"); + + } catch(e:Dynamic) { + FileSystem.deleteFile(tmpfile); + throw e; + } + } + +} + + diff --git a/src/jaegertracing/thrift/lib/haxe/test/src/TestBase.hx b/src/jaegertracing/thrift/lib/haxe/test/src/TestBase.hx new file mode 100644 index 000000000..12327737a --- /dev/null +++ b/src/jaegertracing/thrift/lib/haxe/test/src/TestBase.hx @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package; + +import org.apache.thrift.*; +import org.apache.thrift.protocol.*; +import org.apache.thrift.transport.*; +import org.apache.thrift.server.*; +import org.apache.thrift.meta_data.*; + + +class TestBase { + + private function new() { + // override, if necessary + } + + public static function Run(server : Bool) : Void { + throw new AbstractMethodError(); + } + + public static function Expect( expr : Bool, info : String, ?pos : haxe.PosInfos) : Void { + if( ! expr) { + throw ('Test "$info" failed at '+pos.methodName+' in '+pos.fileName+':'+pos.lineNumber); + } + } + +} +
\ No newline at end of file |