diff options
author | joe <joe@jerkface.net> | 2017-06-04 22:39:14 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-06-04 22:39:14 -0400 |
commit | 219d72ebde4bab5a516a86608dcb3aede75c1611 (patch) | |
tree | df111d38c3532b9342f30c1bad98ef095569d54f /src/Network/KRPC/Method.hs | |
parent | 713cee07450697e40811e74059739da02dd604c7 (diff) |
WIP: Adapting DHT to Tox network.
Diffstat (limited to 'src/Network/KRPC/Method.hs')
-rw-r--r-- | src/Network/KRPC/Method.hs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/Network/KRPC/Method.hs b/src/Network/KRPC/Method.hs index 916b38a8..2a791924 100644 --- a/src/Network/KRPC/Method.hs +++ b/src/Network/KRPC/Method.hs | |||
@@ -7,6 +7,7 @@ | |||
7 | -- | 7 | -- |
8 | -- Normally, you don't need to import this module. | 8 | -- Normally, you don't need to import this module. |
9 | -- | 9 | -- |
10 | {-# LANGUAGE CPP #-} | ||
10 | {-# LANGUAGE RankNTypes #-} | 11 | {-# LANGUAGE RankNTypes #-} |
11 | {-# LANGUAGE MultiParamTypeClasses #-} | 12 | {-# LANGUAGE MultiParamTypeClasses #-} |
12 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 13 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
@@ -17,7 +18,11 @@ module Network.KRPC.Method | |||
17 | , KRPC (..) | 18 | , KRPC (..) |
18 | ) where | 19 | ) where |
19 | 20 | ||
21 | #ifdef VERSION_bencoding | ||
20 | import Data.BEncode (BEncode) | 22 | import Data.BEncode (BEncode) |
23 | #else | ||
24 | import Data.Serialize | ||
25 | #endif | ||
21 | import Data.ByteString.Char8 as BC | 26 | import Data.ByteString.Char8 as BC |
22 | import Data.Char | 27 | import Data.Char |
23 | import Data.Monoid | 28 | import Data.Monoid |
@@ -38,7 +43,12 @@ import Network.KRPC.Message | |||
38 | -- * result: Type of return value of the method. | 43 | -- * result: Type of return value of the method. |
39 | -- | 44 | -- |
40 | newtype Method param result = Method { methodName :: MethodName } | 45 | newtype Method param result = Method { methodName :: MethodName } |
41 | deriving (Eq, Ord, IsString, BEncode) | 46 | deriving ( Eq, Ord |
47 | #ifdef VERSION_bencoding | ||
48 | , IsString | ||
49 | , BEncode | ||
50 | #endif | ||
51 | ) | ||
42 | 52 | ||
43 | -- | Example: | 53 | -- | Example: |
44 | -- | 54 | -- |
@@ -49,7 +59,11 @@ instance (Typeable a, Typeable b) => Show (Method a b) where | |||
49 | 59 | ||
50 | showsMethod :: forall a b. ( Typeable a , Typeable b ) => Method a b -> ShowS | 60 | showsMethod :: forall a b. ( Typeable a , Typeable b ) => Method a b -> ShowS |
51 | showsMethod (Method name) = | 61 | showsMethod (Method name) = |
62 | #ifdef VERSION_bencoding | ||
52 | showString (BC.unpack name) <> | 63 | showString (BC.unpack name) <> |
64 | #else | ||
65 | shows (show name) <> | ||
66 | #endif | ||
53 | showString " :: " <> | 67 | showString " :: " <> |
54 | shows paramsTy <> | 68 | shows paramsTy <> |
55 | showString " -> " <> | 69 | showString " -> " <> |
@@ -72,7 +86,13 @@ showsMethod (Method name) = | |||
72 | -- method = \"ping\" | 86 | -- method = \"ping\" |
73 | -- @ | 87 | -- @ |
74 | -- | 88 | -- |
75 | class (Typeable req, BEncode req, Typeable resp, BEncode resp) | 89 | class ( Typeable req, Typeable resp |
90 | #ifdef VERSION_bencoding | ||
91 | , BEncode req, BEncode resp | ||
92 | #else | ||
93 | , Serialize req, Serialize resp | ||
94 | #endif | ||
95 | ) | ||
76 | => KRPC req resp where | 96 | => KRPC req resp where |
77 | 97 | ||
78 | -- | Method name. Default implementation uses lowercased @req@ | 98 | -- | Method name. Default implementation uses lowercased @req@ |
@@ -80,8 +100,10 @@ class (Typeable req, BEncode req, Typeable resp, BEncode resp) | |||
80 | -- | 100 | -- |
81 | method :: Method req resp | 101 | method :: Method req resp |
82 | 102 | ||
103 | #ifdef VERSION_bencoding | ||
83 | -- TODO add underscores | 104 | -- TODO add underscores |
84 | default method :: Typeable req => Method req resp | 105 | default method :: Typeable req => Method req resp |
85 | method = Method $ fromString $ L.map toLower $ show $ typeOf hole | 106 | method = Method $ fromString $ L.map toLower $ show $ typeOf hole |
86 | where | 107 | where |
87 | hole = error "krpc.method: impossible" :: req | 108 | hole = error "krpc.method: impossible" :: req |
109 | #endif | ||