summaryrefslogtreecommitdiff
path: root/src/Remote/KRPC.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-05-11 21:31:05 +0400
committerSam T <pxqr.sta@gmail.com>2013-05-11 21:31:05 +0400
commit3806b3513f04dd360badf438fa103334dd32933c (patch)
tree28eeb1f784a218fd179ea6d4e1a9ce73ad7cbdef /src/Remote/KRPC.hs
parent96c554f6ab63e6e207d0c7e65d3ef1cdef7baa9c (diff)
~ Separate method implementation.
This will break everything for now.
Diffstat (limited to 'src/Remote/KRPC.hs')
-rw-r--r--src/Remote/KRPC.hs39
1 files changed, 23 insertions, 16 deletions
diff --git a/src/Remote/KRPC.hs b/src/Remote/KRPC.hs
index a6318ccd..8f2027f2 100644
--- a/src/Remote/KRPC.hs
+++ b/src/Remote/KRPC.hs
@@ -17,7 +17,7 @@ module Remote.KRPC
17 , call, async, await 17 , call, async, await
18 18
19 -- * Server 19 -- * Server
20 , handler, server 20 , server
21 ) where 21 ) where
22 22
23import Control.Exception 23import Control.Exception
@@ -46,14 +46,14 @@ type RemoteAddr = KRemoteAddr
46 46
47queryCall :: BEncodable param 47queryCall :: BEncodable param
48 => KRemote -> KRemoteAddr 48 => KRemote -> KRemoteAddr
49 -> Method remote param result -> param -> IO () 49 -> Method param result -> param -> IO ()
50queryCall sock addr m arg = sendMessage q addr sock 50queryCall sock addr m arg = sendMessage q addr sock
51 where 51 where
52 q = kquery (L.head (methodName m)) [(L.head (methodParams m), toBEncode arg)] 52 q = kquery (L.head (methodName m)) [(L.head (methodParams m), toBEncode arg)]
53 53
54getResult :: BEncodable result 54getResult :: BEncodable result
55 => KRemote -> KRemoteAddr 55 => KRemote -> KRemoteAddr
56 -> Method remote param result -> IO result 56 -> Method param result -> IO result
57getResult sock addr m = do 57getResult sock addr m = do
58 resp <- recvResponse addr sock 58 resp <- recvResponse addr sock
59 case resp of 59 case resp of
@@ -76,7 +76,7 @@ getResult sock addr m = do
76call :: (MonadBaseControl IO host, MonadIO host) 76call :: (MonadBaseControl IO host, MonadIO host)
77 => (BEncodable param, BEncodable result) 77 => (BEncodable param, BEncodable result)
78 => RemoteAddr 78 => RemoteAddr
79 -> Method remote param result 79 -> Method param result
80 -> param 80 -> param
81 -> host result 81 -> host result
82call addr m arg = liftIO $ withRemote $ \sock -> do 82call addr m arg = liftIO $ withRemote $ \sock -> do
@@ -86,10 +86,11 @@ call addr m arg = liftIO $ withRemote $ \sock -> do
86 86
87newtype Async result = Async { waitResult :: IO result } 87newtype Async result = Async { waitResult :: IO result }
88 88
89-- TODO document errorneous usage
89async :: MonadIO host 90async :: MonadIO host
90 => (BEncodable param, BEncodable result) 91 => (BEncodable param, BEncodable result)
91 => RemoteAddr 92 => RemoteAddr
92 -> Method remote param result 93 -> Method param result
93 -> param 94 -> param
94 -> host (Async result) 95 -> host (Async result)
95async addr m arg = do 96async addr m arg = do
@@ -102,31 +103,37 @@ await :: MonadIO host => Async result -> host result
102await = liftIO . waitResult 103await = liftIO . waitResult
103 104
104-- TODO better name 105-- TODO better name
105type MHandler remote = Method remote BEncode (Result BEncode) 106type MHandler remote = ( Method BEncode (Result BEncode)
107 , BEncode -> remote (Result BEncode)
108 )
106 109
107handler :: forall (remote :: * -> *) (param :: *) (result :: *). 110-- we can safely erase types in (==>)
111(==>) :: forall (remote :: * -> *) (param :: *) (result :: *).
108 (BEncodable param, BEncodable result) 112 (BEncodable param, BEncodable result)
109 => Monad remote 113 => Monad remote
110 => Method remote param result 114 => Method param result
111 -> Method remote BEncode (Result BEncode) 115 -> (param -> remote result)
112handler m = m { methodBody = \x -> do 116 -> MHandler remote
113 case fromBEncode x of 117m ==> body = undefined
114 Right a -> liftM (Right . toBEncode) (methodBody m a) 118 where
119 newbody x = case fromBEncode x of
120 Right a -> liftM (Right . toBEncode) (body a)
115 Left e -> return (Left e) 121 Left e -> return (Left e)
116 } 122
117 123
118-- TODO: allow forkIO 124-- TODO: allow forkIO
125-- TODO: allow overloading
119server :: (MonadBaseControl IO remote, MonadIO remote) 126server :: (MonadBaseControl IO remote, MonadIO remote)
120 => PortNumber 127 => PortNumber
121 -> [MHandler remote] 128 -> [MHandler remote]
122 -> remote () 129 -> remote ()
123server servport ms = remoteServer servport $ \_ q -> do 130server servport ms = remoteServer servport $ \_ q -> do
124 let name = queryMethod q 131 let name = queryMethod q
125 let args = queryArgs q 132 let args = undefined -- queryArgs q
126 let m = L.head ms 133 let m = L.head ms
127 res <- methodBody m (snd (L.head (M.toList args))) 134 res <- undefined -- methodBody m (snd (L.head (M.toList args)))
128 case res of 135 case res of
129 Left r -> return (Left (ProtocolError (T.pack r))) 136 Left r -> return (Left (ProtocolError (T.pack r)))
130 Right r -> do 137 Right r -> do
131 let retName = L.head (methodVals m) 138 let retName = undefined -- L.head (methodVals m)
132 return (Right (kresponse [(retName, r)])) 139 return (Right (kresponse [(retName, r)]))