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