-
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
16 changed files
with
438 additions
and
26 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
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 @@ | ||
# Metadata tutorial |
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,3 @@ | ||
import Data.ProtoLens.Setup | ||
|
||
main = defaultMainGeneratingProtos "proto" |
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,98 @@ | ||
module Client (main) where | ||
|
||
import Control.Monad.Catch | ||
import Control.Monad.State | ||
import Crypto.Hash.SHA256 qualified as SHA256 | ||
import Data.ByteString (ByteString) | ||
import Data.ByteString qualified as BS | ||
import Data.Text qualified as Text | ||
import System.Environment | ||
import System.IO | ||
|
||
import Network.GRPC.Client | ||
import Network.GRPC.Common | ||
import Network.GRPC.Common.Protobuf | ||
import Network.GRPC.Common.StreamElem qualified as StreamElem | ||
|
||
import Proto.API.Fileserver | ||
|
||
{------------------------------------------------------------------------------- | ||
Very simple progress bar | ||
-------------------------------------------------------------------------------} | ||
|
||
newtype ProgressT m a = WrapProgressT { | ||
unwrapProgressT :: StateT [Integer] m a | ||
} | ||
deriving newtype (Functor, Applicative, Monad, MonadTrans, MonadIO) | ||
|
||
instance MonadState s m => MonadState s (ProgressT m) where | ||
state = lift . state | ||
|
||
runProgressT :: (MonadMask m, MonadIO m) => Integer -> ProgressT m a -> m a | ||
runProgressT totalSize = | ||
(`finally` liftIO (putStrLn " done")) | ||
. flip evalStateT dots | ||
. unwrapProgressT | ||
where | ||
numDots = 50 | ||
stepSize = totalSize `div` fromIntegral numDots | ||
dots = take numDots $ iterate (+ stepSize) stepSize | ||
|
||
updateProgressBar :: forall m. MonadIO m => Integer -> ProgressT m () | ||
updateProgressBar currentSize = do | ||
WrapProgressT $ StateT go | ||
liftIO $ hFlush stdout | ||
where | ||
go :: [Integer] -> m ((), [Integer]) | ||
go [] = return ((), []) | ||
go (d:ds) | ||
| d <= currentSize = liftIO (putChar '.') >> go ds | ||
| otherwise = return ((), d:ds) | ||
|
||
{------------------------------------------------------------------------------- | ||
RPC | ||
-------------------------------------------------------------------------------} | ||
|
||
processPartial :: | ||
Handle | ||
-> Proto Partial | ||
-> ProgressT (StateT (SHA256.Ctx, Integer) IO) () | ||
processPartial h partial = do | ||
liftIO $ BS.hPut h chunk | ||
accSize <- state $ \(ctx, currentSize) -> | ||
let currentSize' = currentSize + fromIntegral (BS.length chunk) | ||
in (currentSize', (SHA256.update ctx chunk, currentSize')) | ||
updateProgressBar accSize | ||
where | ||
chunk :: ByteString | ||
chunk = partial ^. #chunk | ||
|
||
download :: Connection -> String -> String -> IO () | ||
download conn inp out = do | ||
withRPC conn def (Proxy @(Protobuf Fileserver "download")) $ \call -> do | ||
sendFinalInput call $ defMessage & #name .~ Text.pack inp | ||
|
||
-- Wait for initial metadata, telling us how big the file is | ||
DownloadStart{downloadSize} <- recvResponseInitialMetadata call | ||
|
||
-- Process each chunk | ||
(DownloadDone{downloadHash = theirHash}, (ourHash, _)) <- | ||
withFile out WriteMode $ \h -> | ||
flip runStateT (SHA256.init, 0) . runProgressT downloadSize $ | ||
StreamElem.whileNext_ (recvOutput call) (processPartial h) | ||
|
||
-- Check hash | ||
putStrLn $ "Hash match: " ++ show (theirHash == SHA256.finalize ourHash) | ||
|
||
{------------------------------------------------------------------------------- | ||
Main application | ||
-------------------------------------------------------------------------------} | ||
|
||
main :: IO () | ||
main = do | ||
[inp, out] <- getArgs | ||
withConnection def server $ \conn -> do | ||
download conn inp out | ||
where | ||
server :: Server | ||
server = ServerInsecure $ Address "127.0.0.1" defaultInsecurePort Nothing |
Oops, something went wrong.