From f330ff1070580d042d1bc15e52be3fac4f59c601 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Thu, 20 Aug 2020 18:14:23 +0200 Subject: [PATCH] successful post publishing with MonadState and random relay selection --- app/Experiment.hs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/app/Experiment.hs b/app/Experiment.hs index c7abbcb..51b8e88 100644 --- a/app/Experiment.hs +++ b/app/Experiment.hs @@ -1,3 +1,44 @@ +{-# LANGUAGE OverloadedStrings #-} + module Main where -main = putStrLn "This gives us ALL the insights!" +import System.Random +import Control.Concurrent +import Control.Monad (forM_) +import Control.Monad.State.Class +import Control.Monad.State.Strict (evalStateT) +import Control.Monad.IO.Class +import qualified Network.HTTP.Client as HTTP + +import Hash2Pub.PostService (clientPublishPost, Hashtag) + +-- placeholder post data definition + +tagsToPostTo = [ "JustSomeTag", "WantAnotherTag234", "HereWeGoAgain", "Oyä", "通信端末" ] + +knownRelays :: [(String, Int)] +knownRelays = + [ ("animalliberation.social", 3342) + , ("hostux.social", 3343) + , ("social.diskseven.com", 3344) + , ("social.imirhil.fr", 3345) + ] + +main :: IO () +main = do + -- initialise HTTP manager + httpMan <- HTTP.newManager HTTP.defaultManagerSettings + -- initialise RNG + let initRGen = mkStdGen 12 + -- cycle through tags and post to a random instance + evalStateT (forM_ (cycle tagsToPostTo) $ publishPostRandom httpMan) initRGen + -- wait for a specified time + +publishPostRandom :: (RandomGen g, MonadIO m, MonadState g m) => HTTP.Manager -> Hashtag -> m () +publishPostRandom httpman tag = do + index <- state $ randomR (0, length knownRelays - 1) + let (pubHost, pubPort) = knownRelays !! index + _ <- liftIO . forkIO $ do + postResult <- liftIO $ clientPublishPost httpman pubHost pubPort ("foobar #" <> tag) + either putStrLn (const $ pure ()) postResult + liftIO $ threadDelay 500