summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjim@bo <jim@bo>2018-06-24 18:50:44 -0400
committerjim@bo <jim@bo>2018-06-24 18:50:44 -0400
commit47637306b8c19b0b7363f9a2642c7190470a2c93 (patch)
tree070565ea9211ef7f782b736458ba1acc129493ea
parenta5fad52f1e1ca6d8ebfcbb448f19014225368777 (diff)
call setTerminated from destroySession
-rw-r--r--examples/dhtd.hs4
-rw-r--r--src/Network/Tox.hs22
-rw-r--r--src/Network/Tox/Crypto/Handlers.hs12
3 files changed, 33 insertions, 5 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index bf7d7162..f4a2544c 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -1734,7 +1734,9 @@ main = do
1734 keysdb <- Tox.newKeysDatabase 1734 keysdb <- Tox.newKeysDatabase
1735 1735
1736 _crypto <- Tox.newCrypto 1736 _crypto <- Tox.newCrypto
1737 _netCryptoSessionsState <- Tox.newSessionsState _crypto Tox.defaultUnRecHook Tox.defaultCryptoDataHooks 1737 let emptyDestroyHook :: Tox.NetCryptoSession -> IO ()
1738 emptyDestroyHook session = dput XNetCrypto $ "SESSION DESTROY HOOK NOT ADDED ! publkey= " ++ show (Tox.key2id (Tox.ncTheirPublicKey session))
1739 _netCryptoSessionsState <- Tox.newSessionsState _crypto emptyDestroyHook Tox.defaultUnRecHook Tox.defaultCryptoDataHooks
1738 (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- case porttox opts of 1740 (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- case porttox opts of
1739 "" -> return (Nothing,return (), Map.empty, return [],[]) 1741 "" -> return (Nothing,return (), Map.empty, return [],[])
1740 toxport -> do 1742 toxport -> do
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index cebbebfb..26488657 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -354,6 +354,18 @@ getOnionAlias crypto dhtself remoteNode = atomically $ do
354 _ -> ni { nodeId = key2id (onionAliasPublic crypto) } 354 _ -> ni { nodeId = key2id (onionAliasPublic crypto) }
355 return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing 355 return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing
356 356
357mkDefaultDestroyHook :: ContactInfo extra -> NetCryptoSession -> IO ()
358mkDefaultDestroyHook roster = \session -> do
359 now <- getPOSIXTime
360 r <- atomically $ do
361 accounts <- readTVar (accounts roster) -- :: STM (HashMap NodeId (Account extra)
362 let mbAccount = HashMap.lookup (key2id $ ncMyPublicKey session) accounts -- :: STM (Maybe (Account extra)
363 case mbAccount of
364 Just account -> Right <$> setTerminated now (ncTheirPublicKey session) account
365 Nothing -> return . Left $ "(defaultDestroyHook) their is no account! pubkey=" ++ show (key2id (ncTheirPublicKey session))
366 case r of
367 Left msg -> dput XMan msg
368 _ -> return ()
357 369
358newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. 370newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for.
359 -> SockAddr -- ^ Bind-address to listen on. 371 -> SockAddr -- ^ Bind-address to listen on.
@@ -362,14 +374,18 @@ newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rende
362 -> IO (Tox extra) 374 -> IO (Tox extra)
363newTox keydb addr mbSessionsState suppliedDHTKey = do 375newTox keydb addr mbSessionsState suppliedDHTKey = do
364 (udp,sock) <- {- addVerbosity <$> -} udpTransport' addr 376 (udp,sock) <- {- addVerbosity <$> -} udpTransport' addr
377 roster <- newContactInfo
365 (crypto0,sessionsState0) <- case mbSessionsState of 378 (crypto0,sessionsState0) <- case mbSessionsState of
366 Nothing -> do 379 Nothing -> do
367 crypto <- newCrypto 380 crypto <- newCrypto
368 sessionsState <- newSessionsState crypto defaultUnRecHook defaultCryptoDataHooks 381 sessionsState <- newSessionsState crypto (mkDefaultDestroyHook roster) defaultUnRecHook defaultCryptoDataHooks
369 return (crypto,sessionsState) 382 return (crypto,sessionsState)
370 Just s -> return (transportCrypto s, s) 383 Just s -> do
384 let oldhook = defaultDestroyHook s
385 oldhook' = filter ((==0) . fst) oldhook
386 newhook = (0,mkDefaultDestroyHook roster):oldhook'
387 return (transportCrypto s, s { defaultDestroyHook = newhook})
371 388
372 roster <- newContactInfo
373 let -- patch in supplied DHT key 389 let -- patch in supplied DHT key
374 crypto1 = fromMaybe crypto0 $do 390 crypto1 = fromMaybe crypto0 $do
375 k <- suppliedDHTKey 391 k <- suppliedDHTKey
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs
index 2e00d61b..85e6192a 100644
--- a/src/Network/Tox/Crypto/Handlers.hs
+++ b/src/Network/Tox/Crypto/Handlers.hs
@@ -326,6 +326,7 @@ data NetCryptoSession = NCrypto
326 TVar 326 TVar
327 , ncUnrecognizedHook :: TVar (MessageType -> NetCryptoHook) 327 , ncUnrecognizedHook :: TVar (MessageType -> NetCryptoHook)
328 , ncIdleEventHooks :: TVar [(Int,NetCryptoSession -> IO ())] 328 , ncIdleEventHooks :: TVar [(Int,NetCryptoSession -> IO ())]
329 , ncDestoryHooks :: TVar [(Int,NetCryptoSession -> IO ())]
329 , ncIncomingTypeArray :: TVar MsgTypeArray 330 , ncIncomingTypeArray :: TVar MsgTypeArray
330 -- ^ This array maps 255 Id bytes to MessageType 331 -- ^ This array maps 255 Id bytes to MessageType
331 -- It should contain all messages this session understands. 332 -- It should contain all messages this session understands.
@@ -412,6 +413,7 @@ data NetCryptoSessions = NCSessions
412 , defaultHooks :: Map.Map MessageType [NetCryptoHook] 413 , defaultHooks :: Map.Map MessageType [NetCryptoHook]
413 , defaultUnrecognizedHook :: MessageType -> NetCryptoHook 414 , defaultUnrecognizedHook :: MessageType -> NetCryptoHook
414 , defaultIdleEventHooks :: [(Int,NetCryptoSession -> IO ())] 415 , defaultIdleEventHooks :: [(Int,NetCryptoSession -> IO ())]
416 , defaultDestroyHook :: [(Int,NetCryptoSession -> IO ())]
415 , sessionView :: SessionView 417 , sessionView :: SessionView
416 , msgTypeArray :: MsgTypeArray 418 , msgTypeArray :: MsgTypeArray
417 , inboundQueueCapacity :: Word32 419 , inboundQueueCapacity :: Word32
@@ -445,10 +447,11 @@ forgetCrypto crypto (NCSessions {netCryptoSessions,netCryptoSessionsByKey}) sess
445 ys -> Just ys) sPubKey) 447 ys -> Just ys) sPubKey)
446 448
447newSessionsState :: TransportCrypto 449newSessionsState :: TransportCrypto
450 -> (NetCryptoSession -> IO ()) -- ^ default destroy hook
448 -> (MessageType -> NetCryptoHook) -- ^ default hook 451 -> (MessageType -> NetCryptoHook) -- ^ default hook
449 -> Map.Map MessageType [NetCryptoHook] -- ^ all hooks, can be empty to start 452 -> Map.Map MessageType [NetCryptoHook] -- ^ all hooks, can be empty to start
450 -> IO NetCryptoSessions 453 -> IO NetCryptoSessions
451newSessionsState crypto unrechook hooks = do 454newSessionsState crypto destroyHook unrechook hooks = do
452 x <- atomically $ newTVar Map.empty 455 x <- atomically $ newTVar Map.empty
453 x2 <- atomically $ newTVar Map.empty 456 x2 <- atomically $ newTVar Map.empty
454 nick <- atomically $ newTVar B.empty 457 nick <- atomically $ newTVar B.empty
@@ -479,6 +482,7 @@ newSessionsState crypto unrechook hooks = do
479 , defaultHooks = hooks 482 , defaultHooks = hooks
480 , defaultUnrecognizedHook = unrechook 483 , defaultUnrecognizedHook = unrechook
481 , defaultIdleEventHooks = [(0,handleRequestsOutOfOrder)] 484 , defaultIdleEventHooks = [(0,handleRequestsOutOfOrder)]
485 , defaultDestroyHook = [(0,destroyHook)]
482 , sessionView = SessionView 486 , sessionView = SessionView
483 { svNick = nick 487 { svNick = nick
484 , svStatus = status 488 , svStatus = status
@@ -609,6 +613,7 @@ freshCryptoSession sessions
609 ncHooks0 <- newTVar (defaultHooks sessions) 613 ncHooks0 <- newTVar (defaultHooks sessions)
610 ncUnrecognizedHook0 <- newTVar (defaultUnrecognizedHook sessions) 614 ncUnrecognizedHook0 <- newTVar (defaultUnrecognizedHook sessions)
611 ncIdleEventHooks0 <- newTVar (defaultIdleEventHooks sessions) 615 ncIdleEventHooks0 <- newTVar (defaultIdleEventHooks sessions)
616 ncDestoryHooks0 <- newTVar (defaultDestroyHook sessions)
612 ncIncomingTypeArray0 <- newTVar (msgTypeArray sessions) 617 ncIncomingTypeArray0 <- newTVar (msgTypeArray sessions)
613 let idMap = foldl (\mp (x,y) -> W64.insert x y mp) W64.empty (zip [0..255] [0..255]) 618 let idMap = foldl (\mp (x,y) -> W64.insert x y mp) W64.empty (zip [0..255] [0..255])
614 (ncOutgoingIdMap0,lossyEscapeIdMap,losslessEscapeIdMap,ncOutHooks0) <- do 619 (ncOutgoingIdMap0,lossyEscapeIdMap,losslessEscapeIdMap,ncOutHooks0) <- do
@@ -684,6 +689,7 @@ freshCryptoSession sessions
684 , ncOutHooks = ncOutHooks0 689 , ncOutHooks = ncOutHooks0
685 , ncUnrecognizedHook = ncUnrecognizedHook0 690 , ncUnrecognizedHook = ncUnrecognizedHook0
686 , ncIdleEventHooks = ncIdleEventHooks0 691 , ncIdleEventHooks = ncIdleEventHooks0
692 , ncDestoryHooks = ncDestoryHooks0
687 , ncAllSessions = sessions 693 , ncAllSessions = sessions
688 , ncIncomingTypeArray = ncIncomingTypeArray0 694 , ncIncomingTypeArray = ncIncomingTypeArray0
689 , ncOutgoingIdMap = ncOutgoingIdMap0 695 , ncOutgoingIdMap = ncOutgoingIdMap0
@@ -892,6 +898,10 @@ runUponHandshake netCryptoSession0 addr pktoq = do
892 898
893destroySession :: NetCryptoSession -> IO () 899destroySession :: NetCryptoSession -> IO ()
894destroySession session = do 900destroySession session = do
901 -- first run all destory hooks
902 hooks <- atomically (readTVar (ncDestoryHooks session))
903 forM_ hooks $ \(key,hook) -> hook session
904 -- now clean up threads
895 let allsessions = ncAllSessions session 905 let allsessions = ncAllSessions session
896 sid = ncSessionId session 906 sid = ncSessionId session
897 stopThread :: TVar (Maybe ThreadId) -> IO () 907 stopThread :: TVar (Maybe ThreadId) -> IO ()