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.hs | 39 +++++++++++++++++++++------------------ src/Plugins/Monitors/Net.hs | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/src/Plugins/Monitors.hs b/src/Plugins/Monitors.hs index a531e26..bea91eb 100644 --- a/src/Plugins/Monitors.hs +++ b/src/Plugins/Monitors.hs @@ -48,24 +48,25 @@ import Plugins.Monitors.Volume import Plugins.Monitors.Mpris #endif -data Monitors = Weather Station Args Rate - | Network Interface Args Rate - | BatteryP [String] Args Rate - | DiskU DiskSpec Args Rate - | DiskIO DiskSpec Args Rate - | Thermal Zone Args Rate - | ThermalZone ZoneNo Args Rate - | Memory Args Rate - | Swap Args Rate - | Cpu Args Rate - | MultiCpu Args Rate - | Battery Args Rate - | Brightness Args Rate - | CpuFreq Args Rate - | CoreTemp Args Rate - | TopProc Args Rate - | TopMem Args Rate - | Uptime Args Rate +data Monitors = Weather Station Args Rate + | Network Interface Args Rate + | DynNetwork [Interface] Args Rate + | BatteryP [String] Args Rate + | DiskU DiskSpec Args Rate + | DiskIO DiskSpec Args Rate + | Thermal Zone Args Rate + | ThermalZone ZoneNo Args Rate + | Memory Args Rate + | Swap Args Rate + | Cpu Args Rate + | MultiCpu Args Rate + | Battery Args Rate + | Brightness Args Rate + | CpuFreq Args Rate + | CoreTemp Args Rate + | TopProc Args Rate + | TopMem Args Rate + | Uptime Args Rate #ifdef IWLIB | Wireless Interface Args Rate #endif @@ -95,6 +96,7 @@ type DiskSpec = [(String, String)] instance Exec Monitors where alias (Weather s _ _) = s alias (Network i _ _) = i + alias (DynNetwork _ _ _) = "dynnetwork" alias (Thermal z _ _) = z alias (ThermalZone z _ _) = "thermal" ++ show z alias (Memory _ _) = "memory" @@ -126,6 +128,7 @@ instance Exec Monitors where alias (Mpris2 _ _ _) = "mpris2" #endif start (Network i a r) = startNet i a r + start (DynNetwork is a r) = startDynNet is a r start (Cpu a r) = startCpu a r start (MultiCpu a r) = startMultiCpu a r start (TopProc a r) = startTop a r 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