wilfrem | 2c69b5a | 2015-04-20 19:24:50 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * 'License'); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | //This is the server side Node test handler for the standard |
| 21 | // Apache Thrift test service. |
| 22 | |
| 23 | import ttypes = require("./gen-nodejs/ThriftTest_types"); |
| 24 | import thrift = require("thrift"); |
| 25 | import Thrift = thrift.Thrift; |
| 26 | import Q = require("q"); |
| 27 | |
| 28 | |
| 29 | export class SyncThriftTestHandler { |
| 30 | testVoid(): Q.IPromise<void> { |
| 31 | //console.log('testVoid()'); |
| 32 | return Q.resolve<void>(undefined); |
| 33 | } |
| 34 | testMapMap(hello: number) { |
| 35 | //console.log('testMapMap(' + hello + ')'); |
| 36 | |
| 37 | var mapmap: {[key: number]: {[key: number]: number; }} = []; |
| 38 | var pos: {[key: number]: number; } = []; |
| 39 | var neg: {[key: number]: number; } = []; |
| 40 | for (var i = 1; i < 5; i++) { |
| 41 | pos[i] = i; |
| 42 | neg[-i] = -i; |
| 43 | } |
| 44 | mapmap[4] = pos; |
| 45 | mapmap[-4] = neg; |
| 46 | |
| 47 | return Q.resolve(mapmap); |
| 48 | } |
| 49 | testInsanity(argument: ttypes.Insanity): Q.IPromise<{ [k: number]: any; }> { |
| 50 | const first_map: { [k: number]: any; } = []; |
| 51 | const second_map: { [k: number]: any; } = []; |
| 52 | |
| 53 | first_map[ttypes.Numberz.TWO] = argument; |
| 54 | first_map[ttypes.Numberz.THREE] = argument; |
| 55 | |
| 56 | const looney = new ttypes.Insanity(); |
| 57 | second_map[ttypes.Numberz.SIX] = looney; |
| 58 | |
| 59 | const insane: { [k: number]: any; } = []; |
| 60 | insane[1] = first_map; |
| 61 | insane[2] = second_map; |
| 62 | |
| 63 | return Q.resolve(insane); |
| 64 | } |
| 65 | testMulti(arg0: any, arg1: number, arg2: number, arg3: { [k: number]: string; }, arg4: ttypes.Numberz, arg5: number) { |
| 66 | var hello = new ttypes.Xtruct(); |
| 67 | hello.string_thing = 'Hello2'; |
| 68 | hello.byte_thing = arg0; |
| 69 | hello.i32_thing = arg1; |
| 70 | hello.i64_thing = arg2; |
| 71 | return Q.resolve(hello); |
| 72 | } |
| 73 | testException(arg: string): Q.IPromise<void> { |
| 74 | if (arg === 'Xception') { |
| 75 | var x = new ttypes.Xception(); |
| 76 | x.errorCode = 1001; |
| 77 | x.message = arg; |
| 78 | throw x; |
| 79 | } else if (arg === 'TException') { |
| 80 | throw new Thrift.TException(arg); |
| 81 | } else { |
| 82 | return Q.resolve(); |
| 83 | } |
| 84 | } |
| 85 | testMultiException(arg0: string, arg1: string) { |
| 86 | if (arg0 === ('Xception')) { |
| 87 | var x = new ttypes.Xception(); |
| 88 | x.errorCode = 1001; |
| 89 | x.message = 'This is an Xception'; |
| 90 | throw x; |
| 91 | } else if (arg0 === ('Xception2')) { |
| 92 | var x2 = new ttypes.Xception2(); |
| 93 | x2.errorCode = 2002; |
| 94 | x2.struct_thing = new ttypes.Xtruct(); |
| 95 | x2.struct_thing.string_thing = 'This is an Xception2'; |
| 96 | throw x2; |
| 97 | } |
| 98 | |
| 99 | var res = new ttypes.Xtruct(); |
| 100 | res.string_thing = arg1; |
| 101 | return Q.resolve(res); |
| 102 | } |
| 103 | testOneway(sleepFor: number) { |
| 104 | } |
| 105 | |
| 106 | testString(thing: string) { |
| 107 | return Q.resolve(thing); |
| 108 | } |
| 109 | testBool(thing: boolean) { |
| 110 | return Q.resolve(thing); |
| 111 | } |
| 112 | testByte(thing: number) { |
| 113 | return Q.resolve(thing); |
| 114 | } |
| 115 | testI32(thing: number) { |
| 116 | return Q.resolve(thing); |
| 117 | } |
| 118 | testI64(thing: number) { |
| 119 | return Q.resolve(thing); |
| 120 | } |
| 121 | testDouble(thing: number) { |
| 122 | return Q.resolve(thing); |
| 123 | } |
| 124 | testBinary(thing: Buffer) { |
| 125 | return Q.resolve(thing); |
| 126 | } |
| 127 | testStruct(thing: ttypes.Xtruct) { |
| 128 | return Q.resolve(thing); |
| 129 | } |
| 130 | testNest(thing: ttypes.Xtruct2) { |
| 131 | return Q.resolve(thing); |
| 132 | } |
| 133 | testMap(thing: { [k: number]: number; }) { |
| 134 | return Q.resolve(thing); |
| 135 | } |
| 136 | testStringMap(thing: { [k: string]: string; }) { |
| 137 | return Q.resolve(thing); |
| 138 | } |
| 139 | testSet(thing: number[]) { |
| 140 | return Q.resolve(thing); |
| 141 | } |
| 142 | testList(thing: number[]) { |
| 143 | return Q.resolve(thing); |
| 144 | } |
| 145 | testEnum(thing: ttypes.Numberz) { |
| 146 | return Q.resolve(thing); |
| 147 | } |
| 148 | testTypedef(thing: number) { |
| 149 | return Q.resolve(thing); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | export class AsyncThriftTestHandler { |
| 154 | private syncHandler: SyncThriftTestHandler; |
| 155 | constructor() { |
| 156 | this.syncHandler = new SyncThriftTestHandler(); |
| 157 | } |
| 158 | |
| 159 | testVoid(callback: (result: void) => void): Q.IPromise<void> { |
| 160 | callback(undefined); |
| 161 | return Q.resolve(); |
| 162 | } |
| 163 | testMapMap(hello: number, |
| 164 | callback: (err: any, result: { [k: number]: { [k: number]: number; }; }) => void): |
| 165 | Q.IPromise<{ [k: number]: { [k: number]: number; }; }> { |
| 166 | |
| 167 | var mapmap: {[key: number]: {[key: number]: number; }} = []; |
| 168 | var pos: {[key: number]: number; } = []; |
| 169 | var neg: {[key: number]: number; } = []; |
| 170 | for (var i = 1; i < 5; i++) { |
| 171 | pos[i] = i; |
| 172 | neg[-i] = -i; |
| 173 | } |
| 174 | mapmap[4] = pos; |
| 175 | mapmap[-4] = neg; |
| 176 | |
| 177 | callback(null, mapmap); |
| 178 | return Q.resolve(); |
| 179 | } |
| 180 | testInsanity(argument: ttypes.Insanity, callback?: (err: any, result: { [k: number]: any; }) => void): Q.IPromise<{ [k: number]: any; }> { |
| 181 | const first_map: { [k: number]: any; } = []; |
| 182 | const second_map: { [k: number]: any; } = []; |
| 183 | |
| 184 | first_map[ttypes.Numberz.TWO] = argument; |
| 185 | first_map[ttypes.Numberz.THREE] = argument; |
| 186 | |
| 187 | const looney = new ttypes.Insanity(); |
| 188 | second_map[ttypes.Numberz.SIX] = looney; |
| 189 | |
| 190 | const insane: { [k: number]: any; } = []; |
| 191 | insane[1] = first_map; |
| 192 | insane[2] = second_map; |
| 193 | |
| 194 | if (callback !== undefined){ |
| 195 | callback(null, insane); |
| 196 | } |
| 197 | return Q.resolve(); |
| 198 | } |
| 199 | testMulti(arg0: any, arg1: number, arg2: number, arg3: { [k: number]: string; }, arg4: ttypes.Numberz, arg5: number, result: Function): Q.IPromise<ttypes.Xtruct> { |
| 200 | var hello = this.syncHandler.testMulti(arg0, arg1, arg2, arg3, arg4, arg5); |
| 201 | hello.then(hello => result(null, hello)); |
| 202 | return Q.resolve(); |
| 203 | } |
| 204 | testException(arg: string, result: (err: any) => void): Q.IPromise<void> { |
| 205 | if (arg === 'Xception') { |
| 206 | var x = new ttypes.Xception(); |
| 207 | x.errorCode = 1001; |
| 208 | x.message = arg; |
| 209 | result(x); |
| 210 | } else if (arg === 'TException') { |
| 211 | result(new Thrift.TException(arg)); |
| 212 | } else { |
| 213 | result(null); |
| 214 | } |
| 215 | return Q.resolve(); |
| 216 | } |
| 217 | testMultiException(arg0: string, arg1: string, result: (err: any, res?: ttypes.Xtruct) => void): Q.IPromise<ttypes.Xtruct> { |
| 218 | if (arg0 === ('Xception')) { |
| 219 | var x = new ttypes.Xception(); |
| 220 | x.errorCode = 1001; |
| 221 | x.message = 'This is an Xception'; |
| 222 | result(x); |
| 223 | } else if (arg0 === ('Xception2')) { |
| 224 | var x2 = new ttypes.Xception2(); |
| 225 | x2.errorCode = 2002; |
| 226 | x2.struct_thing = new ttypes.Xtruct(); |
| 227 | x2.struct_thing.string_thing = 'This is an Xception2'; |
| 228 | result(x2); |
| 229 | } else { |
| 230 | var res = new ttypes.Xtruct(); |
| 231 | res.string_thing = arg1; |
| 232 | result(null, res); |
| 233 | } |
| 234 | return Q.resolve(); |
| 235 | } |
| 236 | testOneway(sleepFor: number, result: Function) { |
| 237 | this.syncHandler.testOneway(sleepFor); |
| 238 | } |
| 239 | testString(thing: string, callback: (err: any, result: string) => void): Q.IPromise<string> { |
| 240 | callback(null, thing); |
| 241 | return Q.resolve(); |
| 242 | } |
| 243 | testByte(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 244 | callback(null, thing); |
| 245 | return Q.resolve(); |
| 246 | } |
| 247 | testBool(thing: boolean, callback: (err: any, result: boolean) => void ): Q.IPromise<boolean> { |
| 248 | callback(null, thing); |
| 249 | return Q.resolve(); |
| 250 | } |
| 251 | testI32(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 252 | callback(null, thing); |
| 253 | return Q.resolve(); |
| 254 | } |
| 255 | testI64(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 256 | callback(null, thing); |
| 257 | return Q.resolve(); |
| 258 | } |
| 259 | testDouble(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 260 | callback(null, thing); |
| 261 | return Q.resolve(); |
| 262 | } |
| 263 | testBinary(thing: Buffer, callback: (err: any, result: Buffer) => void): Q.IPromise<Buffer> { |
| 264 | callback(null, thing); |
| 265 | return Q.resolve(); |
| 266 | } |
| 267 | testStruct(thing: ttypes.Xtruct, callback: (err: any, result: ttypes.Xtruct) => void): Q.IPromise<ttypes.Xtruct> { |
| 268 | callback(null, thing); |
| 269 | return Q.resolve(); |
| 270 | } |
| 271 | testNest(thing: ttypes.Xtruct2, callback: (err: any, result: ttypes.Xtruct2) => void): Q.IPromise<ttypes.Xtruct2> { |
| 272 | callback(null, thing); |
| 273 | return Q.resolve(); |
| 274 | } |
| 275 | testMap(thing: { [k: number]: number; }, callback: (err: any, result: { [k: number]: number; }) => void): Q.IPromise<{ [k: number]: number; }> { |
| 276 | callback(null, thing); |
| 277 | return Q.resolve(); |
| 278 | } |
| 279 | testStringMap(thing: { [k: string]: string; }, callback: (err: any, result: { [k: string]: string; }) => void): Q.IPromise<{ [k: string]: string; }> { |
| 280 | callback(null, thing); |
| 281 | return Q.resolve(); |
| 282 | } |
| 283 | testSet(thing: number[], callback: (err: any, result: number[]) => void): Q.IPromise<number[]> { |
| 284 | callback(null, thing); |
| 285 | return Q.resolve(); |
| 286 | } |
| 287 | testList(thing: number[], callback: (err: any, result: number[]) => void): Q.IPromise<number[]> { |
| 288 | callback(null, thing); |
| 289 | return Q.resolve(); |
| 290 | } |
| 291 | testEnum(thing: ttypes.Numberz, callback: (err: any, result: ttypes.Numberz) => void): Q.IPromise<ttypes.Numberz> { |
| 292 | callback(null, thing); |
| 293 | return Q.resolve(); |
| 294 | } |
| 295 | testTypedef(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 296 | callback(null, thing); |
| 297 | return Q.resolve(); |
| 298 | } |
| 299 | } |