@@ -84,7 +84,8 @@ import Data.IntSet (IntSet)
84import qualified Data.IntSet as IntSet (empty, insert, elems, singleton, null, delete, unions)
84import qualified Data.IntSet as IntSet (empty, insert, elems, singleton, null, delete, unions)
85import Data.Map (Map)
85import Data.Map (Map)
86import qualified Data.Map as Map (empty, elems, size)
86import qualified Data.Map as Map (empty, elems, size)
87−import Data.Lens.Lazy (Lens, lens, intMapLens, mapLens, (^.), (^=), (^+=), (^%=))
87+import Data.Accessor (Accessor, accessor, (^.), (^=), (^:))
88+import qualified Data.Accessor.Container as DAC (mapMaybe, intMapMaybe)
88import System.IO (hPutStrLn, stderr)
89import System.IO (hPutStrLn, stderr)
90-- $design
91-- $design
@@ -181,7 +182,7 @@ import System.IO (hPutStrLn, stderr)
181-- Internal datatypes --
182-- Internal datatypes --
182--------------------------------------------------------------------------------
183--------------------------------------------------------------------------------
184−-- We use underscores for fields that we might update (using lenses)
185+-- We use underscores for fields that we might update (using accessores)
186-- All data types follow the same structure:
187-- All data types follow the same structure:
@@ -396,7 +397,7 @@ apiClose theirEndPoint connId connAlive = void . tryIO $ do
397 writeIORef connAlive False
398 writeIORef connAlive False
398 sendOn vst [encodeInt32 CloseConnection, encodeInt32 connId]
399 sendOn vst [encodeInt32 CloseConnection, encodeInt32 connId]
399− return (RemoteEndPointValid . (remoteOutgoing ^+= (-1)) $ vst)
400+ return (RemoteEndPointValid . (remoteOutgoing ^: (+ 1)) $ vst)
401 return (RemoteEndPointValid vst)
402 return (RemoteEndPointValid vst)
402 RemoteEndPointClosed -> do
403 RemoteEndPointClosed -> do
@@ -559,7 +560,7 @@ requestConnectionTo ourEndPoint theirAddress = go
559 endPointStateSnapshot <- modifyMVar theirState $ \st ->
560 endPointStateSnapshot <- modifyMVar theirState $ \st ->
560 case st of
561 case st of
561 RemoteEndPointValid ep ->
562 RemoteEndPointValid ep ->
562− return (RemoteEndPointValid . (remoteOutgoing ^+= 1) $ ep, st)
563+ return (RemoteEndPointValid . (remoteOutgoing ^: (+ 1)) $ ep, st)
564 return (st, st)
565 return (st, st)
@@ -595,7 +596,7 @@ requestConnectionTo ourEndPoint theirAddress = go
595 let failureHandler :: IOException -> IO b
596 let failureHandler :: IOException -> IO b
596 failureHandler err = do
597 failureHandler err = do
597 modifyMVar_ theirState $ \(RemoteEndPointValid ep) ->
598 modifyMVar_ theirState $ \(RemoteEndPointValid ep) ->
598− return (RemoteEndPointValid . (remoteOutgoing ^+= (-1)) $ ep)
599+ return (RemoteEndPointValid . (remoteOutgoing ^: (+ 1)) $ ep)
599 -- TODO: should we call closeIfUnused here?
600 -- TODO: should we call closeIfUnused here?
600 throw $ FailedWith ConnectFailed (show err)
601 throw $ FailedWith ConnectFailed (show err)
@@ -754,7 +755,7 @@ doRemoteRequest (ourEndPoint, theirEndPoint) header = do
754 reqId <- modifyMVar (localState ourEndPoint) $ \st -> case st of
755 reqId <- modifyMVar (localState ourEndPoint) $ \st -> case st of
755 LocalEndPointValid vst -> do
756 LocalEndPointValid vst -> do
756 let reqId = vst ^. nextCtrlRequestId
757 let reqId = vst ^. nextCtrlRequestId
757− return (LocalEndPointValid . (nextCtrlRequestId ^+= 1) . (pendingCtrlRequestsAt reqId ^= Just reply) $ vst, reqId)
758+ return (LocalEndPointValid . (nextCtrlRequestId ^: (+ 1)) . (pendingCtrlRequestsAt reqId ^= Just reply) $ vst, reqId)
758 LocalEndPointClosed ->
759 LocalEndPointClosed ->
759 throw (userError "Local endpoint closed")
760 throw (userError "Local endpoint closed")
760 withMVar (remoteState theirEndPoint) $ \(RemoteEndPointValid vst) ->
761 withMVar (remoteState theirEndPoint) $ \(RemoteEndPointValid vst) ->
@@ -785,7 +786,7 @@ forkEndPointThread ourEndPoint p =
785 modifyMVar ourState $ \st -> case st of
786 modifyMVar ourState $ \st -> case st of
786 LocalEndPointValid vst -> do
787 LocalEndPointValid vst -> do
787 tid <- forkIO (p >> removeThread)
788 tid <- forkIO (p >> removeThread)
788− return (LocalEndPointValid . (internalThreads ^%= (tid :)) $ vst, True)
789+ return (LocalEndPointValid . (internalThreads ^: (tid :)) $ vst, True)
789 LocalEndPointClosed ->
790 LocalEndPointClosed ->
790 return (LocalEndPointClosed, False)
791 return (LocalEndPointClosed, False)
@@ -794,7 +795,7 @@ forkEndPointThread ourEndPoint p =
794 tid <- myThreadId
795 tid <- myThreadId
795 modifyMVar_ ourState $ \st -> case st of
796 modifyMVar_ ourState $ \st -> case st of
796 LocalEndPointValid vst ->
797 LocalEndPointValid vst ->
797− return (LocalEndPointValid . (internalThreads ^%= filter (/= tid)) $ vst)
798+ return (LocalEndPointValid . (internalThreads ^: filter (/= tid)) $ vst)
798 LocalEndPointClosed ->
799 LocalEndPointClosed ->
799 return LocalEndPointClosed
800 return LocalEndPointClosed
@@ -944,7 +945,7 @@ handleIncomingMessages (ourEndPoint, theirEndPoint) = do
944 modifyMVar_ theirState $ \st -> do
945 modifyMVar_ theirState $ \st -> do
945 vst <- case st of
946 vst <- case st of
946 RemoteEndPointValid vst ->
947 RemoteEndPointValid vst ->
947− return (remoteIncoming ^%= IntSet.insert newId $ vst)
948+ return (remoteIncoming ^: IntSet.insert newId $ vst)
948 RemoteEndPointClosing resolved vst -> do
949 RemoteEndPointClosing resolved vst -> do
949 -- If the endpoint is in closing state that means we send a
950 -- If the endpoint is in closing state that means we send a
950 -- CloseSocket request to the remote endpoint. If the remote
951 -- CloseSocket request to the remote endpoint. If the remote
@@ -987,7 +988,7 @@ handleIncomingMessages (ourEndPoint, theirEndPoint) = do
987 -- TODO: we should check that this connection is in fact open
988 -- TODO: we should check that this connection is in fact open
988 writeChan ourChannel (ConnectionClosed cid)
989 writeChan ourChannel (ConnectionClosed cid)
989 modifyMVar_ theirState $ \(RemoteEndPointValid vst) ->
990 modifyMVar_ theirState $ \(RemoteEndPointValid vst) ->
990− return (RemoteEndPointValid . (remoteIncoming ^%= IntSet.delete cid) $ vst)
991+ return (RemoteEndPointValid . (remoteIncoming ^: IntSet.delete cid) $ vst)
991 closeIfUnused theirEndPoint
992 closeIfUnused theirEndPoint
993 -- Close the socket (if we don't have any outgoing connections)
994 -- Close the socket (if we don't have any outgoing connections)
@@ -1090,38 +1091,38 @@ firstNonReservedConnectionId :: ConnectionId
1090firstNonReservedConnectionId = 1024
1091firstNonReservedConnectionId = 1024
1092--------------------------------------------------------------------------------
1093--------------------------------------------------------------------------------
1093−-- Lens definitions --
1094+-- Accessor definitions --
1094--------------------------------------------------------------------------------
1095--------------------------------------------------------------------------------
1096−localEndPoints :: Lens ValidTransportState (Map EndPointAddress LocalEndPoint)
1097+localEndPoints :: Accessor ValidTransportState (Map EndPointAddress LocalEndPoint)
1097−localEndPoints = lens _localEndPoints (\es st -> st { _localEndPoints = es })
1098+localEndPoints = accessor _localEndPoints (\es st -> st { _localEndPoints = es })
1099−pendingCtrlRequests :: Lens ValidLocalEndPointState (IntMap (MVar [ByteString]))
1100+pendingCtrlRequests :: Accessor ValidLocalEndPointState (IntMap (MVar [ByteString]))
1100−pendingCtrlRequests = lens _pendingCtrlRequests (\rep st -> st { _pendingCtrlRequests = rep })
1101+pendingCtrlRequests = accessor _pendingCtrlRequests (\rep st -> st { _pendingCtrlRequests = rep })
1102−nextCtrlRequestId :: Lens ValidLocalEndPointState ControlRequestId
1103+nextCtrlRequestId :: Accessor ValidLocalEndPointState ControlRequestId
1103−nextCtrlRequestId = lens _nextCtrlRequestId (\cid st -> st { _nextCtrlRequestId = cid })
1104+nextCtrlRequestId = accessor _nextCtrlRequestId (\cid st -> st { _nextCtrlRequestId = cid })
1105−nextConnectionId :: Lens ValidLocalEndPointState ConnectionId
1106+nextConnectionId :: Accessor ValidLocalEndPointState ConnectionId
1106−nextConnectionId = lens _nextConnectionId (\cix st -> st { _nextConnectionId = cix })
1107+nextConnectionId = accessor _nextConnectionId (\cix st -> st { _nextConnectionId = cix })
1108−localConnections :: Lens ValidLocalEndPointState (Map EndPointAddress RemoteEndPoint)
1109+localConnections :: Accessor ValidLocalEndPointState (Map EndPointAddress RemoteEndPoint)
1109−localConnections = lens _localConnections (\es st -> st { _localConnections = es })
1110+localConnections = accessor _localConnections (\es st -> st { _localConnections = es })
1111−internalThreads :: Lens ValidLocalEndPointState [ThreadId]
1112+internalThreads :: Accessor ValidLocalEndPointState [ThreadId]
1112−internalThreads = lens _internalThreads (\ts st -> st { _internalThreads = ts })
1113+internalThreads = accessor _internalThreads (\ts st -> st { _internalThreads = ts })
1114−localEndPointAt :: EndPointAddress -> Lens ValidTransportState (Maybe LocalEndPoint)
1115+localEndPointAt :: EndPointAddress -> Accessor ValidTransportState (Maybe LocalEndPoint)
1115−localEndPointAt addr = localEndPoints >>> mapLens addr
1116+localEndPointAt addr = localEndPoints >>> DAC.mapMaybe addr
1117−pendingCtrlRequestsAt :: ControlRequestId -> Lens ValidLocalEndPointState (Maybe (MVar [ByteString]))
1118+pendingCtrlRequestsAt :: ControlRequestId -> Accessor ValidLocalEndPointState (Maybe (MVar [ByteString]))
1118−pendingCtrlRequestsAt ix = pendingCtrlRequests >>> intMapLens (fromIntegral ix)
1119+pendingCtrlRequestsAt ix = pendingCtrlRequests >>> DAC.intMapMaybe (fromIntegral ix)
1120−localConnectionTo :: EndPointAddress -> Lens ValidLocalEndPointState (Maybe RemoteEndPoint)
1121+localConnectionTo :: EndPointAddress -> Accessor ValidLocalEndPointState (Maybe RemoteEndPoint)
1121−localConnectionTo addr = localConnections >>> mapLens addr
1122+localConnectionTo addr = localConnections >>> DAC.mapMaybe addr
1123−remoteOutgoing :: Lens ValidRemoteEndPointState Int
1124+remoteOutgoing :: Accessor ValidRemoteEndPointState Int
1124−remoteOutgoing = lens _remoteOutgoing (\cs conn -> conn { _remoteOutgoing = cs })
1125+remoteOutgoing = accessor _remoteOutgoing (\cs conn -> conn { _remoteOutgoing = cs })
1126−remoteIncoming :: Lens ValidRemoteEndPointState IntSet
1127+remoteIncoming :: Accessor ValidRemoteEndPointState IntSet
1127−remoteIncoming = lens _remoteIncoming (\cs conn -> conn { _remoteIncoming = cs })
1128+remoteIncoming = accessor _remoteIncoming (\cs conn -> conn { _remoteIncoming = cs })