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
10 -- http://www.apache.org/licenses/LICENSE-2.0
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
20 {-# LANGUAGE OverloadedStrings #-}
22 import qualified Calculator
23 import Calculator_Iface
25 import SharedService_Iface
29 import Thrift.Protocol.Binary
30 import Thrift.Transport
37 import Control.Exception (throw)
38 import Control.Concurrent.MVar
39 import qualified Data.Map as M
43 data CalculatorHandler = CalculatorHandler {mathLog :: MVar (M.Map Int32 SharedStruct)}
45 newCalculatorHandler = do
47 return $ CalculatorHandler log
49 instance SharedService_Iface CalculatorHandler where
51 myLog <- readMVar (mathLog self)
55 instance Calculator_Iface CalculatorHandler where
60 printf "add(%d,%d)\n" n1 n2
63 calculate self mlogid mwork = do
64 printf "calculate(%d, %s)\n" logid (show work)
66 let val = case op work of
74 if num2 work == 0 then
77 invalidOperation_whatOp = fromIntegral $ fromEnum $ op work,
78 invalidOperation_why = "Cannot divide by 0"
81 num1 work `div` num2 work
83 let logEntry = SharedStruct logid (fromString $ show $ val)
84 modifyMVar_ (mathLog self) $ return .(M.insert logid logEntry)
89 -- stupid dynamic languages f'ing it up
100 handler <- newCalculatorHandler
101 print "Starting the server..."
102 runBasicServer handler Calculator.process 9090