Preliminary streaming mode support (client streaming, server streaming, bidirectional) (#37)
* Tweak runOps param order, inline common op sequences, clean up serverHandleNormalCall
* More ops sequence inlining for clarity, experimenting with Managed
* Checkpoint: preliminary support for all streaming modes; much cleanup/refactoring and api design still needed
* Use mempty for default StatusDetails; tweak bad status matching mechanism
* Preliminary user-facing, server-streaming, low-level api and test
* renaming wibbles
* Preliminary user-facing, client-streaming, low-level api and test
* Move sendMsgs comb to Network.GRPC.LowLevel.Op; misc cleanup/DCR
* Modify bidi streaming to omit request payload
* Add transformers dep
* Preliminary user-facing low-level bidirectional streaming api and test
* Fix missing peek import
* Remove TimeoutSeconds params on streaming mode functions
* Fix serverHandleNormalCall rebase wart
* Fix rebase warts; minor hlint fixes and wibbles
* Post-rebase tweaks to optional payload use in serverRequestCall (i.e., now respects payloadHandling again)
* Cleanup/refactor serverRequestCall
* Fix comment
* Change ServerRWHandler type so that handler does not have to invoke a finalizer
* Change ServerReaderHandler type so that handler does not have to invoke a finalizer
* Simplify serverWriter interface and ServerWriterHandler structure
* Simplify serverRW (get rid of exec param), improve bidi streaming tests
* Use ExceptT in serverRW impl
* Change ServerRWHandler type to pass recv/send operations.
* Renaming
* Define ClientRWHandler, pass recv/send ops
* wibbles
* Use ExceptT in clientRW impl
* Add DataKinded phantom typing to RegisteredMethod; misc cleanup
* Simplify sendMsgs interface; add SingleSend type and related helpers
* Rename SingleSend to SendSingle, use ExceptT to clean up {client,server}Writer and sendMsgs
* More ExceptT cleanup in clientWriter
* Factor out reusable bits of clientWriter
* Shrink ServerReaderHandler
* Delete stale comments
* Use common machinery for all streaming modes; make handler types more consistent
* wibbles
2016-07-06 15:59:38 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2016-06-08 18:41:58 +02:00
|
|
|
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
|
2016-06-03 19:34:09 +02:00
|
|
|
|
2016-07-11 00:04:31 +02:00
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
import Control.Monad
|
2016-06-08 18:41:58 +02:00
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import Network.GRPC.LowLevel
|
2016-07-11 00:04:31 +02:00
|
|
|
import Network.GRPC.LowLevel.Call
|
2016-06-08 18:41:58 +02:00
|
|
|
import qualified Network.GRPC.LowLevel.Server.Unregistered as U
|
2016-06-13 22:51:53 +02:00
|
|
|
import qualified Network.GRPC.LowLevel.Call.Unregistered as U
|
2016-06-03 19:34:09 +02:00
|
|
|
|
|
|
|
serverMeta :: MetadataMap
|
2016-06-08 18:41:58 +02:00
|
|
|
serverMeta = [("test_meta", "test_meta_value")]
|
2016-06-03 19:34:09 +02:00
|
|
|
|
2016-06-22 19:41:14 +02:00
|
|
|
handler :: U.ServerCall
|
|
|
|
-> ByteString
|
2016-06-16 17:23:54 +02:00
|
|
|
-> IO (ByteString, MetadataMap, StatusCode, StatusDetails)
|
2016-06-22 19:41:14 +02:00
|
|
|
handler U.ServerCall{..} reqBody = do
|
2016-06-03 19:34:09 +02:00
|
|
|
--putStrLn $ "Got request for method: " ++ show method
|
|
|
|
--putStrLn $ "Got metadata: " ++ show reqMeta
|
2016-06-16 17:23:54 +02:00
|
|
|
return (reqBody, serverMeta, StatusOk, StatusDetails "")
|
2016-06-03 19:34:09 +02:00
|
|
|
|
|
|
|
unregMain :: IO ()
|
|
|
|
unregMain = withGRPC $ \grpc -> do
|
2016-06-22 22:07:38 +02:00
|
|
|
withServer grpc (ServerConfig "localhost" 50051 [] []) $ \server -> forever $ do
|
2016-06-22 19:41:14 +02:00
|
|
|
result <- U.serverHandleNormalCall server serverMeta handler
|
2016-06-03 19:34:09 +02:00
|
|
|
case result of
|
|
|
|
Left x -> putStrLn $ "handle call result error: " ++ show x
|
|
|
|
Right _ -> return ()
|
|
|
|
|
|
|
|
regMain :: IO ()
|
|
|
|
regMain = withGRPC $ \grpc -> do
|
|
|
|
let methods = [(MethodName "/echo.Echo/DoEcho", Normal)]
|
2016-06-22 22:07:38 +02:00
|
|
|
withServer grpc (ServerConfig "localhost" 50051 methods []) $ \server ->
|
2016-06-03 19:34:09 +02:00
|
|
|
forever $ do
|
Preliminary streaming mode support (client streaming, server streaming, bidirectional) (#37)
* Tweak runOps param order, inline common op sequences, clean up serverHandleNormalCall
* More ops sequence inlining for clarity, experimenting with Managed
* Checkpoint: preliminary support for all streaming modes; much cleanup/refactoring and api design still needed
* Use mempty for default StatusDetails; tweak bad status matching mechanism
* Preliminary user-facing, server-streaming, low-level api and test
* renaming wibbles
* Preliminary user-facing, client-streaming, low-level api and test
* Move sendMsgs comb to Network.GRPC.LowLevel.Op; misc cleanup/DCR
* Modify bidi streaming to omit request payload
* Add transformers dep
* Preliminary user-facing low-level bidirectional streaming api and test
* Fix missing peek import
* Remove TimeoutSeconds params on streaming mode functions
* Fix serverHandleNormalCall rebase wart
* Fix rebase warts; minor hlint fixes and wibbles
* Post-rebase tweaks to optional payload use in serverRequestCall (i.e., now respects payloadHandling again)
* Cleanup/refactor serverRequestCall
* Fix comment
* Change ServerRWHandler type so that handler does not have to invoke a finalizer
* Change ServerReaderHandler type so that handler does not have to invoke a finalizer
* Simplify serverWriter interface and ServerWriterHandler structure
* Simplify serverRW (get rid of exec param), improve bidi streaming tests
* Use ExceptT in serverRW impl
* Change ServerRWHandler type to pass recv/send operations.
* Renaming
* Define ClientRWHandler, pass recv/send ops
* wibbles
* Use ExceptT in clientRW impl
* Add DataKinded phantom typing to RegisteredMethod; misc cleanup
* Simplify sendMsgs interface; add SingleSend type and related helpers
* Rename SingleSend to SendSingle, use ExceptT to clean up {client,server}Writer and sendMsgs
* More ExceptT cleanup in clientWriter
* Factor out reusable bits of clientWriter
* Shrink ServerReaderHandler
* Delete stale comments
* Use common machinery for all streaming modes; make handler types more consistent
* wibbles
2016-07-06 15:59:38 +02:00
|
|
|
let method = head (normalMethods server)
|
2016-06-22 19:41:14 +02:00
|
|
|
result <- serverHandleNormalCall server method serverMeta $
|
2016-06-16 17:23:54 +02:00
|
|
|
\_call reqBody _reqMeta -> return (reqBody, serverMeta, StatusOk,
|
2016-06-13 22:51:53 +02:00
|
|
|
StatusDetails "")
|
2016-06-03 19:34:09 +02:00
|
|
|
case result of
|
|
|
|
Left x -> putStrLn $ "registered call result error: " ++ show x
|
|
|
|
Right _ -> return ()
|
|
|
|
|
2016-07-11 00:04:31 +02:00
|
|
|
tputStrLn x = do
|
|
|
|
tid <- myThreadId
|
|
|
|
putStrLn $ "[" ++ show tid ++ "]: " ++ x
|
|
|
|
|
Preliminary streaming mode support (client streaming, server streaming, bidirectional) (#37)
* Tweak runOps param order, inline common op sequences, clean up serverHandleNormalCall
* More ops sequence inlining for clarity, experimenting with Managed
* Checkpoint: preliminary support for all streaming modes; much cleanup/refactoring and api design still needed
* Use mempty for default StatusDetails; tweak bad status matching mechanism
* Preliminary user-facing, server-streaming, low-level api and test
* renaming wibbles
* Preliminary user-facing, client-streaming, low-level api and test
* Move sendMsgs comb to Network.GRPC.LowLevel.Op; misc cleanup/DCR
* Modify bidi streaming to omit request payload
* Add transformers dep
* Preliminary user-facing low-level bidirectional streaming api and test
* Fix missing peek import
* Remove TimeoutSeconds params on streaming mode functions
* Fix serverHandleNormalCall rebase wart
* Fix rebase warts; minor hlint fixes and wibbles
* Post-rebase tweaks to optional payload use in serverRequestCall (i.e., now respects payloadHandling again)
* Cleanup/refactor serverRequestCall
* Fix comment
* Change ServerRWHandler type so that handler does not have to invoke a finalizer
* Change ServerReaderHandler type so that handler does not have to invoke a finalizer
* Simplify serverWriter interface and ServerWriterHandler structure
* Simplify serverRW (get rid of exec param), improve bidi streaming tests
* Use ExceptT in serverRW impl
* Change ServerRWHandler type to pass recv/send operations.
* Renaming
* Define ClientRWHandler, pass recv/send ops
* wibbles
* Use ExceptT in clientRW impl
* Add DataKinded phantom typing to RegisteredMethod; misc cleanup
* Simplify sendMsgs interface; add SingleSend type and related helpers
* Rename SingleSend to SendSingle, use ExceptT to clean up {client,server}Writer and sendMsgs
* More ExceptT cleanup in clientWriter
* Factor out reusable bits of clientWriter
* Shrink ServerReaderHandler
* Delete stale comments
* Use common machinery for all streaming modes; make handler types more consistent
* wibbles
2016-07-06 15:59:38 +02:00
|
|
|
regLoop :: Server -> RegisteredMethod 'Normal -> IO ()
|
2016-06-03 19:34:09 +02:00
|
|
|
regLoop server method = forever $ do
|
2016-07-11 00:04:31 +02:00
|
|
|
-- tputStrLn "about to block on call handler"
|
2016-06-22 19:41:14 +02:00
|
|
|
result <- serverHandleNormalCall server method serverMeta $
|
2016-07-11 00:04:31 +02:00
|
|
|
\_call reqBody _reqMeta ->
|
|
|
|
return (reqBody, serverMeta, StatusOk, StatusDetails "")
|
2016-06-03 19:34:09 +02:00
|
|
|
case result of
|
2016-07-11 00:04:31 +02:00
|
|
|
Left x -> error $! "registered call result error: " ++ show x
|
2016-06-03 19:34:09 +02:00
|
|
|
Right _ -> return ()
|
|
|
|
|
|
|
|
regMainThreaded :: IO ()
|
|
|
|
regMainThreaded = do
|
|
|
|
withGRPC $ \grpc -> do
|
|
|
|
let methods = [(MethodName "/echo.Echo/DoEcho", Normal)]
|
2016-06-22 22:07:38 +02:00
|
|
|
withServer grpc (ServerConfig "localhost" 50051 methods []) $ \server -> do
|
Preliminary streaming mode support (client streaming, server streaming, bidirectional) (#37)
* Tweak runOps param order, inline common op sequences, clean up serverHandleNormalCall
* More ops sequence inlining for clarity, experimenting with Managed
* Checkpoint: preliminary support for all streaming modes; much cleanup/refactoring and api design still needed
* Use mempty for default StatusDetails; tweak bad status matching mechanism
* Preliminary user-facing, server-streaming, low-level api and test
* renaming wibbles
* Preliminary user-facing, client-streaming, low-level api and test
* Move sendMsgs comb to Network.GRPC.LowLevel.Op; misc cleanup/DCR
* Modify bidi streaming to omit request payload
* Add transformers dep
* Preliminary user-facing low-level bidirectional streaming api and test
* Fix missing peek import
* Remove TimeoutSeconds params on streaming mode functions
* Fix serverHandleNormalCall rebase wart
* Fix rebase warts; minor hlint fixes and wibbles
* Post-rebase tweaks to optional payload use in serverRequestCall (i.e., now respects payloadHandling again)
* Cleanup/refactor serverRequestCall
* Fix comment
* Change ServerRWHandler type so that handler does not have to invoke a finalizer
* Change ServerReaderHandler type so that handler does not have to invoke a finalizer
* Simplify serverWriter interface and ServerWriterHandler structure
* Simplify serverRW (get rid of exec param), improve bidi streaming tests
* Use ExceptT in serverRW impl
* Change ServerRWHandler type to pass recv/send operations.
* Renaming
* Define ClientRWHandler, pass recv/send ops
* wibbles
* Use ExceptT in clientRW impl
* Add DataKinded phantom typing to RegisteredMethod; misc cleanup
* Simplify sendMsgs interface; add SingleSend type and related helpers
* Rename SingleSend to SendSingle, use ExceptT to clean up {client,server}Writer and sendMsgs
* More ExceptT cleanup in clientWriter
* Factor out reusable bits of clientWriter
* Shrink ServerReaderHandler
* Delete stale comments
* Use common machinery for all streaming modes; make handler types more consistent
* wibbles
2016-07-06 15:59:38 +02:00
|
|
|
let method = head (normalMethods server)
|
2016-07-11 00:04:31 +02:00
|
|
|
tids <- replicateM 7 $ async $ do tputStrLn "starting handler"
|
|
|
|
regLoop server method
|
|
|
|
waitAnyCancel tids
|
|
|
|
tputStrLn "finishing"
|
2016-06-03 19:34:09 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = regMainThreaded
|