1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package;
21
22import org.apache.thrift.*;
23import org.apache.thrift.protocol.*;
24import org.apache.thrift.transport.*;
25import org.apache.thrift.server.*;
26import org.apache.thrift.meta_data.*;
27import org.apache.thrift.helper.*;
28import uuid.Uuid;
29
30import haxe.Int32;
31import haxe.Int64;
32import haxe.io.Bytes;
33import haxe.ds.IntMap;
34import haxe.ds.StringMap;
35import haxe.ds.ObjectMap;
36
37import thrift.test.*;  // generated code
38
39
40class TestServerHandler implements ThriftTest_service {
41
42    public var server:TServer;
43
44    public function new() {
45    }
46
47    /**
48    * Prints "testVoid()" and returns nothing.
49    */
50    public function testVoid():Void
51    {
52        trace("testVoid()");
53    }
54
55    /**
56    * Prints 'testBool("%s")' where '%s' with thing as 'true' or 'false'
57    * @param bool  thing - the bool data to print
58    * @return bool  - returns the bool 'thing'
59    *
60    * @param thing
61    */
62    public function testBool(thing : Bool) : Bool
63    {
64        trace('testBool($thing)');
65        return thing;
66    }
67
68    /**
69    * Prints 'testString("%s")' with thing as '%s'
70    * @param string thing - the string to print
71    * @return string - returns the string 'thing'
72    *
73    * @param thing
74    */
75    public function testString(thing:String):String
76    {
77        trace("teststring(\"" + thing + "\")");
78        return thing;
79    }
80
81    /**
82    * Prints 'testByte("%d")' with thing as '%d'
83    * @param byte thing - the byte to print
84    * @return byte - returns the byte 'thing'
85    *
86    * @param thing
87    */
88    public function testByte(thing:haxe.Int32):haxe.Int32
89    {
90        trace("testByte(" + thing + ")");
91        return thing;
92    }
93
94    /**
95    * Prints 'testI32("%d")' with thing as '%d'
96    * @param i32 thing - the i32 to print
97    * @return i32 - returns the i32 'thing'
98    *
99    * @param thing
100    */
101    public function testI32(thing:haxe.Int32):haxe.Int32
102    {
103        trace("testI32(" + thing + ")");
104        return thing;
105    }
106
107    /**
108    * Prints 'testI64("%d")' with thing as '%d'
109    * @param i64 thing - the i64 to print
110    * @return i64 - returns the i64 'thing'
111    *
112    * @param thing
113    */
114    public function testI64(thing:haxe.Int64):haxe.Int64
115    {
116        trace("testI64(" + thing + ")");
117        return thing;
118    }
119
120    /**
121    * Prints 'testDouble("%f")' with thing as '%f'
122    * @param double thing - the double to print
123    * @return double - returns the double 'thing'
124    *
125    * @param thing
126    */
127    public function testDouble(thing:Float):Float
128    {
129        trace("testDouble(" + thing + ")");
130        return thing;
131    }
132
133    /**
134     * Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
135     * @param binary  thing - the binary data to print
136     * @return binary  - returns the binary 'thing'
137     *
138     * @param thing
139     */
140    public function testBinary(thing : haxe.io.Bytes) : haxe.io.Bytes
141    {
142        var hex = "";
143        for ( i in 0 ... thing.length) {
144            hex += StringTools.hex( thing.get(i), 2);
145        }
146        trace('testBinary($hex)');
147        return thing;
148    }
149
150    /**
151     * Prints 'testUuid("%s")'
152     * @param Uuid  thing - the uuid to print
153     * @return Uuid  - returns the uuid 'thing'
154     *
155     * @param thing
156     */
157    public function testUuid(thing : String) : String
158    {
159        trace('testUuid($thing)');
160        return thing;
161    }
162
163    /**
164    * Prints 'testStruct("{%s}")' where thing has been formatted
165    *  into a string of comma separated values
166    * @param Xtruct thing - the Xtruct to print
167    * @return Xtruct - returns the Xtruct 'thing'
168    *
169    * @param thing
170    */
171    public function testStruct(thing:Xtruct):Xtruct
172    {
173        trace("testStruct({" +
174                          "\"" + thing.string_thing + "\", " +
175                          thing.byte_thing + ", " +
176                          thing.i32_thing + ", " +
177                          Int64.toStr(thing.i64_thing) + "})");
178        return thing;
179    }
180
181    /**
182    * Prints 'testNest("{%s}")' where thing has been formatted
183    *  into a string of the nested struct
184    * @param Xtruct2 thing - the Xtruct2 to print
185    * @return Xtruct2 - returns the Xtruct2 'thing'
186    *
187    * @param thing
188    */
189    public function testNest(nest:Xtruct2):Xtruct2
190    {
191        var thing:Xtruct = nest.struct_thing;
192        trace("testNest({" +
193                          nest.byte_thing + ", {" +
194                          "\"" + thing.string_thing + "\", " +
195                          thing.byte_thing + ", " +
196                          thing.i32_thing + ", " +
197                          Int64.toStr(thing.i64_thing) + "}, " +
198                          nest.i32_thing + "})");
199        return nest;
200    }
201
202    /**
203    * Prints 'testMap("{%s")' where thing has been formatted
204    *  into a string of  'key => value' pairs
205    *  separated by commas and new lines
206    * @param map<i32,i32> thing - the map<i32,i32> to print
207    * @return map<i32,i32> - returns the map<i32,i32> 'thing'
208    *
209    * @param thing
210    */
211    public function testMap(thing:IntMap<haxe.Int32>):IntMap<haxe.Int32>
212    {
213        trace("testMap({");
214        var first:Bool = true;
215        for (key in thing.keys()) {
216            if (first) {
217                first = false;
218            } else {
219                trace(", ");
220            };
221            trace(key + " => " + thing.get(key));
222        };
223        trace("})");
224        return thing;
225    }
226
227    /**
228    * Prints 'testStringMap("{%s}")' where thing has been formatted
229    *  into a string of  'key => value' pairs
230    *  separated by commas and new lines
231    * @param map<string,string> thing - the map<string,string> to print
232    * @return map<string,string> - returns the map<string,string> 'thing'
233    *
234    * @param thing
235    */
236    public function testStringMap(thing:StringMap<String>):StringMap<String>
237    {
238        trace("testStringMap({");
239        var first:Bool = true;
240        for (key in thing.keys()) {
241            if (first) {
242                first = false;
243            } else {
244                trace(", ");
245            };
246            trace(key + " => " + thing.get(key));
247        };
248        trace("})");
249        return thing;
250    }
251
252    /**
253    * Prints 'testSet("{%s}")' where thing has been formatted
254    *  into a string of  values
255    *  separated by commas and new lines
256    * @param set<i32> thing - the set<i32> to print
257    * @return set<i32> - returns the set<i32> 'thing'
258    *
259    * @param thing
260    */
261    public function testSet(thing:IntSet):IntSet
262    {
263        trace("testSet({");
264        var first:Bool = true;
265        for (elem in thing) {
266            if (first) {
267                first = false;
268            } else {
269                trace(", ");
270            };
271            trace(elem);
272        };
273        trace("})");
274        return thing;
275    }
276
277    /**
278    * Prints 'testList("{%s}")' where thing has been formatted
279    *  into a string of  values
280    *  separated by commas and new lines
281    * @param list<i32> thing - the list<i32> to print
282    * @return list<i32> - returns the list<i32> 'thing'
283    *
284    * @param thing
285    */
286    public function testList(thing:List<haxe.Int32>):List<haxe.Int32>
287    {
288        trace("testList({");
289        var first:Bool = true;
290        for (elem in thing) {
291            if (first) {
292                first = false;
293            } else {
294                trace(", ");
295            };
296            trace(elem);
297        };
298        trace("})");
299        return thing;
300    }
301
302    /**
303    * Prints 'testEnum("%d")' where thing has been formatted into it's numeric value
304    * @param Numberz thing - the Numberz to print
305    * @return Numberz - returns the Numberz 'thing'
306    *
307    * @param thing
308    */
309    public function testEnum(thing:Int):Int
310    {
311        trace("testEnum(" + thing + ")");
312        return thing;
313    }
314
315    /**
316    * Prints 'testTypedef("%d")' with thing as '%d'
317    * @param UserId thing - the UserId to print
318    * @return UserId - returns the UserId 'thing'
319    *
320    * @param thing
321    */
322    public function testTypedef(thing:haxe.Int64):haxe.Int64
323    {
324        trace("testTypedef(" + thing + ")");
325        return thing;
326    }
327
328    /**
329    * Prints 'testMapMap("%d")' with hello as '%d'
330    * @param i32 hello - the i32 to print
331    * @return map<i32,map<i32,i32>> - returns a dictionary with these values:
332    *   {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, },
333    *     4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, }
334    *
335    * @param hello
336    */
337    public function testMapMap(hello:haxe.Int32):IntMap<IntMap<haxe.Int32>>
338    {
339        trace("testMapMap(" + hello + ")");
340        var mapmap = new IntMap<IntMap<Int>>();
341        var pos = new IntMap<Int>();
342        var neg = new IntMap<Int>();
343        for (i in 1 ... 5) {
344            pos.set(i, i);
345            neg.set(-i, -i);
346        };
347        mapmap.set(4, pos);
348        mapmap.set(-4, neg);
349        return mapmap;
350    }
351
352    /**
353    * So you think you've got this all worked, out eh?
354    *
355    * Creates a the returned map with these values and prints it out:
356    *   { 1 => { 2 => argument,
357    *            3 => argument,
358    *          },
359    *     2 => { 6 => <empty Insanity struct>, },
360    *   }
361    * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
362    *
363    * @param argument
364    */
365    public function testInsanity(argument : Insanity) : Int64Map< IntMap< Insanity>>
366    {
367        trace("testInsanity()");
368
369        var first_map = new IntMap< Insanity>();
370        first_map.set(Numberz.TWO, argument);
371        first_map.set(Numberz.THREE, argument);
372
373        var second_map = new IntMap< Insanity>();
374        var looney = new Insanity();
375        second_map.set(Numberz.SIX, looney);
376
377        var insane = new Int64Map< IntMap< Insanity>>();
378        insane.set( Int64.make(0,1), first_map);
379        insane.set( Int64.make(0,2), second_map);
380
381        return insane;
382    }
383
384    /**
385    * Prints 'testMulti()'
386    * @param byte arg0 -
387    * @param i32 arg1 -
388    * @param i64 arg2 -
389    * @param map<i16, string> arg3 -
390    * @param Numberz arg4 -
391    * @param UserId arg5 -
392    * @return Xtruct - returns an Xtruct
393    *    with string_thing = "Hello2, byte_thing = arg0, i32_thing = arg1
394    *    and i64_thing = arg2
395    *
396    * @param arg0
397    * @param arg1
398    * @param arg2
399    * @param arg3
400    * @param arg4
401    * @param arg5
402    */
403    public function testMulti(arg0:haxe.Int32, arg1:haxe.Int32, arg2:haxe.Int64,
404        arg3:IntMap<String>, arg4:Int, arg5:haxe.Int64):Xtruct
405    {
406        trace("testMulti()");
407        var hello = new Xtruct();
408        hello.string_thing = "Hello2";
409        hello.byte_thing = arg0;
410        hello.i32_thing = arg1;
411        hello.i64_thing = arg2;
412        return hello;
413    }
414
415    /**
416    * Print 'testException(%s)' with arg as '%s'
417    * @param string arg - a string indication what type of exception to throw
418    * if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
419    * elsen if arg == "TException" throw TException
420    * else do not throw anything
421    *
422    * @param arg
423    */
424    public function testException(arg:String):Void
425    {
426        trace("testException(" + arg + ")");
427        if (arg == "Xception") {
428            var x = new Xception();
429            x.errorCode = 1001;
430            x.message = arg;
431            throw x;
432        };
433        if (arg == "TException") {
434            throw new TException();
435        };
436        return;
437    }
438
439    /**
440    * Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s'
441    * @param string arg - a string indication what type of exception to throw
442    * if arg0 == "Xception"
443    * throw Xception with errorCode = 1001 and message = "This is an Xception"
444    * else if arg0 == "Xception2"
445    * throw Xception2 with errorCode = 2002 and message = "This is an Xception2"
446    * else do not throw anything
447    * @return Xtruct - an Xtruct with string_thing = arg1
448    *
449    * @param arg0
450    * @param arg1
451    */
452    public function testMultiException(arg0:String, arg1:String):Xtruct
453    {
454        trace("testMultiException(" + arg0 + ", " + arg1 + ")");
455        if (arg0 == "Xception") {
456            var x = new Xception();
457            x.errorCode = 1001;
458            x.message = "This is an Xception";
459            throw x;
460        } else if (arg0 == "Xception2") {
461            var x = new Xception2();
462            x.errorCode = 2002;
463            x.struct_thing = new Xtruct();
464            x.struct_thing.string_thing = "This is an Xception2";
465            throw x;
466        };
467        var result = new Xtruct();
468        result.string_thing = arg1;
469        return result;
470    }
471
472    /**
473    * Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d'
474    * sleep 'secondsToSleep'
475    * Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d'
476    * @param i32 secondsToSleep - the number of seconds to sleep
477    *
478    * @param secondsToSleep
479    */
480    public function testOneway(secondsToSleep:haxe.Int32):Void
481    {
482		#if sys
483        trace("testOneway(" + secondsToSleep + "), sleeping...");
484        Sys.sleep(secondsToSleep);
485		#end
486        trace("testOneway finished");
487    }
488
489    public function testStop():Void
490    {
491        if (server != null) {
492            server.Stop();
493        };
494    }
495}
496