henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [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 | |
| 20 | var thrift = require("thrift"); |
| 21 | var Calculator = require("./gen-nodejs/Calculator"); |
| 22 | var ttypes = require("./gen-nodejs/tutorial_types"); |
| 23 | var SharedStruct = require("./gen-nodejs/shared_types").SharedStruct; |
| 24 | |
| 25 | var data = {}; |
| 26 | |
| 27 | var server = thrift.createServer(Calculator, { |
| 28 | ping: function() { |
| 29 | console.log("ping()"); |
| 30 | }, |
| 31 | |
| 32 | add: function(n1, n2) { |
| 33 | console.log("add(", n1, ",", n2, ")"); |
| 34 | return n1 + n2; |
| 35 | }, |
| 36 | |
| 37 | calculate: function(logid, work) { |
| 38 | console.log("calculate(", logid, ",", work, ")"); |
| 39 | |
| 40 | var val = 0; |
| 41 | if (work.op == ttypes.Operation.ADD) { |
| 42 | val = work.num1 + work.num2; |
| 43 | } else if (work.op === ttypes.Operation.SUBTRACT) { |
| 44 | val = work.num1 - work.num2; |
| 45 | } else if (work.op === ttypes.Operation.MULTIPLY) { |
| 46 | val = work.num1 * work.num2; |
| 47 | } else if (work.op === ttypes.Operation.DIVIDE) { |
| 48 | if (work.num2 === 0) { |
| 49 | var x = new ttypes.InvalidOperation(); |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 50 | x.whatOp = work.op; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 51 | x.why = 'Cannot divide by 0'; |
| 52 | throw x; |
| 53 | } |
| 54 | val = work.num1 / work.num2; |
| 55 | } else { |
| 56 | var x = new ttypes.InvalidOperation(); |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 57 | x.whatOp = work.op; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 58 | x.why = 'Invalid operation'; |
| 59 | throw x; |
| 60 | } |
| 61 | |
| 62 | var entry = new SharedStruct(); |
| 63 | entry.key = logid; |
| 64 | entry.value = ""+val; |
| 65 | data[logid] = entry; |
| 66 | return val; |
| 67 | }, |
| 68 | |
| 69 | getStruct: function(key) { |
| 70 | console.log("getStruct(", key, ")"); |
| 71 | return data[key]; |
| 72 | }, |
| 73 | |
| 74 | zip: function() { |
| 75 | console.log("zip()"); |
| 76 | } |
| 77 | |
| 78 | }); |
| 79 | |
| 80 | server.listen(9090); |