summaryrefslogtreecommitdiff
path: root/src/Network/KRPC
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-06-30 19:09:08 -0400
committerjoe <joe@jerkface.net>2017-06-30 19:09:08 -0400
commitb53ece2247e46d5eea9e433a54d0a833216fcc6d (patch)
tree8fa779cefba8dc978819644a53eb855d8abf9799 /src/Network/KRPC
parente1b2fc9c7a5efd828a8c66f3e3a1d0a547397080 (diff)
Bug fixes.
Diffstat (limited to 'src/Network/KRPC')
-rw-r--r--src/Network/KRPC/Method.hs43
1 files changed, 17 insertions, 26 deletions
diff --git a/src/Network/KRPC/Method.hs b/src/Network/KRPC/Method.hs
index d0eb136a..84c7fe4c 100644
--- a/src/Network/KRPC/Method.hs
+++ b/src/Network/KRPC/Method.hs
@@ -9,12 +9,14 @@
9-- 9--
10{-# LANGUAGE CPP #-} 10{-# LANGUAGE CPP #-}
11{-# LANGUAGE DefaultSignatures #-} 11{-# LANGUAGE DefaultSignatures #-}
12{-# LANGUAGE FlexibleContexts #-}
13{-# LANGUAGE FunctionalDependencies #-}
12{-# LANGUAGE GeneralizedNewtypeDeriving #-} 14{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13{-# LANGUAGE MultiParamTypeClasses #-} 15{-# LANGUAGE MultiParamTypeClasses #-}
14{-# LANGUAGE RankNTypes #-} 16{-# LANGUAGE RankNTypes #-}
15{-# LANGUAGE ScopedTypeVariables #-} 17{-# LANGUAGE ScopedTypeVariables #-}
18{-# LANGUAGE StandaloneDeriving #-}
16{-# LANGUAGE TypeFamilies #-} 19{-# LANGUAGE TypeFamilies #-}
17{-# LANGUAGE FunctionalDependencies #-}
18module Network.KRPC.Method 20module Network.KRPC.Method
19 ( Method (..) 21 ( Method (..)
20 , KRPC (..) 22 , KRPC (..)
@@ -32,6 +34,7 @@ import Data.List as L
32import Data.String 34import Data.String
33import Data.Typeable 35import Data.Typeable
34import Network.DatagramServer.Mainline 36import Network.DatagramServer.Mainline
37import Network.DatagramServer.Types
35 38
36 39
37-- | Method datatype used to describe method name, parameters and 40-- | Method datatype used to describe method name, parameters and
@@ -44,28 +47,24 @@ import Network.DatagramServer.Mainline
44-- 47--
45-- * result: Type of return value of the method. 48-- * result: Type of return value of the method.
46-- 49--
47newtype Method param result = Method { methodName :: MethodName } 50newtype Method dht param result = Method { methodName :: QueryMethod dht }
48 deriving ( Eq, Ord 51
49#ifdef VERSION_bencoding 52deriving instance Eq (QueryMethod dht) => Eq (Method dht param result)
50 , IsString 53deriving instance Ord (QueryMethod dht) => Ord (Method dht param result)
51 , BEncode 54deriving instance IsString (QueryMethod dht) => IsString (Method dht param result)
52#endif 55deriving instance BEncode (QueryMethod dht) => BEncode (Method dht param result)
53 )
54 56
55-- | Example: 57-- | Example:
56-- 58--
57-- @show (Method \"concat\" :: [Int] Int) == \"concat :: [Int] -> Int\"@ 59-- @show (Method \"concat\" :: [Int] Int) == \"concat :: [Int] -> Int\"@
58-- 60--
59instance (Typeable a, Typeable b) => Show (Method a b) where 61instance (Show (QueryMethod dht), Typeable a, Typeable b) => Show (Method dht a b) where
60 showsPrec _ = showsMethod 62 showsPrec _ = showsMethod
61 63
62showsMethod :: forall a b. ( Typeable a , Typeable b ) => Method a b -> ShowS 64showsMethod :: forall dht a b. ( Show (QueryMethod dht), Typeable a , Typeable b ) => Method dht a b -> ShowS
63showsMethod (Method name) = 65showsMethod (Method name) =
64#ifdef VERSION_bencoding 66 -- showString (BC.unpack name) <>
65 showString (BC.unpack name) <>
66#else
67 shows (show name) <> 67 shows (show name) <>
68#endif
69 showString " :: " <> 68 showString " :: " <>
70 shows paramsTy <> 69 shows paramsTy <>
71 showString " -> " <> 70 showString " -> " <>
@@ -88,24 +87,16 @@ showsMethod (Method name) =
88-- method = \"ping\" 87-- method = \"ping\"
89-- @ 88-- @
90-- 89--
91class ( Typeable req, Typeable resp 90class ( Typeable req, Typeable resp)
92-- #ifdef VERSION_bencoding 91 => KRPC dht req resp | req -> resp, resp -> req where
93 -- , BEncode req, BEncode resp
94-- #else
95 -- , Serialize req, Serialize resp
96-- #endif
97 )
98 => KRPC req resp | req -> resp, resp -> req where
99 92
100 -- | Method name. Default implementation uses lowercased @req@ 93 -- | Method name. Default implementation uses lowercased @req@
101 -- datatype name. 94 -- datatype name.
102 -- 95 --
103 method :: Method req resp 96 method :: Method dht req resp
104 97
105#ifdef VERSION_bencoding
106 -- TODO add underscores 98 -- TODO add underscores
107 default method :: Typeable req => Method req resp 99 default method :: (IsString (QueryMethod dht), Typeable req) => Method dht req resp
108 method = Method $ fromString $ L.map toLower $ show $ typeOf hole 100 method = Method $ fromString $ L.map toLower $ show $ typeOf hole
109 where 101 where
110 hole = error "krpc.method: impossible" :: req 102 hole = error "krpc.method: impossible" :: req
111#endif