module Monky.Examples.Network
( getNetworkHandles'
, getStaticHandle
, getDynamicHandle
, NetworkDynHandle
, NetworkStaticHandle
)
where
import Data.Text (Text)
import qualified Data.Text as T
import Data.IORef (readIORef)
import Monky.Examples.Utility
import Monky.Modules
import Monky.Network
import qualified Monky.Network.Dynamic as D
import qualified Monky.Network.Static as S
data NetworkHandles' = NH' Text NetworkHandles
data NetworkDynHandle = DH Text D.UHandles
data NetworkStaticHandle = SH Text S.NetworkHandle
getNetworkHandles'
:: Text
-> [String]
-> IO NetworkHandles'
getNetworkHandles' e = fmap (NH' e) . getNetworkHandles
getStaticHandle
:: Text
-> String
-> IO NetworkStaticHandle
getStaticHandle e = fmap (SH e) . S.getNetworkHandle
getDynamicHandle
:: Text
-> (String -> Bool)
-> IO NetworkDynHandle
getDynamicHandle e = fmap (DH e) . D.getUHandles
formatNNetworkText :: Text -> Maybe (Int, Int) -> Text
formatNNetworkText e Nothing = e
formatNNetworkText _ (Just (r, w)) =
convertUnit r "B" "k" "M" "G" `T.append` (' ' `T.cons` convertUnit w "B" "k" "M" "G")
getNNetworkText :: Text -> NetworkHandles -> IO [MonkyOut]
getNNetworkText e nh = do
nv <- getReadWriteMulti nh
return [MonkyPlain $ formatNNetworkText e nv]
getNDNetworkText :: Text -> D.Handles -> IO [MonkyOut]
getNDNetworkText e nh = do
nv <- D.getMultiReadWrite nh
return [MonkyPlain $ formatNNetworkText e nv]
instance PollModule NetworkDynHandle where
getOutput (DH e h) = getNDNetworkText e =<< readIORef (fst h)
instance PollModule NetworkHandles' where
getOutput (NH' e h) = getNNetworkText e h
instance PollModule NetworkStaticHandle where
getOutput (SH e h) = do
rates <- S.getReadWrite h
return [MonkyPlain $ formatNNetworkText e rates]