Roger Meier | 9a3a564 | 2013-04-27 23:09:40 +0200 | [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 | open Arg |
| 21 | open Thrift |
| 22 | open Tutorial_types |
| 23 | open Shared_types |
| 24 | |
| 25 | exception Die;; |
| 26 | let sod = function |
| 27 | Some v -> v |
| 28 | | None -> raise Die;; |
| 29 | |
| 30 | type connection = { |
| 31 | trans : Transport.t ; |
| 32 | proto : Thrift.Protocol.t; |
| 33 | calc : Calculator.client ; |
| 34 | } |
| 35 | |
| 36 | let connect ~host port = |
| 37 | let tx = new TSocket.t host port in |
| 38 | let proto = new TBinaryProtocol.t tx in |
| 39 | let calc = new Calculator.client proto proto in |
| 40 | tx#opn; |
| 41 | { trans = tx ; proto = proto; calc = calc } |
| 42 | ;; |
| 43 | |
| 44 | let doclient () = |
| 45 | let cli = connect ~host:"127.0.0.1" 9090 in |
| 46 | try |
| 47 | cli.calc#ping ; |
| 48 | Printf.printf "ping()\n" ; flush stdout ; |
| 49 | (let sum = cli.calc#add (Int32.of_int 1) (Int32.of_int 1) in |
| 50 | Printf.printf "1+1=%ld\n" sum ; |
| 51 | flush stdout) ; |
| 52 | (let w = new work in |
| 53 | w#set_op Operation.DIVIDE ; |
| 54 | w#set_num1 (Int32.of_int 1) ; |
| 55 | w#set_num2 (Int32.of_int 0) ; |
| 56 | try |
| 57 | let quotient = cli.calc#calculate (Int32.of_int 1) w in |
| 58 | Printf.printf "Whoa? We can divide by zero!\n" ; flush stdout |
| 59 | with InvalidOperation io -> |
| 60 | Printf.printf "InvalidOperation: %s\n" io#grab_why ; flush stdout) ; |
| 61 | (let w = new work in |
| 62 | w#set_op Operation.SUBTRACT ; |
| 63 | w#set_num1 (Int32.of_int 15) ; |
| 64 | w#set_num2 (Int32.of_int 10) ; |
| 65 | let diff = cli.calc#calculate (Int32.of_int 1) w in |
| 66 | Printf.printf "15-10=%ld\n" diff ; flush stdout) ; |
| 67 | (let ss = cli.calc#getStruct (Int32.of_int 1) in |
| 68 | Printf.printf "Check log: %s\n" ss#grab_value ; flush stdout) ; |
| 69 | cli.trans#close |
| 70 | with Transport.E (_,what) -> |
| 71 | Printf.printf "ERROR: %s\n" what ; flush stdout |
| 72 | ;; |
| 73 | |
| 74 | doclient();; |