2020-08-27 00:33:19 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-08-20 18:14:23 +02:00
|
|
|
|
2020-08-19 15:49:39 +02:00
|
|
|
module Main where
|
|
|
|
|
2020-08-27 00:33:19 +02:00
|
|
|
import Control.Concurrent
|
2020-09-02 21:37:01 +02:00
|
|
|
import Control.Monad (forM_)
|
|
|
|
import qualified Data.Text.Lazy as Txt
|
|
|
|
import qualified Data.Text.Lazy.IO as TxtI
|
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import System.Environment (getArgs)
|
2020-08-20 18:14:23 +02:00
|
|
|
|
2020-09-02 21:37:01 +02:00
|
|
|
import Hash2Pub.PostService (Hashtag, clientPublishPost)
|
2020-08-20 18:14:23 +02:00
|
|
|
|
2020-09-02 21:37:01 +02:00
|
|
|
-- configuration constants
|
|
|
|
timelineFile = "../simulationData/inputs/generated/timeline_sample.csv"
|
2020-08-20 18:14:23 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2020-09-02 21:37:01 +02:00
|
|
|
-- read CLI parameters
|
|
|
|
speedupStr : _ <- getArgs
|
|
|
|
-- read and parse timeline schedule
|
|
|
|
-- relying on lazyness of HaskellIO, hoping it does not introduce too strong delays
|
|
|
|
postEvents <- parseSchedule <$> TxtI.readFile timelineFile
|
|
|
|
-- actually schedule and send the post events
|
|
|
|
executeSchedule (read speedupStr) postEvents
|
|
|
|
pure ()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parseSchedule :: Txt.Text
|
|
|
|
-> [(Int, Hashtag, (String, Int))] -- ^ [(delay in microseconds, hashtag, (hostname, port))]
|
|
|
|
parseSchedule = fmap (parseEntry . Txt.split (== ';')) . Txt.lines
|
|
|
|
where
|
|
|
|
parseEntry [delayT, contactT, tag] =
|
|
|
|
(read $ Txt.unpack delayT, tag, read $ Txt.unpack contactT)
|
2020-09-14 14:57:25 +02:00
|
|
|
parseEntry entry = error $ "invalid schedule input format: " <> show entry
|
2020-09-02 21:37:01 +02:00
|
|
|
|
|
|
|
executeSchedule :: Int -- ^ speedup factor
|
|
|
|
-> [(Int, Hashtag, (String, Int))] -- ^ [(delay in microseconds, hashtag, (hostname, port))]
|
|
|
|
-> IO ()
|
|
|
|
executeSchedule speedup events = do
|
2020-08-20 18:14:23 +02:00
|
|
|
-- initialise HTTP manager
|
2020-09-22 19:47:39 +02:00
|
|
|
httpMan <- HTTP.newManager $ HTTP.defaultManagerSettings { HTTP.managerResponseTimeout = HTTP.responseTimeoutMicro 60000000 }
|
2020-09-02 21:37:01 +02:00
|
|
|
forM_ events $ \(delay, tag, (pubHost, pubPort)) -> do
|
|
|
|
_ <- forkIO $
|
|
|
|
clientPublishPost httpMan pubHost pubPort ("foobar #" <> tag)
|
|
|
|
>>= either putStrLn (const $ pure ())
|
|
|
|
-- while threadDelay gives only minimum delay guarantees, let's hope the
|
|
|
|
-- additional delays are negligible
|
|
|
|
-- otherwise: evaluate usage of https://hackage.haskell.org/package/schedule-0.3.0.0/docs/Data-Schedule.html
|
|
|
|
threadDelay $ delay `div` speedup
|