diff options
Diffstat (limited to 'src/Network/KRPC.hs')
-rw-r--r-- | src/Network/KRPC.hs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/Network/KRPC.hs b/src/Network/KRPC.hs new file mode 100644 index 00000000..d185fb4c --- /dev/null +++ b/src/Network/KRPC.hs | |||
@@ -0,0 +1,91 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam Truzjan 2013, 2014 | ||
3 | -- License : BSD3 | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : experimental | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | -- This module provides safe remote procedure call. One important | ||
9 | -- point is exceptions and errors, so to be able handle them | ||
10 | -- properly we need to investigate a bit about how this all works. | ||
11 | -- Internally, in order to make method invokation KRPC makes the | ||
12 | -- following steps: | ||
13 | -- | ||
14 | -- * Caller serialize arguments to bencoded bytestrings; | ||
15 | -- | ||
16 | -- * Caller send bytestring data over UDP to the callee; | ||
17 | -- | ||
18 | -- * Callee receive and decode arguments to the method and method | ||
19 | -- name. If it can't decode then it send 'ProtocolError' back to the | ||
20 | -- caller; | ||
21 | -- | ||
22 | -- * Callee search for the @method name@ in the method table. | ||
23 | -- If it not present in the table then callee send 'MethodUnknown' | ||
24 | -- back to the caller; | ||
25 | -- | ||
26 | -- * Callee check if argument names match. If not it send | ||
27 | -- 'ProtocolError' back; | ||
28 | -- | ||
29 | -- * Callee make the actuall call to the plain old haskell | ||
30 | -- function. If the function throw exception then callee send | ||
31 | -- 'ServerError' back. | ||
32 | -- | ||
33 | -- * Callee serialize result of the function to bencoded bytestring. | ||
34 | -- | ||
35 | -- * Callee encode result to bencoded bytestring and send it back | ||
36 | -- to the caller. | ||
37 | -- | ||
38 | -- * Caller check if return values names match with the signature | ||
39 | -- it called in the first step. | ||
40 | -- | ||
41 | -- * Caller extracts results and finally return results of the | ||
42 | -- procedure call as ordinary haskell values. | ||
43 | -- | ||
44 | -- If every other error occurred then caller get the | ||
45 | -- 'GenericError'. All errors returned by callee are throwed as | ||
46 | -- ordinary haskell exceptions at caller side. Also note that both | ||
47 | -- caller and callee use plain UDP, so KRPC is unreliable. | ||
48 | -- | ||
49 | -- For async 'query' use @async@ package. | ||
50 | -- | ||
51 | -- For protocol details see "Network.KRPC.Message" module. | ||
52 | -- | ||
53 | module Network.KRPC | ||
54 | ( -- * Methods | ||
55 | Method | ||
56 | , KRPC (..) | ||
57 | |||
58 | -- * RPC | ||
59 | -- ** Query | ||
60 | , QueryFailure (..) | ||
61 | , query | ||
62 | , query' | ||
63 | , queryRaw | ||
64 | , getQueryCount | ||
65 | |||
66 | -- ** Handler | ||
67 | , HandlerFailure (..) | ||
68 | , Handler | ||
69 | , handler | ||
70 | |||
71 | -- * Manager | ||
72 | , MonadKRPC (..) | ||
73 | , Options (..) | ||
74 | , def | ||
75 | , Manager | ||
76 | , newManager | ||
77 | , closeManager | ||
78 | , withManager | ||
79 | , isActive | ||
80 | , listen | ||
81 | |||
82 | -- * Re-exports | ||
83 | , ErrorCode (..) | ||
84 | , SockAddr (..) | ||
85 | ) where | ||
86 | |||
87 | import Data.Default.Class | ||
88 | import Network.KRPC.Message | ||
89 | import Network.KRPC.Method | ||
90 | import Network.KRPC.Manager | ||
91 | import Network.Socket (SockAddr (..)) | ||