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 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 20 | var thrift = require("thrift"); |
| 21 | var Calculator = require("./gen-nodejs/Calculator"); |
| 22 | var ttypes = require("./gen-nodejs/tutorial_types"); |
| 23 | const assert = require("assert"); |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 24 | |
Mark Sonnabaum | c2256fc | 2016-08-25 09:08:47 -0500 | [diff] [blame] | 25 | var transport = thrift.TBufferedTransport; |
| 26 | var protocol = thrift.TBinaryProtocol; |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 27 | |
| 28 | var connection = thrift.createConnection("localhost", 9090, { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 29 | transport: transport, |
| 30 | protocol: protocol, |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 31 | }); |
| 32 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 33 | connection.on("error", function (err) { |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 34 | assert(false, err); |
| 35 | }); |
| 36 | |
| 37 | // Create a Calculator client with the connection |
| 38 | var client = thrift.createClient(Calculator, connection); |
| 39 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 40 | client.ping(function (err, response) { |
| 41 | console.log("ping()"); |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 42 | }); |
| 43 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 44 | client.add(1, 1, function (err, response) { |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 45 | console.log("1+1=" + response); |
| 46 | }); |
| 47 | |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 48 | work = new ttypes.Work(); |
| 49 | work.op = ttypes.Operation.DIVIDE; |
| 50 | work.num1 = 1; |
| 51 | work.num2 = 0; |
| 52 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 53 | client.calculate(1, work, function (err, message) { |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 54 | if (err) { |
| 55 | console.log("InvalidOperation " + err); |
| 56 | } else { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 57 | console.log("Whoa? You know how to divide by zero?"); |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 58 | } |
| 59 | }); |
| 60 | |
| 61 | work.op = ttypes.Operation.SUBTRACT; |
| 62 | work.num1 = 15; |
| 63 | work.num2 = 10; |
| 64 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 65 | client.calculate(1, work, function (err, message) { |
| 66 | console.log("15-10=" + message); |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 67 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame^] | 68 | client.getStruct(1, function (err, message) { |
| 69 | console.log("Check log: " + message.value); |
Roger Meier | 64998e2 | 2014-01-27 21:15:56 +0100 | [diff] [blame] | 70 | |
| 71 | //close the connection once we're done |
| 72 | connection.end(); |
| 73 | }); |
| 74 | }); |