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"); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 27 | import Int64 = require("node-int64"); |
wilfrem | 2c69b5a | 2015-04-20 19:24:50 +0900 | [diff] [blame] | 28 | |
| 29 | |
| 30 | export class SyncThriftTestHandler { |
| 31 | testVoid(): Q.IPromise<void> { |
| 32 | //console.log('testVoid()'); |
| 33 | return Q.resolve<void>(undefined); |
| 34 | } |
| 35 | testMapMap(hello: number) { |
| 36 | //console.log('testMapMap(' + hello + ')'); |
| 37 | |
| 38 | var mapmap: {[key: number]: {[key: number]: number; }} = []; |
| 39 | var pos: {[key: number]: number; } = []; |
| 40 | var neg: {[key: number]: number; } = []; |
| 41 | for (var i = 1; i < 5; i++) { |
| 42 | pos[i] = i; |
| 43 | neg[-i] = -i; |
| 44 | } |
| 45 | mapmap[4] = pos; |
| 46 | mapmap[-4] = neg; |
| 47 | |
| 48 | return Q.resolve(mapmap); |
| 49 | } |
| 50 | testInsanity(argument: ttypes.Insanity): Q.IPromise<{ [k: number]: any; }> { |
| 51 | const first_map: { [k: number]: any; } = []; |
| 52 | const second_map: { [k: number]: any; } = []; |
| 53 | |
| 54 | first_map[ttypes.Numberz.TWO] = argument; |
| 55 | first_map[ttypes.Numberz.THREE] = argument; |
| 56 | |
| 57 | const looney = new ttypes.Insanity(); |
| 58 | second_map[ttypes.Numberz.SIX] = looney; |
| 59 | |
| 60 | const insane: { [k: number]: any; } = []; |
| 61 | insane[1] = first_map; |
| 62 | insane[2] = second_map; |
| 63 | |
| 64 | return Q.resolve(insane); |
| 65 | } |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 66 | testMulti(arg0: any, arg1: number, arg2: Int64, arg3: { [k: number]: string; }, arg4: ttypes.Numberz, arg5: number) { |
wilfrem | 2c69b5a | 2015-04-20 19:24:50 +0900 | [diff] [blame] | 67 | var hello = new ttypes.Xtruct(); |
| 68 | hello.string_thing = 'Hello2'; |
| 69 | hello.byte_thing = arg0; |
| 70 | hello.i32_thing = arg1; |
| 71 | hello.i64_thing = arg2; |
| 72 | return Q.resolve(hello); |
| 73 | } |
| 74 | testException(arg: string): Q.IPromise<void> { |
| 75 | if (arg === 'Xception') { |
| 76 | var x = new ttypes.Xception(); |
| 77 | x.errorCode = 1001; |
| 78 | x.message = arg; |
| 79 | throw x; |
| 80 | } else if (arg === 'TException') { |
| 81 | throw new Thrift.TException(arg); |
| 82 | } else { |
| 83 | return Q.resolve(); |
| 84 | } |
| 85 | } |
| 86 | testMultiException(arg0: string, arg1: string) { |
| 87 | if (arg0 === ('Xception')) { |
| 88 | var x = new ttypes.Xception(); |
| 89 | x.errorCode = 1001; |
| 90 | x.message = 'This is an Xception'; |
| 91 | throw x; |
| 92 | } else if (arg0 === ('Xception2')) { |
| 93 | var x2 = new ttypes.Xception2(); |
| 94 | x2.errorCode = 2002; |
| 95 | x2.struct_thing = new ttypes.Xtruct(); |
| 96 | x2.struct_thing.string_thing = 'This is an Xception2'; |
| 97 | throw x2; |
| 98 | } |
| 99 | |
| 100 | var res = new ttypes.Xtruct(); |
| 101 | res.string_thing = arg1; |
| 102 | return Q.resolve(res); |
| 103 | } |
| 104 | testOneway(sleepFor: number) { |
| 105 | } |
| 106 | |
| 107 | testString(thing: string) { |
| 108 | return Q.resolve(thing); |
| 109 | } |
| 110 | testBool(thing: boolean) { |
| 111 | return Q.resolve(thing); |
| 112 | } |
| 113 | testByte(thing: number) { |
| 114 | return Q.resolve(thing); |
| 115 | } |
| 116 | testI32(thing: number) { |
| 117 | return Q.resolve(thing); |
| 118 | } |
| 119 | testI64(thing: number) { |
| 120 | return Q.resolve(thing); |
| 121 | } |
| 122 | testDouble(thing: number) { |
| 123 | return Q.resolve(thing); |
| 124 | } |
| 125 | testBinary(thing: Buffer) { |
| 126 | return Q.resolve(thing); |
| 127 | } |
| 128 | testStruct(thing: ttypes.Xtruct) { |
| 129 | return Q.resolve(thing); |
| 130 | } |
| 131 | testNest(thing: ttypes.Xtruct2) { |
| 132 | return Q.resolve(thing); |
| 133 | } |
| 134 | testMap(thing: { [k: number]: number; }) { |
| 135 | return Q.resolve(thing); |
| 136 | } |
| 137 | testStringMap(thing: { [k: string]: string; }) { |
| 138 | return Q.resolve(thing); |
| 139 | } |
| 140 | testSet(thing: number[]) { |
| 141 | return Q.resolve(thing); |
| 142 | } |
| 143 | testList(thing: number[]) { |
| 144 | return Q.resolve(thing); |
| 145 | } |
| 146 | testEnum(thing: ttypes.Numberz) { |
| 147 | return Q.resolve(thing); |
| 148 | } |
| 149 | testTypedef(thing: number) { |
| 150 | return Q.resolve(thing); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | export class AsyncThriftTestHandler { |
| 155 | private syncHandler: SyncThriftTestHandler; |
| 156 | constructor() { |
| 157 | this.syncHandler = new SyncThriftTestHandler(); |
| 158 | } |
| 159 | |
| 160 | testVoid(callback: (result: void) => void): Q.IPromise<void> { |
| 161 | callback(undefined); |
| 162 | return Q.resolve(); |
| 163 | } |
| 164 | testMapMap(hello: number, |
| 165 | callback: (err: any, result: { [k: number]: { [k: number]: number; }; }) => void): |
| 166 | Q.IPromise<{ [k: number]: { [k: number]: number; }; }> { |
| 167 | |
| 168 | var mapmap: {[key: number]: {[key: number]: number; }} = []; |
| 169 | var pos: {[key: number]: number; } = []; |
| 170 | var neg: {[key: number]: number; } = []; |
| 171 | for (var i = 1; i < 5; i++) { |
| 172 | pos[i] = i; |
| 173 | neg[-i] = -i; |
| 174 | } |
| 175 | mapmap[4] = pos; |
| 176 | mapmap[-4] = neg; |
| 177 | |
| 178 | callback(null, mapmap); |
| 179 | return Q.resolve(); |
| 180 | } |
| 181 | testInsanity(argument: ttypes.Insanity, callback?: (err: any, result: { [k: number]: any; }) => void): Q.IPromise<{ [k: number]: any; }> { |
| 182 | const first_map: { [k: number]: any; } = []; |
| 183 | const second_map: { [k: number]: any; } = []; |
| 184 | |
| 185 | first_map[ttypes.Numberz.TWO] = argument; |
| 186 | first_map[ttypes.Numberz.THREE] = argument; |
| 187 | |
| 188 | const looney = new ttypes.Insanity(); |
| 189 | second_map[ttypes.Numberz.SIX] = looney; |
| 190 | |
| 191 | const insane: { [k: number]: any; } = []; |
| 192 | insane[1] = first_map; |
| 193 | insane[2] = second_map; |
| 194 | |
| 195 | if (callback !== undefined){ |
| 196 | callback(null, insane); |
| 197 | } |
| 198 | return Q.resolve(); |
| 199 | } |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 200 | testMulti(arg0: any, arg1: number, arg2: Int64, arg3: { [k: number]: string; }, arg4: ttypes.Numberz, arg5: number, result: Function): Q.IPromise<ttypes.Xtruct> { |
wilfrem | 2c69b5a | 2015-04-20 19:24:50 +0900 | [diff] [blame] | 201 | var hello = this.syncHandler.testMulti(arg0, arg1, arg2, arg3, arg4, arg5); |
| 202 | hello.then(hello => result(null, hello)); |
| 203 | return Q.resolve(); |
| 204 | } |
| 205 | testException(arg: string, result: (err: any) => void): Q.IPromise<void> { |
| 206 | if (arg === 'Xception') { |
| 207 | var x = new ttypes.Xception(); |
| 208 | x.errorCode = 1001; |
| 209 | x.message = arg; |
| 210 | result(x); |
| 211 | } else if (arg === 'TException') { |
| 212 | result(new Thrift.TException(arg)); |
| 213 | } else { |
| 214 | result(null); |
| 215 | } |
| 216 | return Q.resolve(); |
| 217 | } |
| 218 | testMultiException(arg0: string, arg1: string, result: (err: any, res?: ttypes.Xtruct) => void): Q.IPromise<ttypes.Xtruct> { |
| 219 | if (arg0 === ('Xception')) { |
| 220 | var x = new ttypes.Xception(); |
| 221 | x.errorCode = 1001; |
| 222 | x.message = 'This is an Xception'; |
| 223 | result(x); |
| 224 | } else if (arg0 === ('Xception2')) { |
| 225 | var x2 = new ttypes.Xception2(); |
| 226 | x2.errorCode = 2002; |
| 227 | x2.struct_thing = new ttypes.Xtruct(); |
| 228 | x2.struct_thing.string_thing = 'This is an Xception2'; |
| 229 | result(x2); |
| 230 | } else { |
| 231 | var res = new ttypes.Xtruct(); |
| 232 | res.string_thing = arg1; |
| 233 | result(null, res); |
| 234 | } |
| 235 | return Q.resolve(); |
| 236 | } |
| 237 | testOneway(sleepFor: number, result: Function) { |
| 238 | this.syncHandler.testOneway(sleepFor); |
| 239 | } |
| 240 | testString(thing: string, callback: (err: any, result: string) => void): Q.IPromise<string> { |
| 241 | callback(null, thing); |
| 242 | return Q.resolve(); |
| 243 | } |
| 244 | testByte(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 245 | callback(null, thing); |
| 246 | return Q.resolve(); |
| 247 | } |
| 248 | testBool(thing: boolean, callback: (err: any, result: boolean) => void ): Q.IPromise<boolean> { |
| 249 | callback(null, thing); |
| 250 | return Q.resolve(); |
| 251 | } |
| 252 | testI32(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 253 | callback(null, thing); |
| 254 | return Q.resolve(); |
| 255 | } |
| 256 | testI64(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 257 | callback(null, thing); |
| 258 | return Q.resolve(); |
| 259 | } |
| 260 | testDouble(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 261 | callback(null, thing); |
| 262 | return Q.resolve(); |
| 263 | } |
| 264 | testBinary(thing: Buffer, callback: (err: any, result: Buffer) => void): Q.IPromise<Buffer> { |
| 265 | callback(null, thing); |
| 266 | return Q.resolve(); |
| 267 | } |
| 268 | testStruct(thing: ttypes.Xtruct, callback: (err: any, result: ttypes.Xtruct) => void): Q.IPromise<ttypes.Xtruct> { |
| 269 | callback(null, thing); |
| 270 | return Q.resolve(); |
| 271 | } |
| 272 | testNest(thing: ttypes.Xtruct2, callback: (err: any, result: ttypes.Xtruct2) => void): Q.IPromise<ttypes.Xtruct2> { |
| 273 | callback(null, thing); |
| 274 | return Q.resolve(); |
| 275 | } |
| 276 | testMap(thing: { [k: number]: number; }, callback: (err: any, result: { [k: number]: number; }) => void): Q.IPromise<{ [k: number]: number; }> { |
| 277 | callback(null, thing); |
| 278 | return Q.resolve(); |
| 279 | } |
| 280 | testStringMap(thing: { [k: string]: string; }, callback: (err: any, result: { [k: string]: string; }) => void): Q.IPromise<{ [k: string]: string; }> { |
| 281 | callback(null, thing); |
| 282 | return Q.resolve(); |
| 283 | } |
| 284 | testSet(thing: number[], callback: (err: any, result: number[]) => void): Q.IPromise<number[]> { |
| 285 | callback(null, thing); |
| 286 | return Q.resolve(); |
| 287 | } |
| 288 | testList(thing: number[], callback: (err: any, result: number[]) => void): Q.IPromise<number[]> { |
| 289 | callback(null, thing); |
| 290 | return Q.resolve(); |
| 291 | } |
| 292 | testEnum(thing: ttypes.Numberz, callback: (err: any, result: ttypes.Numberz) => void): Q.IPromise<ttypes.Numberz> { |
| 293 | callback(null, thing); |
| 294 | return Q.resolve(); |
| 295 | } |
| 296 | testTypedef(thing: number, callback: (err: any, result: number) => void): Q.IPromise<number> { |
| 297 | callback(null, thing); |
| 298 | return Q.resolve(); |
| 299 | } |
| 300 | } |