diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-05-12 01:07:34 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-05-12 01:07:34 +0400 |
commit | fd62eb70fe87b471d29cb994a60ad88f58b33ca9 (patch) | |
tree | ed8c7d69f10c1b874cc03c7d6e5064fb81b94482 /src/Remote/KRPC.hs | |
parent | 7614ed760e137219fb4e36288abf1e78eacb2266 (diff) |
~ Prepare to scheme check.
Diffstat (limited to 'src/Remote/KRPC.hs')
-rw-r--r-- | src/Remote/KRPC.hs | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/src/Remote/KRPC.hs b/src/Remote/KRPC.hs index 8f2027f2..22dbf3aa 100644 --- a/src/Remote/KRPC.hs +++ b/src/Remote/KRPC.hs | |||
@@ -10,6 +10,7 @@ | |||
10 | {-# LANGUAGE OverloadedStrings #-} | 10 | {-# LANGUAGE OverloadedStrings #-} |
11 | {-# LANGUAGE FlexibleContexts, DeriveDataTypeable #-} | 11 | {-# LANGUAGE FlexibleContexts, DeriveDataTypeable #-} |
12 | {-# LANGUAGE ExplicitForAll, KindSignatures #-} | 12 | {-# LANGUAGE ExplicitForAll, KindSignatures #-} |
13 | {-# LANGUAGE ViewPatterns #-} | ||
13 | module Remote.KRPC | 14 | module Remote.KRPC |
14 | ( module Remote.KRPC.Method, RemoteAddr | 15 | ( module Remote.KRPC.Method, RemoteAddr |
15 | 16 | ||
@@ -17,7 +18,7 @@ module Remote.KRPC | |||
17 | , call, async, await | 18 | , call, async, await |
18 | 19 | ||
19 | -- * Server | 20 | -- * Server |
20 | , server | 21 | , (==>), server |
21 | ) where | 22 | ) where |
22 | 23 | ||
23 | import Control.Exception | 24 | import Control.Exception |
@@ -27,6 +28,7 @@ import Control.Monad.IO.Class | |||
27 | import Data.BEncode | 28 | import Data.BEncode |
28 | import Data.List as L | 29 | import Data.List as L |
29 | import Data.Map as M | 30 | import Data.Map as M |
31 | import Data.Set as S | ||
30 | import Data.Text as T | 32 | import Data.Text as T |
31 | import Data.Typeable | 33 | import Data.Typeable |
32 | import Network | 34 | import Network |
@@ -51,6 +53,7 @@ queryCall sock addr m arg = sendMessage q addr sock | |||
51 | where | 53 | where |
52 | q = kquery (L.head (methodName m)) [(L.head (methodParams m), toBEncode arg)] | 54 | q = kquery (L.head (methodName m)) [(L.head (methodParams m), toBEncode arg)] |
53 | 55 | ||
56 | -- TODO check scheme | ||
54 | getResult :: BEncodable result | 57 | getResult :: BEncodable result |
55 | => KRemote -> KRemoteAddr | 58 | => KRemote -> KRemoteAddr |
56 | -> Method param result -> IO result | 59 | -> Method param result -> IO result |
@@ -58,7 +61,7 @@ getResult sock addr m = do | |||
58 | resp <- recvResponse addr sock | 61 | resp <- recvResponse addr sock |
59 | case resp of | 62 | case resp of |
60 | Left e -> throw (RPCException e) | 63 | Left e -> throw (RPCException e) |
61 | Right (KResponse dict) -> do | 64 | Right (respVals -> dict) -> do |
62 | let valName = L.head (methodVals m) | 65 | let valName = L.head (methodVals m) |
63 | case M.lookup valName dict of | 66 | case M.lookup valName dict of |
64 | Just val | Right res <- fromBEncode val -> return res | 67 | Just val | Right res <- fromBEncode val -> return res |
@@ -102,10 +105,10 @@ async addr m arg = do | |||
102 | await :: MonadIO host => Async result -> host result | 105 | await :: MonadIO host => Async result -> host result |
103 | await = liftIO . waitResult | 106 | await = liftIO . waitResult |
104 | 107 | ||
105 | -- TODO better name | 108 | type HandlerBody remote = (BEncode -> remote (Result BEncode), KResponseScheme) |
106 | type MHandler remote = ( Method BEncode (Result BEncode) | 109 | |
107 | , BEncode -> remote (Result BEncode) | 110 | type MethodHandler remote = (KQueryScheme, HandlerBody remote) |
108 | ) | 111 | |
109 | 112 | ||
110 | -- we can safely erase types in (==>) | 113 | -- we can safely erase types in (==>) |
111 | (==>) :: forall (remote :: * -> *) (param :: *) (result :: *). | 114 | (==>) :: forall (remote :: * -> *) (param :: *) (result :: *). |
@@ -113,8 +116,8 @@ type MHandler remote = ( Method BEncode (Result BEncode) | |||
113 | => Monad remote | 116 | => Monad remote |
114 | => Method param result | 117 | => Method param result |
115 | -> (param -> remote result) | 118 | -> (param -> remote result) |
116 | -> MHandler remote | 119 | -> MethodHandler remote |
117 | m ==> body = undefined | 120 | m ==> body = (methodQueryScheme m, (newbody, methodRespScheme m)) |
118 | where | 121 | where |
119 | newbody x = case fromBEncode x of | 122 | newbody x = case fromBEncode x of |
120 | Right a -> liftM (Right . toBEncode) (body a) | 123 | Right a -> liftM (Right . toBEncode) (body a) |
@@ -125,15 +128,28 @@ m ==> body = undefined | |||
125 | -- TODO: allow overloading | 128 | -- TODO: allow overloading |
126 | server :: (MonadBaseControl IO remote, MonadIO remote) | 129 | server :: (MonadBaseControl IO remote, MonadIO remote) |
127 | => PortNumber | 130 | => PortNumber |
128 | -> [MHandler remote] | 131 | -> [MethodHandler remote] |
129 | -> remote () | 132 | -> remote () |
130 | server servport ms = remoteServer servport $ \_ q -> do | 133 | server servport handlers = do |
131 | let name = queryMethod q | 134 | remoteServer servport $ \_ q -> do |
132 | let args = undefined -- queryArgs q | 135 | case dispatch (scheme q) of |
133 | let m = L.head ms | 136 | Nothing -> return (Left (MethodUnknown "method")) |
134 | res <- undefined -- methodBody m (snd (L.head (M.toList args))) | 137 | Just (m, rsc) -> do |
135 | case res of | 138 | let arg = snd (L.head (M.toList (queryArgs q))) |
136 | Left r -> return (Left (ProtocolError (T.pack r))) | 139 | |
137 | Right r -> do | 140 | res <- invoke m arg |
138 | let retName = undefined -- L.head (methodVals m) | 141 | let valName = L.head (S.toList (rscVals rsc)) |
139 | return (Right (kresponse [(retName, r)])) | 142 | return $ bimap (ProtocolError . T.pack) |
143 | (kresponse . return . (,) valName) res | ||
144 | where | ||
145 | handlerMap = M.fromList handlers | ||
146 | |||
147 | -- dispatch :: KQueryScheme -> MethodHandler remote | ||
148 | dispatch s | Just m <- M.lookup s handlerMap = return m | ||
149 | | otherwise = Nothing | ||
150 | |||
151 | -- invoke :: MethodHandler remote -> BEncode -> remote BEncode | ||
152 | invoke m args = m args | ||
153 | |||
154 | bimap f _ (Left x) = Left (f x) | ||
155 | bimap _ g (Right x) = Right (g x) \ No newline at end of file | ||