summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/KRPC/Method.hs45
1 files changed, 31 insertions, 14 deletions
diff --git a/src/Network/KRPC/Method.hs b/src/Network/KRPC/Method.hs
index f4392f35..f70923f5 100644
--- a/src/Network/KRPC/Method.hs
+++ b/src/Network/KRPC/Method.hs
@@ -1,3 +1,12 @@
1-- |
2-- Copyright : (c) Sam Truzjan 2013
3-- License : BSD3
4-- Maintainer : pxqr.sta@gmail.com
5-- Stability : experimental
6-- Portability : portable
7--
8-- Normally, you don't need to import this module.
9--
1{-# LANGUAGE RankNTypes #-} 10{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE MultiParamTypeClasses #-} 11{-# LANGUAGE MultiParamTypeClasses #-}
3{-# LANGUAGE FunctionalDependencies #-} 12{-# LANGUAGE FunctionalDependencies #-}
@@ -10,6 +19,7 @@ module Network.KRPC.Method
10 ) where 19 ) where
11 20
12import Data.BEncode (BEncode) 21import Data.BEncode (BEncode)
22import Data.ByteString.Char8 as BC
13import Data.Char 23import Data.Char
14import Data.Monoid 24import Data.Monoid
15import Data.List as L 25import Data.List as L
@@ -18,31 +28,30 @@ import Data.Typeable
18import Network.KRPC.Message 28import Network.KRPC.Message
19 29
20 30
21-- | Method datatype used to describe name, parameters and return 31-- | Method datatype used to describe method name, parameters and
22-- values of procedure. Client use a method to /invoke/, server 32-- return values of procedure. Client use a method to /invoke/, server
23-- /implements/ the method to make the actual work. 33-- /implements/ the method to make the actual work.
24-- 34--
25-- We use the following fantom types to ensure type-safiety: 35-- We use the following fantom types to ensure type-safiety:
26-- 36--
27-- * param: Type of method parameters. Ordinary Tuple type used 37-- * param: Type of method parameters.
28-- to specify more than one parameter, so for example @Method
29-- (Int, Int) result@ will take two arguments.
30-- 38--
31-- * result: Type of return value of the method. Similarly, 39-- * result: Type of return value of the method.
32-- tuple used to specify more than one return value, so for
33-- exsample @Method (Foo, Bar) (Bar, Foo)@ will take two arguments
34-- and return two values.
35-- 40--
36newtype Method param result = Method MethodName 41newtype Method param result = Method MethodName
37 deriving (Eq, Ord, IsString, BEncode) 42 deriving (Eq, Ord, IsString, BEncode)
38 43
44-- | Example:
45--
46-- @show (Method \"concat\" :: [Int] Int) == \"concat :: [Int] -> Int\"@
47--
39instance (Typeable a, Typeable b) => Show (Method a b) where 48instance (Typeable a, Typeable b) => Show (Method a b) where
40 showsPrec _ = showsMethod 49 showsPrec _ = showsMethod
41 50
42showsMethod :: forall a. forall b. Typeable a => Typeable b 51showsMethod :: forall a. forall b. Typeable a => Typeable b
43 => Method a b -> ShowS 52 => Method a b -> ShowS
44showsMethod (Method name) = 53showsMethod (Method name) =
45 shows name <> 54 showString (BC.unpack name) <>
46 showString " :: " <> 55 showString " :: " <>
47 shows paramsTy <> 56 shows paramsTy <>
48 showString " -> " <> 57 showString " -> " <>
@@ -52,15 +61,23 @@ showsMethod (Method name) =
52 paramsTy = typeOf (impossible :: a) 61 paramsTy = typeOf (impossible :: a)
53 valuesTy = typeOf (impossible :: b) 62 valuesTy = typeOf (impossible :: b)
54 63
55-- | Example: 64-- | In order to perform or handle KRPC query you need to provide
65-- corresponding 'KRPC' class.
66--
67-- Example:
68--
56-- @ 69-- @
57-- data Ping = Ping Text deriving BEncode 70-- data Ping = Ping Text deriving BEncode
58-- data Pong = Pong Text deriving BEncode 71-- data Pong = Pong Text deriving BEncode
59-- 72--
60-- instance KRPC Ping Pong where 73-- instance 'KRPC' Ping Pong where
61-- method = "ping" 74-- method = \"ping\"
62-- @ 75-- @
76--
63class (BEncode req, BEncode resp) => KRPC req resp | req -> resp where 77class (BEncode req, BEncode resp) => KRPC req resp | req -> resp where
78 -- | Method name. Default implementation uses lowercased @req@
79 -- datatype name.
80 --
64 method :: Method req resp 81 method :: Method req resp
65 82
66 -- TODO add underscores 83 -- TODO add underscores