Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +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 | |
| 20 | module Main where |
| 21 | |
| 22 | |
| 23 | import qualified Control.Exception |
| 24 | import qualified Data.ByteString.Lazy as DBL |
| 25 | import qualified Maybe |
| 26 | import qualified Network |
| 27 | |
| 28 | import Thrift.Protocol.Binary |
| 29 | import Thrift.Server |
| 30 | import Thrift.Transport.Handle |
| 31 | |
| 32 | import qualified ThriftTestUtils |
| 33 | |
| 34 | import qualified DebugProtoTest_Types as Types |
| 35 | import qualified Inherited |
| 36 | import qualified Inherited_Client as IClient |
| 37 | import qualified Inherited_Iface as IIface |
| 38 | import qualified Srv_Client as SClient |
| 39 | import qualified Srv_Iface as SIface |
| 40 | |
| 41 | -- we don't actually need this import, but force it to check the code generator exports proper Haskell syntax |
| 42 | import qualified Srv() |
| 43 | |
| 44 | |
| 45 | data InheritedHandler = InheritedHandler |
| 46 | instance SIface.Srv_Iface InheritedHandler where |
| 47 | janky _ arg = do |
| 48 | ThriftTestUtils.serverLog $ "Got janky method call: " ++ show arg |
| 49 | return $ 31 |
| 50 | |
| 51 | voidMethod _ = do |
| 52 | ThriftTestUtils.serverLog "Got voidMethod method call" |
| 53 | return () |
| 54 | |
| 55 | primitiveMethod _ = do |
| 56 | ThriftTestUtils.serverLog "Got primitiveMethod call" |
| 57 | return $ 42 |
| 58 | |
| 59 | structMethod _ = do |
| 60 | ThriftTestUtils.serverLog "Got structMethod call" |
| 61 | return $ Types.CompactProtoTestStruct { |
| 62 | Types.f_CompactProtoTestStruct_a_byte = Just 0x01, |
| 63 | Types.f_CompactProtoTestStruct_a_i16 = Just 0x02, |
| 64 | Types.f_CompactProtoTestStruct_a_i32 = Just 0x03, |
| 65 | Types.f_CompactProtoTestStruct_a_i64 = Just 0x04, |
| 66 | Types.f_CompactProtoTestStruct_a_double = Just 0.1, |
| 67 | Types.f_CompactProtoTestStruct_a_string = Just "abcdef", |
| 68 | Types.f_CompactProtoTestStruct_a_binary = Just DBL.empty, |
| 69 | Types.f_CompactProtoTestStruct_true_field = Just True, |
| 70 | Types.f_CompactProtoTestStruct_false_field = Just False, |
| 71 | Types.f_CompactProtoTestStruct_empty_struct_field = Just Types.Empty, |
| 72 | |
| 73 | Types.f_CompactProtoTestStruct_byte_list = Nothing, |
| 74 | Types.f_CompactProtoTestStruct_i16_list = Nothing, |
| 75 | Types.f_CompactProtoTestStruct_i32_list = Nothing, |
| 76 | Types.f_CompactProtoTestStruct_i64_list = Nothing, |
| 77 | Types.f_CompactProtoTestStruct_double_list = Nothing, |
| 78 | Types.f_CompactProtoTestStruct_string_list = Nothing, |
| 79 | Types.f_CompactProtoTestStruct_binary_list = Nothing, |
| 80 | Types.f_CompactProtoTestStruct_boolean_list = Nothing, |
| 81 | Types.f_CompactProtoTestStruct_struct_list = Just [Types.Empty], |
| 82 | |
| 83 | Types.f_CompactProtoTestStruct_byte_set = Nothing, |
| 84 | Types.f_CompactProtoTestStruct_i16_set = Nothing, |
| 85 | Types.f_CompactProtoTestStruct_i32_set = Nothing, |
| 86 | Types.f_CompactProtoTestStruct_i64_set = Nothing, |
| 87 | Types.f_CompactProtoTestStruct_double_set = Nothing, |
| 88 | Types.f_CompactProtoTestStruct_string_set = Nothing, |
| 89 | Types.f_CompactProtoTestStruct_binary_set = Nothing, |
| 90 | Types.f_CompactProtoTestStruct_boolean_set = Nothing, |
| 91 | Types.f_CompactProtoTestStruct_struct_set = Nothing, |
| 92 | |
| 93 | Types.f_CompactProtoTestStruct_byte_byte_map = Nothing, |
| 94 | Types.f_CompactProtoTestStruct_i16_byte_map = Nothing, |
| 95 | Types.f_CompactProtoTestStruct_i32_byte_map = Nothing, |
| 96 | Types.f_CompactProtoTestStruct_i64_byte_map = Nothing, |
| 97 | Types.f_CompactProtoTestStruct_double_byte_map = Nothing, |
| 98 | Types.f_CompactProtoTestStruct_string_byte_map = Nothing, |
| 99 | Types.f_CompactProtoTestStruct_binary_byte_map = Nothing, |
| 100 | Types.f_CompactProtoTestStruct_boolean_byte_map = Nothing, |
| 101 | |
| 102 | Types.f_CompactProtoTestStruct_byte_i16_map = Nothing, |
| 103 | Types.f_CompactProtoTestStruct_byte_i32_map = Nothing, |
| 104 | Types.f_CompactProtoTestStruct_byte_i64_map = Nothing, |
| 105 | Types.f_CompactProtoTestStruct_byte_double_map = Nothing, |
| 106 | Types.f_CompactProtoTestStruct_byte_string_map = Nothing, |
| 107 | Types.f_CompactProtoTestStruct_byte_binary_map = Nothing, |
| 108 | Types.f_CompactProtoTestStruct_byte_boolean_map = Nothing, |
| 109 | |
| 110 | Types.f_CompactProtoTestStruct_list_byte_map = Nothing, |
| 111 | Types.f_CompactProtoTestStruct_set_byte_map = Nothing, |
| 112 | Types.f_CompactProtoTestStruct_map_byte_map = Nothing, |
| 113 | |
| 114 | Types.f_CompactProtoTestStruct_byte_map_map = Nothing, |
| 115 | Types.f_CompactProtoTestStruct_byte_set_map = Nothing, |
| 116 | Types.f_CompactProtoTestStruct_byte_list_map = Nothing } |
| 117 | |
| 118 | methodWithDefaultArgs _ arg = do |
| 119 | ThriftTestUtils.serverLog $ "Got methodWithDefaultArgs: " ++ show arg |
| 120 | return () |
| 121 | |
| 122 | onewayMethod _ = do |
| 123 | ThriftTestUtils.serverLog "Got onewayMethod" |
| 124 | |
| 125 | instance IIface.Inherited_Iface InheritedHandler where |
| 126 | identity _ arg = do |
| 127 | ThriftTestUtils.serverLog $ "Got identity method: " ++ show arg |
| 128 | return $ Maybe.fromJust arg |
| 129 | |
| 130 | client :: (String, Network.PortID) -> IO () |
| 131 | client addr = do |
| 132 | to <- hOpen addr |
| 133 | let p = BinaryProtocol to |
| 134 | let ps = (p,p) |
| 135 | |
| 136 | v1 <- SClient.janky ps 42 |
| 137 | ThriftTestUtils.clientLog $ show v1 |
| 138 | |
| 139 | SClient.voidMethod ps |
| 140 | |
| 141 | v2 <- SClient.primitiveMethod ps |
| 142 | ThriftTestUtils.clientLog $ show v2 |
| 143 | |
| 144 | v3 <- SClient.structMethod ps |
| 145 | ThriftTestUtils.clientLog $ show v3 |
| 146 | |
| 147 | SClient.methodWithDefaultArgs ps 42 |
| 148 | |
| 149 | SClient.onewayMethod ps |
| 150 | |
| 151 | v4 <- IClient.identity ps 42 |
| 152 | ThriftTestUtils.clientLog $ show v4 |
| 153 | |
| 154 | return () |
| 155 | |
| 156 | server :: Network.PortNumber -> IO () |
| 157 | server port = do |
| 158 | ThriftTestUtils.serverLog "Ready..." |
| 159 | (runBasicServer InheritedHandler Inherited.process port) |
| 160 | `Control.Exception.catch` |
| 161 | (\(TransportExn s _) -> error $ "FAILURE: " ++ show s) |
| 162 | |
| 163 | main :: IO () |
| 164 | main = ThriftTestUtils.runTest server client |