diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dhtd.hs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 67888eb4..2abaecdd 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -132,6 +132,26 @@ reportTable bkts = map (show *** show . fst) | |||
132 | $ R.toList | 132 | $ R.toList |
133 | $ bkts | 133 | $ bkts |
134 | 134 | ||
135 | reportResult :: Show ni => | ||
136 | String | ||
137 | -> (r -> String) | ||
138 | -> (tok -> Maybe String) | ||
139 | -> Handle | ||
140 | -> Either String ([ni],[r],tok) | ||
141 | -> IO () | ||
142 | reportResult meth showR showTok h (Left e) = hPutClient h e | ||
143 | reportResult meth showR showTok h (Right (ns,rs,tok)) = do | ||
144 | hPutClient h $ showReport report | ||
145 | where | ||
146 | report = intercalate [("","")] [ tok_r , node_r , result_r ] | ||
147 | |||
148 | tok_r = maybe [] (pure . ("token:",)) $ showTok tok | ||
149 | |||
150 | node_r = map ( ("n",) . show ) ns | ||
151 | |||
152 | result_r | (meth=="node") = [] | ||
153 | | otherwise = map ( (take 1 meth,) . showR ) rs | ||
154 | |||
135 | data Session = Session | 155 | data Session = Session |
136 | { netname :: String | 156 | { netname :: String |
137 | , dhts :: Map.Map String DHT | 157 | , dhts :: Map.Map String DHT |
@@ -215,6 +235,29 @@ clientSession s@Session{..} sock cnum h = do | |||
215 | let rs = [" ", show result] | 235 | let rs = [" ", show result] |
216 | hPutClient h $ unlines rs | 236 | hPutClient h $ unlines rs |
217 | Left er -> hPutClient h er | 237 | Left er -> hPutClient h er |
238 | ("g", s) | Just DHT{..} <- Map.lookup netname dhts | ||
239 | -> cmd0 $ do | ||
240 | -- arguments: method | ||
241 | -- nid | ||
242 | -- (optional dest-ni) | ||
243 | self <- atomically $ thisNode <$> readTVar dhtBuckets | ||
244 | let (method,xs) = break isSpace $ dropWhile isSpace s | ||
245 | (nidstr,ys) = break isSpace $ dropWhile isSpace xs | ||
246 | destination = dropWhile isSpace ys | ||
247 | goQuery qry = either (hPutClient h . ("Bad search target: "++)) | ||
248 | (goTarget qry) | ||
249 | $ dhtParseId nidstr | ||
250 | goTarget DHTQuery{..} nid = | ||
251 | go nid >>= reportResult method qshowR qshowTok h | ||
252 | where | ||
253 | go | null destination = fmap Right . qhandler self | ||
254 | | otherwise = case readEither destination of | ||
255 | Right ni -> fmap (maybe (Left "Timeout.") Right) | ||
256 | . flip (searchQuery qsearch) ni | ||
257 | Left e -> const $ return $ Left ("Bad destination: "++e) | ||
258 | maybe (hPutClient h ("Unsupported method: "++method)) | ||
259 | goQuery | ||
260 | $ Map.lookup method dhtQuery | ||
218 | 261 | ||
219 | ("save", _) | Just dht <- Map.lookup netname dhts | 262 | ("save", _) | Just dht <- Map.lookup netname dhts |
220 | -> cmd0 $ do | 263 | -> cmd0 $ do |