respond to and handle Leave requests

contributes to #28
This commit is contained in:
Trolli Schmittlauch 2020-06-06 18:02:40 +02:00
parent 53308080db
commit e00da9b84f

View file

@ -38,7 +38,7 @@ import Data.Foldable (foldl', foldr')
import Data.Functor.Identity import Data.Functor.Identity
import Data.IP (IPv6, fromHostAddress6, import Data.IP (IPv6, fromHostAddress6,
toHostAddress6) toHostAddress6)
import Data.List (sortBy) import Data.List (delete, sortBy)
import qualified Data.Map as Map import qualified Data.Map as Map
import Data.Maybe (fromJust, fromMaybe, mapMaybe, import Data.Maybe (fromJust, fromMaybe, mapMaybe,
maybe) maybe)
@ -209,6 +209,32 @@ handleIncomingRequest nsSTM sendQ msgSet sourceAddr = do
-- TODO: could all these respond* functions be in STM instead of IO? -- TODO: could all these respond* functions be in STM instead of IO?
-- | Respond to a Leave request by removing the leaving node from local data structures
-- and confirming with response.
-- TODO: copy over key data from leaver and confirm
respondLeave :: LocalNodeStateSTM -> Set.Set FediChordMessage -> IO (Map.Map Integer BS.ByteString)
respondLeave nsSTM msgSet = do
responseMsg <- atomically $ do
nsSnap <- readTVar nsSTM
let
aRequestPart = Set.elemAt 0 msgSet
senderID = getNid . sender $ aRequestPart
-- remove leaving node from successors, predecessors and NodeCache
writeTQueue (cacheWriteQueue nsSnap) $ deleteCacheEntry senderID
writeTVar nsSTM $
setPredecessors (delete senderID $ predecessors nsSnap)
. setSuccessors (delete senderID $ successors nsSnap) $ nsSnap
-- TODO: handle handover of key data
let leaveResponse = Response {
responseTo = requestID aRequestPart
, senderID = getNid nsSnap
, part = if Set.size msgSet == 1 then 1 else fromIntegral $ Set.size msgSet + 1
, isFinalPart = False
, action = Leave
, payload = Just LeaveResponsePayload
}
pure leaveResponse
pure $ serialiseMessage sendMessageSize responseMsg
-- | respond to stabilise requests by returning successor and predecessor list -- | respond to stabilise requests by returning successor and predecessor list
respondStabilise :: LocalNodeStateSTM -> Set.Set FediChordMessage -> IO (Map.Map Integer BS.ByteString) respondStabilise :: LocalNodeStateSTM -> Set.Set FediChordMessage -> IO (Map.Map Integer BS.ByteString)