diff options
Diffstat (limited to 'src/Network/KRPC/Message.hs')
-rw-r--r-- | src/Network/KRPC/Message.hs | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/src/Network/KRPC/Message.hs b/src/Network/KRPC/Message.hs index d48fa8ac..19f9fc9e 100644 --- a/src/Network/KRPC/Message.hs +++ b/src/Network/KRPC/Message.hs | |||
@@ -13,14 +13,16 @@ | |||
13 | -- See <http://www.bittorrent.org/beps/bep_0005.html#krpc-protocol> | 13 | -- See <http://www.bittorrent.org/beps/bep_0005.html#krpc-protocol> |
14 | -- | 14 | -- |
15 | {-# LANGUAGE CPP #-} | 15 | {-# LANGUAGE CPP #-} |
16 | {-# LANGUAGE OverloadedStrings #-} | 16 | {-# LANGUAGE DefaultSignatures #-} |
17 | {-# LANGUAGE DeriveDataTypeable #-} | ||
18 | {-# LANGUAGE DeriveFunctor #-} | ||
19 | {-# LANGUAGE DeriveTraversable #-} | ||
17 | {-# LANGUAGE FlexibleContexts #-} | 20 | {-# LANGUAGE FlexibleContexts #-} |
18 | {-# LANGUAGE FlexibleInstances #-} | 21 | {-# LANGUAGE FlexibleInstances #-} |
19 | {-# LANGUAGE TypeSynonymInstances #-} | ||
20 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
21 | {-# LANGUAGE FunctionalDependencies #-} | 22 | {-# LANGUAGE FunctionalDependencies #-} |
22 | {-# LANGUAGE DefaultSignatures #-} | 23 | {-# LANGUAGE MultiParamTypeClasses #-} |
23 | {-# LANGUAGE DeriveDataTypeable #-} | 24 | {-# LANGUAGE OverloadedStrings #-} |
25 | {-# LANGUAGE TypeSynonymInstances #-} | ||
24 | module Network.KRPC.Message | 26 | module Network.KRPC.Message |
25 | ( -- * Transaction | 27 | ( -- * Transaction |
26 | TransactionId | 28 | TransactionId |
@@ -32,7 +34,8 @@ module Network.KRPC.Message | |||
32 | , unknownMessage | 34 | , unknownMessage |
33 | 35 | ||
34 | -- * Query | 36 | -- * Query |
35 | , KQuery(..) | 37 | , KQueryOf(..) |
38 | , KQuery | ||
36 | #ifndef VERSION_bencoding | 39 | #ifndef VERSION_bencoding |
37 | , queryArgs | 40 | , queryArgs |
38 | , queryMethod | 41 | , queryMethod |
@@ -41,11 +44,13 @@ module Network.KRPC.Message | |||
41 | , MethodName | 44 | , MethodName |
42 | 45 | ||
43 | -- * Response | 46 | -- * Response |
44 | , KResponse(..) | 47 | , KResponseOf(..) |
48 | , KResponse | ||
45 | , ReflectedIP(..) | 49 | , ReflectedIP(..) |
46 | 50 | ||
47 | -- * Message | 51 | -- * Message |
48 | , KMessage (..) | 52 | , KMessageOf (..) |
53 | , KMessage | ||
49 | , KQueryArgs | 54 | , KQueryArgs |
50 | 55 | ||
51 | ) where | 56 | ) where |
@@ -208,11 +213,13 @@ type KQueryArgs = ByteString -- msgPayload | |||
208 | -- callee and pass arguments in. Therefore query may be only sent from | 213 | -- callee and pass arguments in. Therefore query may be only sent from |
209 | -- client to server but not in the opposite direction. | 214 | -- client to server but not in the opposite direction. |
210 | -- | 215 | -- |
211 | data KQuery = KQuery | 216 | data KQueryOf a = KQuery |
212 | { queryArgs :: !KQueryArgs -- ^ values to be passed to method; | 217 | { queryArgs :: !a -- ^ values to be passed to method; |
213 | , queryMethod :: !MethodName -- ^ method to call; | 218 | , queryMethod :: !MethodName -- ^ method to call; |
214 | , queryId :: !TransactionId -- ^ one-time query token. | 219 | , queryId :: !TransactionId -- ^ one-time query token. |
215 | } deriving ( Show, Eq, Ord, Typeable, Read ) | 220 | } deriving ( Show, Eq, Ord, Typeable, Read, Functor, Foldable, Traversable ) |
221 | |||
222 | type KQuery = KQueryOf KQueryArgs | ||
216 | 223 | ||
217 | -- | Queries, or KRPC message dictionaries with a \"y\" value of | 224 | -- | Queries, or KRPC message dictionaries with a \"y\" value of |
218 | -- \"q\", contain two additional keys; \"q\" and \"a\". Key \"q\" has | 225 | -- \"q\", contain two additional keys; \"q\" and \"a\". Key \"q\" has |
@@ -223,7 +230,7 @@ data KQuery = KQuery | |||
223 | -- | 230 | -- |
224 | -- > { "t" : "aa", "y" : "q", "q" : "ping", "a" : { "msg" : "hi!" } } | 231 | -- > { "t" : "aa", "y" : "q", "q" : "ping", "a" : { "msg" : "hi!" } } |
225 | -- | 232 | -- |
226 | instance BEncode KQuery where | 233 | instance (Typeable a, BEncode a) => BEncode (KQueryOf a) where |
227 | toBEncode KQuery {..} = toDict $ | 234 | toBEncode KQuery {..} = toDict $ |
228 | "a" .=! queryArgs | 235 | "a" .=! queryArgs |
229 | .: "q" .=! queryMethod | 236 | .: "q" .=! queryMethod |
@@ -288,11 +295,13 @@ encodeAddr _ = B.empty | |||
288 | -- * KResponse can be only sent from server to client. | 295 | -- * KResponse can be only sent from server to client. |
289 | -- | 296 | -- |
290 | #ifdef VERSION_bencoding | 297 | #ifdef VERSION_bencoding |
291 | data KResponse = KResponse | 298 | data KResponseOf a = KResponse |
292 | { respVals :: KQueryArgs -- ^ 'BDict' containing return values; | 299 | { respVals :: a -- ^ 'BDict' containing return values; |
293 | , respId :: TransactionId -- ^ match to the corresponding 'queryId'. | 300 | , respId :: TransactionId -- ^ match to the corresponding 'queryId'. |
294 | , respIP :: Maybe ReflectedIP | 301 | , respIP :: Maybe ReflectedIP |
295 | } deriving (Show, Eq, Ord, Typeable) | 302 | } deriving (Show, Eq, Ord, Typeable, Functor, Foldable, Traversable) |
303 | |||
304 | type KResponse = KResponseOf KQueryArgs | ||
296 | 305 | ||
297 | -- | Responses, or KRPC message dictionaries with a \"y\" value of | 306 | -- | Responses, or KRPC message dictionaries with a \"y\" value of |
298 | -- \"r\", contain one additional key \"r\". The value of \"r\" is a | 307 | -- \"r\", contain one additional key \"r\". The value of \"r\" is a |
@@ -302,7 +311,7 @@ data KResponse = KResponse | |||
302 | -- | 311 | -- |
303 | -- > { "t" : "aa", "y" : "r", "r" : { "msg" : "you've sent: hi!" } } | 312 | -- > { "t" : "aa", "y" : "r", "r" : { "msg" : "you've sent: hi!" } } |
304 | -- | 313 | -- |
305 | instance BEncode KResponse where | 314 | instance (Typeable a, BEncode a) => BEncode (KResponseOf a) where |
306 | toBEncode KResponse {..} = toDict $ | 315 | toBEncode KResponse {..} = toDict $ |
307 | "ip" .=? respIP | 316 | "ip" .=? respIP |
308 | .: "r" .=! respVals | 317 | .: "r" .=! respVals |
@@ -329,11 +338,13 @@ respIP = Nothing :: Maybe ReflectedIP | |||
329 | 338 | ||
330 | #ifdef VERSION_bencoding | 339 | #ifdef VERSION_bencoding |
331 | -- | Generic KRPC message. | 340 | -- | Generic KRPC message. |
332 | data KMessage | 341 | data KMessageOf a |
333 | = Q KQuery | 342 | = Q (KQueryOf a) |
334 | | R KResponse | 343 | | R (KResponseOf a) |
335 | | E KError | 344 | | E KError |
336 | deriving (Show, Eq) | 345 | deriving (Show, Eq, Functor, Foldable, Traversable) |
346 | |||
347 | type KMessage = KMessageOf KQueryArgs | ||
337 | 348 | ||
338 | instance BEncode KMessage where | 349 | instance BEncode KMessage where |
339 | toBEncode (Q q) = toBEncode q | 350 | toBEncode (Q q) = toBEncode q |