From 53308080db63fb4e30dcc35e672b1e2a40c1cf41 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Sat, 6 Jun 2020 17:31:20 +0200 Subject: [PATCH] respond to Stabilise requests --- src/Hash2Pub/DHTProtocol.hs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Hash2Pub/DHTProtocol.hs b/src/Hash2Pub/DHTProtocol.hs index 983fdc2..2df852b 100644 --- a/src/Hash2Pub/DHTProtocol.hs +++ b/src/Hash2Pub/DHTProtocol.hs @@ -195,22 +195,44 @@ handleIncomingRequest nsSTM sendQ msgSet sourceAddr = do -- -- ToDo: figure out what happens if not joined -- QueryID -> Just <$> respondQueryID nsSTM msgSet -- -- only when joined --- Leave -> if isJoined_ ns then Just <$> respondLeave nsSTM msgSet else Nothing +-- Leave -> if isJoined_ ns then Just <$> respondLeave nsSTM msgSet else pure Nothing -- -- only when joined --- Stabilise -> if isJoined_ ns then Just <$> respondStabilise nsSTM msgSet else Nothing --- ) + Stabilise -> if isJoined_ ns then Just <$> respondStabilise nsSTM msgSet else pure Nothing + ) -- for single part request, response starts with part number 1. For multipart requests, response starts with part number n+1. -- TODO: determine request type only from first part, but catch RecSelError on each record access when folding, because otherwise different request type parts can make this crash -- TODO: test case: mixed message types of parts - ) -- placeholder - -- ....... response sending ....... -- TODO: could all these respond* functions be in STM instead of IO? + +-- | respond to stabilise requests by returning successor and predecessor list +respondStabilise :: LocalNodeStateSTM -> Set.Set FediChordMessage -> IO (Map.Map Integer BS.ByteString) +respondStabilise nsSTM msgSet = do + nsSnap <- readTVarIO nsSTM + let + aRequestPart = Set.elemAt 0 msgSet + responsePayload = StabiliseResponsePayload { + stabiliseSuccessors = successors nsSnap + , stabilisePredecessors = predecessors nsSnap + } + stabiliseResponse = Response { + responseTo = requestID aRequestPart + , senderID = getNid nsSnap + , part = if Set.size msgSet == 1 then 1 else fromIntegral $ Set.size msgSet + 1 + , isFinalPart = False + , action = Stabilise + , payload = Just responsePayload + } + -- TODO: return service endpoint for copying over key data + pure $ serialiseMessage sendMessageSize stabiliseResponse + + +-- | respond to Ping request by returning all active vserver NodeStates respondPing :: LocalNodeStateSTM -> Set.Set FediChordMessage -> IO (Map.Map Integer BS.ByteString) respondPing nsSTM msgSet = do -- TODO: respond with all active VS when implementing k-choices