David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [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 | |
David Reiss | 06a9312 | 2009-03-30 21:22:52 +0000 | [diff] [blame] | 20 | using System; |
| 21 | using Thrift; |
| 22 | using Thrift.Protocol; |
| 23 | using Thrift.Server; |
| 24 | using Thrift.Transport; |
| 25 | |
| 26 | |
| 27 | namespace CSharpTutorial |
| 28 | { |
| 29 | public class CSharpClient |
| 30 | { |
| 31 | public static void Main() |
| 32 | { |
| 33 | try |
| 34 | { |
| 35 | TTransport transport = new TSocket("localhost", 9090); |
| 36 | TProtocol protocol = new TBinaryProtocol(transport); |
| 37 | Calculator.Client client = new Calculator.Client(protocol); |
| 38 | |
| 39 | transport.Open(); |
David Reiss | 06a9312 | 2009-03-30 21:22:52 +0000 | [diff] [blame] | 40 | try |
| 41 | { |
Jens Geyer | 102bca4 | 2013-06-25 22:21:29 +0200 | [diff] [blame] | 42 | client.ping(); |
| 43 | Console.WriteLine("ping()"); |
David Reiss | 06a9312 | 2009-03-30 21:22:52 +0000 | [diff] [blame] | 44 | |
Jens Geyer | 102bca4 | 2013-06-25 22:21:29 +0200 | [diff] [blame] | 45 | int sum = client.add(1, 1); |
| 46 | Console.WriteLine("1+1={0}", sum); |
David Reiss | 06a9312 | 2009-03-30 21:22:52 +0000 | [diff] [blame] | 47 | |
Jens Geyer | 102bca4 | 2013-06-25 22:21:29 +0200 | [diff] [blame] | 48 | Work work = new Work(); |
David Reiss | 06a9312 | 2009-03-30 21:22:52 +0000 | [diff] [blame] | 49 | |
Jens Geyer | 102bca4 | 2013-06-25 22:21:29 +0200 | [diff] [blame] | 50 | work.Op = Operation.DIVIDE; |
| 51 | work.Num1 = 1; |
| 52 | work.Num2 = 0; |
| 53 | try |
| 54 | { |
| 55 | int quotient = client.calculate(1, work); |
| 56 | Console.WriteLine("Whoa we can divide by 0"); |
| 57 | } |
| 58 | catch (InvalidOperation io) |
| 59 | { |
| 60 | Console.WriteLine("Invalid operation: " + io.Why); |
| 61 | } |
| 62 | |
| 63 | work.Op = Operation.SUBTRACT; |
| 64 | work.Num1 = 15; |
| 65 | work.Num2 = 10; |
| 66 | try |
| 67 | { |
| 68 | int diff = client.calculate(1, work); |
| 69 | Console.WriteLine("15-10={0}", diff); |
| 70 | } |
| 71 | catch (InvalidOperation io) |
| 72 | { |
| 73 | Console.WriteLine("Invalid operation: " + io.Why); |
| 74 | } |
| 75 | |
| 76 | SharedStruct log = client.getStruct(1); |
| 77 | Console.WriteLine("Check log: {0}", log.Value); |
| 78 | |
| 79 | } |
| 80 | finally |
| 81 | { |
| 82 | transport.Close(); |
| 83 | } |
David Reiss | 06a9312 | 2009-03-30 21:22:52 +0000 | [diff] [blame] | 84 | } |
| 85 | catch (TApplicationException x) |
| 86 | { |
| 87 | Console.WriteLine(x.StackTrace); |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | } |
| 92 | } |