blob: 41df441cd397d312d72aad1c0dccaf465b2d6420 [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',
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090060 'testBool',
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080061 'testByte',
62 'testI32',
63 'testI64',
64 'testDouble',
65 'testStruct',
66 'testNest',
67 'testMap',
68 'testStringMap',
69 'testSet',
70 'testList',
71 'testEnum',
72 'testTypedef'
73];
74
75function testVoid() {
Randy Abernethyd8187c52015-02-16 01:25:53 -080076 //console.log('testVoid()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080077}
78
79function testVoidAsync(result) {
80 result(testVoid());
81}
82
83function testMapMap(hello) {
Randy Abernethyd8187c52015-02-16 01:25:53 -080084 //console.log('testMapMap(' + hello + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080085
86 var mapmap = [];
87 var pos = [];
88 var neg = [];
89 for (var i = 1; i < 5; i++) {
90 pos[i] = i;
91 neg[-i] = -i;
92 }
93 mapmap[4] = pos;
94 mapmap[-4] = neg;
95
96 return mapmap;
97}
98
99function testInsanity(argument) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800100 //console.log('testInsanity(');
101 //console.log(argument);
102 //console.log(')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800103
104 var hello = new ttypes.Xtruct();
105 hello.string_thing = 'Hello2';
106 hello.byte_thing = 2;
107 hello.i32_thing = 2;
108 hello.i64_thing = 2;
109
110 var goodbye = new ttypes.Xtruct();
111 goodbye.string_thing = 'Goodbye4';
112 goodbye.byte_thing = 4;
113 goodbye.i32_thing = 4;
114 goodbye.i64_thing = 4;
115
116 var crazy = new ttypes.Insanity();
117 crazy.userMap = [];
118 crazy.userMap[ttypes.Numberz.EIGHT] = 8;
119 crazy.userMap[ttypes.Numberz.FIVE] = 5;
120 crazy.xtructs = [goodbye, hello];
121
122 var first_map = [];
123 var second_map = [];
124
125 first_map[ttypes.Numberz.TWO] = crazy;
126 first_map[ttypes.Numberz.THREE] = crazy;
127
128 var looney = new ttypes.Insanity();
129 second_map[ttypes.Numberz.SIX] = looney;
130
131 var insane = [];
132 insane[1] = first_map;
133 insane[2] = second_map;
134
Randy Abernethyd8187c52015-02-16 01:25:53 -0800135 //console.log('insane result:');
136 //console.log(insane);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800137 return insane;
138}
139
140function testMulti(arg0, arg1, arg2, arg3, arg4, arg5) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800141 //console.log('testMulti()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800142
143 var hello = new ttypes.Xtruct();
144 hello.string_thing = 'Hello2';
145 hello.byte_thing = arg0;
146 hello.i32_thing = arg1;
147 hello.i64_thing = arg2;
148 return hello;
149}
150
151function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) {
152 var hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5);
153 result(null, hello);
154}
155
156function testException(arg) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800157 //console.log('testException('+arg+')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800158 if (arg === 'Xception') {
159 var x = new ttypes.Xception();
160 x.errorCode = 1001;
161 x.message = arg;
162 throw x;
163 } else if (arg === 'TException') {
164 throw new TException(arg);
165 } else {
166 return;
167 }
168}
169
170function testExceptionAsync(arg, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800171 //console.log('testException('+arg+')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800172 if (arg === 'Xception') {
173 var x = new ttypes.Xception();
174 x.errorCode = 1001;
175 x.message = arg;
176 result(x);
177 } else if (arg === 'TException') {
178 result(new TException(arg));
179 } else {
180 result(null);
181 }
182}
183
184function testMultiException(arg0, arg1) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800185 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800186 if (arg0 === ('Xception')) {
187 var x = new ttypes.Xception();
188 x.errorCode = 1001;
189 x.message = 'This is an Xception';
190 throw x;
191 } else if (arg0 === ('Xception2')) {
192 var x2 = new ttypes.Xception2();
193 x2.errorCode = 2002;
194 x2.struct_thing = new ttypes.Xtruct();
195 x2.struct_thing.string_thing = 'This is an Xception2';
196 throw x2;
197 }
198
199 var res = new ttypes.Xtruct();
200 res.string_thing = arg1;
201 return res;
202}
203
204function testMultiExceptionAsync(arg0, arg1, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800205 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800206 if (arg0 === ('Xception')) {
207 var x = new ttypes.Xception();
208 x.errorCode = 1001;
209 x.message = 'This is an Xception';
210 result(x);
211 } else if (arg0 === ('Xception2')) {
212 var x2 = new ttypes.Xception2();
213 x2.errorCode = 2002;
214 x2.struct_thing = new ttypes.Xtruct();
215 x2.struct_thing.string_thing = 'This is an Xception2';
216 result(x2);
Randy Abernethybd60b922015-02-26 16:59:14 -0800217 } else {
218 var res = new ttypes.Xtruct();
219 res.string_thing = arg1;
220 result(null, res);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800221 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800222}
223
224function testOneway(sleepFor) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800225 //console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800226}
227
228function testOnewayAsync(sleepFor, result) {
229 testOneway(sleepFor);
230}
231
232identityHandlers.forEach(function(label) {
233 syncHandlers[label] = makeSyncHandler(label);
234 asyncHandlers[label] = makeAsyncHandler(label);
235});
236
237['testMapMap', 'testInsanity'].forEach(function(label) {
238 asyncHandlers[label] = makeAsyncHandler(label);
239});
240
241exports.ThriftTestHandler = asyncHandlers;
242
243exports.AsyncThriftTestHandler = asyncHandlers;
244exports.SyncThriftTestHandler = asyncHandlers;