diff --git a/Hash2Pub/FediChord.asn1 b/Hash2Pub/FediChord.asn1 index e2a7ab5..7c53cb0 100644 --- a/Hash2Pub/FediChord.asn1 +++ b/Hash2Pub/FediChord.asn1 @@ -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 { diff --git a/Hash2Pub/src/Hash2Pub/ASN1Coding.hs b/Hash2Pub/src/Hash2Pub/ASN1Coding.hs index 47d91f1..ed20e1d 100644 --- a/Hash2Pub/src/Hash2Pub/ASN1Coding.hs +++ b/Hash2Pub/src/Hash2Pub/ASN1Coding.hs @@ -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 diff --git a/Hash2Pub/src/Hash2Pub/DHTProtocol.hs b/Hash2Pub/src/Hash2Pub/DHTProtocol.hs index f61cfd8..d02b312 100644 --- a/Hash2Pub/src/Hash2Pub/DHTProtocol.hs +++ b/Hash2Pub/src/Hash2Pub/DHTProtocol.hs @@ -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 = diff --git a/Hash2Pub/test/FediChordSpec.hs b/Hash2Pub/test/FediChordSpec.hs index 363aa54..e72b516 100644 --- a/Hash2Pub/test/FediChordSpec.hs +++ b/Hash2Pub/test/FediChordSpec.hs @@ -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