From a9de120224fa1dbb1f740d3d51d8bde6432c0884 Mon Sep 17 00:00:00 2001 From: Reto Habluetzel Date: Wed, 29 Aug 2012 23:12:08 +0200 Subject: added dynnetwork, which selects the busiest network of a list and displays it --- src/Plugins/Monitors/Net.hs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'src/Plugins/Monitors/Net.hs') diff --git a/src/Plugins/Monitors/Net.hs b/src/Plugins/Monitors/Net.hs index 768907c..170da07 100644 --- a/src/Plugins/Monitors/Net.hs +++ b/src/Plugins/Monitors/Net.hs @@ -13,12 +13,16 @@ -- ----------------------------------------------------------------------------- -module Plugins.Monitors.Net (startNet) where +module Plugins.Monitors.Net ( + startNet + , startDynNet + ) where import Plugins.Monitors.Common import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Data.Time.Clock (UTCTime, getCurrentTime, diffUTCTime) +import Control.Monad (forM, filterM, liftM) import qualified Data.ByteString.Lazy.Char8 as B @@ -28,6 +32,23 @@ data NetDev = NA type NetDevRef = IORef (NetDev, UTCTime) +{- The more information available, the better. + - Note that names don't matter. Therefore, if only the names differ, + - a compare evaluates to EQ while (==) evaluates to False. + -} +instance Ord NetDev where + compare NA NA = EQ + compare NA _ = LT + compare _ NA = GT + compare (NI _) (NI _) = EQ + compare (NI _) (ND _ _ _) = LT + compare (ND _ _ _) (NI _) = GT + compare (ND _ x1 y1) (ND _ x2 y2) = + if downcmp /= EQ + then downcmp + else y1 `compare` y2 + where downcmp = x1 `compare` x2 + netConfig :: IO MConfig netConfig = mkMConfig ": KB|KB" -- template @@ -103,9 +124,30 @@ parseNet nref nd = do runNet :: NetDevRef -> String -> [String] -> Monitor String runNet nref i _ = io (parseNet nref i) >>= printNet +parseNets :: [(NetDevRef, String)] -> IO [NetDev] +parseNets = mapM (\(ref, i) -> parseNet ref i) + +runNets :: [(NetDevRef, String)] -> [String] -> Monitor String +runNets refs _ = io (parseActive refs) >>= printNet + where parseActive refs = parseNets refs >>= return . selectActive + +selectActive :: [NetDev] -> NetDev +selectActive = maximum + startNet :: String -> [String] -> Int -> (String -> IO ()) -> IO () startNet i a r cb = do t0 <- getCurrentTime nref <- newIORef (NA, t0) _ <- parseNet nref i runM a netConfig (runNet nref i) r cb + +startDynNet :: [String] -> [String] -> Int -> (String -> IO ()) -> IO () +startDynNet is a r cb = do + refs <- forM is $ \i -> do + t <- getCurrentTime + nref <- newIORef (NA, t) + _ <- parseNet nref i + return (nref, i) + runM a netConfig (runNets refs) r cb + +-- TODO: Prelude.head: empty list -- cgit v1.2.3