blob: da329067703dca6c33a0ca724a550c821cdf47e9 [file] [log] [blame]
raa2e4e562014-03-29 01:14:48 -07001/*
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 Abernethy96f4f072015-02-10 02:29:15 -080020//This is the server side Node test handler for the standard
raa2e4e562014-03-29 01:14:48 -070021// Apache Thrift test service.
22
23var ttypes = require('./gen-nodejs/ThriftTest_types');
Randy Abernethyd60f9782014-03-28 10:36:38 -070024var TException = require('thrift').Thrift.TException;
raa2e4e562014-03-29 01:14:48 -070025
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080026function makeSyncHandler(label) {
27 return function(thing) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080028 //console.log(label + '(\'' + thing + '\')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080029 return thing;
raa2e4e562014-03-29 01:14:48 -070030 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080031}
32
33var syncHandlers = {
34 testVoid: testVoid,
35 testMapMap: testMapMap,
36 testInsanity: testInsanity,
37 testMulti: testMulti,
38 testException: testException,
39 testMultiException: testMultiException,
40 testOneway: testOneway
41};
42
43function makeAsyncHandler(label) {
44 return function(thing, result) {
45 thing = syncHandlers[label](thing);
46 result(null, thing);
47 }
48}
49
50var asyncHandlers = {
51 testVoid: testVoidAsync,
52 testMulti: testMultiAsync,
53 testException: testExceptionAsync,
54 testMultiException: testMultiExceptionAsync,
55 testOneway: testOnewayAsync
56};
57
58var 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
74function testVoid() {
Randy Abernethyd8187c52015-02-16 01:25:53 -080075 //console.log('testVoid()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080076}
77
78function testVoidAsync(result) {
79 result(testVoid());
80}
81
82function testMapMap(hello) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080083 //console.log('testMapMap(' + hello + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080084
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
98function testInsanity(argument) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080099 //console.log('testInsanity(');
100 //console.log(argument);
101 //console.log(')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800102
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 Abernethyd8187c52015-02-16 01:25:53 -0800134 //console.log('insane result:');
135 //console.log(insane);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800136 return insane;
137}
138
139function testMulti(arg0, arg1, arg2, arg3, arg4, arg5) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800140 //console.log('testMulti()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800141
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
150function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) {
151 var hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5);
152 result(null, hello);
153}
154
155function testException(arg) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800156 //console.log('testException('+arg+')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800157 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
169function testExceptionAsync(arg, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800170 //console.log('testException('+arg+')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800171 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
183function testMultiException(arg0, arg1) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800184 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800185 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
203function testMultiExceptionAsync(arg0, arg1, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800204 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800205 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 Abernethybd60b922015-02-26 16:59:14 -0800216 } else {
217 var res = new ttypes.Xtruct();
218 res.string_thing = arg1;
219 result(null, res);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800220 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800221}
222
223function testOneway(sleepFor) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800224 //console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800225}
226
227function testOnewayAsync(sleepFor, result) {
228 testOneway(sleepFor);
229}
230
231identityHandlers.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
240exports.ThriftTestHandler = asyncHandlers;
241
242exports.AsyncThriftTestHandler = asyncHandlers;
243exports.SyncThriftTestHandler = asyncHandlers;