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 | class calc_handler = |
| 31 | object (self) |
| 32 | inherit Calculator.iface |
| 33 | val log = Hashtbl.create 23 |
| 34 | method ping = Printf.printf "ping()\n" ; flush stdout |
| 35 | method add a b = |
| 36 | Printf.printf"add(%ld,%ld)\n" (sod a) (sod b); flush stdout ; |
| 37 | Int32.add (sod a) (sod b) |
| 38 | method calculate logid w = |
| 39 | let w = sod w in |
| 40 | Printf.printf "calculate(%ld,{%ld,%ld,%ld})\n" (sod logid) (Operation.to_i w#grab_op) w#grab_num1 w#grab_num2; flush stdout ; |
| 41 | let rv = |
| 42 | match w#grab_op with |
| 43 | Operation.ADD -> |
| 44 | Int32.add w#grab_num1 w#grab_num2 |
| 45 | | Operation.SUBTRACT -> |
| 46 | Int32.sub w#grab_num1 w#grab_num2 |
| 47 | | Operation.MULTIPLY -> |
| 48 | Int32.mul w#grab_num1 w#grab_num2 |
| 49 | | Operation.DIVIDE -> |
| 50 | if w#grab_num2 = Int32.zero then |
| 51 | let io = new invalidOperation in |
Konrad Grochowski | 3b115df | 2015-05-18 17:58:36 +0200 | [diff] [blame] | 52 | io#set_whatOp (Operation.to_i w#grab_op) ; |
Roger Meier | 9a3a564 | 2013-04-27 23:09:40 +0200 | [diff] [blame] | 53 | io#set_why "Cannot divide by 0" ; |
| 54 | raise (InvalidOperation io) |
| 55 | else |
| 56 | Int32.div w#grab_num1 w#grab_num2 in |
| 57 | |
| 58 | let ss = new sharedStruct in |
| 59 | ss#set_key (sod logid) ; |
| 60 | let buffer = Int32.to_string rv in |
| 61 | ss#set_value buffer ; |
| 62 | Hashtbl.add log (sod logid) ss ; |
| 63 | rv |
| 64 | |
| 65 | method zip = |
| 66 | Printf.printf "zip()\n"; flush stdout |
| 67 | |
| 68 | method getStruct logid = |
| 69 | Printf.printf "getStruct(%ld)\n" (sod logid) ; flush stdout ; |
| 70 | Hashtbl.find log (sod logid) |
| 71 | |
| 72 | end |
| 73 | |
| 74 | let doserver () = |
| 75 | let h = new calc_handler in |
| 76 | let proc = new Calculator.processor h in |
| 77 | let port = 9090 in |
| 78 | let pf = new TBinaryProtocol.factory in |
| 79 | let server = new TThreadedServer.t |
| 80 | proc |
| 81 | (new TServerSocket.t port) |
| 82 | (new Transport.factory) |
| 83 | pf |
| 84 | pf |
| 85 | in |
| 86 | server#serve |
| 87 | ;; |
| 88 | |
| 89 | doserver();; |