diff options
-rw-r--r-- | examples/dhtd.hs | 62 |
1 files changed, 38 insertions, 24 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 8ceafd00..89b747be 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -150,18 +150,19 @@ data DHTQuery nid ni = forall addr r tok. | |||
150 | 150 | ||
151 | data DHTAnnouncable nid = forall dta tok ni r. | 151 | data DHTAnnouncable nid = forall dta tok ni r. |
152 | ( Show r | 152 | ( Show r |
153 | , Typeable dta | 153 | , Typeable dta -- information being announced |
154 | , Typeable tok | 154 | , Typeable tok -- token |
155 | , Typeable r | 155 | , Typeable r -- search result |
156 | , Typeable ni | 156 | , Typeable ni -- node |
157 | ) => DHTAnnouncable | 157 | ) => DHTAnnouncable |
158 | { announceParseData :: String -> Either String dta | 158 | { announceParseData :: String -> Either String dta |
159 | , announceParseToken :: dta -> String -> Either String tok | 159 | , announceParseToken :: dta -> String -> Either String tok |
160 | , announceParseAddress :: String -> Either String ni | 160 | , announceParseAddress :: String -> Either String ni |
161 | , announceSendData :: Either (dta -> r -> IO (Maybe r)) | 161 | , announceSendData :: Either (dta -> r -> IO (Maybe r)) -- TODO ( String {- search name -} |
162 | (dta -> tok -> Maybe ni -> IO (Maybe r)) | 162 | -- , PublicKey {- me -} -> dta -> r -> IO ()) |
163 | , announceInterval :: POSIXTime | 163 | (dta -> tok -> Maybe ni -> IO (Maybe r)) |
164 | , qresultAddr :: dta -> nid | 164 | , announceInterval :: POSIXTime |
165 | , announceTarget :: dta -> nid | ||
165 | } | 166 | } |
166 | 167 | ||
167 | data DHTSearch nid ni = forall addr tok r. DHTSearch | 168 | data DHTSearch nid ni = forall addr tok r. DHTSearch |
@@ -584,6 +585,7 @@ clientSession s@Session{..} sock cnum h = do | |||
584 | hPutClient h . showReport $ map mkrow ks ++ case skey >>= encodeSecret of | 585 | hPutClient h . showReport $ map mkrow ks ++ case skey >>= encodeSecret of |
585 | Just x -> [("",""),("dht-key:",B.unpack x)] | 586 | Just x -> [("",""),("dht-key:",B.unpack x)] |
586 | Nothing -> [] | 587 | Nothing -> [] |
588 | -- TODO | ("sel":secs) <- words s -> select active key | ||
587 | | ("add":secs) <- words s | 589 | | ("add":secs) <- words s |
588 | , mbSecs <- map (decodeSecret . B.pack) secs | 590 | , mbSecs <- map (decodeSecret . B.pack) secs |
589 | , all isJust mbSecs -> cmd0 $ do | 591 | , all isJust mbSecs -> cmd0 $ do |
@@ -783,7 +785,7 @@ clientSession s@Session{..} sock cnum h = do | |||
783 | DHTAnnouncable { announceSendData | 785 | DHTAnnouncable { announceSendData |
784 | , announceParseData | 786 | , announceParseData |
785 | , announceInterval | 787 | , announceInterval |
786 | , qresultAddr } <- a | 788 | , announceTarget } <- a |
787 | DHTQuery { qsearch } <- q | 789 | DHTQuery { qsearch } <- q |
788 | asend <- either (const Nothing) Just announceSendData | 790 | asend <- either (const Nothing) Just announceSendData |
789 | Refl <- matchingTok qsearch asend | 791 | Refl <- matchingTok qsearch asend |
@@ -793,7 +795,7 @@ clientSession s@Session{..} sock cnum h = do | |||
793 | DHTAnnouncable { announceSendData | 795 | DHTAnnouncable { announceSendData |
794 | , announceParseData | 796 | , announceParseData |
795 | , announceInterval | 797 | , announceInterval |
796 | , qresultAddr } <- a | 798 | , announceTarget } <- a |
797 | DHTQuery { qsearch } <- q | 799 | DHTQuery { qsearch } <- q |
798 | asend <- either (const Nothing) Just announceSendData | 800 | asend <- either (const Nothing) Just announceSendData |
799 | Refl <- matchingNI qsearch asend | 801 | Refl <- matchingNI qsearch asend |
@@ -802,7 +804,7 @@ clientSession s@Session{..} sock cnum h = do | |||
802 | DHTAnnouncable { announceSendData | 804 | DHTAnnouncable { announceSendData |
803 | , announceParseData | 805 | , announceParseData |
804 | , announceInterval | 806 | , announceInterval |
805 | , qresultAddr } <- a | 807 | , announceTarget } <- a |
806 | DHTQuery { qsearch } <- q | 808 | DHTQuery { qsearch } <- q |
807 | asend <- either (const Nothing) Just announceSendData | 809 | asend <- either (const Nothing) Just announceSendData |
808 | (Refl, Refl) <- matchingResult qsearch asend | 810 | (Refl, Refl) <- matchingResult qsearch asend |
@@ -813,12 +815,12 @@ clientSession s@Session{..} sock cnum h = do | |||
813 | doit op announcer | 815 | doit op announcer |
814 | akey | 816 | akey |
815 | (AnnounceMethod qsearch (Right asend) dhtBuckets | 817 | (AnnounceMethod qsearch (Right asend) dhtBuckets |
816 | (qresultAddr dta) | 818 | (announceTarget dta) |
817 | announceInterval) | 819 | announceInterval) |
818 | dta | 820 | dta |
819 | case op of | 821 | case op of |
820 | '+' -> hPutClient h $ "Announcing at " ++ show (qresultAddr dta) ++ "." | 822 | '+' -> hPutClient h $ "Announcing at " ++ show (announceTarget dta) ++ "." |
821 | '-' -> hPutClient h $ "Canceling " ++ show (qresultAddr dta) ++ "." | 823 | '-' -> hPutClient h $ "Canceling " ++ show (announceTarget dta) ++ "." |
822 | let aerror = unlines | 824 | let aerror = unlines |
823 | [ "announce error." | 825 | [ "announce error." |
824 | , "method = " ++ method | 826 | , "method = " ++ method |
@@ -1136,7 +1138,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1136 | , announceParseData = readEither | 1138 | , announceParseData = readEither |
1137 | , announceParseToken = const $ readEither | 1139 | , announceParseToken = const $ readEither |
1138 | , announceInterval = 60 -- TODO: Is one minute good? | 1140 | , announceInterval = 60 -- TODO: Is one minute good? |
1139 | , qresultAddr = (read . show) -- TODO: InfoHash -> NodeId -- peer | 1141 | , announceTarget = (read . show) -- TODO: InfoHash -> NodeId -- peer |
1140 | }) | 1142 | }) |
1141 | , ("port", DHTAnnouncable { announceParseData = readEither | 1143 | , ("port", DHTAnnouncable { announceParseData = readEither |
1142 | , announceParseToken = \_ _ -> return () | 1144 | , announceParseToken = \_ _ -> return () |
@@ -1147,7 +1149,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1147 | Just _ -> return Nothing | 1149 | Just _ -> return Nothing |
1148 | , announceInterval = 0 -- TODO: The "port" setting should probably | 1150 | , announceInterval = 0 -- TODO: The "port" setting should probably |
1149 | -- be a command rather than an announcement. | 1151 | -- be a command rather than an announcement. |
1150 | , qresultAddr = const $ Mainline.zeroID | 1152 | , announceTarget = const $ Mainline.zeroID |
1151 | })] | 1153 | })] |
1152 | 1154 | ||
1153 | , dhtSecretKey = return Nothing | 1155 | , dhtSecretKey = return Nothing |
@@ -1243,7 +1245,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1243 | , announceParseAddress = readEither | 1245 | , announceParseAddress = readEither |
1244 | , announceParseToken = const $ readEither | 1246 | , announceParseToken = const $ readEither |
1245 | , announceParseData = fmap Tox.id2key . readEither | 1247 | , announceParseData = fmap Tox.id2key . readEither |
1246 | , qresultAddr = Tox.key2id -- toxid | 1248 | , announceTarget = Tox.key2id -- toxid |
1247 | 1249 | ||
1248 | -- For peers we are announcing ourselves to, if we are not | 1250 | -- For peers we are announcing ourselves to, if we are not |
1249 | -- announced to them toxcore tries every 3 seconds to | 1251 | -- announced to them toxcore tries every 3 seconds to |
@@ -1261,6 +1263,12 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1261 | 1263 | ||
1262 | }) | 1264 | }) |
1263 | -- FIXME: Should use announceSendData = Left ... | 1265 | -- FIXME: Should use announceSendData = Left ... |
1266 | -- | ||
1267 | -- Current Desired | ||
1268 | -- ni = AnnouncedRendezvous --> NodeInfo | ||
1269 | -- r = () --> Rendezvous | ||
1270 | -- tok = () --> Nonce32 ... or PublicKey(me) ? | ||
1271 | -- dta = PublicKey(me) --> PublicKey(them) | ||
1264 | , ("dhtkey", DHTAnnouncable { announceSendData = Right $ \pubkey () -> \case | 1272 | , ("dhtkey", DHTAnnouncable { announceSendData = Right $ \pubkey () -> \case |
1265 | Just addr -> do | 1273 | Just addr -> do |
1266 | dkey <- Tox.getContactInfo tox | 1274 | dkey <- Tox.getContactInfo tox |
@@ -1270,10 +1278,10 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1270 | (pubkey,Tox.OnionDHTPublicKey dkey) | 1278 | (pubkey,Tox.OnionDHTPublicKey dkey) |
1271 | return $ Just () | 1279 | return $ Just () |
1272 | Nothing -> return Nothing | 1280 | Nothing -> return Nothing |
1273 | , announceParseAddress = readEither | 1281 | , announceParseAddress = readEither |
1274 | , announceParseToken = \_ _ -> return () | 1282 | , announceParseToken = \_ _ -> return () |
1275 | , announceParseData = fmap Tox.id2key . readEither | 1283 | , announceParseData = fmap Tox.id2key . readEither |
1276 | , qresultAddr = Tox.key2id | 1284 | , announceTarget = Tox.key2id |
1277 | 1285 | ||
1278 | -- We send this packet every 30 seconds if there is more | 1286 | -- We send this packet every 30 seconds if there is more |
1279 | -- than one peer (in the 8) that says they our friend is | 1287 | -- than one peer (in the 8) that says they our friend is |
@@ -1293,6 +1301,12 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1293 | 1301 | ||
1294 | }) | 1302 | }) |
1295 | -- FIXME: Should use announceSendData = Left ... | 1303 | -- FIXME: Should use announceSendData = Left ... |
1304 | -- | ||
1305 | -- Current Desired | ||
1306 | -- ni = AnnouncedRendezvous --> NodeInfo | ||
1307 | -- r = () --> Rendezvous | ||
1308 | -- tok = NoSpam --> Nonce32 ... or PublicKey(me) ? | ||
1309 | -- dta = PublicKey(me) --> PublicKey(them) + NoSpam | ||
1296 | , ("friend", DHTAnnouncable { announceSendData = Right $ \pubkey nospam -> \case | 1310 | , ("friend", DHTAnnouncable { announceSendData = Right $ \pubkey nospam -> \case |
1297 | Just addr -> do | 1311 | Just addr -> do |
1298 | let fr = Tox.FriendRequest nospam txt | 1312 | let fr = Tox.FriendRequest nospam txt |
@@ -1312,7 +1326,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1312 | (Tox.verifyChecksum pubkey) | 1326 | (Tox.verifyChecksum pubkey) |
1313 | chksum | 1327 | chksum |
1314 | return nospam | 1328 | return nospam |
1315 | , qresultAddr = Tox.key2id | 1329 | , announceTarget = Tox.key2id |
1316 | 1330 | ||
1317 | -- Friend requests are sent with exponentially increasing | 1331 | -- Friend requests are sent with exponentially increasing |
1318 | -- interval of 2 seconds, 4 seconds, 8 seconds, etc... in | 1332 | -- interval of 2 seconds, 4 seconds, 8 seconds, etc... in |