summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-13 02:29:01 -0400
committerjoe <joe@jerkface.net>2017-07-13 02:29:01 -0400
commit41a9b6cde4d087b11c95f12a015d02bf0848ca04 (patch)
tree291800d5d4352c4548c8b8f90a18f3b7cad7036e
parent908ca2d33232362655eda8147f460a1a5cd61a9e (diff)
To encode a query, we need the transaction id.
-rw-r--r--src/Network/QueryResponse.hs24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/Network/QueryResponse.hs b/src/Network/QueryResponse.hs
index 58db3a71..b6fea4a9 100644
--- a/src/Network/QueryResponse.hs
+++ b/src/Network/QueryResponse.hs
@@ -97,17 +97,18 @@ data Client err tbl meth tid addr x ctx = Client
97 97
98-- | An incomming message can be classified into three cases. 98-- | An incomming message can be classified into three cases.
99data MessageClass err meth tid 99data MessageClass err meth tid
100 = IsQuery meth -- ^ An unsolicited query is handled based on it's /meth/ value. 100 = IsQuery meth tid -- ^ An unsolicited query is handled based on it's /meth/ value. Any response
101 | IsResponse tid -- ^ A response to a outgoing query we associated with a /tid/ value. 101 -- should include the provided /tid/ value.
102 | IsUnknown err -- ^ None of the above. 102 | IsResponse tid -- ^ A response to a outgoing query we associated with a /tid/ value.
103 | IsUnknown err -- ^ None of the above.
103 104
104-- | Handler for an inbound query of type _x_ from an address of type _addr_. 105-- | Handler for an inbound query of type _x_ from an address of type _addr_.
105data MethodHandler err addr x ctx = forall a b. MethodHandler 106data MethodHandler err tid addr x ctx = forall a b. MethodHandler
106 { -- | Parse the query into a more specific type for this method. 107 { -- | Parse the query into a more specific type for this method.
107 methodParse :: x -> Either err a 108 methodParse :: x -> Either err a
108 -- | Serialize the response for transmission, given a context /ctx/ and the origin 109 -- | Serialize the response for transmission, given a context /ctx/ and the origin
109 -- and destination addresses. 110 -- and destination addresses.
110 , methodSerialize :: ctx -> addr -> addr -> b -> x 111 , methodSerialize :: ctx -> tid -> addr -> addr -> b -> x
111 -- | Fully typed action to perform upon the query. The remote origin 112 -- | Fully typed action to perform upon the query. The remote origin
112 -- address of the query is provided to the handler. 113 -- address of the query is provided to the handler.
113 , methodAction :: addr -> a -> IO b 114 , methodAction :: addr -> a -> IO b
@@ -116,14 +117,15 @@ data MethodHandler err addr x ctx = forall a b. MethodHandler
116-- | Attempt to invoke a 'MethodHandler' upon a given inbound query. If the 117-- | Attempt to invoke a 'MethodHandler' upon a given inbound query. If the
117-- parse is successful, the returned IO action will construct our reply. 118-- parse is successful, the returned IO action will construct our reply.
118-- Otherwise, a parse err is returned. 119-- Otherwise, a parse err is returned.
119dispatchQuery :: MethodHandler err addr x ctx -- ^ Handler to invoke. 120dispatchQuery :: MethodHandler err tid addr x ctx -- ^ Handler to invoke.
120 -> ctx -- ^ Arbitrary context used during serialization. 121 -> ctx -- ^ Arbitrary context used during serialization.
122 -> tid -- ^ The transaction id for this query\/response session.
121 -> addr -- ^ Our own address, to which the query was sent. 123 -> addr -- ^ Our own address, to which the query was sent.
122 -> x -- ^ The query packet. 124 -> x -- ^ The query packet.
123 -> addr -- ^ The origin address of the query. 125 -> addr -- ^ The origin address of the query.
124 -> Either err (IO x) 126 -> Either err (IO x)
125dispatchQuery (MethodHandler unwrapQ wrapR f) ctx self x addr = 127dispatchQuery (MethodHandler unwrapQ wrapR f) ctx tid self x addr =
126 fmap (\a -> wrapR ctx self addr <$> f addr a) $ unwrapQ x 128 fmap (\a -> wrapR ctx tid self addr <$> f addr a) $ unwrapQ x
127 129
128-- | These four parameters are required to implement an ougoing query. A 130-- | These four parameters are required to implement an ougoing query. A
129-- peer-to-peer algorithm will define a 'MethodSerializer' for every 'MethodHandler' that 131-- peer-to-peer algorithm will define a 'MethodSerializer' for every 'MethodHandler' that
@@ -234,7 +236,7 @@ data DispatchMethods tbl err meth tid addr x ctx = DispatchMethods
234 { -- | Clasify an inbound packet as a query or response. 236 { -- | Clasify an inbound packet as a query or response.
235 classifyInbound :: x -> MessageClass err meth tid 237 classifyInbound :: x -> MessageClass err meth tid
236 -- | Lookup the handler for a inbound query. 238 -- | Lookup the handler for a inbound query.
237 , lookupHandler :: meth -> Maybe (MethodHandler err addr x ctx) 239 , lookupHandler :: meth -> Maybe (MethodHandler err tid addr x ctx)
238 -- | Methods for handling incomming responses. 240 -- | Methods for handling incomming responses.
239 , tableMethods :: TableMethods tbl tid x 241 , tableMethods :: TableMethods tbl tid x
240 } 242 }
@@ -276,13 +278,13 @@ handleMessage (Client net d err pending whoami) again = do
276 again 278 again
277 Just (Right (plain, addr)) -> do 279 Just (Right (plain, addr)) -> do
278 case classifyInbound d plain of 280 case classifyInbound d plain of
279 IsQuery meth -> case lookupHandler d meth of 281 IsQuery meth tid -> case lookupHandler d meth of
280 Nothing -> reportMissingHandler err meth addr plain 282 Nothing -> reportMissingHandler err meth addr plain
281 Just m -> do 283 Just m -> do
282 (self,ctx) <- whoami 284 (self,ctx) <- whoami
283 either (reportParseError err) 285 either (reportParseError err)
284 (>>= sendMessage net addr) 286 (>>= sendMessage net addr)
285 (dispatchQuery m ctx self plain addr) 287 (dispatchQuery m ctx tid self plain addr)
286 IsResponse tid -> do 288 IsResponse tid -> do
287 action <- atomically $ do 289 action <- atomically $ do
288 ts0 <- readTVar pending 290 ts0 <- readTVar pending