Skip to content

Commit

Permalink
sim-net: apply some hlint suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
coot committed Oct 26, 2023
1 parent 882e35e commit acafe6a
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions ouroboros-network-framework/src/Simulation/Network/Snocket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NamedFieldPuns #-}
Expand Down Expand Up @@ -372,9 +372,9 @@ class GlobalAddressScheme addr where
-- ones are IPv6.
--
instance GlobalAddressScheme Int where
getAddressType (TestAddress n) = if n `mod` 2 == 0
then IPv4Address
else IPv6Address
getAddressType (TestAddress n) = if even n
then IPv4Address
else IPv6Address
ephemeralAddress IPv4Address n = TestAddress $ (-2) * fromIntegral n
ephemeralAddress IPv6Address n = TestAddress $ (-1) * fromIntegral n + 1

Expand Down Expand Up @@ -427,8 +427,7 @@ withSnocket tr defaultBearerInfo scriptMap k = do
-> return $ Just (NotReleasedListeningSockets (Map.keys lstFDMap) err)

| not (Map.null connMap)
-> return $ Just (NotReleasedConnections ( fmap connState
$ connMap
-> return $ Just (NotReleasedConnections ( connState <$> connMap
) err)

| otherwise
Expand Down Expand Up @@ -531,7 +530,7 @@ instance Show addr => Show (FD_ m addr) where

-- | File descriptor type.
--
newtype FD m peerAddr = FD { fdVar :: (StrictTVar m (FD_ m peerAddr)) }
newtype FD m peerAddr = FD { fdVar :: StrictTVar m (FD_ m peerAddr) }


makeFDBearer :: forall addr m.
Expand All @@ -542,7 +541,7 @@ makeFDBearer :: forall addr m.
)
=> MakeBearer m (FD m (TestAddress addr))
makeFDBearer = MakeBearer $ \sduTimeout muxTracer FD { fdVar } -> do
fd_ <- atomically (readTVar fdVar)
fd_ <- readTVarIO fdVar
case fd_ of
FDUninitialised {} ->
throwIO (invalidError fd_)
Expand Down Expand Up @@ -665,7 +664,7 @@ mkSnocket state tr = Snocket { getLocalAddr
-> m (Either (FD_ m (TestAddress addr))
(TestAddress addr))
getLocalAddrM FD { fdVar } = do
fd_ <- atomically (readTVar fdVar)
fd_ <- readTVarIO fdVar
return $ case fd_ of
FDUninitialised Nothing -> Left fd_
FDUninitialised (Just peerAddr) -> Right peerAddr
Expand All @@ -680,7 +679,7 @@ mkSnocket state tr = Snocket { getLocalAddr
-> m (Either (FD_ m (TestAddress addr))
(TestAddress addr))
getRemoteAddrM FD { fdVar } = do
fd_ <- atomically (readTVar fdVar)
fd_ <- readTVarIO fdVar
return $ case fd_ of
FDUninitialised {} -> Left fd_
FDListening {} -> Left fd_
Expand Down Expand Up @@ -760,7 +759,7 @@ mkSnocket state tr = Snocket { getLocalAddr

connect :: FD m (TestAddress addr) -> TestAddress addr -> m ()
connect fd@FD { fdVar = fdVarLocal } remoteAddress = do
fd_ <- atomically (readTVar fdVarLocal)
fd_ <- readTVarIO fdVarLocal
traceWith' fd (STConnecting fd_ remoteAddress)
case fd_ of
-- Mask asynchronous exceptions. Only unmask when we really block
Expand Down Expand Up @@ -866,7 +865,7 @@ mkSnocket state tr = Snocket { getLocalAddr
<$> readTVar (nsConnections state)
case lstFd of
-- error cases
(Nothing) ->
Nothing ->
return (Left (connectIOError connId "no such listening socket"))
(Just FDUninitialised {}) ->
return (Left (connectIOError connId "unitialised listening socket"))
Expand Down Expand Up @@ -937,14 +936,13 @@ mkSnocket state tr = Snocket { getLocalAddr
(Map.delete (normaliseId connId))
>> throwIO e)
$ unmask (atomically $ runFirstToFinish $
(FirstToFinish $ do
FirstToFinish (do
LazySTM.readTVar timeoutVar >>= check
modifyTVar (nsConnections state)
(Map.delete (normaliseId connId))
return Nothing
)
return Nothing)
<>
(FirstToFinish $ do
FirstToFinish (do
mbConn <- Map.lookup (normaliseId connId)
<$> readTVar (nsConnections state)
case mbConn of
Expand Down Expand Up @@ -1177,24 +1175,22 @@ mkSnocket state tr = Snocket { getLocalAddr
, mkSockType fd
)
)
( \ result ->
case result of
Left {} -> return ()
Right (chann, connId) -> uninterruptibleMask_ $ do
acClose (cwiChannelLocal chann)
atomically $
modifyTVar (nsConnections state)
(Map.update
(\conn@Connection { connState } ->
case connState of
FIN ->
Nothing
_ ->
Just conn { connState = FIN })
(normaliseId connId))
( \ case
Left {} -> return ()
Right (chann, connId) -> uninterruptibleMask_ $ do
acClose (cwiChannelLocal chann)
atomically $
modifyTVar (nsConnections state)
(Map.update
(\conn@Connection { connState } ->
case connState of
FIN ->
Nothing
_ ->
Just conn { connState = FIN })
(normaliseId connId))
)
$ \ result ->
case result of
$ \ case
Left (err, mbLocalAddr, mbConnIdAndChann, fdType) -> do
uninterruptibleMask_ $
traverse_ (\(connId, chann) -> do
Expand Down Expand Up @@ -1343,7 +1339,7 @@ mkSnocket state tr = Snocket { getLocalAddr
bitraverse_
(\(connId, fdType, _) -> do
openState <- fmap connState . Map.lookup (normaliseId connId)
<$> atomically (readTVar (nsConnections state))
<$> readTVarIO (nsConnections state)
traceWith tr (WithAddr (Just (localAddress connId))
(Just (remoteAddress connId))
(STClosed fdType (Just openState)))
Expand Down

0 comments on commit acafe6a

Please sign in to comment.