From 12dfc56a7321ffeb204e3978b6010992ee4df8dc Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Sat, 19 Sep 2020 23:01:55 +0200 Subject: [PATCH] fediChordInit returns a RealNode, manages vservers as map - contributes to #34 --- src/Hash2Pub/FediChord.hs | 20 +++++++++++--------- src/Hash2Pub/FediChordTypes.hs | 6 ++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Hash2Pub/FediChord.hs b/src/Hash2Pub/FediChord.hs index 9f14a1e..0dcba44 100644 --- a/src/Hash2Pub/FediChord.hs +++ b/src/Hash2Pub/FediChord.hs @@ -73,6 +73,8 @@ import Data.IP (IPv6, fromHostAddress6, toHostAddress6) import Data.List ((\\)) import qualified Data.Map.Strict as Map +import qualified Data.HashMap.Strict as HMap +import Data.HashMap.Strict (HashMap) import Data.Maybe (catMaybes, fromJust, fromMaybe, isJust, isNothing, mapMaybe) import qualified Data.Set as Set @@ -96,11 +98,11 @@ import Debug.Trace (trace) fediChordInit :: (Service s (RealNodeSTM s)) => FediChordConf -> (RealNodeSTM s -> IO (s (RealNodeSTM s))) -- ^ runner function for service - -> IO (Socket, LocalNodeStateSTM s) + -> IO (Socket, RealNodeSTM s) fediChordInit initConf serviceRunner = do emptyLookupCache <- newTVarIO Map.empty let realNode = RealNode { - vservers = [] + vservers = HMap.empty , nodeConfig = initConf , bootstrapNodes = confBootstrapNodes initConf , lookupCacheSTM = emptyLookupCache @@ -110,13 +112,13 @@ fediChordInit initConf serviceRunner = do -- launch service and set the reference in the RealNode serv <- serviceRunner realNodeSTM atomically . modifyTVar' realNodeSTM $ \rn -> rn { nodeService = serv } - -- initialise a single vserver - initialState <- nodeStateInit realNodeSTM - initialStateSTM <- newTVarIO initialState + -- TODO: k-choices way of joining, so far just initialise a single vserver + firstVS <- nodeStateInit realNodeSTM + firstVSSTM <- newTVarIO firstVS -- add vserver to list at RealNode - atomically . modifyTVar' realNodeSTM $ \rn -> rn { vservers = initialStateSTM:vservers rn } - serverSock <- mkServerSocket (getIpAddr initialState) (getDhtPort initialState) - pure (serverSock, initialStateSTM) + atomically . modifyTVar' realNodeSTM $ \rn -> rn { vservers = HMap.insert (getNid firstVS) firstVSSTM (vservers rn) } + serverSock <- mkServerSocket (confIP initConf) (fromIntegral $ confDhtPort initConf) + pure (serverSock, realNodeSTM) -- | initialises the 'NodeState' for this local node. -- Separated from 'fediChordInit' to be usable in tests. @@ -757,7 +759,7 @@ updateLookupCache :: RealNodeSTM s -> NodeID -> IO (Maybe (String, PortNumber)) updateLookupCache nodeSTM keyToLookup = do (node, lookupSource) <- atomically $ do node <- readTVar nodeSTM - let firstVs = headMay (vservers node) + let firstVs = headMay (HMap.elems $ vservers node) lookupSource <- case firstVs of Nothing -> pure Nothing Just vs -> Just <$> readTVar vs diff --git a/src/Hash2Pub/FediChordTypes.hs b/src/Hash2Pub/FediChordTypes.hs index d8bbe4c..fd9d0f9 100644 --- a/src/Hash2Pub/FediChordTypes.hs +++ b/src/Hash2Pub/FediChordTypes.hs @@ -71,6 +71,8 @@ import Data.Function (on) import qualified Data.Hashable as Hashable import Data.List (delete, nub, sortBy) import qualified Data.Map.Strict as Map +import Data.HashMap.Strict (HashMap) +import qualified Data.HashMap.Strict as HMap import Data.Maybe (fromJust, fromMaybe, isJust, isNothing, mapMaybe) import qualified Data.Set as Set @@ -151,8 +153,8 @@ a `localCompare` b -- Also contains shared data and config values. -- TODO: more data structures for k-choices bookkeeping data RealNode s = RealNode - { vservers :: [LocalNodeStateSTM s] - -- ^ references to all active versers + { vservers :: HashMap NodeID (LocalNodeStateSTM s) + -- ^ map of all active VServer node IDs to their node state , nodeConfig :: FediChordConf -- ^ holds the initial configuration read at program start , bootstrapNodes :: [(String, PortNumber)]