summaryrefslogtreecommitdiff
path: root/src/Remote/KRPC.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-05-12 01:07:34 +0400
committerSam T <pxqr.sta@gmail.com>2013-05-12 01:07:34 +0400
commitfd62eb70fe87b471d29cb994a60ad88f58b33ca9 (patch)
treeed8c7d69f10c1b874cc03c7d6e5064fb81b94482 /src/Remote/KRPC.hs
parent7614ed760e137219fb4e36288abf1e78eacb2266 (diff)
~ Prepare to scheme check.
Diffstat (limited to 'src/Remote/KRPC.hs')
-rw-r--r--src/Remote/KRPC.hs54
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 #-}
13module Remote.KRPC 14module 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
23import Control.Exception 24import Control.Exception
@@ -27,6 +28,7 @@ import Control.Monad.IO.Class
27import Data.BEncode 28import Data.BEncode
28import Data.List as L 29import Data.List as L
29import Data.Map as M 30import Data.Map as M
31import Data.Set as S
30import Data.Text as T 32import Data.Text as T
31import Data.Typeable 33import Data.Typeable
32import Network 34import 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
54getResult :: BEncodable result 57getResult :: 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
102await :: MonadIO host => Async result -> host result 105await :: MonadIO host => Async result -> host result
103await = liftIO . waitResult 106await = liftIO . waitResult
104 107
105-- TODO better name 108type HandlerBody remote = (BEncode -> remote (Result BEncode), KResponseScheme)
106type MHandler remote = ( Method BEncode (Result BEncode) 109
107 , BEncode -> remote (Result BEncode) 110type 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
117m ==> body = undefined 120m ==> 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
126server :: (MonadBaseControl IO remote, MonadIO remote) 129server :: (MonadBaseControl IO remote, MonadIO remote)
127 => PortNumber 130 => PortNumber
128 -> [MHandler remote] 131 -> [MethodHandler remote]
129 -> remote () 132 -> remote ()
130server servport ms = remoteServer servport $ \_ q -> do 133server 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