FediChordMessage: last part has falg instead of parts number in each msg

Motivation: Including the number of parts in each message part requires
the total number of parts to be known in advance, making dynamic
responses based on the received data difficult
This commit is contained in:
Trolli Schmittlauch 2020-05-30 13:07:28 +02:00
parent fea9660f80
commit f8d444d5b6
3 changed files with 25 additions and 17 deletions

View file

@ -4,14 +4,16 @@ NodeID ::= INTEGER (0..115792089237316195423570985008687907853269984665640564039
Domain ::= VisibleString
Partnum ::= INTEGER (0..150)
Action ::= ENUMERATED {queryID, join, leave, stabilise, ping}
Request ::= SEQUENCE {
action Action,
requestID INTEGER (0..4294967295), -- arbitrarily restricting to an unsigned 32bit integer
sender NodeState,
parts INTEGER (1..150), -- number of message parts
part INTEGER (1..150), -- part number of this message, starts at 1
part Partnum, -- part number of this message, starts at 1
finalPart BOOLEAN, -- flag indicating this `part` to be the last of this reuest
actionPayload CHOICE {
queryIDRequestPayload QueryIDRequestPayload,
joinRequestPayload JoinRequestPayload,
@ -27,8 +29,8 @@ Request ::= SEQUENCE {
Response ::= SEQUENCE {
responseTo INTEGER (0..4294967295), -- arbitrarily restricting to an unsigned 32bit integer
senderID NodeID,
parts INTEGER (0..150),
part INTEGER (0..150),
part Partnum,
finalPart BOOLEAN, -- flag indicating this `part` to be the last of this response
action Action,
actionPayload CHOICE {
queryIDResponsePayload QueryIDResponsePayload,

View file

@ -103,7 +103,7 @@ serialiseMessage maxBytesLength msg =
modifyMessage i (partNum, pl) pls = (partNum, msg {
part = partNum
, payload = Just pl
, parts = fromIntegral i
, isFinalPart = partNum == fromIntegral i
}):pls
-- part starts at 1
payloadParts :: Int -> Maybe [(Integer, ActionPayload)]
@ -216,23 +216,22 @@ encodeQueryResult FORWARD{} = Enumerated 1
encodeMessage :: FediChordMessage -- ^ the 'FediChordMessage to be encoded
-> [ASN1]
encodeMessage
(Request requestID sender parts part action requestPayload) =
(Request requestID sender part isFinalPart action requestPayload) =
Start Sequence
: (Enumerated . fromIntegral . fromEnum $ action)
: IntVal requestID
: encodeNodeState sender
<> [
IntVal parts
, IntVal part ]
<> [IntVal part
, Boolean isFinalPart]
<> maybe [] encodePayload requestPayload
<> [End Sequence]
encodeMessage
(Response responseTo senderID parts part action responsePayload) = [
(Response responseTo senderID part isFinalPart action responsePayload) = [
Start Sequence
, IntVal responseTo
, IntVal . getNodeID $ senderID
, IntVal parts
, IntVal part
, Boolean isFinalPart
, Enumerated . fromIntegral . fromEnum $ action]
<> maybe [] encodePayload responsePayload
<> [End Sequence]
@ -265,8 +264,8 @@ parseRequest :: Action -> ParseASN1 FediChordMessage
parseRequest action = do
requestID <- parseInteger
sender <- parseNodeState
parts <- parseInteger
part <- parseInteger
isFinalPart <- parseBool
hasPayload <- hasNext
payload <- if not hasPayload then pure Nothing else Just <$> case action of
QueryID -> parseQueryIDRequest
@ -275,13 +274,13 @@ parseRequest action = do
Stabilise -> parseStabiliseRequest
Ping -> parsePingRequest
pure $ Request requestID sender parts part action payload
pure $ Request requestID sender part isFinalPart action payload
parseResponse :: Integer -> ParseASN1 FediChordMessage
parseResponse responseTo = do
senderID <- fromInteger <$> parseInteger :: ParseASN1 NodeID
parts <- parseInteger
part <- parseInteger
isFinalPart <- parseBool
action <- parseEnum :: ParseASN1 Action
hasPayload <- hasNext
payload <- if not hasPayload then pure Nothing else Just <$> case action of
@ -291,7 +290,14 @@ parseResponse responseTo = do
Stabilise -> parseStabiliseResponse
Ping -> parsePingResponse
pure $ Response responseTo senderID parts part action payload
pure $ Response responseTo senderID part isFinalPart action payload
parseBool :: ParseASN1 Bool
parseBool = do
i <- getNext
case i of
Boolean parsed -> pure parsed
x -> throwParseError $ "Expected Boolean but got " <> show x
parseInteger :: ParseASN1 Integer
parseInteger = do

View file

@ -21,8 +21,8 @@ data Action = QueryID
data FediChordMessage = Request
{ requestID :: Integer
, sender :: RemoteNodeState
, parts :: Integer
, part :: Integer
, isFinalPart :: Bool
-- ^ part starts at 1
, action :: Action
, payload :: Maybe ActionPayload
@ -30,8 +30,8 @@ data FediChordMessage = Request
| Response
{ responseTo :: Integer
, senderID :: NodeID
, parts :: Integer
, part :: Integer
, isFinalPart :: Bool
, action :: Action
, payload :: Maybe ActionPayload
}