henrique | 2fdd916 | 2013-08-28 14:03:34 +0200 | [diff] [blame] | 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 | var thrift = require('thrift'); |
| 20 | var Thrift = thrift.Thrift; |
| 21 | var ttransport = require('transport'); |
| 22 | |
| 23 | var ThriftTest = require('./gen-nodejs/ThriftTest'), |
| 24 | SecondService = require('./gen-nodejs/SecondService'), |
| 25 | ttypes = require('./gen-nodejs/ThriftTest_types'); |
| 26 | |
| 27 | var ThriftTestHandler = { |
| 28 | testVoid: function(result) { |
| 29 | console.log('testVoid()'); |
| 30 | result(null); |
| 31 | }, |
| 32 | |
| 33 | testString: function(thing, result) { |
| 34 | console.log('testString(\'' + thing + '\')'); |
| 35 | result(null, thing); |
| 36 | }, |
| 37 | |
| 38 | testByte: function(thing, result) { |
| 39 | console.log('testByte(' + thing + ')'); |
| 40 | result(null, thing); |
| 41 | }, |
| 42 | |
| 43 | testI32: function(thing, result) { |
| 44 | console.log('testI32(' + thing + ')'); |
| 45 | result(null, thing); |
| 46 | }, |
| 47 | |
| 48 | testI64: function(thing, result) { |
| 49 | console.log('testI64(' + thing + ')'); |
| 50 | result(null, thing); |
| 51 | }, |
| 52 | |
| 53 | testDouble: function(thing, result) { |
| 54 | console.log('testDouble(' + thing + ')'); |
| 55 | result(null, thing); |
| 56 | }, |
| 57 | |
| 58 | testStruct: function(thing, result) { |
| 59 | console.log('testStruct('); |
| 60 | console.log(thing); |
| 61 | console.log(')'); |
| 62 | result(null, thing); |
| 63 | }, |
| 64 | |
| 65 | testNest: function(nest, result) { |
| 66 | console.log('testNest('); |
| 67 | console.log(nest); |
| 68 | console.log(')'); |
| 69 | result(null, nest); |
| 70 | }, |
| 71 | |
| 72 | testMap: function(thing, result) { |
| 73 | console.log('testMap('); |
| 74 | console.log(thing); |
| 75 | console.log(')'); |
| 76 | result(null, thing); |
| 77 | }, |
| 78 | |
| 79 | testStringMap: function(thing, result) { |
| 80 | console.log('testStringMap('); |
| 81 | console.log(thing); |
| 82 | console.log(')'); |
| 83 | result(null, thing); |
| 84 | }, |
| 85 | |
| 86 | testSet: function(thing, result) { |
| 87 | console.log('testSet('); |
| 88 | console.log(thing); |
| 89 | console.log(')'); |
| 90 | result(null, thing); |
| 91 | }, |
| 92 | |
| 93 | testList: function(thing, result) { |
| 94 | console.log('testList('); |
| 95 | console.log(thing); |
| 96 | console.log(')'); |
| 97 | result(null, thing); |
| 98 | }, |
| 99 | |
| 100 | testEnum: function(thing, result) { |
| 101 | console.log('testEnum(' + thing + ')'); |
| 102 | result(null, thing); |
| 103 | }, |
| 104 | |
| 105 | testTypedef: function(thing, result) { |
| 106 | console.log('testTypedef(' + thing + ')'); |
| 107 | result(null, thing); |
| 108 | }, |
| 109 | |
| 110 | testMapMap: function(hello, result) { |
| 111 | console.log('testMapMap(' + hello + ')'); |
| 112 | |
| 113 | var mapmap = []; |
| 114 | var pos = []; |
| 115 | var neg = []; |
| 116 | for (var i = 1; i < 5; i++) { |
| 117 | pos[i] = i; |
| 118 | neg[-i] = -i; |
| 119 | } |
| 120 | mapmap[4] = pos; |
| 121 | mapmap[-4] = neg; |
| 122 | |
| 123 | result(null, mapmap); |
| 124 | }, |
| 125 | |
| 126 | testInsanity: function(argument, result) { |
| 127 | console.log('testInsanity('); |
| 128 | console.log(argument); |
| 129 | console.log(')'); |
| 130 | |
| 131 | var hello = new ttypes.Xtruct(); |
| 132 | hello.string_thing = 'Hello2'; |
| 133 | hello.byte_thing = 2; |
| 134 | hello.i32_thing = 2; |
| 135 | hello.i64_thing = 2; |
| 136 | |
| 137 | var goodbye = new ttypes.Xtruct(); |
| 138 | goodbye.string_thing = 'Goodbye4'; |
| 139 | goodbye.byte_thing = 4; |
| 140 | goodbye.i32_thing = 4; |
| 141 | goodbye.i64_thing = 4; |
| 142 | |
| 143 | var crazy = new ttypes.Insanity(); |
| 144 | crazy.userMap = []; |
| 145 | crazy.userMap[ttypes.Numberz.EIGHT] = 8; |
| 146 | crazy.userMap[ttypes.Numberz.FIVE] = 5; |
| 147 | crazy.xtructs = [goodbye, hello]; |
| 148 | |
| 149 | var first_map = []; |
| 150 | var second_map = []; |
| 151 | |
| 152 | first_map[ttypes.Numberz.TWO] = crazy; |
| 153 | first_map[ttypes.Numberz.THREE] = crazy; |
| 154 | |
| 155 | var looney = new ttypes.Insanity(); |
| 156 | second_map[ttypes.Numberz.SIX] = looney; |
| 157 | |
| 158 | var insane = []; |
| 159 | insane[1] = first_map; |
| 160 | insane[2] = second_map; |
| 161 | |
| 162 | console.log('insane result:'); |
| 163 | console.log(insane); |
| 164 | result(null, insane); |
| 165 | }, |
| 166 | |
| 167 | testMulti: function(arg0, arg1, arg2, arg3, arg4, arg5, result) { |
| 168 | console.log('testMulti()'); |
| 169 | |
| 170 | var hello = new ttypes.Xtruct();; |
| 171 | hello.string_thing = 'Hello2'; |
| 172 | hello.byte_thing = arg0; |
| 173 | hello.i32_thing = arg1; |
| 174 | hello.i64_thing = arg2; |
| 175 | result(null, hello); |
| 176 | }, |
| 177 | |
| 178 | testException: function(arg, result) { |
| 179 | console.log('testException(' + arg + ')'); |
| 180 | if (arg === 'Xception') { |
| 181 | var x = new ttypes.Xception(); |
| 182 | x.errorCode = 1001; |
| 183 | x.message = arg; |
| 184 | result(x); |
| 185 | } else if (arg === 'TException') { |
| 186 | result(new Thrift.TException(arg)); |
| 187 | } else { |
| 188 | result(null); |
| 189 | } |
| 190 | }, |
| 191 | |
| 192 | testMultiException: function(arg0, arg1, result) { |
| 193 | console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
| 194 | if (arg0 === ('Xception')) { |
| 195 | var x = new ttypes.Xception(); |
| 196 | x.errorCode = 1001; |
| 197 | x.message = 'This is an Xception'; |
| 198 | result(x); |
| 199 | } else if (arg0 === ('Xception2')) { |
| 200 | var x = new ttypes.Xception2(); |
| 201 | x.errorCode = 2002; |
| 202 | x.struct_thing = new ttypes.Xtruct(); |
| 203 | x.struct_thing.string_thing = 'This is an Xception2'; |
| 204 | result(x); |
| 205 | } |
| 206 | |
| 207 | var res = new ttypes.Xtruct(); |
| 208 | res.string_thing = arg1; |
| 209 | result(null, res); |
| 210 | }, |
| 211 | |
| 212 | testOneway: function(sleepFor, result) { |
| 213 | console.log('testOneway(' + sleepFor + ') => sleeping...'); |
| 214 | setTimeout(function() { |
| 215 | console.log('Done sleeping for testOneway!'); |
| 216 | }, sleepFor * 1000); //seconds |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | var SecondServiceHandler = { |
| 221 | secondtestString: function(thing, result) { |
| 222 | console.log('testString(\'' + thing + '\')'); |
| 223 | result(null, thing); |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | var processor = new thrift.MultiplexedProcessor(); |
| 228 | |
| 229 | processor.registerProcessor( |
| 230 | "ThriftTest", |
| 231 | new ThriftTest.Processor(ThriftTestHandler)); |
| 232 | |
| 233 | processor.registerProcessor( |
| 234 | "SecondService", |
| 235 | new SecondService.Processor(SecondServiceHandler)); |
| 236 | |
| 237 | |
| 238 | var server = thrift.createMultiplexServer(processor, { //server options |
| 239 | 'transport': ttransport.TFramedTransport |
| 240 | }); |
| 241 | |
| 242 | server.listen(9090); |