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'); |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 21 | var Calculator = require('./gen-nodejs/Calculator'); |
| 22 | var ttypes = require('./gen-nodejs/tutorial_types'); |
James E. King, III | f74943c | 2017-04-06 21:04:56 -0400 | [diff] [blame] | 23 | const assert = require('assert'); |
henrique | 3123623 | 2014-02-23 20:16:44 +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; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 27 | |
| 28 | var connection = thrift.createConnection("localhost", 9090, { |
| 29 | transport : transport, |
| 30 | protocol : protocol |
| 31 | }); |
| 32 | |
| 33 | connection.on('error', function(err) { |
| 34 | assert(false, err); |
| 35 | }); |
| 36 | |
| 37 | // Create a Calculator client with the connection |
| 38 | var client = thrift.createClient(Calculator, connection); |
| 39 | |
| 40 | |
| 41 | client.ping() |
| 42 | .then(function() { |
| 43 | console.log('ping()'); |
| 44 | }); |
| 45 | |
| 46 | client.add(1,1) |
| 47 | .then(function(response) { |
| 48 | console.log("1+1=" + response); |
| 49 | }); |
| 50 | |
| 51 | work = new ttypes.Work(); |
| 52 | work.op = ttypes.Operation.DIVIDE; |
| 53 | work.num1 = 1; |
| 54 | work.num2 = 0; |
| 55 | |
| 56 | client.calculate(1, work) |
| 57 | .then(function(message) { |
| 58 | console.log('Whoa? You know how to divide by zero?'); |
| 59 | }) |
Chandler May | c3d66d2 | 2022-12-18 14:10:53 -0500 | [diff] [blame^] | 60 | .catch(function(err) { |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 61 | console.log("InvalidOperation " + err); |
| 62 | }); |
| 63 | |
| 64 | |
| 65 | work.op = ttypes.Operation.SUBTRACT; |
| 66 | work.num1 = 15; |
| 67 | work.num2 = 10; |
| 68 | |
| 69 | client.calculate(1, work) |
| 70 | .then(function(value) { |
| 71 | console.log('15-10=' + value); |
| 72 | return client.getStruct(1); |
| 73 | }) |
| 74 | .then(function(message) { |
| 75 | console.log('Check log: ' + message.value); |
| 76 | }) |
Chandler May | c3d66d2 | 2022-12-18 14:10:53 -0500 | [diff] [blame^] | 77 | .finally(function() { |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 78 | //close the connection once we're done |
| 79 | connection.end(); |
| 80 | }); |