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.
22const helpers = require("./helpers");
23const ttypes = require(`./${helpers.genPath}/ThriftTest_types`);
24const TException = require("thrift").Thrift.TException;
25
26function makeSyncHandler() {
27  return function(thing) {
28    return thing;
29  };
30}
31
32const syncHandlers = {
33  testVoid: testVoid,
34  testMapMap: testMapMap,
35  testInsanity: testInsanity,
36  testMulti: testMulti,
37  testException: testException,
38  testMultiException: testMultiException,
39  testOneway: testOneway
40};
41
42function makeAsyncHandler(label) {
43  return function(thing, result) {
44    thing = syncHandlers[label](thing);
45    result(null, thing);
46  };
47}
48
49const asyncHandlers = {
50  testVoid: testVoidAsync,
51  testMulti: testMultiAsync,
52  testException: testExceptionAsync,
53  testMultiException: testMultiExceptionAsync,
54  testOneway: testOnewayAsync
55};
56
57const identityHandlers = [
58  "testString",
59  "testBool",
60  "testByte",
61  "testI32",
62  "testI64",
63  "testDouble",
64  "testBinary",
65  "testStruct",
66  "testNest",
67  "testMap",
68  "testStringMap",
69  "testSet",
70  "testList",
71  "testEnum",
72  "testTypedef"
73];
74
75function testVoid() {
76  //console.log('testVoid()');
77}
78
79function testVoidAsync(result) {
80  result(testVoid());
81}
82
83function testMapMap() {
84  const mapmap = [];
85  const pos = [];
86  const neg = [];
87  for (let i = 1; i < 5; i++) {
88    pos[i] = i;
89    neg[-i] = -i;
90  }
91  mapmap[4] = pos;
92  mapmap[-4] = neg;
93
94  return mapmap;
95}
96
97function testInsanity(argument) {
98  //console.log('testInsanity(');
99  //console.log(argument);
100  //console.log(')');
101
102  const first_map = [];
103  const second_map = [];
104
105  first_map[ttypes.Numberz.TWO] = argument;
106  first_map[ttypes.Numberz.THREE] = argument;
107
108  const looney = new ttypes.Insanity();
109  second_map[ttypes.Numberz.SIX] = looney;
110
111  const insane = [];
112  insane[1] = first_map;
113  insane[2] = second_map;
114
115  //console.log('insane result:');
116  //console.log(insane);
117  return insane;
118}
119
120function testMulti(arg0, arg1, arg2) {
121  //console.log('testMulti()');
122
123  const hello = new ttypes.Xtruct();
124  hello.string_thing = "Hello2";
125  hello.byte_thing = arg0;
126  hello.i32_thing = arg1;
127  hello.i64_thing = arg2;
128  return hello;
129}
130
131function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) {
132  const hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5);
133  result(null, hello);
134}
135
136function testException(arg) {
137  //console.log('testException('+arg+')');
138  if (arg === "Xception") {
139    const x = new ttypes.Xception();
140    x.errorCode = 1001;
141    x.message = arg;
142    throw x;
143  } else if (arg === "TException") {
144    throw new TException(arg);
145  } else {
146    return;
147  }
148}
149
150function testExceptionAsync(arg, result) {
151  //console.log('testException('+arg+')');
152  if (arg === "Xception") {
153    const x = new ttypes.Xception();
154    x.errorCode = 1001;
155    x.message = arg;
156    result(x);
157  } else if (arg === "TException") {
158    result(new TException(arg));
159  } else {
160    result(null);
161  }
162}
163
164function testMultiException(arg0, arg1) {
165  //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
166  if (arg0 === "Xception") {
167    const x = new ttypes.Xception();
168    x.errorCode = 1001;
169    x.message = "This is an Xception";
170    throw x;
171  } else if (arg0 === "Xception2") {
172    const x2 = new ttypes.Xception2();
173    x2.errorCode = 2002;
174    x2.struct_thing = new ttypes.Xtruct();
175    x2.struct_thing.string_thing = "This is an Xception2";
176    throw x2;
177  }
178
179  const res = new ttypes.Xtruct();
180  res.string_thing = arg1;
181  return res;
182}
183
184function testMultiExceptionAsync(arg0, arg1, result) {
185  //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
186  if (arg0 === "Xception") {
187    const x = new ttypes.Xception();
188    x.errorCode = 1001;
189    x.message = "This is an Xception";
190    result(x);
191  } else if (arg0 === "Xception2") {
192    const 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    result(x2);
197  } else {
198    const res = new ttypes.Xtruct();
199    res.string_thing = arg1;
200    result(null, res);
201  }
202}
203
204//console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
205function testOneway() {}
206
207function testOnewayAsync(sleepFor) {
208  testOneway(sleepFor);
209}
210
211identityHandlers.forEach(function(label) {
212  syncHandlers[label] = makeSyncHandler(label);
213  asyncHandlers[label] = makeAsyncHandler(label);
214});
215
216["testMapMap", "testInsanity"].forEach(function(label) {
217  asyncHandlers[label] = makeAsyncHandler(label);
218});
219
220exports.ThriftTestHandler = asyncHandlers;
221