{-# LANGUAGE ScopedTypeVariables #-}
module Thrift.Server
( runBasicServer
, runThreadedServer
) where
import Control.Concurrent ( forkIO )
import Control.Exception
import Control.Monad ( forever, when )
import Network
import System.IO
import Thrift
import Thrift.Transport.Handle()
import Thrift.Protocol.Binary
runThreadedServer :: (Protocol i, Protocol o)
=> (Socket -> IO (i, o))
-> h
-> (h -> (i, o) -> IO Bool)
-> PortID
-> IO a
runThreadedServer accepter hand proc_ port = do
socket <- listenOn port
acceptLoop (accepter socket) (proc_ hand)
runBasicServer :: h
-> (h -> (BinaryProtocol Handle, BinaryProtocol Handle) -> IO Bool)
-> PortNumber
-> IO a
runBasicServer hand proc_ port = runThreadedServer binaryAccept hand proc_ (PortNumber port)
where binaryAccept s = do
(h, _, _) <- accept s
return (BinaryProtocol h, BinaryProtocol h)
acceptLoop :: IO t -> (t -> IO Bool) -> IO a
acceptLoop accepter proc_ = forever $
do ps <- accepter
forkIO $ handle (\(_ :: SomeException) -> return ())
(loop $ proc_ ps)
where loop m = do { continue <- m; when continue (loop m) }