summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-05-14 14:37:06 +0400
committerSam T <pxqr.sta@gmail.com>2013-05-14 14:37:06 +0400
commit0d11413c087536e34999c3d2295cace55600af4a (patch)
tree82d951efe9227e8dc8186c0a93257866568b20e4 /src
parentdca81a23bcec19ab7562322c2eb988b286afe944 (diff)
~ Expose some functions.
Diffstat (limited to 'src')
-rw-r--r--src/Remote/KRPC.hs29
-rw-r--r--src/Remote/KRPC/Protocol.hs15
2 files changed, 28 insertions, 16 deletions
diff --git a/src/Remote/KRPC.hs b/src/Remote/KRPC.hs
index 0e9838f1..e1ad0853 100644
--- a/src/Remote/KRPC.hs
+++ b/src/Remote/KRPC.hs
@@ -100,6 +100,10 @@ module Remote.KRPC
100 100
101 -- * Server 101 -- * Server
102 , MethodHandler, (==>), server 102 , MethodHandler, (==>), server
103
104 -- * Internal
105 , call_
106 , withRemote
103 ) where 107 ) where
104 108
105import Control.Applicative 109import Control.Applicative
@@ -186,7 +190,8 @@ injectVals ps (toBEncode -> BList as) = L.zip ps as
186injectVals _ _ = error "KRPC.injectVals: impossible" 190injectVals _ _ = error "KRPC.injectVals: impossible"
187{-# INLINE injectVals #-} 191{-# INLINE injectVals #-}
188 192
189 193-- | Alias to Socket, through might change in future.
194type Remote = Socket
190 195
191-- | Represent any error mentioned by protocol specification that 196-- | Represent any error mentioned by protocol specification that
192-- 'call', 'await' might throw. 197-- 'call', 'await' might throw.
@@ -208,10 +213,10 @@ queryCall sock addr m arg = sendMessage q addr sock
208 q = kquery (methodName m) (injectVals (methodParams m) arg) 213 q = kquery (methodName m) (injectVals (methodParams m) arg)
209 214
210getResult :: BEncodable result 215getResult :: BEncodable result
211 => KRemote -> KRemoteAddr 216 => KRemote
212 -> Method param result -> IO result 217 -> Method param result -> IO result
213getResult sock addr m = do 218getResult sock m = do
214 resp <- recvResponse addr sock 219 resp <- recvResponse sock
215 case resp of 220 case resp of
216 Left e -> throw (RPCException e) 221 Left e -> throw (RPCException e)
217 Right (respVals -> dict) -> do 222 Right (respVals -> dict) -> do
@@ -228,9 +233,19 @@ call :: (MonadBaseControl IO host, MonadIO host)
228 -> Method param result -- ^ Procedure to call. 233 -> Method param result -- ^ Procedure to call.
229 -> param -- ^ Arguments passed by callee to procedure. 234 -> param -- ^ Arguments passed by callee to procedure.
230 -> host result -- ^ Values returned by callee from the procedure. 235 -> host result -- ^ Values returned by callee from the procedure.
231call addr m arg = liftIO $ withRemote $ \sock -> do 236call addr m arg = liftIO $ withRemote $ \sock -> do call_ sock addr m arg
237
238-- | The same as 'call' but use already opened socket.
239call_ :: (MonadBaseControl IO host, MonadIO host)
240 => (BEncodable param, BEncodable result)
241 => Remote -- ^ Socket to use
242 -> RemoteAddr -- ^ Address of callee.
243 -> Method param result -- ^ Procedure to call.
244 -> param -- ^ Arguments passed by callee to procedure.
245 -> host result -- ^ Values returned by callee from the procedure.
246call_ sock addr m arg = liftIO $ do
232 queryCall sock addr m arg 247 queryCall sock addr m arg
233 getResult sock addr m 248 getResult sock m
234 249
235 250
236-- | Asynchonous result typically get from 'async' call. Used to defer 251-- | Asynchonous result typically get from 'async' call. Used to defer
@@ -265,7 +280,7 @@ async addr m arg = do
265 liftIO $ withRemote $ \sock -> 280 liftIO $ withRemote $ \sock ->
266 queryCall sock addr m arg 281 queryCall sock addr m arg
267 return $ Async $ withRemote $ \sock -> 282 return $ Async $ withRemote $ \sock ->
268 getResult sock addr m 283 getResult sock m
269 284
270-- | Will wait until the callee finished processing of procedure call 285-- | Will wait until the callee finished processing of procedure call
271-- and return its results. Throws 'RPCException' on any error 286-- and return its results. Throws 'RPCException' on any error
diff --git a/src/Remote/KRPC/Protocol.hs b/src/Remote/KRPC/Protocol.hs
index 29aaefed..3f3b16d0 100644
--- a/src/Remote/KRPC/Protocol.hs
+++ b/src/Remote/KRPC/Protocol.hs
@@ -162,11 +162,6 @@ kresponse = KResponse . M.fromList
162 162
163type KRemoteAddr = (HostAddress, PortNumber) 163type KRemoteAddr = (HostAddress, PortNumber)
164 164
165remoteAddr :: KRemoteAddr -> SockAddr
166remoteAddr = SockAddrInet <$> snd <*> fst
167{-# INLINE remoteAddr #-}
168
169
170type KRemote = Socket 165type KRemote = Socket
171 166
172withRemote :: (MonadBaseControl IO m, MonadIO m) => (KRemote -> m a) -> m a 167withRemote :: (MonadBaseControl IO m, MonadIO m) => (KRemote -> m a) -> m a
@@ -176,8 +171,11 @@ withRemote = bracket (liftIO (socket AF_INET Datagram defaultProtocol))
176 171
177 172
178maxMsgSize :: Int 173maxMsgSize :: Int
179maxMsgSize = 512
180{-# INLINE maxMsgSize #-} 174{-# INLINE maxMsgSize #-}
175-- release
176--maxMsgSize = 512 -- size of payload of one udp packet
177-- bench
178maxMsgSize = 64 * 1024 -- max udp size
181 179
182 180
183-- TODO eliminate toStrict 181-- TODO eliminate toStrict
@@ -189,9 +187,8 @@ sendMessage msg (host, port) sock =
189 187
190 188
191-- TODO check scheme 189-- TODO check scheme
192recvResponse :: KRemoteAddr -> KRemote -> IO (Either KError KResponse) 190recvResponse :: KRemote -> IO (Either KError KResponse)
193recvResponse addr sock = do 191recvResponse sock = do
194 connect sock (remoteAddr addr)
195 (raw, _) <- recvFrom sock maxMsgSize 192 (raw, _) <- recvFrom sock maxMsgSize
196 return $ case decoded raw of 193 return $ case decoded raw of
197 Right resp -> Right resp 194 Right resp -> Right resp