summaryrefslogtreecommitdiff
path: root/examples/dhtd.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-10-24 20:46:35 -0400
committerjoe <joe@jerkface.net>2017-10-24 20:46:35 -0400
commitf0f355d6ff8a68b5240301f882f6d5a9a77fdba1 (patch)
treebfb415ea0ecdd51650e28fc60c9a8ba4dcc7f150 /examples/dhtd.hs
parentc31ed656d55bbdb387d91464e51840e90503223a (diff)
Added Roster data and dhtkey handler.
Diffstat (limited to 'examples/dhtd.hs')
-rw-r--r--examples/dhtd.hs23
1 files changed, 16 insertions, 7 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index aebf16cc..cf0328e8 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -73,6 +73,7 @@ import qualified Network.Tox.DHT.Handlers as Tox
73import qualified Network.Tox.Onion.Transport as Tox 73import qualified Network.Tox.Onion.Transport as Tox
74import qualified Network.Tox.Onion.Handlers as Tox 74import qualified Network.Tox.Onion.Handlers as Tox
75import Data.Typeable 75import Data.Typeable
76import Roster
76 77
77showReport :: [(String,String)] -> String 78showReport :: [(String,String)] -> String
78showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs 79showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs
@@ -292,6 +293,7 @@ data Session = Session
292 , swarms :: Mainline.SwarmsDatabase 293 , swarms :: Mainline.SwarmsDatabase
293 , toxkeys :: TVar Tox.AnnouncedKeys 294 , toxkeys :: TVar Tox.AnnouncedKeys
294 , userkeys :: TVar [(SecretKey,PublicKey)] 295 , userkeys :: TVar [(SecretKey,PublicKey)]
296 , roster :: Roster
295 , signalQuit :: MVar () 297 , signalQuit :: MVar ()
296 } 298 }
297 299
@@ -394,6 +396,7 @@ clientSession s@Session{..} sock cnum h = do
394 oldks <- atomically $ do 396 oldks <- atomically $ do
395 ks <- readTVar userkeys 397 ks <- readTVar userkeys
396 modifyTVar userkeys ((secret,pubkey):) 398 modifyTVar userkeys ((secret,pubkey):)
399 addRoster roster secret
397 return ks 400 return ks
398 let asString = show . Tox.key2id 401 let asString = show . Tox.key2id
399 hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks 402 hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks
@@ -408,8 +411,11 @@ clientSession s@Session{..} sock cnum h = do
408 f x = error (concat ["Assertion fail at ", __FILE__, ":", show __LINE__]) 411 f x = error (concat ["Assertion fail at ", __FILE__, ":", show __LINE__])
409 let toPair x = (x,toPublic x) 412 let toPair x = (x,toPublic x)
410 pairs = map (toPair . f) mbSecs 413 pairs = map (toPair . f) mbSecs
411 oldks <- atomically $ readTVar userkeys 414 oldks <- atomically $ do
412 atomically $ modifyTVar userkeys (pairs ++) 415 oldks <- readTVar userkeys
416 modifyTVar userkeys (pairs ++)
417 forM pairs $ \(sk,_) -> addRoster roster sk
418 return oldks
413 hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks 419 hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks
414 ++ map (mappend " *" . show . Tox.key2id .snd) pairs 420 ++ map (mappend " *" . show . Tox.key2id .snd) pairs
415 | ("del":secs) <- words s 421 | ("del":secs) <- words s
@@ -421,6 +427,7 @@ clientSession s@Session{..} sock cnum h = do
421 pairs = map (toPair . f) mbSecs 427 pairs = map (toPair . f) mbSecs
422 ks <- atomically $ do 428 ks <- atomically $ do
423 modifyTVar userkeys (filter (`notElem` pairs) ) 429 modifyTVar userkeys (filter (`notElem` pairs) )
430 forM pairs $ \(_,pk) -> delRoster roster pk
424 readTVar userkeys 431 readTVar userkeys
425 hPutClient h . showReport $ map mkrow ks 432 hPutClient h . showReport $ map mkrow ks
426 ("g", s) | Just DHT{..} <- Map.lookup netname dhts 433 ("g", s) | Just DHT{..} <- Map.lookup netname dhts
@@ -672,9 +679,8 @@ main = do
672 679
673 keysdb <- Tox.newKeysDatabase 680 keysdb <- Tox.newKeysDatabase
674 681
675 (toxids,quitTox,toxdhts,toxips,taddrs) <- case porttox opts of 682 (mbtox,quitTox,toxdhts,toxips,taddrs) <- case porttox opts of
676 "" -> do keys <- atomically $ newTVar [] 683 "" -> return (Nothing,return (), Map.empty, return [],[])
677 return (keys,return (), Map.empty, return [],[])
678 toxport -> do 684 toxport -> do
679 addrTox <- getBindAddress toxport (ip6tox opts) 685 addrTox <- getBindAddress toxport (ip6tox opts)
680 tox <- Tox.newTox keysdb addrTox 686 tox <- Tox.newTox keysdb addrTox
@@ -755,14 +761,16 @@ main = do
755 ips :: IO [SockAddr] 761 ips :: IO [SockAddr]
756 ips = readExternals Tox.nodeAddr [ Tox.routing4 $ Tox.toxRouting tox 762 ips = readExternals Tox.nodeAddr [ Tox.routing4 $ Tox.toxRouting tox
757 , Tox.routing6 $ Tox.toxRouting tox ] 763 , Tox.routing6 $ Tox.toxRouting tox ]
758 return (userKeys (Tox.toxCryptoKeys tox), quitTox, dhts, ips, [addrTox]) 764 return (Just tox, quitTox, dhts, ips, [addrTox])
759
760 _ <- UPNP.requestPorts "dht-client" $ map (Datagram,) $ baddrs ++ taddrs 765 _ <- UPNP.requestPorts "dht-client" $ map (Datagram,) $ baddrs ++ taddrs
761 766
762 let dhts = Map.union btdhts toxdhts 767 let dhts = Map.union btdhts toxdhts
763 768
764 waitForSignal <- do 769 waitForSignal <- do
765 signalQuit <- newEmptyMVar 770 signalQuit <- newEmptyMVar
771 (toxids,rstr) <- fromMaybe ((,) <$> atomically (newTVar []) <*> newRoster) $ do
772 tox <- mbtox
773 return $ return ( userKeys (Tox.toxCryptoKeys tox), Tox.toxRoster tox )
766 let session = clientSession $ Session 774 let session = clientSession $ Session
767 { netname = concat $ take 1 $ Map.keys dhts -- initial default DHT 775 { netname = concat $ take 1 $ Map.keys dhts -- initial default DHT
768 , dhts = dhts -- all DHTs 776 , dhts = dhts -- all DHTs
@@ -770,6 +778,7 @@ main = do
770 , swarms = swarms 778 , swarms = swarms
771 , toxkeys = keysdb 779 , toxkeys = keysdb
772 , userkeys = toxids 780 , userkeys = toxids
781 , roster = rstr
773 , externalAddresses = liftM2 (++) btips toxips 782 , externalAddresses = liftM2 (++) btips toxips
774 } 783 }
775 srv <- streamServer (withSession session) (SockAddrUnix "dht.sock") 784 srv <- streamServer (withSession session) (SockAddrUnix "dht.sock")