send and parse Ping requests

contributes to #29 #44
This commit is contained in:
Trolli Schmittlauch 2020-06-19 23:03:27 +02:00
parent 00ff2bf071
commit 3482876d9b

View file

@ -16,6 +16,7 @@ module Hash2Pub.DHTProtocol
, sendQueryIdMessage , sendQueryIdMessage
, requestQueryID , requestQueryID
, requestJoin , requestJoin
, requestPing
, requestStabilise , requestStabilise
, queryIdLookupLoop , queryIdLookupLoop
, resolve , resolve
@ -531,6 +532,35 @@ requestStabilise ns neighbour = do
) responses ) responses
requestPing :: LocalNodeState -- ^ sending node
-> RemoteNodeState -- ^ node to be PINGed
-> IO (Either String [RemoteNodeState]) -- ^ all active vServers of the pinged node
requestPing ns target = do
responses <- bracket (mkSendSocket (getDomain target) (getDhtPort target)) close (fmap Right . sendRequestTo 5000 3 (\rid ->
Request {
requestID = rid
, sender = toRemoteNodeState ns
, part = 1
, isFinalPart = False
, action = Ping
, payload = Just PingRequestPayload
}
)
) `catch` (\e -> pure . Left $ displayException (e :: IOException))
either
-- forward IO error messages
(pure . Left)
(\respSet -> do
-- fold all reply parts together
let responseVss = foldr' (\msg acc ->
maybe acc (foldr' (:) acc) (pingNodeStates <$> payload msg)
)
[] respSet
pure $ if null responseVss
then Left "no active vServer IDs returned, ignoring node"
else Right responseVss
) responses
-- | Generic function for sending a request over a connected socket and collecting the response. -- | Generic function for sending a request over a connected socket and collecting the response.