diff options
-rw-r--r-- | examples/dhtd.hs | 49 | ||||
-rw-r--r-- | src/Data/PacketQueue.hs | 9 |
2 files changed, 57 insertions, 1 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 2f903d5f..fed2976f 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -34,6 +34,7 @@ import Data.Conduit as C | |||
34 | import Data.Function | 34 | import Data.Function |
35 | import Data.Hashable | 35 | import Data.Hashable |
36 | import Data.List | 36 | import Data.List |
37 | import Data.Word | ||
37 | import qualified Data.IntMap.Strict as IntMap | 38 | import qualified Data.IntMap.Strict as IntMap |
38 | import qualified Data.Map.Strict as Map | 39 | import qualified Data.Map.Strict as Map |
39 | import Data.Maybe | 40 | import Data.Maybe |
@@ -394,6 +395,7 @@ data Session = Session | |||
394 | , toxkeys :: TVar Tox.AnnouncedKeys | 395 | , toxkeys :: TVar Tox.AnnouncedKeys |
395 | , userkeys :: TVar [(SecretKey,PublicKey)] | 396 | , userkeys :: TVar [(SecretKey,PublicKey)] |
396 | , roster :: Tox.ContactInfo | 397 | , roster :: Tox.ContactInfo |
398 | , sessions :: TVar [PerSession] | ||
397 | , connectionManager :: Maybe ConnectionManager | 399 | , connectionManager :: Maybe ConnectionManager |
398 | , onionRouter :: OnionRouter | 400 | , onionRouter :: OnionRouter |
399 | , announcer :: Announcer | 401 | , announcer :: Announcer |
@@ -470,6 +472,7 @@ clientSession s@Session{..} sock cnum h = do | |||
470 | , ["ls"] | 472 | , ["ls"] |
471 | , ["k"] | 473 | , ["k"] |
472 | , ["roster"] | 474 | , ["roster"] |
475 | , ["sessions"] | ||
473 | , ["onion"] | 476 | , ["onion"] |
474 | , ["g"] | 477 | , ["g"] |
475 | , ["p"] | 478 | , ["p"] |
@@ -666,6 +669,33 @@ clientSession s@Session{..} sock cnum h = do | |||
666 | hPutClientChunk h $ unlines [ dns, "", "Friend Requests" ] | 669 | hPutClientChunk h $ unlines [ dns, "", "Friend Requests" ] |
667 | hPutClient h $ showReport frs | 670 | hPutClient h $ showReport frs |
668 | 671 | ||
672 | ("sessions", s) | "" <- strp s | ||
673 | -> cmd0 $ do | ||
674 | sessions' <- atomically $ readTVar sessions :: IO [PerSession] | ||
675 | let sessionsReport = mapM showPerSession sessions' | ||
676 | headers = ["Key", "NextMsg", "Handled","Unhandled"] | ||
677 | showPerSession (PerSession | ||
678 | { perSessionMsgs = msgQ | ||
679 | , perSessionPublicKey = pubKey | ||
680 | , perSessionAddr = sockAddr | ||
681 | , perSessionNumVar = msgNumVar | ||
682 | }) = do | ||
683 | num <- atomically (readTVar msgNumVar) | ||
684 | as <- atomically (packetQueueViewList msgQ) | ||
685 | let (h,u) = partition (fst . snd) as | ||
686 | countHandled = length h | ||
687 | countUnhandled = length u | ||
688 | return [ show (Tox.key2id pubKey) -- "Key" | ||
689 | , show num -- "NextMsg" | ||
690 | , show countHandled -- "Handled" | ||
691 | , show countUnhandled -- "Unhandled" | ||
692 | ] | ||
693 | if null sessions' | ||
694 | then hPutClient h "No sessions." | ||
695 | else do | ||
696 | rows <- sessionsReport | ||
697 | hPutClient h (showColumns (headers:rows)) | ||
698 | |||
669 | ("onion", s) -> cmd0 $ join $ atomically $ do | 699 | ("onion", s) -> cmd0 $ join $ atomically $ do |
670 | rm <- readTVar $ routeMap onionRouter | 700 | rm <- readTVar $ routeMap onionRouter |
671 | ts <- readTVar $ trampolineNodes onionRouter | 701 | ts <- readTVar $ trampolineNodes onionRouter |
@@ -1218,6 +1248,7 @@ announceToxJabberPeer echan laddr saddr pingflag tsrc tsnk | |||
1218 | data PerSession = PerSession { perSessionMsgs :: PacketQueue (Bool{-Handled?-},Tox.CryptoMessage) | 1248 | data PerSession = PerSession { perSessionMsgs :: PacketQueue (Bool{-Handled?-},Tox.CryptoMessage) |
1219 | , perSessionPublicKey :: PublicKey | 1249 | , perSessionPublicKey :: PublicKey |
1220 | , perSessionAddr :: SockAddr | 1250 | , perSessionAddr :: SockAddr |
1251 | , perSessionNumVar :: TVar Word32 | ||
1221 | } | 1252 | } |
1222 | 1253 | ||
1223 | main :: IO () | 1254 | main :: IO () |
@@ -1328,7 +1359,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1328 | crypto <- Tox.newCrypto | 1359 | crypto <- Tox.newCrypto |
1329 | netCryptoSessionsState <- Tox.newSessionsState crypto Tox.defaultUnRecHook Tox.defaultCryptoDataHooks | 1360 | netCryptoSessionsState <- Tox.newSessionsState crypto Tox.defaultUnRecHook Tox.defaultCryptoDataHooks |
1330 | sessions <- atomically (newTVar []) :: IO (TVar [PerSession]) | 1361 | sessions <- atomically (newTVar []) :: IO (TVar [PerSession]) |
1331 | (mbtox,quitTox,toxdhts,toxips,taddrs) <- case porttox opts of | 1362 | (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- case porttox opts of |
1332 | "" -> return (Nothing,return (), Map.empty, return [],[]) | 1363 | "" -> return (Nothing,return (), Map.empty, return [],[]) |
1333 | toxport -> do | 1364 | toxport -> do |
1334 | addrTox <- getBindAddress toxport (ip6tox opts) | 1365 | addrTox <- getBindAddress toxport (ip6tox opts) |
@@ -1538,6 +1569,17 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1538 | 1569 | ||
1539 | forM_ (take 1 taddrs) $ \addrTox -> do | 1570 | forM_ (take 1 taddrs) $ \addrTox -> do |
1540 | atomically $ Tox.addNewSessionHook netCryptoSessionsState $ \mbNoSpam netcrypto -> do | 1571 | atomically $ Tox.addNewSessionHook netCryptoSessionsState $ \mbNoSpam netcrypto -> do |
1572 | -- allsessionsMap <- atomically $ readTVar (netCryptoSessions netCryptoSessionsState) | ||
1573 | let sockAddr = Tox.ncSockAddr netcrypto | ||
1574 | pubKey = Tox.ncTheirPublicKey netcrypto | ||
1575 | msgQ <- atomically (Data.PacketQueue.new 10 0 :: STM (PacketQueue (Bool,Tox.CryptoMessage))) | ||
1576 | msgNumVar <- atomically (newTVar 0) | ||
1577 | let perSession = PerSession { perSessionMsgs = msgQ | ||
1578 | , perSessionPublicKey = pubKey | ||
1579 | , perSessionAddr = sockAddr | ||
1580 | , perSessionNumVar = msgNumVar | ||
1581 | } | ||
1582 | atomically $ modifyTVar' sessions (perSession:) | ||
1541 | tmchan <- atomically newTMChan | 1583 | tmchan <- atomically newTMChan |
1542 | let Just pingMachine = Tox.ncPingMachine netcrypto | 1584 | let Just pingMachine = Tox.ncPingMachine netcrypto |
1543 | pingflag = readTVar (pingFlag pingMachine) | 1585 | pingflag = readTVar (pingFlag pingMachine) |
@@ -1555,6 +1597,10 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1555 | Tox.forgetCrypto crypto netCryptoSessionsState netcrypto | 1597 | Tox.forgetCrypto crypto netCryptoSessionsState netcrypto |
1556 | return Nothing | 1598 | return Nothing |
1557 | handleIncoming mTyp session cm = do | 1599 | handleIncoming mTyp session cm = do |
1600 | atomically $ do | ||
1601 | num <- readTVar msgNumVar | ||
1602 | enqueue msgQ num (False,cm) | ||
1603 | modifyTVar' msgNumVar (+1) | ||
1558 | atomically $ writeTMChan tmchan cm -- (Tox.bufferData cd) | 1604 | atomically $ writeTMChan tmchan cm -- (Tox.bufferData cd) |
1559 | return Nothing | 1605 | return Nothing |
1560 | atomically $ writeTVar (Tox.ncUnrecognizedHook netcrypto) handleIncoming | 1606 | atomically $ writeTVar (Tox.ncUnrecognizedHook netcrypto) handleIncoming |
@@ -1585,6 +1631,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1585 | , toxkeys = keysdb | 1631 | , toxkeys = keysdb |
1586 | , userkeys = toxids | 1632 | , userkeys = toxids |
1587 | , roster = rstr | 1633 | , roster = rstr |
1634 | , sessions = sessions | ||
1588 | , connectionManager = ConnectionManager <$> mconns | 1635 | , connectionManager = ConnectionManager <$> mconns |
1589 | , onionRouter = orouter | 1636 | , onionRouter = orouter |
1590 | , externalAddresses = liftM2 (++) btips toxips | 1637 | , externalAddresses = liftM2 (++) btips toxips |
diff --git a/src/Data/PacketQueue.hs b/src/Data/PacketQueue.hs index b7737656..a4c99cab 100644 --- a/src/Data/PacketQueue.hs +++ b/src/Data/PacketQueue.hs | |||
@@ -11,6 +11,7 @@ module Data.PacketQueue | |||
11 | , enqueue | 11 | , enqueue |
12 | , observeOutOfBand | 12 | , observeOutOfBand |
13 | , PacketOutQueue | 13 | , PacketOutQueue |
14 | , packetQueueViewList | ||
14 | , newOutGoing | 15 | , newOutGoing |
15 | , readyOutGoing | 16 | , readyOutGoing |
16 | , tryAppendQueueOutgoing | 17 | , tryAppendQueueOutgoing |
@@ -25,6 +26,7 @@ import Control.Concurrent.STM.TArray | |||
25 | import Control.Monad | 26 | import Control.Monad |
26 | import Data.Word | 27 | import Data.Word |
27 | import Data.Array.MArray | 28 | import Data.Array.MArray |
29 | import Data.Maybe | ||
28 | 30 | ||
29 | data PacketQueue a = PacketQueue | 31 | data PacketQueue a = PacketQueue |
30 | { pktq :: TArray Word32 (Maybe a) | 32 | { pktq :: TArray Word32 (Maybe a) |
@@ -33,6 +35,13 @@ data PacketQueue a = PacketQueue | |||
33 | , buffend :: TVar Word32 -- on incoming, highest packet number handled + 1 | 35 | , buffend :: TVar Word32 -- on incoming, highest packet number handled + 1 |
34 | } | 36 | } |
35 | 37 | ||
38 | packetQueueViewList :: PacketQueue a -> STM [(Word32,a)] | ||
39 | packetQueueViewList p = do | ||
40 | let f (n,Nothing) = Nothing | ||
41 | f (n,Just x) = Just (n,x) | ||
42 | catMaybes . map f <$> getAssocs (pktq p) | ||
43 | |||
44 | |||
36 | -- | Create a new PacketQueue. | 45 | -- | Create a new PacketQueue. |
37 | new :: Word32 -- ^ Capacity of queue. | 46 | new :: Word32 -- ^ Capacity of queue. |
38 | -> Word32 -- ^ Initial sequence number. | 47 | -> Word32 -- ^ Initial sequence number. |