{-# LANGUAGE ScopedTypeVariables #-}
module Ouroboros.Network.Protocol.KeepAlive.Examples where
import Ouroboros.Network.Protocol.KeepAlive.Client
import Ouroboros.Network.Protocol.KeepAlive.Server
import Ouroboros.Network.Protocol.KeepAlive.Type
keepAliveClientApply :: forall acc m. Monad m
=> (acc -> acc)
-> acc
-> Int
-> KeepAliveClient m acc
keepAliveClientApply :: forall acc (m :: * -> *).
Monad m =>
(acc -> acc) -> acc -> Int -> KeepAliveClient m acc
keepAliveClientApply acc -> acc
f acc
aa Int
an = m (KeepAliveClientSt m acc) -> KeepAliveClient m acc
forall (m :: * -> *) a.
m (KeepAliveClientSt m a) -> KeepAliveClient m a
KeepAliveClient (m (KeepAliveClientSt m acc) -> KeepAliveClient m acc)
-> m (KeepAliveClientSt m acc) -> KeepAliveClient m acc
forall a b. (a -> b) -> a -> b
$ KeepAliveClientSt m acc -> m (KeepAliveClientSt m acc)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (acc -> Int -> KeepAliveClientSt m acc
go acc
aa Int
an)
where
go :: acc -> Int -> KeepAliveClientSt m acc
go :: acc -> Int -> KeepAliveClientSt m acc
go acc
acc Int
n
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
= m acc -> KeepAliveClientSt m acc
forall (m :: * -> *) a. m a -> KeepAliveClientSt m a
SendMsgDone (acc -> m acc
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure acc
acc)
| Bool
otherwise
= Cookie -> m (KeepAliveClientSt m acc) -> KeepAliveClientSt m acc
forall (m :: * -> *) a.
Cookie -> m (KeepAliveClientSt m a) -> KeepAliveClientSt m a
SendMsgKeepAlive (Word16 -> Cookie
Cookie (Word16 -> Cookie) -> Word16 -> Cookie
forall a b. (a -> b) -> a -> b
$ Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n) (m (KeepAliveClientSt m acc) -> KeepAliveClientSt m acc)
-> m (KeepAliveClientSt m acc) -> KeepAliveClientSt m acc
forall a b. (a -> b) -> a -> b
$
KeepAliveClientSt m acc -> m (KeepAliveClientSt m acc)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (KeepAliveClientSt m acc -> m (KeepAliveClientSt m acc))
-> KeepAliveClientSt m acc -> m (KeepAliveClientSt m acc)
forall a b. (a -> b) -> a -> b
$ acc -> Int -> KeepAliveClientSt m acc
go (acc -> acc
f acc
acc) (Int -> Int
forall a. Enum a => a -> a
pred Int
n)
keepAliveServerCount :: forall m. Applicative m
=> KeepAliveServer m Int
keepAliveServerCount :: forall (m :: * -> *). Applicative m => KeepAliveServer m Int
keepAliveServerCount = Int -> KeepAliveServer m Int
forall {m :: * -> *} {t}.
(Applicative m, Enum t) =>
t -> KeepAliveServer m t
go Int
0
where
go :: t -> KeepAliveServer m t
go t
n =
KeepAliveServer {
recvMsgDone :: m t
recvMsgDone = t -> m t
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure t
n,
recvMsgKeepAlive :: m (KeepAliveServer m t)
recvMsgKeepAlive = KeepAliveServer m t -> m (KeepAliveServer m t)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (t -> KeepAliveServer m t
go (t -> t
forall a. Enum a => a -> a
succ t
n))
}