{-# LANGUAGE DataKinds       #-}
{-# LANGUAGE GADTs           #-}
{-# LANGUAGE RecordWildCards #-}

module Network.TypedProtocol.PingPong.Server where

import           Network.TypedProtocol.Core
import           Network.TypedProtocol.Peer.Server
import           Network.TypedProtocol.PingPong.Type


data PingPongServer m a = PingPongServer {
    -- | The client sent us a ping message. We have no choices here, and
    -- the response is nullary, all we have are local effects.
    forall (m :: * -> *) a.
PingPongServer m a -> m (PingPongServer m a)
recvMsgPing :: m (PingPongServer m a)

    -- | The client terminated. Here we have a pure return value, but we
    -- could have done another action in 'm' if we wanted to.
  , forall (m :: * -> *) a. PingPongServer m a -> a
recvMsgDone :: a
  }


-- | Interpret a particular server action sequence into the server side of the
-- 'PingPong' protocol.
--
pingPongServerPeer
  :: Monad m
  => PingPongServer m a
  -> Server PingPong NonPipelined StIdle m a
pingPongServerPeer :: forall (m :: * -> *) a.
Monad m =>
PingPongServer m a -> Server PingPong 'NonPipelined 'StIdle m a
pingPongServerPeer PingPongServer{a
m (PingPongServer m a)
recvMsgPing :: forall (m :: * -> *) a.
PingPongServer m a -> m (PingPongServer m a)
recvMsgDone :: forall (m :: * -> *) a. PingPongServer m a -> a
recvMsgPing :: m (PingPongServer m a)
recvMsgDone :: a
..} =

    -- In the 'StIdle' the server is awaiting a request message
    (forall (st' :: PingPong).
 Message PingPong 'StIdle st'
 -> Server PingPong 'NonPipelined st' m a)
-> Server PingPong 'NonPipelined 'StIdle m a
forall ps (pl :: IsPipelined) (st :: ps) (m :: * -> *) a.
(StateTokenI st, StateAgency st ~ 'ClientAgency,
 Outstanding pl ~ 'Z) =>
(forall (st' :: ps). Message ps st st' -> Server ps pl st' m a)
-> Server ps pl st m a
Await ((forall (st' :: PingPong).
  Message PingPong 'StIdle st'
  -> Server PingPong 'NonPipelined st' m a)
 -> Server PingPong 'NonPipelined 'StIdle m a)
-> (forall (st' :: PingPong).
    Message PingPong 'StIdle st'
    -> Server PingPong 'NonPipelined st' m a)
-> Server PingPong 'NonPipelined 'StIdle m a
forall a b. (a -> b) -> a -> b
$ \Message PingPong 'StIdle st'
req ->

    -- The client got to choose between two messages and we have to handle
    -- either of them
    case Message PingPong 'StIdle st'
req of

      -- The client sent the done transition, so we're in the 'StDone' state
      -- so all we can do is stop using 'done', with a return value.
      Message PingPong 'StIdle st'
R:MessagePingPongfromto 'StIdle st'
MsgDone -> a -> Server PingPong 'NonPipelined st' m a
forall ps (pl :: IsPipelined) (st :: ps) (m :: * -> *) a.
(StateTokenI st, StateAgency st ~ 'NobodyAgency,
 Outstanding pl ~ 'Z) =>
a -> Server ps pl st m a
Done a
recvMsgDone

      -- The client sent us a ping request, so now we're in the 'StBusy' state
      -- which means it's the server's turn to send.
      Message PingPong 'StIdle st'
R:MessagePingPongfromto 'StIdle st'
MsgPing -> m (Server PingPong 'NonPipelined st' m a)
-> Server PingPong 'NonPipelined st' m a
forall ps (pl :: IsPipelined) (st :: ps) (m :: * -> *) a.
m (Server ps pl st m a) -> Server ps pl st m a
Effect (m (Server PingPong 'NonPipelined st' m a)
 -> Server PingPong 'NonPipelined st' m a)
-> m (Server PingPong 'NonPipelined st' m a)
-> Server PingPong 'NonPipelined st' m a
forall a b. (a -> b) -> a -> b
$ do
        next <- m (PingPongServer m a)
recvMsgPing
        pure $ Yield MsgPong (pingPongServerPeer next)