blob: 06724e619975fcbb320a64e65435b0c764d43c76 [file] [log] [blame]
Roger Meiereaa61d82012-01-12 21:38:29 +00001/*
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 */
19var thrift = require('thrift');
20var Thrift = thrift.Thrift;
21
22var ThriftTest = require('./gen-nodejs/ThriftTest'),
23 ttypes = require('./gen-nodejs/ThriftTest_types');
24
25
26var server = thrift.createServer(ThriftTest, {
27 testVoid: function(result) {
28 console.log('testVoid()');
29 result(null);
30 },
31
32 testString: function(thing, result) {
33 console.log('testString(\'' + thing + '\')');
34 result(null, thing);
35 },
36
37 testByte: function(thing, result) {
38 console.log('testByte(' + thing + ')');
39 result(null, thing);
40 },
41
42 testI32: function(thing, result) {
43 console.log('testI32(' + thing + ')');
44 result(null, thing);
45 },
46
47 testI64: function(thing, result) {
48 console.log('testI64(' + thing + ')');
49 result(null, thing);
50 },
51
52 testDouble: function(thing, result) {
53 console.log('testDouble(' + thing + ')');
54 result(null, thing);
55 },
56
57 testStruct: function(thing, result) {
58 console.log('testStruct(');
59 console.log(thing);
60 console.log(')');
61 result(null, thing);
62 },
63
64 testNest: function(nest, result) {
65 console.log('testNest(');
66 console.log(nest);
67 console.log(')');
68 result(null, nest);
69 },
70
71 testMap: function(thing, result) {
72 console.log('testMap(');
73 console.log(thing);
74 console.log(')');
75 result(null, thing);
76 },
77
78 testStringMap: function(thing, result) {
79 console.log('testStringMap(');
80 console.log(thing);
81 console.log(')');
82 result(null, thing);
83 },
84
85 testSet: function(thing, result) {
86 console.log('testSet(');
87 console.log(thing);
88 console.log(')');
89 result(null, thing);
90 },
91
92 testList: function(thing, result) {
93 console.log('testList(');
94 console.log(thing);
95 console.log(')');
96 result(null, thing);
97 },
98
99 testEnum: function(thing, result) {
100 console.log('testEnum(' + thing + ')');
101 result(null, thing);
102 },
103
104 testTypedef: function(thing, result) {
105 console.log('testTypedef(' + thing + ')');
106 result(null, thing);
107 },
108
109 testMapMap: function(hello, result) {
110 console.log('testMapMap(' + hello + ')');
111
112 var mapmap = [];
113 var pos = [];
114 var neg = [];
115 for (var i = 1; i < 5; i++) {
116 pos[i] = i;
117 neg[-i] = -i;
118 }
119 mapmap[4] = pos;
120 mapmap[-4] = neg;
121
122 result(null, mapmap);
123 },
124
125 testInsanity: function(argument, result) {
Henrique08c34ea2013-05-11 19:29:19 +0200126 console.log('testInsanity(');
127 console.log(argument);
128 console.log(')');
Roger Meiereaa61d82012-01-12 21:38:29 +0000129
130 var hello = new ttypes.Xtruct();
131 hello.string_thing = 'Hello2';
132 hello.byte_thing = 2;
133 hello.i32_thing = 2;
134 hello.i64_thing = 2;
135
136 var goodbye = new ttypes.Xtruct();
137 goodbye.string_thing = 'Goodbye4';
138 goodbye.byte_thing = 4;
139 goodbye.i32_thing = 4;
140 goodbye.i64_thing = 4;
141
142 var crazy = new ttypes.Insanity();
143 crazy.userMap = [];
144 crazy.userMap[ttypes.Numberz.EIGHT] = 8;
145 crazy.userMap[ttypes.Numberz.FIVE] = 5;
Henrique08c34ea2013-05-11 19:29:19 +0200146 crazy.xtructs = [goodbye, hello];
Roger Meiereaa61d82012-01-12 21:38:29 +0000147
148 var first_map = [];
149 var second_map = [];
150
151 first_map[ttypes.Numberz.TWO] = crazy;
152 first_map[ttypes.Numberz.THREE] = crazy;
153
154 var looney = new ttypes.Insanity();
155 second_map[ttypes.Numberz.SIX] = looney;
156
157 var insane = [];
158 insane[1] = first_map;
159 insane[2] = second_map;
160
Henrique08c34ea2013-05-11 19:29:19 +0200161 console.log('insane result:');
162 console.log(insane);
Roger Meiereaa61d82012-01-12 21:38:29 +0000163 result(null, insane);
164 },
165
166 testMulti: function(arg0, arg1, arg2, arg3, arg4, arg5, result) {
167 console.log('testMulti()');
168
169 var hello = new ttypes.Xtruct();;
170 hello.string_thing = 'Hello2';
171 hello.byte_thing = arg0;
172 hello.i32_thing = arg1;
173 hello.i64_thing = arg2;
174 result(null, hello);
175 },
176
177 testException: function(arg, result) {
178 console.log('testException('+arg+')');
179 if (arg === 'Xception') {
180 var x = new ttypes.Xception();
181 x.errorCode = 1001;
182 x.message = arg;
183 result(x);
Henrique08c34ea2013-05-11 19:29:19 +0200184 } else if (arg === 'TException') {
Roger Meiereaa61d82012-01-12 21:38:29 +0000185 result(new Thrift.TException(arg));
186 } else {
Henrique08c34ea2013-05-11 19:29:19 +0200187 result(null);
Roger Meiereaa61d82012-01-12 21:38:29 +0000188 }
189 },
190
191 testMultiException: function(arg0, arg1, result) {
192 console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
193 if (arg0 === ('Xception')) {
Henrique08c34ea2013-05-11 19:29:19 +0200194 var x = new ttypes.Xception();
Roger Meiereaa61d82012-01-12 21:38:29 +0000195 x.errorCode = 1001;
196 x.message = 'This is an Xception';
197 result(x);
198 } else if (arg0 === ('Xception2')) {
Henrique08c34ea2013-05-11 19:29:19 +0200199 var x = new ttypes.Xception2();
Roger Meiereaa61d82012-01-12 21:38:29 +0000200 x.errorCode = 2002;
Henrique08c34ea2013-05-11 19:29:19 +0200201 x.struct_thing = new ttypes.Xtruct();
Roger Meiereaa61d82012-01-12 21:38:29 +0000202 x.struct_thing.string_thing = 'This is an Xception2';
203 result(x);
204 }
205
Henrique08c34ea2013-05-11 19:29:19 +0200206 var res = new ttypes.Xtruct();
Roger Meiereaa61d82012-01-12 21:38:29 +0000207 res.string_thing = arg1;
208 result(null, res);
209 },
210
211 testOneway: function(sleepFor, result) {
212 console.log('testOneway(' + sleepFor + ') => sleeping...');
213 setTimeout(function(){
Henrique08c34ea2013-05-11 19:29:19 +0200214 console.log('Done sleeping for testOneway!');
215 }, sleepFor*1000); //seconds
Roger Meiereaa61d82012-01-12 21:38:29 +0000216 }
217});
218
219server.listen(9090);