From b6928ea8086be91995333766eb9688cadc2e172f Mon Sep 17 00:00:00 2001 From: Johannes Lund Date: Thu, 7 Mar 2019 14:36:55 +0100 Subject: [PATCH] do stuff --- app/server/Launcher.hs | 45 ++++++++++++++++++++++++++++++++++++++++++ app/server/Main.hs | 35 ++++++++++++++++++++++++++++++-- cardano-wallet.cabal | 5 +++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 app/server/Launcher.hs diff --git a/app/server/Launcher.hs b/app/server/Launcher.hs new file mode 100644 index 00000000000..42c2893b5ed --- /dev/null +++ b/app/server/Launcher.hs @@ -0,0 +1,45 @@ +module Launcher where + +import Control.Concurrent.Async +import Prelude +import System.Process + +-- What is @bracket@? +-- +-- When you want to acquire a resource, do some work with it, and then release +-- the resource, it is a good idea to use bracket, because bracket will install +-- the necessary exception handler to release the resource in the event that an +-- exception is raised during the computation. If an exception is raised, then +-- bracket will re-raise the exception (after performing the release). +-- +-- http://hackage.haskell.org/package/base-4.12.0.0/docs/Control-Exception-Base.html#v:bracket + + + + + + + + + +-- | Run an external process alongside a haskell IO action +-- +-- Both are expected to run forever. +-- +-- Sadly you cannot quit this in ghci +bracketProcess :: CreateProcess -> IO () -> IO () +bracketProcess process action = do + _ <- withCreateProcess + process {delegate_ctlc = True, std_out = CreatePipe } + $ \_stdin _stdout _stderr ph -> do + -- if this IO action throws, @withCreateProcess@ will kill the node and + -- rethrow. + concurrently action (node ph) + return () + where + node ph = do + _ <- waitForProcess ph + fail "why is the node gone?" + + + diff --git a/app/server/Main.hs b/app/server/Main.hs index 9407a950321..aa30febeb8c 100644 --- a/app/server/Main.hs +++ b/app/server/Main.hs @@ -9,8 +9,10 @@ -} module Main where +import Control.Concurrent import Control.Monad ( when ) +import Launcher import Prelude import System.Console.Docopt ( Arguments @@ -25,6 +27,7 @@ import System.Console.Docopt ) import System.Environment ( getArgs ) +import System.Process import Text.Read ( readMaybe ) @@ -60,10 +63,38 @@ main = do walletPort <- getArg args (longOption "wallet-server-port") readInt putStrLn $ - "TODO: start wallet on port " ++ (show walletPort) ++ - ",\n connecting to " ++ (show network) ++ + "Starting wallet on port " ++ (show walletPort) ++ + ",\n connecting to " ++ (show network) ++ " node on port " ++ (show nodePort) + bracketProcess + (nodeHttpBridgeOn nodePort network) + (walletOn walletPort) + + +sleep :: CreateProcess +sleep = (proc "sleep" ["10"]) + + +-- | +-- Requires cardano-http-bridge to be installed +-- +-- git clone https://github.com/input-output-hk/cardano-http-bridge +-- follow instructions in its readme +-- cargo install --path . +-- +nodeHttpBridgeOn :: Int -> Network -> CreateProcess +nodeHttpBridgeOn port _network = + (proc "cardano-http-bridge" ["start", "--port", show port]) + +-- TODO: Start actual wallet +walletOn :: Int -> IO () +walletOn _port = go + where + go = do + putStrLn "I'm wallet-ing" + threadDelay $ 200*1000 + go -- Functions for parsing the values of command line options -- diff --git a/cardano-wallet.cabal b/cardano-wallet.cabal index 936f0657cb9..e19d664df55 100644 --- a/cardano-wallet.cabal +++ b/cardano-wallet.cabal @@ -65,8 +65,13 @@ executable cardano-wallet-server build-depends: base , docopt + , process + , async + , unix hs-source-dirs: app/server + other-modules: + Launcher main-is: Main.hs