diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-26 09:58:23 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-26 09:58:23 +0400 |
commit | 03ea33f3a3f231e92fcb11808185ae8d059f40d1 (patch) | |
tree | 5e24497dd835603b38084591a78ca5c934c031d8 /src/Network | |
parent | a4ffcfc78e4e7515b5fb270806e484c6cc38302b (diff) |
Fix node info list serialization
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/DHT/Message.hs | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/src/Network/BitTorrent/DHT/Message.hs b/src/Network/BitTorrent/DHT/Message.hs index 9000a9be..0733a85d 100644 --- a/src/Network/BitTorrent/DHT/Message.hs +++ b/src/Network/BitTorrent/DHT/Message.hs | |||
@@ -101,7 +101,7 @@ instance BEncode Ping where | |||
101 | fromBEncode _ = pure Ping | 101 | fromBEncode _ = pure Ping |
102 | 102 | ||
103 | -- | \"q\" = \"ping\" | 103 | -- | \"q\" = \"ping\" |
104 | instance KRPC (Query Ping) [Ping] where | 104 | instance KRPC (Query Ping) (Response Ping) where |
105 | method = "ping" | 105 | method = "ping" |
106 | 106 | ||
107 | {----------------------------------------------------------------------- | 107 | {----------------------------------------------------------------------- |
@@ -130,15 +130,18 @@ newtype NodeFound ip = NodeFound [NodeInfo ip] | |||
130 | nodes_key :: BKey | 130 | nodes_key :: BKey |
131 | nodes_key = "nodes" | 131 | nodes_key = "nodes" |
132 | 132 | ||
133 | binary :: Serialize a => BE.Get BS.ByteString -> BE.Get a | 133 | binary :: Serialize a => BKey -> BE.Get [a] |
134 | binary m = m >>= either fail return . S.decode | 134 | binary k = field (req k) >>= either (fail . format) return . |
135 | runGet (many get) | ||
136 | where | ||
137 | format str = "fail to deserialize " ++ show k ++ " field: " ++ str | ||
135 | 138 | ||
136 | instance (Typeable ip, Serialize ip) => BEncode (NodeFound ip) where | 139 | instance (Typeable ip, Serialize ip) => BEncode (NodeFound ip) where |
137 | toBEncode (NodeFound ns) = toDict $ | 140 | toBEncode (NodeFound ns) = toDict $ |
138 | nodes_key .=! S.encode ns | 141 | nodes_key .=! S.encode ns |
139 | .: endDict | 142 | .: endDict |
140 | 143 | ||
141 | fromBEncode = fromDict $ NodeFound <$> binary (field (req nodes_key)) | 144 | fromBEncode = fromDict $ NodeFound <$> binary nodes_key |
142 | 145 | ||
143 | -- | \"q\" == \"find_node\" | 146 | -- | \"q\" == \"find_node\" |
144 | instance (Serialize ip, Typeable ip) | 147 | instance (Serialize ip, Typeable ip) |
@@ -188,8 +191,8 @@ instance (Typeable ip, Serialize ip) => BEncode (GotPeers ip) where | |||
188 | 191 | ||
189 | fromBEncode = fromDict $ GotPeers <$> getPeerList <*>! token_key | 192 | fromBEncode = fromDict $ GotPeers <$> getPeerList <*>! token_key |
190 | where | 193 | where |
191 | getPeerList = Right <$> binary (field (req peers_key)) | 194 | getPeerList = Right <$> binary peers_key |
192 | <|> Left <$> binary (field (req nodes_key)) | 195 | <|> Left <$> binary nodes_key |
193 | 196 | ||
194 | instance (Typeable ip, Serialize ip) => | 197 | instance (Typeable ip, Serialize ip) => |
195 | KRPC (Query GetPeers) (Response (GotPeers ip)) where | 198 | KRPC (Query GetPeers) (Response (GotPeers ip)) where |