-- |
-- Module      : Network.TLS.Measurement
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
module Network.TLS.Measurement
        ( Measurement(..)
        , newMeasurement
        , addBytesReceived
        , addBytesSent
        , resetBytesCounters
        , incrementNbHandshakes
        ) where

import Network.TLS.Imports

-- | record some data about this connection.
data Measurement = Measurement
        { Measurement -> Word32
nbHandshakes  :: !Word32 -- ^ number of handshakes on this context
        , Measurement -> Word32
bytesReceived :: !Word32 -- ^ bytes received since last handshake
        , Measurement -> Word32
bytesSent     :: !Word32 -- ^ bytes sent since last handshake
        } deriving (Int -> Measurement -> ShowS
[Measurement] -> ShowS
Measurement -> String
(Int -> Measurement -> ShowS)
-> (Measurement -> String)
-> ([Measurement] -> ShowS)
-> Show Measurement
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Measurement] -> ShowS
$cshowList :: [Measurement] -> ShowS
show :: Measurement -> String
$cshow :: Measurement -> String
showsPrec :: Int -> Measurement -> ShowS
$cshowsPrec :: Int -> Measurement -> ShowS
Show,Measurement -> Measurement -> Bool
(Measurement -> Measurement -> Bool)
-> (Measurement -> Measurement -> Bool) -> Eq Measurement
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Measurement -> Measurement -> Bool
$c/= :: Measurement -> Measurement -> Bool
== :: Measurement -> Measurement -> Bool
$c== :: Measurement -> Measurement -> Bool
Eq)

newMeasurement :: Measurement
newMeasurement :: Measurement
newMeasurement = Measurement :: Word32 -> Word32 -> Word32 -> Measurement
Measurement
        { nbHandshakes :: Word32
nbHandshakes  = Word32
0
        , bytesReceived :: Word32
bytesReceived = Word32
0
        , bytesSent :: Word32
bytesSent     = Word32
0
        }

addBytesReceived :: Int -> Measurement -> Measurement
addBytesReceived :: Int -> Measurement -> Measurement
addBytesReceived Int
sz Measurement
measure =
        Measurement
measure { bytesReceived :: Word32
bytesReceived = Measurement -> Word32
bytesReceived Measurement
measure Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sz }

addBytesSent :: Int -> Measurement -> Measurement
addBytesSent :: Int -> Measurement -> Measurement
addBytesSent Int
sz Measurement
measure =
        Measurement
measure { bytesSent :: Word32
bytesSent = Measurement -> Word32
bytesSent Measurement
measure Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sz }

resetBytesCounters :: Measurement -> Measurement
resetBytesCounters :: Measurement -> Measurement
resetBytesCounters Measurement
measure = Measurement
measure { bytesReceived :: Word32
bytesReceived = Word32
0, bytesSent :: Word32
bytesSent = Word32
0 }

incrementNbHandshakes :: Measurement -> Measurement
incrementNbHandshakes :: Measurement -> Measurement
incrementNbHandshakes Measurement
measure =
        Measurement
measure { nbHandshakes :: Word32
nbHandshakes = Measurement -> Word32
nbHandshakes Measurement
measure Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
1 }