diff options
author | joe <joe@jerkface.net> | 2017-10-29 20:52:22 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-10-29 20:52:22 -0400 |
commit | d65e0f858db8d8df25da24c54bc125cc4eede183 (patch) | |
tree | a4346ea4ce19713a32a40c2bac2e325882bcdd8a /examples/dhtd.hs | |
parent | 31bfaac79df26da3d356181c9c6af9c12034656d (diff) |
WIP: "a" command (recurring announcements) (Part 2)
Diffstat (limited to 'examples/dhtd.hs')
-rw-r--r-- | examples/dhtd.hs | 59 |
1 files changed, 45 insertions, 14 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index fc2996a6..14076463 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -17,6 +17,7 @@ | |||
17 | {-# LANGUAGE TypeOperators #-} | 17 | {-# LANGUAGE TypeOperators #-} |
18 | 18 | ||
19 | import Control.Arrow | 19 | import Control.Arrow |
20 | import Control.Applicative | ||
20 | import Control.Concurrent.STM | 21 | import Control.Concurrent.STM |
21 | import Control.DeepSeq | 22 | import Control.DeepSeq |
22 | import Control.Exception | 23 | import Control.Exception |
@@ -107,17 +108,25 @@ hPutClientChunk h s = hPutStr h (' ' : marshalForClient s) | |||
107 | data DHTQuery nid ni = forall addr r tok. | 108 | data DHTQuery nid ni = forall addr r tok. |
108 | ( Ord addr | 109 | ( Ord addr |
109 | , Typeable r | 110 | , Typeable r |
110 | )=> DHTQuery | 111 | , Typeable tok |
112 | , Typeable ni | ||
113 | ) => DHTQuery | ||
111 | { qsearch :: Search nid addr tok ni r | 114 | { qsearch :: Search nid addr tok ni r |
112 | , qhandler :: ni -> nid -> IO ([ni], [r], tok) -- ^ Invoked on local node, when there is no query destination. | 115 | , qhandler :: ni -> nid -> IO ([ni], [r], tok) -- ^ Invoked on local node, when there is no query destination. |
113 | , qshowR :: r -> String | 116 | , qshowR :: r -> String |
114 | , qshowTok :: tok -> Maybe String | 117 | , qshowTok :: tok -> Maybe String |
115 | } | 118 | } |
116 | 119 | ||
117 | data DHTAnnouncable = forall dta ni r. Show r => DHTAnnouncable | 120 | data DHTAnnouncable = forall dta tok ni r. |
118 | { announceParseData :: String -> String -> IO (Either String dta) | 121 | ( Show r |
122 | , Typeable dta | ||
123 | , Typeable tok | ||
124 | , Typeable ni | ||
125 | , Typeable r | ||
126 | ) => DHTAnnouncable | ||
127 | { announceParseData :: String -> String -> IO (Either String (dta,tok)) | ||
119 | , announceParseAddress :: String -> Either String ni | 128 | , announceParseAddress :: String -> Either String ni |
120 | , announceSendData :: dta -> Maybe ni -> IO (Maybe r) | 129 | , announceSendData :: (dta,tok) -> Maybe ni -> IO (Maybe r) |
121 | } | 130 | } |
122 | 131 | ||
123 | data DHTSearch nid ni = forall addr tok r. DHTSearch | 132 | data DHTSearch nid ni = forall addr tok r. DHTSearch |
@@ -538,10 +547,30 @@ clientSession s@Session{..} sock cnum h = do | |||
538 | (dtastr,ys) = break isSpace $ dropWhile isSpace xs | 547 | (dtastr,ys) = break isSpace $ dropWhile isSpace xs |
539 | a = Map.lookup method dhtAnnouncables | 548 | a = Map.lookup method dhtAnnouncables |
540 | q = Map.lookup method dhtQuery | 549 | q = Map.lookup method dhtQuery |
541 | doit '+' = schedule | 550 | doit :: Char -> proxy ni -> Announcer -> AnnounceMethod ni -> info -> IO () |
542 | doit '-' = cancel | 551 | doit '+' _ = schedule |
543 | doit _ = \_ _ _ -> hPutClient h "Starting(+) or canceling(-)?" | 552 | doit '-' _ = cancel |
544 | doit op announcer _announcemethod _what | 553 | doit _ _ = \_ _ _ -> hPutClient h "Starting(+) or canceling(-)?" |
554 | matchingResult :: | ||
555 | ( Typeable sr | ||
556 | , Typeable stok | ||
557 | , Typeable sni | ||
558 | , Typeable pr | ||
559 | , Typeable ptok | ||
560 | , Typeable pni ) | ||
561 | => Search nid addr stok sni sr | ||
562 | -> ((pr,ptok) -> Maybe pni -> IO (Maybe pubr)) | ||
563 | -> Maybe (sr :~: pr, stok :~: ptok, sni :~: pni ) | ||
564 | matchingResult _ _ = liftA3 (\a b c -> (a,b,c)) eqT eqT eqT | ||
565 | mameth = do | ||
566 | DHTQuery {qsearch} <- q | ||
567 | DHTAnnouncable {announceSendData} <- a | ||
568 | (Refl,Refl,nr@Refl) <- matchingResult qsearch announceSendData | ||
569 | return $ doit op nr announcer | ||
570 | (AnnounceMethod qsearch announceSendData) | ||
571 | _what | ||
572 | fromMaybe (hPutClient h "error.") mameth | ||
573 | |||
545 | ("s", s) | Just dht@DHT{..} <- Map.lookup netname dhts | 574 | ("s", s) | Just dht@DHT{..} <- Map.lookup netname dhts |
546 | -> cmd0 $ do | 575 | -> cmd0 $ do |
547 | let (method,xs) = break isSpace s | 576 | let (method,xs) = break isSpace s |
@@ -716,19 +745,21 @@ main = do | |||
716 | , dhtSearches = mainlineSearches | 745 | , dhtSearches = mainlineSearches |
717 | , dhtFallbackNodes = Mainline.bootstrapNodes wantip | 746 | , dhtFallbackNodes = Mainline.bootstrapNodes wantip |
718 | , dhtAnnouncables = Map.fromList | 747 | , dhtAnnouncables = Map.fromList |
719 | [ ("peer", DHTAnnouncable { announceSendData = \dta -> \case | 748 | [ ("peer", DHTAnnouncable { announceSendData = \(ih,tok) -> \case |
720 | Just ni -> Mainline.announce bt dta ni | 749 | Just ni -> do |
750 | port <- atomically $ readTVar peerPort | ||
751 | let dta = Mainline.mkAnnounce port ih tok | ||
752 | Mainline.announce bt dta ni | ||
721 | Nothing -> return Nothing | 753 | Nothing -> return Nothing |
722 | , announceParseAddress = readEither | 754 | , announceParseAddress = readEither |
723 | , announceParseData = \str tokstr -> do | 755 | , announceParseData = \str tokstr -> do |
724 | port <- atomically $ readTVar peerPort | ||
725 | let ih = read str | 756 | let ih = read str |
726 | tok = read tokstr | 757 | tok = read tokstr |
727 | return $ Right $ Mainline.mkAnnounce port ih tok | 758 | return $ Right (ih,tok) |
728 | }) | 759 | }) |
729 | , ("port", DHTAnnouncable { announceParseData = \portstr _ -> return $ readEither portstr | 760 | , ("port", DHTAnnouncable { announceParseData = \portstr _ -> return $ (, ()) <$> readEither portstr |
730 | , announceParseAddress = const $ Right () | 761 | , announceParseAddress = const $ Right () |
731 | , announceSendData = \dta -> \case | 762 | , announceSendData = \(dta,()) -> \case |
732 | Nothing -> do atomically $ writeTVar peerPort (dta :: PortNumber) | 763 | Nothing -> do atomically $ writeTVar peerPort (dta :: PortNumber) |
733 | return $ Just dta | 764 | return $ Just dta |
734 | Just _ -> return Nothing | 765 | Just _ -> return Nothing |