Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [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 | {-# LANGUAGE OverloadedStrings,RecordWildCards #-} |
| 21 | module Main where |
| 22 | |
| 23 | import Control.Exception |
| 24 | import Control.Monad |
| 25 | import Data.Functor |
| 26 | import Data.HashMap.Strict (HashMap) |
| 27 | import Data.List |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 28 | import Data.List.Split |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 29 | import Data.String |
| 30 | import Network |
| 31 | import System.Environment |
| 32 | import System.Exit |
| 33 | import System.IO |
| 34 | import System.Posix.Unistd |
| 35 | import qualified Data.HashMap.Strict as Map |
| 36 | import qualified Data.HashSet as Set |
| 37 | import qualified Data.Text.Lazy as Text |
| 38 | import qualified Data.Vector as Vector |
| 39 | |
| 40 | import ThriftTest |
| 41 | import ThriftTest_Iface |
| 42 | import ThriftTest_Types |
| 43 | |
| 44 | import Thrift |
| 45 | import Thrift.Server |
| 46 | import Thrift.Transport.Framed |
| 47 | import Thrift.Transport.Handle |
| 48 | import Thrift.Protocol.Binary |
| 49 | import Thrift.Protocol.Compact |
| 50 | import Thrift.Protocol.JSON |
| 51 | |
| 52 | data Options = Options |
| 53 | { port :: Int |
| 54 | , domainSocket :: String |
| 55 | , serverType :: ServerType |
| 56 | , transport :: String |
| 57 | , protocol :: ProtocolType |
| 58 | , ssl :: Bool |
| 59 | , workers :: Int |
| 60 | } |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 61 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 62 | data ServerType = Simple |
| 63 | | ThreadPool |
| 64 | | Threaded |
| 65 | | NonBlocking |
| 66 | deriving (Show, Eq) |
| 67 | |
| 68 | instance IsString ServerType where |
| 69 | fromString "simple" = Simple |
| 70 | fromString "thread-pool" = ThreadPool |
| 71 | fromString "threaded" = Threaded |
| 72 | fromString "nonblocking" = NonBlocking |
| 73 | fromString _ = error "not a valid server type" |
| 74 | |
| 75 | data ProtocolType = Binary |
| 76 | | Compact |
| 77 | | JSON |
| 78 | |
| 79 | getProtocol :: String -> ProtocolType |
| 80 | getProtocol "binary" = Binary |
| 81 | getProtocol "compact" = Compact |
| 82 | getProtocol "json" = JSON |
| 83 | getProtocol p = error $"Unsupported Protocol: " ++ p |
| 84 | |
| 85 | defaultOptions :: Options |
| 86 | defaultOptions = Options |
| 87 | { port = 9090 |
| 88 | , domainSocket = "" |
| 89 | , serverType = Threaded |
| 90 | , transport = "framed" |
| 91 | , protocol = Binary |
| 92 | , ssl = False |
| 93 | , workers = 4 |
| 94 | } |
| 95 | |
| 96 | stringifyMap :: (Show a, Show b) => Map.HashMap a b -> String |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 97 | stringifyMap = Data.List.intercalate ", " . Data.List.map joinKV . Map.toList |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 98 | where joinKV (k, v) = show k ++ " => " ++ show v |
| 99 | |
| 100 | stringifySet :: Show a => Set.HashSet a -> String |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 101 | stringifySet = Data.List.intercalate ", " . Data.List.map show . Set.toList |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 102 | |
| 103 | stringifyList :: Show a => Vector.Vector a -> String |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 104 | stringifyList = Data.List.intercalate ", " . Data.List.map show . Vector.toList |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 105 | |
| 106 | data TestHandler = TestHandler |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 107 | instance ThriftTest_Iface TestHandler where |
| 108 | testVoid _ = System.IO.putStrLn "testVoid()" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 109 | |
| 110 | testString _ s = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 111 | System.IO.putStrLn $ "testString(" ++ show s ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 112 | return s |
| 113 | |
| 114 | testByte _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 115 | System.IO.putStrLn $ "testByte(" ++ show x ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 116 | return x |
| 117 | |
| 118 | testI32 _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 119 | System.IO.putStrLn $ "testI32(" ++ show x ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 120 | return x |
| 121 | |
| 122 | testI64 _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 123 | System.IO.putStrLn $ "testI64(" ++ show x ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 124 | return x |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 125 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 126 | testDouble _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 127 | System.IO.putStrLn $ "testDouble(" ++ show x ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 128 | return x |
| 129 | |
| 130 | testStruct _ struct@Xtruct{..} = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 131 | System.IO.putStrLn $ "testStruct({" ++ show xtruct_string_thing |
| 132 | ++ ", " ++ show xtruct_byte_thing |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 133 | ++ ", " ++ show xtruct_i32_thing |
| 134 | ++ ", " ++ show xtruct_i64_thing |
| 135 | ++ "})" |
| 136 | return struct |
| 137 | |
| 138 | testNest _ nest@Xtruct2{..} = do |
| 139 | let Xtruct{..} = xtruct2_struct_thing |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 140 | System.IO.putStrLn $ "testNest({" ++ show xtruct2_byte_thing |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 141 | ++ "{, " ++ show xtruct_string_thing |
| 142 | ++ ", " ++ show xtruct_byte_thing |
| 143 | ++ ", " ++ show xtruct_i32_thing |
| 144 | ++ ", " ++ show xtruct_i64_thing |
| 145 | ++ "}, " ++ show xtruct2_i32_thing |
| 146 | return nest |
| 147 | |
| 148 | testMap _ m = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 149 | System.IO.putStrLn $ "testMap({" ++ stringifyMap m ++ "})" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 150 | return m |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 151 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 152 | testStringMap _ m = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 153 | System.IO.putStrLn $ "testStringMap(" ++ stringifyMap m ++ "})" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 154 | return m |
| 155 | |
| 156 | testSet _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 157 | System.IO.putStrLn $ "testSet({" ++ stringifySet x ++ "})" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 158 | return x |
| 159 | |
| 160 | testList _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 161 | System.IO.putStrLn $ "testList(" ++ stringifyList x ++ "})" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 162 | return x |
| 163 | |
| 164 | testEnum _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 165 | System.IO.putStrLn $ "testEnum(" ++ show x ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 166 | return x |
| 167 | |
| 168 | testTypedef _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 169 | System.IO.putStrLn $ "testTypedef(" ++ show x ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 170 | return x |
| 171 | |
| 172 | testMapMap _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 173 | System.IO.putStrLn $ "testMapMap(" ++ show x ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 174 | return $ Map.fromList [ (-4, Map.fromList [ (-4, -4) |
| 175 | , (-3, -3) |
| 176 | , (-2, -2) |
| 177 | , (-1, -1) |
| 178 | ]) |
| 179 | , (4, Map.fromList [ (1, 1) |
| 180 | , (2, 2) |
| 181 | , (3, 3) |
| 182 | , (4, 4) |
| 183 | ]) |
| 184 | ] |
| 185 | |
| 186 | testInsanity _ x = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 187 | System.IO.putStrLn "testInsanity()" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 188 | return $ Map.fromList [ (1, Map.fromList [ (TWO , x) |
| 189 | , (THREE, x) |
| 190 | ]) |
| 191 | , (2, Map.fromList [ (SIX, default_Insanity) |
| 192 | ]) |
| 193 | ] |
| 194 | |
| 195 | testMulti _ byte i32 i64 _ _ _ = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 196 | System.IO.putStrLn "testMulti()" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 197 | return Xtruct{ xtruct_string_thing = Text.pack "Hello2" |
| 198 | , xtruct_byte_thing = byte |
| 199 | , xtruct_i32_thing = i32 |
| 200 | , xtruct_i64_thing = i64 |
| 201 | } |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 202 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 203 | testException _ s = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 204 | System.IO.putStrLn $ "testException(" ++ show s ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 205 | case s of |
| 206 | "Xception" -> throw $ Xception 1001 s |
| 207 | "TException" -> throw ThriftException |
| 208 | _ -> return () |
| 209 | |
| 210 | testMultiException _ s1 s2 = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 211 | System.IO.putStrLn $ "testMultiException(" ++ show s1 ++ ", " ++ show s2 ++ ")" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 212 | case s1 of |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 213 | "Xception" -> throw $ Xception 1001 "This is an Xception" |
| 214 | "Xception2" -> throw $ Xception2 2002 default_Xtruct |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 215 | "TException" -> throw ThriftException |
| 216 | _ -> return default_Xtruct{ xtruct_string_thing = s2 } |
| 217 | |
| 218 | testOneway _ i = do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 219 | System.IO.putStrLn $ "testOneway(" ++ show i ++ "): Sleeping..." |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 220 | sleep (fromIntegral i) |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 221 | System.IO.putStrLn $ "testOneway(" ++ show i ++ "): done sleeping!" |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 222 | |
| 223 | main :: IO () |
| 224 | main = do |
| 225 | options <- flip parseFlags defaultOptions <$> getArgs |
| 226 | case options of |
| 227 | Nothing -> showHelp |
| 228 | Just Options{..} -> do |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 229 | System.IO.putStrLn $ "Starting \"" ++ show serverType ++ "\" server (" ++ |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 230 | show transport ++ ") listen on: " ++ domainSocket ++ show port |
| 231 | case protocol of |
| 232 | Binary -> runServer BinaryProtocol port |
| 233 | Compact -> runServer CompactProtocol port |
| 234 | JSON -> runServer JSONProtocol port |
| 235 | where |
| 236 | runServer p = runThreadedServer (accepter p) TestHandler ThriftTest.process . PortNumber . fromIntegral |
| 237 | accepter p s = do |
| 238 | (h, _, _) <- accept s |
| 239 | return (p h, p h) |
| 240 | |
| 241 | parseFlags :: [String] -> Options -> Maybe Options |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 242 | parseFlags (flag : flags) opts = do |
| 243 | let pieces = splitOn "=" flag |
| 244 | case pieces of |
| 245 | "--port" : arg : _ -> parseFlags flags opts{ port = read arg } |
| 246 | "--domain-socket" : arg : _ -> parseFlags flags opts{ domainSocket = read arg } |
| 247 | "--server-type" : arg : _ -> parseFlags flags opts{ serverType = fromString arg } |
| 248 | "--transport" : arg : _ -> parseFlags flags opts{ transport = arg } |
| 249 | "--protocol" : arg : _ -> parseFlags flags opts{ protocol = getProtocol arg } |
| 250 | "--workers" : arg : _ -> parseFlags flags opts{ workers = read arg } |
| 251 | "--h" : _ -> Nothing |
| 252 | "--help" : _ -> Nothing |
| 253 | "--ssl" : _ -> parseFlags flags opts{ ssl = True } |
| 254 | "--processor-events" : _ -> parseFlags flags opts |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 255 | parseFlags (flag : arg : flags) opts |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 256 | | flag == "-n" = parseFlags flags opts{ workers = read arg } |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 257 | parseFlags [] opts = Just opts |
| 258 | |
| 259 | showHelp :: IO () |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 260 | showHelp = System.IO.putStrLn |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 261 | "Allowed options:\n\ |
| 262 | \ -h [ --help ] produce help message\n\ |
| 263 | \ --port arg (=9090) Port number to listen\n\ |
| 264 | \ --domain-socket arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift)\n\ |
| 265 | \ --server-type arg (=simple) type of server, \"simple\", \"thread-pool\",\n\ |
| 266 | \ \"threaded\", or \"nonblocking\"\n\ |
| 267 | \ --transport arg (=buffered) transport: buffered, framed, http\n\ |
| 268 | \ --protocol arg (=binary) protocol: binary, compact, json\n\ |
| 269 | \ --ssl Encrypted Transport using SSL\n\ |
| 270 | \ --processor-events processor-events\n\ |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 271 | \ -n [ --workers ] arg (=4) Number of thread pools workers. Only valid for\n\ |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 272 | \ thread-pool server type" |