blob: 5181e5128d243087daf77b6392cbe97f78d0e4ac [file] [log] [blame]
Roger Meier64998e22014-01-27 21:15:56 +01001/*
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
20var thrift = require("thrift");
21var Calculator = require("./gen-nodejs/Calculator");
22var ttypes = require("./gen-nodejs/tutorial_types");
23var SharedStruct = require("./gen-nodejs/shared_types").SharedStruct;
24
25var data = {};
26
27var server = thrift.createServer(Calculator, {
Cameron Martincaef0ed2025-01-15 11:58:39 +010028 ping: function (result) {
Roger Meier64998e22014-01-27 21:15:56 +010029 console.log("ping()");
30 result(null);
31 },
32
Cameron Martincaef0ed2025-01-15 11:58:39 +010033 add: function (n1, n2, result) {
Roger Meier64998e22014-01-27 21:15:56 +010034 console.log("add(", n1, ",", n2, ")");
35 result(null, n1 + n2);
36 },
37
Cameron Martincaef0ed2025-01-15 11:58:39 +010038 calculate: function (logid, work, result) {
Roger Meier64998e22014-01-27 21:15:56 +010039 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 Grochowski3b115df2015-05-18 17:58:36 +020051 x.whatOp = work.op;
Cameron Martincaef0ed2025-01-15 11:58:39 +010052 x.why = "Cannot divide by 0";
Roger Meier64998e22014-01-27 21:15:56 +010053 result(x);
54 return;
55 }
56 val = work.num1 / work.num2;
57 } else {
58 var x = new ttypes.InvalidOperation();
Konrad Grochowski3b115df2015-05-18 17:58:36 +020059 x.whatOp = work.op;
Cameron Martincaef0ed2025-01-15 11:58:39 +010060 x.why = "Invalid operation";
Roger Meier64998e22014-01-27 21:15:56 +010061 result(x);
62 return;
63 }
64
65 var entry = new SharedStruct();
66 entry.key = logid;
Cameron Martincaef0ed2025-01-15 11:58:39 +010067 entry.value = "" + val;
Roger Meier64998e22014-01-27 21:15:56 +010068 data[logid] = entry;
69
70 result(null, val);
71 },
72
Cameron Martincaef0ed2025-01-15 11:58:39 +010073 getStruct: function (key, result) {
Roger Meier64998e22014-01-27 21:15:56 +010074 console.log("getStruct(", key, ")");
75 result(null, data[key]);
76 },
77
Cameron Martincaef0ed2025-01-15 11:58:39 +010078 zip: function () {
Roger Meier64998e22014-01-27 21:15:56 +010079 console.log("zip()");
Cameron Martincaef0ed2025-01-15 11:58:39 +010080 },
Roger Meier64998e22014-01-27 21:15:56 +010081});
82
83server.listen(9090);