Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Apply fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroto Shioi committed Mar 15, 2019
1 parent 88df360 commit 2d486f1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
57 changes: 29 additions & 28 deletions src/Cardano/Shell/NodeIPC/Example.hs
Expand Up @@ -24,6 +24,7 @@ module Cardano.Shell.NodeIPC.Example


import Cardano.Prelude import Cardano.Prelude


import Data.Aeson (ToJSON)
import System.IO (BufferMode (..), hSetBuffering) import System.IO (BufferMode (..), hSetBuffering)
import System.Posix.Process (exitImmediately, forkProcess) import System.Posix.Process (exitImmediately, forkProcess)
import System.Process (createPipe) import System.Process (createPipe)
Expand All @@ -49,51 +50,51 @@ getReadWriteHandles = do
-- Testing -- Testing
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------


nodePort :: Port
nodePort = Port 8090

-- | Example using file descriptor -- | Example using file descriptor
exampleWithFD :: IO (MsgOut, MsgOut) exampleWithFD :: IO (MsgOut, MsgOut)
exampleWithFD = do exampleWithFD = do


(clientReadHandle, clientWriteHandle) <- getReadWriteHandles (clientReadHandle, clientWriteHandle) <- getReadWriteHandles
(serverReadHandle, serverWriteHandle) <- getReadWriteHandles


let nodePort = Port 8090 (_, responses) <-
asyncServer <- async $ startIPC serverReadHandle clientWriteHandle nodePort ipcServer clientWriteHandle Ping
link asyncServer `concurrently`
receieveMessages clientReadHandle


-- Use these functions so you don't pass the wrong handle by mistake return responses
let readClientMessage :: IO MsgOut
readClientMessage = readMessage clientReadHandle

let sendServer :: MsgIn -> IO ()
sendServer = sendMessage serverWriteHandle

-- -- Communication starts here
started <- readClientMessage
sendServer Ping
pong <- readClientMessage -- Pong
return (started, pong)


-- | Example of an IPC using process -- | Example of an IPC using process
exampleWithProcess :: IO (MsgOut, MsgOut) exampleWithProcess :: IO (MsgOut, MsgOut)
exampleWithProcess = do exampleWithProcess = do
(clientReadHandle, clientWriteHandle) <- getReadWriteHandles (clientReadHandle, clientWriteHandle) <- getReadWriteHandles


-- Create a child process that acts as an server -- Create a child process that acts as an server
_ <- finally
(forkProcess $ do
(serverReadHandle, serverWriteHandle) <- getReadWriteHandles
-- Send message to server
sendMessage serverWriteHandle Ping
let nodePort = Port 8090
startIPC serverReadHandle clientWriteHandle nodePort
exitImmediately ExitSuccess
)
(return ())


_ <- forkProcess $ do
ipcServer clientWriteHandle Ping
exitImmediately ExitSuccess
`finally`
return ()

receieveMessages clientReadHandle

-- | IPC server
ipcServer :: (ToJSON msg) => WriteHandle -> msg -> IO ()
ipcServer clientWriteHandle msgin = do
(serverReadHandle, serverWriteHandle) <- getReadWriteHandles
-- Send message to server
sendMessage serverWriteHandle msgin
startIPC serverReadHandle clientWriteHandle nodePort

-- | Read message wigh given 'ReadHandle'
receieveMessages :: ReadHandle -> IO (MsgOut, MsgOut)
receieveMessages clientReadHandle = do
let readClientMessage :: IO MsgOut let readClientMessage :: IO MsgOut
readClientMessage = readMessage clientReadHandle readClientMessage = readMessage clientReadHandle


-- Recieve the messages started <- readClientMessage
started <- readClientMessage -- Start
pong <- readClientMessage -- Pong pong <- readClientMessage -- Pong
return (started, pong) return (started, pong)
11 changes: 5 additions & 6 deletions src/Cardano/Shell/NodeIPC/Lib.hs
Expand Up @@ -184,12 +184,11 @@ startNodeJsIPC port = do
-- responds with 'ReplyPort' with 'Port', -- responds with 'ReplyPort' with 'Port',
ipcListener :: forall m . (MonadIO m, MonadCatch m, MonadMask m) => ReadHandle -> WriteHandle -> Port -> m () ipcListener :: forall m . (MonadIO m, MonadCatch m, MonadMask m) => ReadHandle -> WriteHandle -> Port -> m ()
ipcListener readHandle@(ReadHandle rHndl) writeHandle@(WriteHandle wHndl) (Port port) = ipcListener readHandle@(ReadHandle rHndl) writeHandle@(WriteHandle wHndl) (Port port) =
finally do
(do checkHandles readHandle writeHandle
checkHandles readHandle writeHandle catches handleMsgIn [Handler handler, Handler handleMsgError]
catches handleMsgIn [Handler handler, Handler handleMsgError] `finally`
) shutdown
shutdown
where where
handleMsgIn :: m () handleMsgIn :: m ()
handleMsgIn = do handleMsgIn = do
Expand Down
31 changes: 17 additions & 14 deletions test/NodeIPCSpec.hs
Expand Up @@ -185,20 +185,23 @@ testStartNodeIPC port msg = do
(serverReadHandle, serverWriteHandle) <- getReadWriteHandles (serverReadHandle, serverWriteHandle) <- getReadWriteHandles


-- Start the server -- Start the server
void $ async $ startIPC serverReadHandle clientWriteHandle port (_, responses) <-

startIPC serverReadHandle clientWriteHandle port
-- Use these functions so you don't pass the wrong handle by mistake `concurrently`
let readClientMessage :: IO MsgOut do
readClientMessage = readMessage clientReadHandle -- Use these functions so you don't pass the wrong handle by mistake

let readClientMessage :: IO MsgOut
let sendServer :: msg -> IO () readClientMessage = readMessage clientReadHandle
sendServer = sendMessage serverWriteHandle

let sendServer :: msg -> IO ()
-- Communication starts here sendServer = sendMessage serverWriteHandle
started <- readClientMessage
sendServer msg -- Communication starts here
response <- readClientMessage started <- readClientMessage
return (started, response) sendServer msg
response <- readClientMessage
return (started, response)
return responses


whenLeft :: Applicative m => Either a b -> (a -> m ()) -> m () whenLeft :: Applicative m => Either a b -> (a -> m ()) -> m ()
whenLeft (Left x) f = f x whenLeft (Left x) f = f x
Expand Down

0 comments on commit 2d486f1

Please sign in to comment.