allow messages without payload for ACKing

This commit is contained in:
Trolli Schmittlauch 2020-05-15 22:40:20 +02:00
parent 2b234d65db
commit c304e2103f
4 changed files with 12 additions and 10 deletions

View file

@ -18,7 +18,7 @@ Request ::= SEQUENCE {
leaveRequestPayload LeaveRequestPayload,
stabiliseRequestPayload StabiliseRequestPayload,
pingRequestPayload PingRequestPayload
}
} OPTIONAL -- just for symmetry reasons with response, requests without a payload have no meaning
}
-- idea: different message layouts as a way of distinguishing between
@ -36,7 +36,7 @@ Response ::= SEQUENCE {
leaveResponsePayload LeaveResponsePayload,
stabiliseResponsePayload StabiliseResponsePayload,
pingResponsePayload PingResponsePayload
}
} OPTIONAL -- no payload when just ACKing a previous request
}
NodeState ::= SEQUENCE {

View file

@ -215,7 +215,7 @@ encodeMessage
++ [
IntVal parts
, IntVal part ]
++ encodePayload requestPayload
++ maybe [] encodePayload requestPayload
++ [End Sequence]
encodeMessage
(Response responseTo senderID parts part action responsePayload) = [
@ -225,7 +225,7 @@ encodeMessage
, IntVal parts
, IntVal part
, Enumerated . fromIntegral . fromEnum $ action]
++ encodePayload responsePayload
++ maybe [] encodePayload responsePayload
++ [End Sequence]
-- ===== parser combinators =====
@ -258,7 +258,8 @@ parseRequest action = do
sender <- parseNodeState
parts <- parseInteger
part <- parseInteger
payload <- case action of
hasPayload <- hasNext
payload <- if not hasPayload then return Nothing else Just <$> case action of
QueryID -> parseQueryIDRequest
Join -> parseJoinRequest
Leave -> parseLeaveRequest
@ -273,7 +274,8 @@ parseResponse responseTo = do
parts <- parseInteger
part <- parseInteger
action <- parseEnum :: ParseASN1 Action
payload <- case action of
hasPayload <- hasNext
payload <- if not hasPayload then return Nothing else Just <$> case action of
QueryID -> parseQueryIDResponse
Join -> parseJoinResponse
Leave -> parseLeaveResponse

View file

@ -97,7 +97,7 @@ data FediChordMessage =
, part :: Integer
-- ^ part starts at 0
, action :: Action
, payload :: ActionPayload
, payload :: Maybe ActionPayload
}
| Response {
responseTo :: Integer
@ -105,7 +105,7 @@ data FediChordMessage =
, parts :: Integer
, part :: Integer
, action :: Action
, payload :: ActionPayload
, payload :: Maybe ActionPayload
} deriving (Show, Eq)
data ActionPayload =

View file

@ -212,8 +212,8 @@ spec = do
, action = undefined
, payload = undefined
}
requestWith a pa = requestTemplate {action = a, payload = pa}
responseWith a pa = responseTemplate {action = a, payload = pa}
requestWith a pa = requestTemplate {action = a, payload = Just pa}
responseWith a pa = responseTemplate {action = a, payload = Just pa}
encodeDecodeAndCheck msg = runParseASN1 parseMessage (encodeMessage msg) `shouldBe` pure msg
it "messages are encoded and decoded correctly from and to ASN1" $ do