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