forked from schmittlauch/Hash2Pub
fix parser format inconsistencies
This commit is contained in:
parent
28254a9f83
commit
2f10ea6628
|
@ -20,6 +20,8 @@ import Hash2Pub.FediChord
|
||||||
import Hash2Pub.Utils
|
import Hash2Pub.Utils
|
||||||
import Hash2Pub.DHTProtocol (QueryResponse (..))
|
import Hash2Pub.DHTProtocol (QueryResponse (..))
|
||||||
|
|
||||||
|
import Debug.Trace (trace)
|
||||||
|
|
||||||
data Action =
|
data Action =
|
||||||
QueryID
|
QueryID
|
||||||
| Join
|
| Join
|
||||||
|
@ -270,6 +272,7 @@ encodeMessage
|
||||||
IntVal parts
|
IntVal parts
|
||||||
, IntVal part ]
|
, IntVal part ]
|
||||||
++ encodePayload requestPayload
|
++ encodePayload requestPayload
|
||||||
|
++ [End Sequence]
|
||||||
encodeMessage
|
encodeMessage
|
||||||
(Response responseTo senderID parts part action responsePayload) = [
|
(Response responseTo senderID parts part action responsePayload) = [
|
||||||
Start Sequence
|
Start Sequence
|
||||||
|
@ -279,6 +282,7 @@ encodeMessage
|
||||||
, IntVal part
|
, IntVal part
|
||||||
, Enumerated . fromIntegral . fromEnum $ action]
|
, Enumerated . fromIntegral . fromEnum $ action]
|
||||||
++ encodePayload responsePayload
|
++ encodePayload responsePayload
|
||||||
|
++ [End Sequence]
|
||||||
|
|
||||||
-- ===== parser combinators =====
|
-- ===== parser combinators =====
|
||||||
|
|
||||||
|
@ -290,11 +294,19 @@ parseMessage = do
|
||||||
x -> throwParseError $ "unexpected ASN.1 element " ++ show x
|
x -> throwParseError $ "unexpected ASN.1 element " ++ show x
|
||||||
-- request and response messages are distiguishable by their structure,
|
-- request and response messages are distiguishable by their structure,
|
||||||
-- see ASN.1 schema
|
-- see ASN.1 schema
|
||||||
first <- getNext
|
firstElem <- getNext
|
||||||
case first of
|
message <- case firstElem of
|
||||||
Enumerated a -> parseRequest . toEnum . fromIntegral $ a
|
Enumerated a -> parseRequest . toEnum . fromIntegral $ a
|
||||||
IntVal i -> parseResponse i
|
IntVal i -> parseResponse i
|
||||||
other -> throwParseError $ "unexpected first ASN1 element: " ++ show other
|
other -> throwParseError $ "unexpected first ASN1 element: " ++ show other
|
||||||
|
-- consume sequence end
|
||||||
|
end <- getNext
|
||||||
|
case end of
|
||||||
|
End Sequence -> return ()
|
||||||
|
x -> throwParseError $ "unexpected ASN.1 element " ++ show x
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
parseRequest :: Action -> ParseASN1 FediChordMessage
|
parseRequest :: Action -> ParseASN1 FediChordMessage
|
||||||
parseRequest action = do
|
parseRequest action = do
|
||||||
|
@ -309,11 +321,6 @@ parseRequest action = do
|
||||||
Leave -> parseLeaveRequest
|
Leave -> parseLeaveRequest
|
||||||
Stabilise -> parseStabiliseRequest
|
Stabilise -> parseStabiliseRequest
|
||||||
Ping -> parsePingRequest
|
Ping -> parsePingRequest
|
||||||
-- consume sequence end
|
|
||||||
end <- getNext
|
|
||||||
case end of
|
|
||||||
End Sequence -> return ()
|
|
||||||
x -> throwParseError $ "unexpected ASN.1 element " ++ show x
|
|
||||||
|
|
||||||
return $ Request requestID sender parts part action payload
|
return $ Request requestID sender parts part action payload
|
||||||
|
|
||||||
|
@ -374,7 +381,7 @@ parseNull = do
|
||||||
x -> throwParseError $ "Expected Null but got " ++ show x
|
x -> throwParseError $ "Expected Null but got " ++ show x
|
||||||
|
|
||||||
parseNodeState :: ParseASN1 NodeState
|
parseNodeState :: ParseASN1 NodeState
|
||||||
parseNodeState = do
|
parseNodeState = onNextContainer Sequence $ do
|
||||||
nid' <- fromInteger <$> parseInteger
|
nid' <- fromInteger <$> parseInteger
|
||||||
domain' <- parseString
|
domain' <- parseString
|
||||||
ip' <- bsAsIpAddr <$> parseOctets
|
ip' <- bsAsIpAddr <$> parseOctets
|
||||||
|
@ -387,17 +394,19 @@ parseNodeState = do
|
||||||
, dhtPort = dhtPort'
|
, dhtPort = dhtPort'
|
||||||
, apPort = if apPort' == 0 then Nothing else Just apPort'
|
, apPort = if apPort' == 0 then Nothing else Just apPort'
|
||||||
, vServerID = vServer'
|
, vServerID = vServer'
|
||||||
|
, ipAddr = ip'
|
||||||
|
, internals = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
parseCacheEntry :: ParseASN1 CacheEntry
|
parseCacheEntry :: ParseASN1 CacheEntry
|
||||||
parseCacheEntry = do
|
parseCacheEntry = onNextContainer Sequence $ do
|
||||||
node <- parseNodeState
|
node <- parseNodeState
|
||||||
timestamp <- toEnum . fromIntegral <$> parseInteger
|
timestamp <- toEnum . fromIntegral <$> parseInteger
|
||||||
return $ NodeEntry False node timestamp
|
return $ NodeEntry False node timestamp
|
||||||
|
|
||||||
parseNodeCache :: ParseASN1 [CacheEntry]
|
parseNodeCache :: ParseASN1 [CacheEntry]
|
||||||
parseNodeCache = getMany parseCacheEntry
|
parseNodeCache = onNextContainer Sequence $ getMany parseCacheEntry
|
||||||
|
|
||||||
parseJoinRequest :: ParseASN1 ActionPayload
|
parseJoinRequest :: ParseASN1 ActionPayload
|
||||||
parseJoinRequest = do
|
parseJoinRequest = do
|
||||||
|
@ -430,6 +439,7 @@ parseQueryIDResponse = do
|
||||||
result <- case resultType of
|
result <- case resultType of
|
||||||
0 -> FOUND <$> parseNodeState
|
0 -> FOUND <$> parseNodeState
|
||||||
1 -> FORWARD . Set.fromList <$> parseNodeCache
|
1 -> FORWARD . Set.fromList <$> parseNodeCache
|
||||||
|
_ -> throwParseError "invalid QueryIDResponse type"
|
||||||
return $ QueryIDResponsePayload {
|
return $ QueryIDResponsePayload {
|
||||||
queryResult = result
|
queryResult = result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue