Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Remove trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
edsko committed Dec 3, 2012
1 parent 9f94aa4 commit 49458e9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
76 changes: 38 additions & 38 deletions src/Network/Transport/Chan.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- | In-memory implementation of the Transport API.
module Network.Transport.Chan (createTransport) where

import Network.Transport
import Network.Transport
import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
import Control.Applicative ((<$>))
import Control.Category ((>>>))
Expand All @@ -15,11 +15,11 @@ import qualified Data.Set as Set (empty, elems, insert, delete)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BSC (pack)
import Data.Accessor (Accessor, accessor, (^.), (^=), (^:))
import qualified Data.Accessor.Container as DAC (mapMaybe)
import qualified Data.Accessor.Container as DAC (mapMaybe)

-- Global state: next available "address", mapping from addresses to channels and next available connection
data TransportState = State { _channels :: Map EndPointAddress (Chan Event)
, _nextConnectionId :: Map EndPointAddress ConnectionId
data TransportState = State { _channels :: Map EndPointAddress (Chan Event)
, _nextConnectionId :: Map EndPointAddress ConnectionId
, _multigroups :: Map MulticastAddress (MVar (Set EndPointAddress))
}

Expand All @@ -29,11 +29,11 @@ data TransportState = State { _channels :: Map EndPointAddress (Chan Eve
-- (threads can, and should, create their own endpoints though).
createTransport :: IO Transport
createTransport = do
state <- newMVar State { _channels = Map.empty
state <- newMVar State { _channels = Map.empty
, _nextConnectionId = Map.empty
, _multigroups = Map.empty
}
return Transport { newEndPoint = apiNewEndPoint state
return Transport { newEndPoint = apiNewEndPoint state
, closeTransport = throwIO (userError "closeEndPoint not implemented")
}

Expand All @@ -44,54 +44,54 @@ apiNewEndPoint state = do
addr <- modifyMVar state $ \st -> do
let addr = EndPointAddress . BSC.pack . show . Map.size $ st ^. channels
return ((channelAt addr ^= chan) . (nextConnectionIdAt addr ^= 1) $ st, addr)
return . Right $ EndPoint { receive = readChan chan
return . Right $ EndPoint { receive = readChan chan
, address = addr
, connect = apiConnect addr state
, connect = apiConnect addr state
, closeEndPoint = throwIO (userError "closeEndPoint not implemented")
, newMulticastGroup = apiNewMulticastGroup state addr
, resolveMulticastGroup = apiResolveMulticastGroup state addr
}

-- | Create a new connection
apiConnect :: EndPointAddress
-> MVar TransportState
-> EndPointAddress
-> Reliability
-> ConnectHints
apiConnect :: EndPointAddress
-> MVar TransportState
-> EndPointAddress
-> Reliability
-> ConnectHints
-> IO (Either (TransportError ConnectErrorCode) Connection)
apiConnect myAddress state theirAddress _reliability _hints = do
apiConnect myAddress state theirAddress _reliability _hints = do
(chan, conn) <- modifyMVar state $ \st -> do
let chan = st ^. channelAt theirAddress
let conn = st ^. nextConnectionIdAt theirAddress
return (nextConnectionIdAt theirAddress ^: (+ 1) $ st, (chan, conn))
writeChan chan $ ConnectionOpened conn ReliableOrdered myAddress
connAlive <- newMVar True
return . Right $ Connection { send = apiSend chan conn connAlive
, close = apiClose chan conn connAlive
}
return . Right $ Connection { send = apiSend chan conn connAlive
, close = apiClose chan conn connAlive
}

-- | Send a message over a connection
apiSend :: Chan Event -> ConnectionId -> MVar Bool -> [ByteString] -> IO (Either (TransportError SendErrorCode) ())
apiSend chan conn connAlive msg =
modifyMVar connAlive $ \alive ->
if alive
then do
apiSend chan conn connAlive msg =
modifyMVar connAlive $ \alive ->
if alive
then do
writeChan chan (Received conn msg)
return (alive, Right ())
else
else
return (alive, Left (TransportError SendFailed "Connection closed"))

-- | Close a connection
apiClose :: Chan Event -> ConnectionId -> MVar Bool -> IO ()
apiClose chan conn connAlive =
apiClose chan conn connAlive =
modifyMVar_ connAlive $ \alive -> do
when alive . writeChan chan $ ConnectionClosed conn
return False

-- | Create a new multicast group
apiNewMulticastGroup :: MVar TransportState -> EndPointAddress -> IO (Either (TransportError NewMulticastGroupErrorCode) MulticastGroup)
apiNewMulticastGroup state ourAddress = do
group <- newMVar Set.empty
group <- newMVar Set.empty
groupAddr <- modifyMVar state $ \st -> do
let addr = MulticastAddress . BSC.pack . show . Map.size $ st ^. multigroups
return (multigroupAt addr ^= group $ st, addr)
Expand All @@ -105,30 +105,30 @@ apiNewMulticastGroup state ourAddress = do
-- deleted.
createMulticastGroup :: MVar TransportState -> EndPointAddress -> MulticastAddress -> MVar (Set EndPointAddress) -> MulticastGroup
createMulticastGroup state ourAddress groupAddress group =
MulticastGroup { multicastAddress = groupAddress
MulticastGroup { multicastAddress = groupAddress
, deleteMulticastGroup = modifyMVar_ state $ return . (multigroups ^: Map.delete groupAddress)
, maxMsgSize = Nothing
, multicastSend = \payload -> do
, multicastSend = \payload -> do
cs <- (^. channels) <$> readMVar state
es <- readMVar group
forM_ (Set.elems es) $ \ep -> do
es <- readMVar group
forM_ (Set.elems es) $ \ep -> do
let ch = cs ^. at ep "Invalid endpoint"
writeChan ch (ReceivedMulticast groupAddress payload)
writeChan ch (ReceivedMulticast groupAddress payload)
, multicastSubscribe = modifyMVar_ group $ return . Set.insert ourAddress
, multicastUnsubscribe = modifyMVar_ group $ return . Set.delete ourAddress
, multicastClose = return ()
, multicastClose = return ()
}

-- | Resolve a multicast group
apiResolveMulticastGroup :: MVar TransportState
apiResolveMulticastGroup :: MVar TransportState
-> EndPointAddress
-> MulticastAddress
-> MulticastAddress
-> IO (Either (TransportError ResolveMulticastGroupErrorCode) MulticastGroup)
apiResolveMulticastGroup state ourAddress groupAddress = do
group <- (^. (multigroups >>> DAC.mapMaybe groupAddress)) <$> readMVar state
group <- (^. (multigroups >>> DAC.mapMaybe groupAddress)) <$> readMVar state
case group of
Nothing -> return . Left $ TransportError ResolveMulticastGroupNotFound ("Group " ++ show groupAddress ++ " not found")
Just mvar -> return . Right $ createMulticastGroup state ourAddress groupAddress mvar
Just mvar -> return . Right $ createMulticastGroup state ourAddress groupAddress mvar

--------------------------------------------------------------------------------
-- Lens definitions --
Expand All @@ -141,12 +141,12 @@ nextConnectionId :: Accessor TransportState (Map EndPointAddress ConnectionId)
nextConnectionId = accessor _nextConnectionId (\cid st -> st { _nextConnectionId = cid })

multigroups :: Accessor TransportState (Map MulticastAddress (MVar (Set EndPointAddress)))
multigroups = accessor _multigroups (\gs st -> st { _multigroups = gs })
multigroups = accessor _multigroups (\gs st -> st { _multigroups = gs })

at :: Ord k => k -> String -> Accessor (Map k v) v
at k err = accessor (Map.findWithDefault (error err) k) (Map.insert k)
at k err = accessor (Map.findWithDefault (error err) k) (Map.insert k)

channelAt :: EndPointAddress -> Accessor TransportState (Chan Event)
channelAt :: EndPointAddress -> Accessor TransportState (Chan Event)
channelAt addr = channels >>> at addr "Invalid channel"

nextConnectionIdAt :: EndPointAddress -> Accessor TransportState ConnectionId
Expand Down
2 changes: 1 addition & 1 deletion tests/TestInMemory.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module Main where
import Network.Transport.Tests
import Network.Transport.Chan
import Control.Applicative ((<$>))

main :: IO ()
main = testTransport (Right <$> createTransport)

0 comments on commit 49458e9

Please sign in to comment.