diff options
author | James Crayne <jim.crayne@gmail.com> | 2018-05-29 05:32:43 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2018-05-29 05:37:25 +0000 |
commit | 7a3ced91da125eebbbee399fc36162c2c3b9716d (patch) | |
tree | e9197f4ccd322cffe267fd16c2bf01e0defdc420 /examples/dhtd.hs | |
parent | 1bdc1c4080e07a12ac625272347de7649fee8a04 (diff) |
dhtd & Network.Tox.Crypto.Handlers changes:
* merge PerSession into NetCryptoSession
* define defaultUnRecHook function
* netcrypto command wip for testing
* update to sessions command
Diffstat (limited to 'examples/dhtd.hs')
-rw-r--r-- | examples/dhtd.hs | 84 |
1 files changed, 37 insertions, 47 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 47a4cd46..df8cf1c4 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -107,6 +107,7 @@ import XMPPServer | |||
107 | import Connection | 107 | import Connection |
108 | import ToxToXMPP | 108 | import ToxToXMPP |
109 | import qualified Connection.Tcp as Tcp (ConnectionEvent(..)) | 109 | import qualified Connection.Tcp as Tcp (ConnectionEvent(..)) |
110 | import Control.Concurrent.Supply | ||
110 | 111 | ||
111 | 112 | ||
112 | showReport :: [(String,String)] -> String | 113 | showReport :: [(String,String)] -> String |
@@ -396,7 +397,6 @@ data Session = Session | |||
396 | , userkeys :: TVar [(SecretKey,PublicKey)] | 397 | , userkeys :: TVar [(SecretKey,PublicKey)] |
397 | , roster :: Tox.ContactInfo | 398 | , roster :: Tox.ContactInfo |
398 | , announceToLan :: IO () | 399 | , announceToLan :: IO () |
399 | , sessions :: TVar [PerSession] | ||
400 | , connectionManager :: Maybe ConnectionManager | 400 | , connectionManager :: Maybe ConnectionManager |
401 | , onionRouter :: OnionRouter | 401 | , onionRouter :: OnionRouter |
402 | , announcer :: Announcer | 402 | , announcer :: Announcer |
@@ -433,7 +433,7 @@ clientSession0 s sock cnum h = do | |||
433 | else throwIO e | 433 | else throwIO e |
434 | 434 | ||
435 | readKeys :: TVar [(SecretKey, PublicKey)] | 435 | readKeys :: TVar [(SecretKey, PublicKey)] |
436 | -> TVar (HashMap.HashMap Tox.NodeId Account) | 436 | -> TVar (HashMap.HashMap Tox.NodeId Account) -- ContactInfo { accounts } |
437 | -> STM [(SecretKey, PublicKey)] | 437 | -> STM [(SecretKey, PublicKey)] |
438 | readKeys userkeys roster = do | 438 | readKeys userkeys roster = do |
439 | uks <- readTVar userkeys | 439 | uks <- readTVar userkeys |
@@ -475,6 +475,7 @@ clientSession s@Session{..} sock cnum h = do | |||
475 | , ["k"] | 475 | , ["k"] |
476 | , ["roster"] | 476 | , ["roster"] |
477 | , ["sessions"] | 477 | , ["sessions"] |
478 | , ["netcrypto"] | ||
478 | , ["onion"] | 479 | , ["onion"] |
479 | , ["g"] | 480 | , ["g"] |
480 | , ["p"] | 481 | , ["p"] |
@@ -679,17 +680,19 @@ clientSession s@Session{..} sock cnum h = do | |||
679 | hPutClientChunk h $ unlines [ dns, "", "Friend Requests" ] | 680 | hPutClientChunk h $ unlines [ dns, "", "Friend Requests" ] |
680 | hPutClient h $ showReport frs | 681 | hPutClient h $ showReport frs |
681 | 682 | ||
682 | ("sessions", s) | "" <- strp s | 683 | ("sessions", s') | "" <- strp s' |
683 | -> cmd0 $ do | 684 | -> cmd0 $ do |
684 | sessions' <- atomically $ readTVar sessions :: IO [PerSession] | 685 | sessions <- concat . Map.elems <$> (atomically $ readTVar (Tox.netCryptoSessionsByKey cryptosessions)) |
685 | let sessionsReport = mapM showPerSession sessions' | 686 | let sessionsReport = mapM showPerSession sessions |
686 | headers = ["Key", "NextMsg", "Dropped","Handled","Unhandled"] | 687 | headers = ["SessionID", "YourKey", "TheirKey", "NextMsg", "Dropped","Handled","Unhandled"] |
687 | showPerSession (PerSession | 688 | showPerSession (Tox.NCrypto |
688 | { perSessionMsgs = msgQ | 689 | { ncSessionId = id |
689 | , perSessionPublicKey = pubKey | 690 | , ncMyPublicKey = yourkey |
690 | , perSessionAddr = sockAddr | 691 | , ncTheirPublicKey = theirkey |
691 | , perSessionNumVar = msgNumVar | 692 | , ncLastNMsgs = msgQ |
692 | , perSessionDropCount = dropCntVar | 693 | , ncSockAddr = sockAddr |
694 | , ncMsgNumVar = msgNumVar | ||
695 | , ncDropCntVar = dropCntVar | ||
693 | }) = do | 696 | }) = do |
694 | num <- atomically (readTVar msgNumVar) | 697 | num <- atomically (readTVar msgNumVar) |
695 | dropped <- atomically (readTVar dropCntVar) | 698 | dropped <- atomically (readTVar dropCntVar) |
@@ -697,13 +700,15 @@ clientSession s@Session{..} sock cnum h = do | |||
697 | let (h,u) = partition (fst . snd) as | 700 | let (h,u) = partition (fst . snd) as |
698 | countHandled = length h | 701 | countHandled = length h |
699 | countUnhandled = length u | 702 | countUnhandled = length u |
700 | return [ show (Tox.key2id pubKey) -- "Key" | 703 | return [ printf "%x" id -- "SessionID" |
704 | , show (Tox.key2id yourkey) -- "YourKey" | ||
705 | , show (Tox.key2id theirkey)-- "TheirKey" | ||
701 | , show num -- "NextMsg" | 706 | , show num -- "NextMsg" |
702 | , show dropped -- "Dropped" | 707 | , show dropped -- "Dropped" |
703 | , show countHandled -- "Handled" | 708 | , show countHandled -- "Handled" |
704 | , show countUnhandled -- "Unhandled" | 709 | , show countUnhandled -- "Unhandled" |
705 | ] | 710 | ] |
706 | if null sessions' | 711 | if null sessions |
707 | then hPutClient h "No sessions." | 712 | then hPutClient h "No sessions." |
708 | else do | 713 | else do |
709 | rows <- sessionsReport | 714 | rows <- sessionsReport |
@@ -724,6 +729,19 @@ clientSession s@Session{..} sock cnum h = do | |||
724 | hPutClientChunk h $ "trampolines: " ++ show (IntMap.size ts) ++ "\n" | 729 | hPutClientChunk h $ "trampolines: " ++ show (IntMap.size ts) ++ "\n" |
725 | hPutClient h $ showColumns $ ["","responses","timeouts"]:r | 730 | hPutClient h $ showColumns $ ["","responses","timeouts"]:r |
726 | 731 | ||
732 | ("netcrypto", s) | ||
733 | | Just DHT{..} <- Map.lookup netname dhts | ||
734 | -> cmd0 $ do | ||
735 | case selectedKey of | ||
736 | Nothing -> hPutClient h "No key is selected, see k command." | ||
737 | Just mypubkey -> do | ||
738 | let nidstr = strp s | ||
739 | goParse = either (hPutClient h . ("Bad netcrypto target: "++)) | ||
740 | goTarget | ||
741 | $ dhtParseId nidstr | ||
742 | goTarget nid = do | ||
743 | hPutClient h "TODO: convert selected public key to private, call netCrypto.." | ||
744 | goParse | ||
727 | ("g", s) | Just DHT{..} <- Map.lookup netname dhts | 745 | ("g", s) | Just DHT{..} <- Map.lookup netname dhts |
728 | -> cmd0 $ do | 746 | -> cmd0 $ do |
729 | -- arguments: method | 747 | -- arguments: method |
@@ -1258,12 +1276,6 @@ announceToxJabberPeer echan laddr saddr pingflag tsrc tsnk | |||
1258 | 1276 | ||
1259 | #endif | 1277 | #endif |
1260 | 1278 | ||
1261 | data PerSession = PerSession { perSessionMsgs :: PacketQueue (Bool{-Handled?-},Tox.CryptoMessage) | ||
1262 | , perSessionPublicKey :: PublicKey | ||
1263 | , perSessionAddr :: SockAddr | ||
1264 | , perSessionNumVar :: TVar Word32 | ||
1265 | , perSessionDropCount :: TVar Word32 | ||
1266 | } | ||
1267 | 1279 | ||
1268 | main :: IO () | 1280 | main :: IO () |
1269 | main = runResourceT $ liftBaseWith $ \resT -> do | 1281 | main = runResourceT $ liftBaseWith $ \resT -> do |
@@ -1372,7 +1384,6 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1372 | 1384 | ||
1373 | crypto <- Tox.newCrypto | 1385 | crypto <- Tox.newCrypto |
1374 | netCryptoSessionsState <- Tox.newSessionsState crypto Tox.defaultUnRecHook Tox.defaultCryptoDataHooks | 1386 | netCryptoSessionsState <- Tox.newSessionsState crypto Tox.defaultUnRecHook Tox.defaultCryptoDataHooks |
1375 | sessions <- atomically (newTVar []) :: IO (TVar [PerSession]) | ||
1376 | (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- case porttox opts of | 1387 | (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- case porttox opts of |
1377 | "" -> return (Nothing,return (), Map.empty, return [],[]) | 1388 | "" -> return (Nothing,return (), Map.empty, return [],[]) |
1378 | toxport -> do | 1389 | toxport -> do |
@@ -1602,16 +1613,6 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1602 | -- allsessionsMap <- atomically $ readTVar (netCryptoSessions netCryptoSessionsState) | 1613 | -- allsessionsMap <- atomically $ readTVar (netCryptoSessions netCryptoSessionsState) |
1603 | let sockAddr = Tox.ncSockAddr netcrypto | 1614 | let sockAddr = Tox.ncSockAddr netcrypto |
1604 | pubKey = Tox.ncTheirPublicKey netcrypto | 1615 | pubKey = Tox.ncTheirPublicKey netcrypto |
1605 | msgQ <- atomically (Data.PacketQueue.newOverwrite 10 0 :: STM (PacketQueue (Bool,Tox.CryptoMessage))) | ||
1606 | msgNumVar <- atomically (newTVar 0) | ||
1607 | dropCntVar <- atomically (newTVar 0) | ||
1608 | let perSession = PerSession { perSessionMsgs = msgQ | ||
1609 | , perSessionPublicKey = pubKey | ||
1610 | , perSessionAddr = sockAddr | ||
1611 | , perSessionNumVar = msgNumVar | ||
1612 | , perSessionDropCount = dropCntVar | ||
1613 | } | ||
1614 | atomically $ modifyTVar' sessions (perSession:) | ||
1615 | tmchan <- atomically newTMChan | 1616 | tmchan <- atomically newTMChan |
1616 | let Just pingMachine = Tox.ncPingMachine netcrypto | 1617 | let Just pingMachine = Tox.ncPingMachine netcrypto |
1617 | pingflag = readTVar (pingFlag pingMachine) | 1618 | pingflag = readTVar (pingFlag pingMachine) |
@@ -1624,21 +1625,11 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1624 | announceToxJabberPeer (xmppEventChannel sv) addrTox (Tox.ncSockAddr netcrypto) pingflag xmppSrc xmppSink | 1625 | announceToxJabberPeer (xmppEventChannel sv) addrTox (Tox.ncSockAddr netcrypto) pingflag xmppSrc xmppSink |
1625 | -- TODO: Update toxContactInfo, connected. | 1626 | -- TODO: Update toxContactInfo, connected. |
1626 | #endif | 1627 | #endif |
1627 | let handleIncoming typ session cm | any ($ typ) [Tox.isKillPacket, Tox.isOFFLINE] = atomically $ do | 1628 | atomically $ do |
1628 | closeTMChan tmchan | 1629 | supply <- readTVar (Tox.listenerIDSupply netCryptoSessionsState) |
1629 | Tox.forgetCrypto crypto netCryptoSessionsState netcrypto | 1630 | let (listenerId,supply') = freshId supply |
1630 | return Nothing | 1631 | writeTVar (Tox.listenerIDSupply netCryptoSessionsState) supply' |
1631 | handleIncoming mTyp session cm = do | 1632 | modifyTVar' (Tox.ncListeners netcrypto) (IntMap.insert listenerId (0,tmchan)) |
1632 | atomically $ do | ||
1633 | num <- readTVar msgNumVar | ||
1634 | (wraps,offset) <- enqueue msgQ num (False,cm) | ||
1635 | capacity <- getCapacity msgQ | ||
1636 | let dropped = wraps * capacity + offset | ||
1637 | modifyTVar' msgNumVar (+1) | ||
1638 | writeTVar dropCntVar dropped | ||
1639 | atomically $ writeTMChan tmchan cm -- (Tox.bufferData cd) | ||
1640 | return Nothing | ||
1641 | atomically $ writeTVar (Tox.ncUnrecognizedHook netcrypto) handleIncoming | ||
1642 | return Nothing | 1633 | return Nothing |
1643 | 1634 | ||
1644 | let dhts = Map.union btdhts toxdhts | 1635 | let dhts = Map.union btdhts toxdhts |
@@ -1667,7 +1658,6 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1667 | , userkeys = toxids | 1658 | , userkeys = toxids |
1668 | , roster = rstr | 1659 | , roster = rstr |
1669 | , announceToLan = fromMaybe (return ()) $ Tox.toxAnnounceToLan <$> mbtox | 1660 | , announceToLan = fromMaybe (return ()) $ Tox.toxAnnounceToLan <$> mbtox |
1670 | , sessions = sessions | ||
1671 | , connectionManager = ConnectionManager <$> mconns | 1661 | , connectionManager = ConnectionManager <$> mconns |
1672 | , onionRouter = orouter | 1662 | , onionRouter = orouter |
1673 | , externalAddresses = liftM2 (++) btips toxips | 1663 | , externalAddresses = liftM2 (++) btips toxips |