-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
216 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Copyright (c) 2023-2024, Well-Typed LLP and Anduril Industries Inc. | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
|
||
* Neither the name of Well-Typed LLP, the name of Anduril | ||
Industries Inc., nor the names of other contributors may be | ||
used to endorse or promote products derived from this software | ||
without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Basics tutorial using the low-level API | ||
|
||
See `/tutorials/basics` for the more direct `grapesy` translation of the | ||
[official Basics tutorial](https://grpc.io/docs/languages/python/basics/). | ||
|
||
In this tutorial we re-implement the client using the | ||
`grapesy` [`conduit`](https://hackage.haskell.org/package/conduit) API. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
module Client (main) where | ||
|
||
import Conduit | ||
import Control.Concurrent | ||
import Data.Conduit.List | ||
import Data.Int | ||
import Data.Text (Text) | ||
import System.Random | ||
|
||
import Network.GRPC.Client | ||
import Network.GRPC.Client.StreamType.Conduit | ||
import Network.GRPC.Common | ||
import Network.GRPC.Common.Protobuf | ||
|
||
import RouteGuide | ||
|
||
import Proto.API.RouteGuide | ||
|
||
{------------------------------------------------------------------------------- | ||
Call each of the methods of the RouteGuide service | ||
-------------------------------------------------------------------------------} | ||
|
||
listFeatures :: Connection -> IO () | ||
listFeatures conn = do | ||
let lo = defMessage | ||
& #latitude .~ 400000000 | ||
& #longitude .~ -750000000 | ||
hi = defMessage | ||
& #latitude .~ 420000000 | ||
& #longitude .~ -730000000 | ||
req = defMessage | ||
& #lo .~ lo | ||
& #hi .~ hi | ||
|
||
let sink :: ConduitT (Proto Feature) Void IO () | ||
sink = mapM_C print | ||
|
||
serverStreaming conn (rpc @(Protobuf RouteGuide "listFeatures")) req $ \source -> | ||
runConduit $ source .| sink | ||
|
||
recordRoute :: Connection -> IO () | ||
recordRoute conn = do | ||
db <- getDB | ||
|
||
let source :: ConduitT () (Proto Point) IO () | ||
source = replicateMC 10 $ do | ||
i <- randomRIO (0, length db - 1) | ||
let p = (db !! i) ^. #location | ||
threadDelay 500_000 -- 0.5 seconds | ||
return p | ||
|
||
resp <- clientStreaming_ conn (rpc @(Protobuf RouteGuide "recordRoute")) $ \sink -> | ||
runConduit $ source .| sink | ||
print resp | ||
|
||
routeChat :: Connection -> IO () | ||
routeChat conn = do | ||
|
||
let clientSource :: ConduitT () (Proto RouteNote) IO () | ||
clientSource = sourceList messages | ||
|
||
clientSink :: ConduitT (Proto RouteNote) Void IO () | ||
clientSink = mapM_C print | ||
|
||
biDiStreaming conn (rpc @(Protobuf RouteGuide "routeChat")) $ \serverSink serverSource -> do | ||
runConduit $ clientSource .| serverSink | ||
runConduit $ serverSource .| clientSink | ||
where | ||
messages :: [Proto RouteNote] | ||
messages = [ | ||
makeRouteNote "First message" 0 0 | ||
, makeRouteNote "Second message" 0 1 | ||
, makeRouteNote "Third message" 1 0 | ||
, makeRouteNote "Fourth message" 0 0 | ||
, makeRouteNote "Fifth message" 1 0 | ||
] | ||
|
||
makeRouteNote :: Text -> Int32 -> Int32 -> Proto RouteNote | ||
makeRouteNote message latitude longitude = | ||
let location = | ||
defMessage | ||
& #latitude .~ latitude | ||
& #longitude .~ longitude | ||
in defMessage | ||
& #message .~ message | ||
& #location .~ location | ||
|
||
{------------------------------------------------------------------------------- | ||
Main application | ||
-------------------------------------------------------------------------------} | ||
|
||
main :: IO () | ||
main = | ||
withConnection def server $ \conn -> do | ||
putStrLn "-------------- ListFeatures --------------" | ||
listFeatures conn | ||
|
||
putStrLn "-------------- RecordRoute --------------" | ||
recordRoute conn | ||
|
||
putStrLn "-------------- RouteChat --------------" | ||
routeChat conn | ||
where | ||
server :: Server | ||
server = ServerInsecure $ Address "127.0.0.1" defaultInsecurePort Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
cabal-version: 3.0 | ||
name: conduit-tutorial | ||
synopsis: gRPC basics tutorial for grapesy, using conduits | ||
version: 0.1.0 | ||
license: BSD-3-Clause | ||
license-file: LICENSE | ||
author: Edsko de Vries | ||
maintainer: [email protected] | ||
build-type: Simple | ||
tested-with: GHC==8.10.7 | ||
, GHC==9.2.8 | ||
, GHC==9.4.8 | ||
, GHC==9.6.6 | ||
, GHC==9.8.2 | ||
|
||
common lang | ||
build-depends: base >= 4.14 && < 5 | ||
default-language: Haskell2010 | ||
ghc-options: -Wall | ||
|
||
if impl(ghc >= 9.0) | ||
ghc-options: | ||
-Wunused-packages | ||
|
||
default-extensions: | ||
DataKinds | ||
NumericUnderscores | ||
OverloadedLabels | ||
OverloadedStrings | ||
TypeApplications | ||
|
||
executable route_guide_client | ||
import: lang | ||
main-is: Client.hs | ||
hs-source-dirs: app | ||
ghc-options: -main-is Client | ||
build-depends: basics-tutorial | ||
|
||
build-depends: | ||
, conduit >= 1.3 && < 1.4 | ||
, grapesy >= 0.1 && < 0.2 | ||
, random >= 1.2 && < 1.3 | ||
, text >= 1.2 && < 2.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters