diff --git a/src/Quasar/Network/Connection.hs b/src/Quasar/Network/Connection.hs index e2f747c56283a746f70b04fe0773cab74d88bf58..5b18ddb897b4d96ba8a175d696d0b96c249445b7 100644 --- a/src/Quasar/Network/Connection.hs +++ b/src/Quasar/Network/Connection.hs @@ -7,15 +7,13 @@ module Quasar.Network.Connection ( import Control.Concurrent (threadDelay) import Control.Concurrent.Async (Async, async, cancel, link, waitCatch, withAsync) import Control.Concurrent.MVar -import Control.Exception (Exception(..), SomeException, bracketOnError, catch, interruptible, finally, throwIO, bracketOnError, onException) -import Control.Monad ((>=>), unless, forM_) +import Control.Exception (Exception(..), SomeException, bracketOnError, catch, interruptible, finally, bracketOnError, onException) import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as BSL -import Data.List (intercalate) import qualified Network.Socket as Socket import qualified Network.Socket.ByteString as Socket import qualified Network.Socket.ByteString.Lazy as SocketL -import Prelude +import Quasar.Prelude -- | Abstraction over a bidirectional stream connection (e.g. a socket), to be able to switch to different communication channels (e.g. stdin/stdout or a dummy implementation for unit tests). data Connection = Connection { diff --git a/src/Quasar/Network/Multiplexer.hs b/src/Quasar/Network/Multiplexer.hs index 13ed48eafad01832c295f0de7f3b7e60dc91c56b..82b388ab61240bd0cd1fb30d04f1e19e7c2544ff 100644 --- a/src/Quasar/Network/Multiplexer.hs +++ b/src/Quasar/Network/Multiplexer.hs @@ -24,9 +24,7 @@ module Quasar.Network.Multiplexer ( import Control.Concurrent.Async (AsyncCancelled(..), async, link, race_, wait, waitAnyCancel, withAsync, withAsyncWithUnmask) -import Control.Exception (Exception(..), Handler(..), MaskingState(Unmasked), SomeException(..), catch, catches, handle, interruptible, throwIO, getMaskingState, mask_) -import Control.Monad (when, unless, void) -import Control.Monad.IO.Class (liftIO) +import Control.Exception (Exception(..), Handler(..), MaskingState(Unmasked), SomeException(..), catch, catches, handle, interruptible, getMaskingState, mask_) import Control.Monad.State (StateT, execStateT, runStateT, lift) import qualified Control.Monad.State as State import Control.Concurrent.MVar @@ -39,8 +37,7 @@ import qualified Data.HashMap.Strict as HM import Data.Tuple (swap) import Data.Word import Quasar.Network.Connection -import Prelude -import GHC.Generics +import Quasar.Prelude import System.IO (hPutStrLn, stderr) -- NOTE this module got more complicated than expected and should be refactored to encode the core protocol interactions in pure code, with an IO wrapper that handles usage, callbacks and sending/receiving. diff --git a/src/Quasar/Network/Runtime.hs b/src/Quasar/Network/Runtime.hs index b171ee57cb361b3e347984dba4fd1dce087e0917..5356d3c3df572daba7898ca08a7390257fe951cf 100644 --- a/src/Quasar/Network/Runtime.hs +++ b/src/Quasar/Network/Runtime.hs @@ -42,17 +42,14 @@ module Quasar.Network.Runtime ( import Control.Concurrent (forkFinally) import Control.Concurrent.Async (cancel, link, withAsync, mapConcurrently_) import Control.Exception (SomeException, bracket, bracketOnError, bracketOnError, interruptible, mask_) -import Control.Monad (when, unless, forever, void) -import qualified Control.Monad.State as State import Control.Concurrent.MVar import Data.Binary (Binary, encode, decodeOrFail) import qualified Data.ByteString.Lazy as BSL -import Data.Hashable (Hashable) import qualified Data.HashMap.Strict as HM import qualified Network.Socket as Socket -import Prelude import Quasar.Network.Connection import Quasar.Network.Multiplexer +import Quasar.Prelude import System.Posix.Files (getFileStatus, isSocket, fileExist, removeLink) @@ -305,14 +302,3 @@ newLocalClient server = do withStandaloneClient :: forall p a. (RpcProtocol p, HasProtocolImpl p) => ProtocolImpl p -> (Client p -> IO a) -> IO a withStandaloneClient impl runClientHook = withServer impl [] $ \server -> withLocalClient server runClientHook - - - --- * Helper functions - --- | Lookup and delete a value from a HashMap in one operation -lookupDelete :: forall k v. (Eq k, Hashable k) => k -> HM.HashMap k v -> (HM.HashMap k v, Maybe v) -lookupDelete key m = State.runState fn Nothing - where - fn :: State.State (Maybe v) (HM.HashMap k v) - fn = HM.alterF (\c -> State.put c >> pure Nothing) key m diff --git a/src/Quasar/Network/SocketLocation.hs b/src/Quasar/Network/SocketLocation.hs index f8ed7f1b37a812f2c9e39f43be62dc3e593e4890..afa6ad19442507a17343fc45e2e5ea0f7fe558e0 100644 --- a/src/Quasar/Network/SocketLocation.hs +++ b/src/Quasar/Network/SocketLocation.hs @@ -1,7 +1,7 @@ module Quasar.Network.SocketLocation where import Control.Exception (handle) -import Prelude +import Quasar.Prelude import System.Environment (getEnv) systemSocketPath :: String -> IO FilePath diff --git a/src/Quasar/Network/TH.hs b/src/Quasar/Network/TH.hs index 7af9bef76a0e638ed5b71174b857ba2de118dbcf..a8def53724949d71b235215798c8127dc3ef3a45 100644 --- a/src/Quasar/Network/TH.hs +++ b/src/Quasar/Network/TH.hs @@ -16,8 +16,6 @@ module Quasar.Network.TH ( HasProtocolImpl ) where -import Control.Applicative (liftA2) -import Control.Monad (unless) import Control.Monad.State (State, execState) import qualified Control.Monad.State as State import Data.Binary (Binary) @@ -25,9 +23,9 @@ import Data.Maybe (isNothing) import GHC.Generics import Language.Haskell.TH hiding (interruptible) import Language.Haskell.TH.Syntax -import Prelude import Quasar.Network.Multiplexer import Quasar.Network.Runtime +import Quasar.Prelude data RpcApi = RpcApi { name :: String, diff --git a/test/Quasar/Network/MultiplexerSpec.hs b/test/Quasar/Network/MultiplexerSpec.hs index 88998eb346989a86691f6d6a126e0eea1a481a4f..129128824f0c532192080f41bf27298453fe8339 100644 --- a/test/Quasar/Network/MultiplexerSpec.hs +++ b/test/Quasar/Network/MultiplexerSpec.hs @@ -100,7 +100,7 @@ spec = describe "runMultiplexerProtocol" $ parallel $ do withEchoServer :: (Channel -> IO a) -> IO a -withEchoServer fn = bracket setup close (\(channel, _) -> fn channel) +withEchoServer fn = bracket setup closePair (\(channel, _) -> fn channel) where setup :: IO (Channel, Channel) setup = do @@ -109,8 +109,8 @@ withEchoServer fn = bracket setup close (\(channel, _) -> fn channel) echoChannel <- newMultiplexer MultiplexerSideB echoSocket configureEchoHandler echoChannel pure (mainChannel, echoChannel) - close :: (Channel, Channel) -> IO () - close (x, y) = channelClose x >> channelClose y + closePair :: (Channel, Channel) -> IO () + closePair (x, y) = channelClose x >> channelClose y configureEchoHandler :: Channel -> IO () configureEchoHandler channel = channelSetHandler channel (echoHandler channel) echoHandler :: Channel -> ReceivedMessageResources -> BSL.ByteString -> IO ()