Skip to content
Permalink
Browse files

Refactor node ipc client

  • Loading branch information...
HirotoShioi committed Jun 11, 2019
1 parent 77a569a commit 9a29bc0a1a94414aea38fd45019e71fc0c3d75a1
Showing with 14 additions and 19 deletions.
  1. +14 −18 app/NodeIPCClient/Main.hs
  2. +0 −1 cardano-shell.cabal
@@ -12,8 +12,7 @@ import Cardano.Prelude
import Cardano.Shell.NodeIPC (Port (..), ProtocolDuration (..),
ReadHandle (..), WriteHandle (..),
getHandleFromEnv, startIPC,
startNodeJsIPC)
import Control.Exception.Safe (throwM)
startNodeJsIPC, NodeIPCError)
import System.IO (BufferMode (..), hClose, hSetBuffering)

port :: Port
@@ -29,27 +28,24 @@ port = Port 8090
-- Calling this function directly (ex. @stack exec node-ipc haskell@) will throw an error
-- since there wouldn't be an handle that client will be able to use to communicate with
-- the server.
main :: IO ()
main :: IO (Either NodeIPCError ())
main = do
(cmd:_) <- getArgs
case cmd of
"js" -> void $ startNodeJsIPC SingleMessage port
"haskell" -> bracket acquire restore action
_ -> return ()
"js" -> Right <$> void (startNodeJsIPC SingleMessage port)
"haskell" -> either
(return . Left)
action
=<<
(getHandleFromEnv "NODE_CHANNEL_FD")
_ -> return . Right $ ()
where
acquire :: IO Handle
acquire = do
-- Lookup the Handle that the server has set
serverWHandle <- either throwM return =<< (getHandleFromEnv "NODE_CHANNEL_FD")
hSetBuffering serverWHandle LineBuffering
return serverWHandle

restore :: Handle -> IO ()
restore = hClose

action :: Handle -> IO ()
action :: Handle -> IO (Either NodeIPCError ())
action serverWHandle = do
hSetBuffering serverWHandle LineBuffering
let serverWriteHandle = WriteHandle serverWHandle
let serverReadHandle = ReadHandle stdin
void $ startIPC SingleMessage serverReadHandle serverWriteHandle port
startIPC SingleMessage serverReadHandle serverWriteHandle port
`finally`
hClose serverWHandle

@@ -118,7 +118,6 @@ executable node-ipc
base >=4.7 && <5
, cardano-shell
, cardano-prelude
, safe-exceptions
default-language: Haskell2010
default-extensions: NoImplicitPrelude
OverloadedStrings

0 comments on commit 9a29bc0

Please sign in to comment.
You can’t perform that action at this time.