blob: 056209db4026085582970c7af8aacabb10f1a59d [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) {
126 console.log('testInsanity()');
127
128 var hello = new ttypes.Xtruct();
129 hello.string_thing = 'Hello2';
130 hello.byte_thing = 2;
131 hello.i32_thing = 2;
132 hello.i64_thing = 2;
133
134 var goodbye = new ttypes.Xtruct();
135 goodbye.string_thing = 'Goodbye4';
136 goodbye.byte_thing = 4;
137 goodbye.i32_thing = 4;
138 goodbye.i64_thing = 4;
139
140 var crazy = new ttypes.Insanity();
141 crazy.userMap = [];
142 crazy.userMap[ttypes.Numberz.EIGHT] = 8;
143 crazy.userMap[ttypes.Numberz.FIVE] = 5;
144 crazy.xtructs = [];
145 crazy.xtructs.add(goodbye);
146 crazy.xtructs.add(hello);
147
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
161 result(null, insane);
162 },
163
164 testMulti: function(arg0, arg1, arg2, arg3, arg4, arg5, result) {
165 console.log('testMulti()');
166
167 var hello = new ttypes.Xtruct();;
168 hello.string_thing = 'Hello2';
169 hello.byte_thing = arg0;
170 hello.i32_thing = arg1;
171 hello.i64_thing = arg2;
172 result(null, hello);
173 },
174
175 testException: function(arg, result) {
176 console.log('testException('+arg+')');
177 if (arg === 'Xception') {
178 var x = new ttypes.Xception();
179 x.errorCode = 1001;
180 x.message = arg;
181 result(x);
182 } else if (arg === 'ApplicationException') {
183 result(new Thrift.TException(arg));
184 } else {
185 var res = new ttypes.Xtruct();
186 res.string_thing = arg;
187 result(null, res);
188 }
189 },
190
191 testMultiException: function(arg0, arg1, result) {
192 console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
193 if (arg0 === ('Xception')) {
194 var x = new Xception();
195 x.errorCode = 1001;
196 x.message = 'This is an Xception';
197 result(x);
198 } else if (arg0 === ('Xception2')) {
199 var x = new Xception2();
200 x.errorCode = 2002;
201 x.struct_thing = new Xtruct();
202 x.struct_thing.string_thing = 'This is an Xception2';
203 result(x);
204 }
205
206 var res = new Xtruct();
207 res.string_thing = arg1;
208 result(null, res);
209 },
210
211 testOneway: function(sleepFor, result) {
212 console.log('testOneway(' + sleepFor + ') => sleeping...');
213 setTimeout(function(){
214 console.log('Done sleeping!');
215 }, sleepFor);
216 result(null);
217 }
218});
219
220server.listen(9090);