2020-03-20 19:01:25 +01:00
|
|
|
module Main where
|
|
|
|
|
2020-05-19 12:29:15 +02:00
|
|
|
import Control.Concurrent
|
2020-05-29 17:39:35 +02:00
|
|
|
import Control.Concurrent.Async
|
2020-06-04 22:29:11 +02:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Concurrent.STM.TVar
|
2020-05-27 17:48:01 +02:00
|
|
|
import Control.Exception
|
2020-05-27 19:10:45 +02:00
|
|
|
import Data.Either
|
2020-06-04 22:29:11 +02:00
|
|
|
import Data.IP (IPv6, toHostAddress6)
|
2020-05-19 12:29:15 +02:00
|
|
|
import System.Environment
|
2020-05-12 11:30:55 +02:00
|
|
|
|
2020-05-19 12:29:15 +02:00
|
|
|
import Hash2Pub.FediChord
|
2020-03-20 19:01:25 +01:00
|
|
|
|
|
|
|
main :: IO ()
|
2020-05-12 11:30:55 +02:00
|
|
|
main = do
|
|
|
|
-- ToDo: parse and pass config
|
|
|
|
-- probably use `tomland` for that
|
|
|
|
conf <- readConfig
|
|
|
|
-- ToDo: load persisted caches, bootstrapping nodes …
|
2020-05-12 21:24:56 +02:00
|
|
|
(serverSock, thisNode) <- fediChordInit conf
|
2020-06-04 22:29:11 +02:00
|
|
|
print =<< readTVarIO thisNode
|
2020-05-12 21:24:56 +02:00
|
|
|
print serverSock
|
2020-05-13 19:54:02 +02:00
|
|
|
-- currently no masking is necessary, as there is nothing to clean up
|
|
|
|
cacheWriterThread <- forkIO $ cacheWriter thisNode
|
2020-05-26 20:54:02 +02:00
|
|
|
-- try joining the DHT using one of the provided bootstrapping nodes
|
|
|
|
let
|
|
|
|
tryJoining (bn:bns) = do
|
|
|
|
j <- fediChordBootstrapJoin thisNode bn
|
|
|
|
case j of
|
2020-05-27 18:59:38 +02:00
|
|
|
Left err -> putStrLn ("join error: " <> err) >> tryJoining bns
|
2020-05-26 20:54:02 +02:00
|
|
|
Right joined -> pure $ Right joined
|
|
|
|
tryJoining [] = pure $ Left "Exhausted all bootstrap points for joining."
|
|
|
|
joinedState <- tryJoining $ confBootstrapNodes conf
|
2020-05-29 17:39:35 +02:00
|
|
|
either (\err -> do
|
2020-05-27 18:59:38 +02:00
|
|
|
-- handle unsuccessful join
|
|
|
|
|
|
|
|
putStrLn $ err <> " Error joining, start listening for incoming requests anyways"
|
2020-07-02 01:36:31 +02:00
|
|
|
print =<< readTVarIO thisNode
|
2020-05-29 17:39:35 +02:00
|
|
|
wait =<< async (fediMainThreads serverSock thisNode)
|
2020-05-27 18:59:38 +02:00
|
|
|
-- TODO: periodic retry
|
|
|
|
)
|
2020-05-29 17:39:35 +02:00
|
|
|
(\joinedNS -> do
|
2020-05-27 18:59:38 +02:00
|
|
|
-- launch main eventloop with successfully joined state
|
2020-06-04 22:29:11 +02:00
|
|
|
putStrLn "successful join"
|
2020-05-29 17:39:35 +02:00
|
|
|
wait =<< async (fediMainThreads serverSock thisNode)
|
2020-05-27 18:59:38 +02:00
|
|
|
)
|
|
|
|
joinedState
|
2020-05-19 12:29:15 +02:00
|
|
|
pure ()
|
2020-05-12 11:30:55 +02:00
|
|
|
|
2020-05-26 20:54:02 +02:00
|
|
|
|
2020-05-12 11:30:55 +02:00
|
|
|
readConfig :: IO FediChordConf
|
|
|
|
readConfig = do
|
2020-05-26 09:38:38 +02:00
|
|
|
confDomainString : ipString : portString : bootstrapHost : bootstrapPortString : _ <- getArgs
|
2020-05-19 12:29:15 +02:00
|
|
|
pure $ FediChordConf {
|
2020-05-12 11:30:55 +02:00
|
|
|
confDomain = confDomainString
|
|
|
|
, confIP = toHostAddress6 . read $ ipString
|
|
|
|
, confDhtPort = read portString
|
2020-05-26 09:38:38 +02:00
|
|
|
, confBootstrapNodes = [(bootstrapHost, read bootstrapPortString)]
|
2020-06-20 21:20:32 +02:00
|
|
|
--, confStabiliseInterval = 60
|
2020-05-12 11:30:55 +02:00
|
|
|
}
|