diff options
Diffstat (limited to 'src/Network/QueryResponse.hs')
-rw-r--r-- | src/Network/QueryResponse.hs | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/src/Network/QueryResponse.hs b/src/Network/QueryResponse.hs index 8c793a1a..d467d302 100644 --- a/src/Network/QueryResponse.hs +++ b/src/Network/QueryResponse.hs | |||
@@ -85,7 +85,7 @@ partitionTransport parse encodex encodey tr = | |||
85 | -- | Paritions a 'Transport' into two higher-level transports. Note: An 'MVar' | 85 | -- | Paritions a 'Transport' into two higher-level transports. Note: An 'MVar' |
86 | -- is used to share the same underlying socket, so be sure to fork a thread for | 86 | -- is used to share the same underlying socket, so be sure to fork a thread for |
87 | -- both returned 'Transport's to avoid hanging. | 87 | -- both returned 'Transport's to avoid hanging. |
88 | partitionTransportM :: ((b,a) -> IO (Either (x,xaddr) (y,yaddr))) | 88 | partitionTransportM :: ((b,a) -> IO (Either (x,xaddr) (y,yaddr))) |
89 | -> ((x,xaddr) -> IO (b,a)) | 89 | -> ((x,xaddr) -> IO (b,a)) |
90 | -> ((y,yaddr) -> IO (b,a)) | 90 | -> ((y,yaddr) -> IO (b,a)) |
91 | -> Transport err a b | 91 | -> Transport err a b |
@@ -147,13 +147,13 @@ forkListener name client = do | |||
147 | closeTransport client | 147 | closeTransport client |
148 | killThread thread_id | 148 | killThread thread_id |
149 | 149 | ||
150 | -- | Send a query to a remote peer. Note that this funciton will always time | 150 | -- | Send a query to a remote peer. Note that this function will always time |
151 | -- out if 'forkListener' was never invoked to spawn a thread to receive and | 151 | -- out if 'forkListener' was never invoked to spawn a thread to receive and |
152 | -- dispatch the response. | 152 | -- dispatch the response. |
153 | sendQuery :: | 153 | sendQuery :: |
154 | forall err a b tbl x meth tid addr. | 154 | forall err a b tbl x meth tid addr. |
155 | Client err meth tid addr x -- ^ A query/response implementation. | 155 | Client err meth tid addr x -- ^ A query/response implementation. |
156 | -> MethodSerializer tid addr x meth a b -- ^ Information for marshalling the query. | 156 | -> MethodSerializer tid addr x meth a b -- ^ Information for marshaling the query. |
157 | -> a -- ^ The outbound query. | 157 | -> a -- ^ The outbound query. |
158 | -> addr -- ^ Destination address of query. | 158 | -> addr -- ^ Destination address of query. |
159 | -> IO (Maybe b) -- ^ The response, or 'Nothing' if it timed out. | 159 | -> IO (Maybe b) -- ^ The response, or 'Nothing' if it timed out. |
@@ -198,7 +198,7 @@ data Client err meth tid addr x = forall tbl. Client | |||
198 | , clientResponseId :: tid -> IO tid | 198 | , clientResponseId :: tid -> IO tid |
199 | } | 199 | } |
200 | 200 | ||
201 | -- | An incomming message can be classified into three cases. | 201 | -- | An incoming message can be classified into three cases. |
202 | data MessageClass err meth tid | 202 | data MessageClass err meth tid |
203 | = IsQuery meth tid -- ^ An unsolicited query is handled based on it's /meth/ value. Any response | 203 | = IsQuery meth tid -- ^ An unsolicited query is handled based on it's /meth/ value. Any response |
204 | -- should include the provided /tid/ value. | 204 | -- should include the provided /tid/ value. |
@@ -248,7 +248,7 @@ dispatchQuery (MethodHandler unwrapQ wrapR f) tid self x addr = | |||
248 | dispatchQuery (NoReply unwrapQ f) tid self x addr = | 248 | dispatchQuery (NoReply unwrapQ f) tid self x addr = |
249 | fmap (\a -> f addr a >> return Nothing) $ unwrapQ x | 249 | fmap (\a -> f addr a >> return Nothing) $ unwrapQ x |
250 | 250 | ||
251 | -- | These four parameters are required to implement an ougoing query. A | 251 | -- | These four parameters are required to implement an outgoing query. A |
252 | -- peer-to-peer algorithm will define a 'MethodSerializer' for every 'MethodHandler' that | 252 | -- peer-to-peer algorithm will define a 'MethodSerializer' for every 'MethodHandler' that |
253 | -- might be returned by 'lookupHandler'. | 253 | -- might be returned by 'lookupHandler'. |
254 | data MethodSerializer tid addr x meth a b = MethodSerializer | 254 | data MethodSerializer tid addr x meth a b = MethodSerializer |
@@ -257,19 +257,19 @@ data MethodSerializer tid addr x meth a b = MethodSerializer | |||
257 | -- | A method identifier used for error reporting. This needn't be the | 257 | -- | A method identifier used for error reporting. This needn't be the |
258 | -- same as the /meth/ argument to 'MethodHandler', but it is suggested. | 258 | -- same as the /meth/ argument to 'MethodHandler', but it is suggested. |
259 | , method :: meth | 259 | , method :: meth |
260 | -- | Serialize the outgoing query /a/ into a transmitable packet /x/. | 260 | -- | Serialize the outgoing query /a/ into a transmittable packet /x/. |
261 | -- The /addr/ arguments are, respectively, our own origin address and the | 261 | -- The /addr/ arguments are, respectively, our own origin address and the |
262 | -- destination of the request. The /tid/ argument is useful for attaching | 262 | -- destination of the request. The /tid/ argument is useful for attaching |
263 | -- auxillary notations on all outgoing packets. | 263 | -- auxiliary notations on all outgoing packets. |
264 | , wrapQuery :: tid -> addr -> addr -> a -> x | 264 | , wrapQuery :: tid -> addr -> addr -> a -> x |
265 | -- | Parse an inbound packet /x/ into a response /b/ for this query. | 265 | -- | Parse an inbound packet /x/ into a response /b/ for this query. |
266 | , unwrapResponse :: x -> b | 266 | , unwrapResponse :: x -> b |
267 | } | 267 | } |
268 | 268 | ||
269 | 269 | ||
270 | -- | To dipatch responses to our outbound queries, we require three primitives. | 270 | -- | To dispatch responses to our outbound queries, we require three |
271 | -- See the 'transactionMethods' function to create these primitives out of a | 271 | -- primitives. See the 'transactionMethods' function to create these |
272 | -- lookup table and a generator for transaction ids. | 272 | -- primitives out of a lookup table and a generator for transaction ids. |
273 | -- | 273 | -- |
274 | -- The type variable /d/ is used to represent the current state of the | 274 | -- The type variable /d/ is used to represent the current state of the |
275 | -- transaction generator and the table of pending transactions. | 275 | -- transaction generator and the table of pending transactions. |
@@ -280,7 +280,7 @@ data TransactionMethods d tid x = TransactionMethods | |||
280 | -- that can be used to forget the 'MVar' if the remote peer is not | 280 | -- that can be used to forget the 'MVar' if the remote peer is not |
281 | -- responding. | 281 | -- responding. |
282 | dispatchRegister :: MVar x -> d -> STM (tid, d) | 282 | dispatchRegister :: MVar x -> d -> STM (tid, d) |
283 | -- | This method is invoked when an incomming packet /x/ indicates it is | 283 | -- | This method is invoked when an incoming packet /x/ indicates it is |
284 | -- a response to the transaction with id /tid/. The returned IO action | 284 | -- a response to the transaction with id /tid/. The returned IO action |
285 | -- is will write the packet to the correct 'MVar' thus completing the | 285 | -- is will write the packet to the correct 'MVar' thus completing the |
286 | -- dispatch. | 286 | -- dispatch. |
@@ -353,13 +353,13 @@ transactionMethods (TableMethods insert delete lookup) generate = TransactionMet | |||
353 | Nothing -> return ((g,t), return ()) | 353 | Nothing -> return ((g,t), return ()) |
354 | } | 354 | } |
355 | 355 | ||
356 | -- | A set of methods neccessary for dispatching incomming packets. | 356 | -- | A set of methods necessary for dispatching incoming packets. |
357 | data DispatchMethods tbl err meth tid addr x = DispatchMethods | 357 | data DispatchMethods tbl err meth tid addr x = DispatchMethods |
358 | { -- | Clasify an inbound packet as a query or response. | 358 | { -- | Classify an inbound packet as a query or response. |
359 | classifyInbound :: x -> MessageClass err meth tid | 359 | classifyInbound :: x -> MessageClass err meth tid |
360 | -- | Lookup the handler for a inbound query. | 360 | -- | Lookup the handler for a inbound query. |
361 | , lookupHandler :: meth -> Maybe (MethodHandler err tid addr x) | 361 | , lookupHandler :: meth -> Maybe (MethodHandler err tid addr x) |
362 | -- | Methods for handling incomming responses. | 362 | -- | Methods for handling incoming responses. |
363 | , tableMethods :: TransactionMethods tbl tid x | 363 | , tableMethods :: TransactionMethods tbl tid x |
364 | } | 364 | } |
365 | 365 | ||
@@ -368,19 +368,19 @@ data DispatchMethods tbl err meth tid addr x = DispatchMethods | |||
368 | -- | 368 | -- |
369 | -- [ /addr/ ] Address of remote peer. | 369 | -- [ /addr/ ] Address of remote peer. |
370 | -- | 370 | -- |
371 | -- [ /x/ ] Incomming or outgoing packet. | 371 | -- [ /x/ ] Incoming or outgoing packet. |
372 | -- | 372 | -- |
373 | -- [ /meth/ ] Method id of incomming or outgoing request. | 373 | -- [ /meth/ ] Method id of incoming or outgoing request. |
374 | -- | 374 | -- |
375 | -- [ /tid/ ] Transaction id for outgoing packet. | 375 | -- [ /tid/ ] Transaction id for outgoing packet. |
376 | -- | 376 | -- |
377 | -- [ /err/ ] Error information, typically a 'String'. | 377 | -- [ /err/ ] Error information, typically a 'String'. |
378 | data ErrorReporter addr x meth tid err = ErrorReporter | 378 | data ErrorReporter addr x meth tid err = ErrorReporter |
379 | { -- | Incomming: failed to parse packet. | 379 | { -- | Incoming: failed to parse packet. |
380 | reportParseError :: err -> IO () | 380 | reportParseError :: err -> IO () |
381 | -- | Incomming: no handler for request. | 381 | -- | Incoming: no handler for request. |
382 | , reportMissingHandler :: meth -> addr -> x -> IO () | 382 | , reportMissingHandler :: meth -> addr -> x -> IO () |
383 | -- | Incomming: unable to identify request. | 383 | -- | Incoming: unable to identify request. |
384 | , reportUnknown :: addr -> x -> err -> IO () | 384 | , reportUnknown :: addr -> x -> err -> IO () |
385 | -- | Outgoing: remote peer is not responding. | 385 | -- | Outgoing: remote peer is not responding. |
386 | , reportTimeout :: meth -> tid -> addr -> IO () | 386 | , reportTimeout :: meth -> tid -> addr -> IO () |
@@ -414,9 +414,9 @@ instance Contravariant (ErrorReporter addr x meth tid) where | |||
414 | tim | 414 | tim |
415 | 415 | ||
416 | -- | Handle a single inbound packet and then invoke the given continuation. | 416 | -- | Handle a single inbound packet and then invoke the given continuation. |
417 | -- The 'forkListener' function is implemeneted by passing this function to | 417 | -- The 'forkListener' function is implemented by passing this function to 'fix' |
418 | -- 'fix' in a forked thread that loops until 'awaitMessage' returns 'Nothing' | 418 | -- in a forked thread that loops until 'awaitMessage' returns 'Nothing' or |
419 | -- or throws an exception. | 419 | -- throws an exception. |
420 | handleMessage :: | 420 | handleMessage :: |
421 | Client err meth tid addr x | 421 | Client err meth tid addr x |
422 | -> addr | 422 | -> addr |
@@ -460,20 +460,20 @@ sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6 | |||
460 | sockAddrFamily (SockAddrUnix _ ) = AF_UNIX | 460 | sockAddrFamily (SockAddrUnix _ ) = AF_UNIX |
461 | sockAddrFamily (SockAddrCan _ ) = AF_CAN | 461 | sockAddrFamily (SockAddrCan _ ) = AF_CAN |
462 | 462 | ||
463 | -- | Packets with an empty payload may trigger eof exception. | 463 | -- | Packets with an empty payload may trigger EOF exception. |
464 | -- 'udpTransport' uses this function to avoid throwing in that | 464 | -- 'udpTransport' uses this function to avoid throwing in that |
465 | -- case. | 465 | -- case. |
466 | ignoreEOF :: a -> IOError -> IO a | 466 | ignoreEOF :: a -> IOError -> IO a |
467 | ignoreEOF def e | isEOFError e = pure def | 467 | ignoreEOF def e | isEOFError e = pure def |
468 | | otherwise = throwIO e | 468 | | otherwise = throwIO e |
469 | 469 | ||
470 | -- | Hardcoded maximum packet size for incomming udp packets received via | 470 | -- | Hard-coded maximum packet size for incoming UDP Packets received via |
471 | -- 'udpTransport'. | 471 | -- 'udpTransport'. |
472 | udpBufferSize :: Int | 472 | udpBufferSize :: Int |
473 | udpBufferSize = 65536 | 473 | udpBufferSize = 65536 |
474 | 474 | ||
475 | -- | A 'udpTransport' uses a UDP socket to send and receive 'ByteString's. The | 475 | -- | A 'udpTransport' uses a UDP socket to send and receive 'ByteString's. The |
476 | -- argument is the listen-address for incomming packets. This is a useful | 476 | -- argument is the listen-address for incoming packets. This is a useful |
477 | -- low-level 'Transport' that can be transformed for higher-level protocols | 477 | -- low-level 'Transport' that can be transformed for higher-level protocols |
478 | -- using 'layerTransport'. | 478 | -- using 'layerTransport'. |
479 | udpTransport :: SockAddr -> IO (Transport err SockAddr ByteString) | 479 | udpTransport :: SockAddr -> IO (Transport err SockAddr ByteString) |