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