ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [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 | |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame] | 20 | //This is the server side Node test handler for the standard |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 21 | // Apache Thrift test service. |
| 22 | |
| 23 | var ttypes = require('./gen-nodejs/ThriftTest_types'); |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 24 | var TException = require('thrift').Thrift.TException; |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 25 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 26 | function makeSyncHandler(label) { |
| 27 | return function(thing) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 28 | //console.log(label + '(\'' + thing + '\')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 29 | return thing; |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 30 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | var syncHandlers = { |
| 34 | testVoid: testVoid, |
| 35 | testMapMap: testMapMap, |
| 36 | testInsanity: testInsanity, |
| 37 | testMulti: testMulti, |
| 38 | testException: testException, |
| 39 | testMultiException: testMultiException, |
| 40 | testOneway: testOneway |
| 41 | }; |
| 42 | |
| 43 | function makeAsyncHandler(label) { |
| 44 | return function(thing, result) { |
| 45 | thing = syncHandlers[label](thing); |
| 46 | result(null, thing); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | var asyncHandlers = { |
| 51 | testVoid: testVoidAsync, |
| 52 | testMulti: testMultiAsync, |
| 53 | testException: testExceptionAsync, |
| 54 | testMultiException: testMultiExceptionAsync, |
| 55 | testOneway: testOnewayAsync |
| 56 | }; |
| 57 | |
| 58 | var identityHandlers = [ |
| 59 | 'testString', |
| 60 | 'testByte', |
| 61 | 'testI32', |
| 62 | 'testI64', |
| 63 | 'testDouble', |
| 64 | 'testStruct', |
| 65 | 'testNest', |
| 66 | 'testMap', |
| 67 | 'testStringMap', |
| 68 | 'testSet', |
| 69 | 'testList', |
| 70 | 'testEnum', |
| 71 | 'testTypedef' |
| 72 | ]; |
| 73 | |
| 74 | function testVoid() { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 75 | //console.log('testVoid()'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | function testVoidAsync(result) { |
| 79 | result(testVoid()); |
| 80 | } |
| 81 | |
| 82 | function testMapMap(hello) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 83 | //console.log('testMapMap(' + hello + ')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 84 | |
| 85 | var mapmap = []; |
| 86 | var pos = []; |
| 87 | var neg = []; |
| 88 | for (var i = 1; i < 5; i++) { |
| 89 | pos[i] = i; |
| 90 | neg[-i] = -i; |
| 91 | } |
| 92 | mapmap[4] = pos; |
| 93 | mapmap[-4] = neg; |
| 94 | |
| 95 | return mapmap; |
| 96 | } |
| 97 | |
| 98 | function testInsanity(argument) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 99 | //console.log('testInsanity('); |
| 100 | //console.log(argument); |
| 101 | //console.log(')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 102 | |
| 103 | var hello = new ttypes.Xtruct(); |
| 104 | hello.string_thing = 'Hello2'; |
| 105 | hello.byte_thing = 2; |
| 106 | hello.i32_thing = 2; |
| 107 | hello.i64_thing = 2; |
| 108 | |
| 109 | var goodbye = new ttypes.Xtruct(); |
| 110 | goodbye.string_thing = 'Goodbye4'; |
| 111 | goodbye.byte_thing = 4; |
| 112 | goodbye.i32_thing = 4; |
| 113 | goodbye.i64_thing = 4; |
| 114 | |
| 115 | var crazy = new ttypes.Insanity(); |
| 116 | crazy.userMap = []; |
| 117 | crazy.userMap[ttypes.Numberz.EIGHT] = 8; |
| 118 | crazy.userMap[ttypes.Numberz.FIVE] = 5; |
| 119 | crazy.xtructs = [goodbye, hello]; |
| 120 | |
| 121 | var first_map = []; |
| 122 | var second_map = []; |
| 123 | |
| 124 | first_map[ttypes.Numberz.TWO] = crazy; |
| 125 | first_map[ttypes.Numberz.THREE] = crazy; |
| 126 | |
| 127 | var looney = new ttypes.Insanity(); |
| 128 | second_map[ttypes.Numberz.SIX] = looney; |
| 129 | |
| 130 | var insane = []; |
| 131 | insane[1] = first_map; |
| 132 | insane[2] = second_map; |
| 133 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 134 | //console.log('insane result:'); |
| 135 | //console.log(insane); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 136 | return insane; |
| 137 | } |
| 138 | |
| 139 | function testMulti(arg0, arg1, arg2, arg3, arg4, arg5) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 140 | //console.log('testMulti()'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 141 | |
| 142 | var hello = new ttypes.Xtruct(); |
| 143 | hello.string_thing = 'Hello2'; |
| 144 | hello.byte_thing = arg0; |
| 145 | hello.i32_thing = arg1; |
| 146 | hello.i64_thing = arg2; |
| 147 | return hello; |
| 148 | } |
| 149 | |
| 150 | function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) { |
| 151 | var hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5); |
| 152 | result(null, hello); |
| 153 | } |
| 154 | |
| 155 | function testException(arg) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 156 | //console.log('testException('+arg+')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 157 | if (arg === 'Xception') { |
| 158 | var x = new ttypes.Xception(); |
| 159 | x.errorCode = 1001; |
| 160 | x.message = arg; |
| 161 | throw x; |
| 162 | } else if (arg === 'TException') { |
| 163 | throw new TException(arg); |
| 164 | } else { |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | function testExceptionAsync(arg, result) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 170 | //console.log('testException('+arg+')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 171 | if (arg === 'Xception') { |
| 172 | var x = new ttypes.Xception(); |
| 173 | x.errorCode = 1001; |
| 174 | x.message = arg; |
| 175 | result(x); |
| 176 | } else if (arg === 'TException') { |
| 177 | result(new TException(arg)); |
| 178 | } else { |
| 179 | result(null); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | function testMultiException(arg0, arg1) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 184 | //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 185 | if (arg0 === ('Xception')) { |
| 186 | var x = new ttypes.Xception(); |
| 187 | x.errorCode = 1001; |
| 188 | x.message = 'This is an Xception'; |
| 189 | throw x; |
| 190 | } else if (arg0 === ('Xception2')) { |
| 191 | var x2 = new ttypes.Xception2(); |
| 192 | x2.errorCode = 2002; |
| 193 | x2.struct_thing = new ttypes.Xtruct(); |
| 194 | x2.struct_thing.string_thing = 'This is an Xception2'; |
| 195 | throw x2; |
| 196 | } |
| 197 | |
| 198 | var res = new ttypes.Xtruct(); |
| 199 | res.string_thing = arg1; |
| 200 | return res; |
| 201 | } |
| 202 | |
| 203 | function testMultiExceptionAsync(arg0, arg1, result) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 204 | //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 205 | if (arg0 === ('Xception')) { |
| 206 | var x = new ttypes.Xception(); |
| 207 | x.errorCode = 1001; |
| 208 | x.message = 'This is an Xception'; |
| 209 | result(x); |
| 210 | } else if (arg0 === ('Xception2')) { |
| 211 | var x2 = new ttypes.Xception2(); |
| 212 | x2.errorCode = 2002; |
| 213 | x2.struct_thing = new ttypes.Xtruct(); |
| 214 | x2.struct_thing.string_thing = 'This is an Xception2'; |
| 215 | result(x2); |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame^] | 216 | } else { |
| 217 | var res = new ttypes.Xtruct(); |
| 218 | res.string_thing = arg1; |
| 219 | result(null, res); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 220 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | function testOneway(sleepFor) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 224 | //console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | function testOnewayAsync(sleepFor, result) { |
| 228 | testOneway(sleepFor); |
| 229 | } |
| 230 | |
| 231 | identityHandlers.forEach(function(label) { |
| 232 | syncHandlers[label] = makeSyncHandler(label); |
| 233 | asyncHandlers[label] = makeAsyncHandler(label); |
| 234 | }); |
| 235 | |
| 236 | ['testMapMap', 'testInsanity'].forEach(function(label) { |
| 237 | asyncHandlers[label] = makeAsyncHandler(label); |
| 238 | }); |
| 239 | |
| 240 | exports.ThriftTestHandler = asyncHandlers; |
| 241 | |
| 242 | exports.AsyncThriftTestHandler = asyncHandlers; |
| 243 | exports.SyncThriftTestHandler = asyncHandlers; |