{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Ouroboros.Network.Protocol.Handshake.Type
(
Handshake (..)
, SingHandshake (..)
, Message (..)
, RefuseReason (..)
, HandshakeProtocolError (..)
, HandshakeResult (..)
) where
import Control.Exception
import Data.Map (Map)
import Data.Text (Text)
import Data.Typeable (Typeable)
import Control.DeepSeq
import GHC.Generics
import Network.TypedProtocol.Core
import Ouroboros.Network.Util.ShowProxy (ShowProxy (..))
data Handshake vNumber vParams where
StPropose :: Handshake vNumber vParams
StConfirm :: Handshake vNumber vParams
StDone :: Handshake vNumber vParams
instance ShowProxy (Handshake vNumber vParams) where
showProxy :: Proxy (Handshake vNumber vParams) -> String
showProxy Proxy (Handshake vNumber vParams)
_ = String
"Handshake"
data SingHandshake (st :: Handshake vNumber vParams) where
SingPropose :: SingHandshake StPropose
SingConfirm :: SingHandshake StConfirm
SingDone :: SingHandshake StDone
deriving instance Show (SingHandshake st)
instance StateTokenI StPropose where
stateToken :: StateToken 'StPropose
stateToken = StateToken 'StPropose
SingHandshake 'StPropose
forall {k} {k} {vNumber :: k} {vParams :: k}.
SingHandshake 'StPropose
SingPropose
instance StateTokenI StConfirm where
stateToken :: StateToken 'StConfirm
stateToken = StateToken 'StConfirm
SingHandshake 'StConfirm
forall {k} {k} {vNumber :: k} {vParams :: k}.
SingHandshake 'StConfirm
SingConfirm
instance StateTokenI StDone where
stateToken :: StateToken 'StDone
stateToken = StateToken 'StDone
SingHandshake 'StDone
forall {k} {k} {vNumber :: k} {vParams :: k}. SingHandshake 'StDone
SingDone
data RefuseReason vNumber
= VersionMismatch [vNumber] [Int]
| HandshakeDecodeError vNumber Text
| Refused vNumber Text
deriving (RefuseReason vNumber -> RefuseReason vNumber -> Bool
(RefuseReason vNumber -> RefuseReason vNumber -> Bool)
-> (RefuseReason vNumber -> RefuseReason vNumber -> Bool)
-> Eq (RefuseReason vNumber)
forall vNumber.
Eq vNumber =>
RefuseReason vNumber -> RefuseReason vNumber -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall vNumber.
Eq vNumber =>
RefuseReason vNumber -> RefuseReason vNumber -> Bool
== :: RefuseReason vNumber -> RefuseReason vNumber -> Bool
$c/= :: forall vNumber.
Eq vNumber =>
RefuseReason vNumber -> RefuseReason vNumber -> Bool
/= :: RefuseReason vNumber -> RefuseReason vNumber -> Bool
Eq, Int -> RefuseReason vNumber -> ShowS
[RefuseReason vNumber] -> ShowS
RefuseReason vNumber -> String
(Int -> RefuseReason vNumber -> ShowS)
-> (RefuseReason vNumber -> String)
-> ([RefuseReason vNumber] -> ShowS)
-> Show (RefuseReason vNumber)
forall vNumber.
Show vNumber =>
Int -> RefuseReason vNumber -> ShowS
forall vNumber. Show vNumber => [RefuseReason vNumber] -> ShowS
forall vNumber. Show vNumber => RefuseReason vNumber -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall vNumber.
Show vNumber =>
Int -> RefuseReason vNumber -> ShowS
showsPrec :: Int -> RefuseReason vNumber -> ShowS
$cshow :: forall vNumber. Show vNumber => RefuseReason vNumber -> String
show :: RefuseReason vNumber -> String
$cshowList :: forall vNumber. Show vNumber => [RefuseReason vNumber] -> ShowS
showList :: [RefuseReason vNumber] -> ShowS
Show, (forall x. RefuseReason vNumber -> Rep (RefuseReason vNumber) x)
-> (forall x. Rep (RefuseReason vNumber) x -> RefuseReason vNumber)
-> Generic (RefuseReason vNumber)
forall x. Rep (RefuseReason vNumber) x -> RefuseReason vNumber
forall x. RefuseReason vNumber -> Rep (RefuseReason vNumber) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall vNumber x.
Rep (RefuseReason vNumber) x -> RefuseReason vNumber
forall vNumber x.
RefuseReason vNumber -> Rep (RefuseReason vNumber) x
$cfrom :: forall vNumber x.
RefuseReason vNumber -> Rep (RefuseReason vNumber) x
from :: forall x. RefuseReason vNumber -> Rep (RefuseReason vNumber) x
$cto :: forall vNumber x.
Rep (RefuseReason vNumber) x -> RefuseReason vNumber
to :: forall x. Rep (RefuseReason vNumber) x -> RefuseReason vNumber
Generic, RefuseReason vNumber -> ()
(RefuseReason vNumber -> ()) -> NFData (RefuseReason vNumber)
forall vNumber. NFData vNumber => RefuseReason vNumber -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall vNumber. NFData vNumber => RefuseReason vNumber -> ()
rnf :: RefuseReason vNumber -> ()
NFData)
instance (Typeable vNumber, Show vNumber) => Exception (RefuseReason vNumber)
instance Protocol (Handshake vNumber vParams) where
data Message (Handshake vNumber vParams) from to where
MsgProposeVersions
:: Map vNumber vParams
-> Message (Handshake vNumber vParams) StPropose StConfirm
MsgReplyVersions
:: Map vNumber vParams
-> Message (Handshake vNumber vParams) StConfirm StDone
MsgQueryReply
:: Map vNumber vParams
-> Message (Handshake vNumber vParams) StConfirm StDone
MsgAcceptVersion
:: vNumber
-> vParams
-> Message (Handshake vNumber vParams) StConfirm StDone
MsgRefuse
:: RefuseReason vNumber
-> Message (Handshake vNumber vParams) StConfirm StDone
type StateAgency StPropose = ClientAgency
type StateAgency StConfirm = ServerAgency
type StateAgency StDone = NobodyAgency
type StateToken = SingHandshake
instance ( NFData vNumber
, NFData vParams
) => NFData (Message (Handshake vNumber vParams) from to) where
rnf :: Message (Handshake vNumber vParams) from to -> ()
rnf (MsgProposeVersions Map vNumber vParams
m) = Map vNumber vParams -> ()
forall a. NFData a => a -> ()
rnf Map vNumber vParams
m
rnf (MsgReplyVersions Map vNumber vParams
m) = Map vNumber vParams -> ()
forall a. NFData a => a -> ()
rnf Map vNumber vParams
m
rnf (MsgQueryReply Map vNumber vParams
m) = Map vNumber vParams -> ()
forall a. NFData a => a -> ()
rnf Map vNumber vParams
m
rnf (MsgAcceptVersion vNumber
vn vParams
vp) = vNumber -> ()
forall a. NFData a => a -> ()
rnf vNumber
vn () -> () -> ()
forall a b. a -> b -> b
`seq` vParams -> ()
forall a. NFData a => a -> ()
rnf vParams
vp
rnf (MsgRefuse RefuseReason vNumber
rf) = RefuseReason vNumber -> ()
forall a. NFData a => a -> ()
rnf RefuseReason vNumber
rf
deriving instance (Show vNumber, Show vParams)
=> Show (Message (Handshake vNumber vParams) from to)
data HandshakeProtocolError vNumber
= HandshakeError (RefuseReason vNumber)
| NotRecognisedVersion vNumber
| InvalidServerSelection vNumber Text
| QueryNotSupported
deriving (HandshakeProtocolError vNumber
-> HandshakeProtocolError vNumber -> Bool
(HandshakeProtocolError vNumber
-> HandshakeProtocolError vNumber -> Bool)
-> (HandshakeProtocolError vNumber
-> HandshakeProtocolError vNumber -> Bool)
-> Eq (HandshakeProtocolError vNumber)
forall vNumber.
Eq vNumber =>
HandshakeProtocolError vNumber
-> HandshakeProtocolError vNumber -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall vNumber.
Eq vNumber =>
HandshakeProtocolError vNumber
-> HandshakeProtocolError vNumber -> Bool
== :: HandshakeProtocolError vNumber
-> HandshakeProtocolError vNumber -> Bool
$c/= :: forall vNumber.
Eq vNumber =>
HandshakeProtocolError vNumber
-> HandshakeProtocolError vNumber -> Bool
/= :: HandshakeProtocolError vNumber
-> HandshakeProtocolError vNumber -> Bool
Eq, Int -> HandshakeProtocolError vNumber -> ShowS
[HandshakeProtocolError vNumber] -> ShowS
HandshakeProtocolError vNumber -> String
(Int -> HandshakeProtocolError vNumber -> ShowS)
-> (HandshakeProtocolError vNumber -> String)
-> ([HandshakeProtocolError vNumber] -> ShowS)
-> Show (HandshakeProtocolError vNumber)
forall vNumber.
Show vNumber =>
Int -> HandshakeProtocolError vNumber -> ShowS
forall vNumber.
Show vNumber =>
[HandshakeProtocolError vNumber] -> ShowS
forall vNumber.
Show vNumber =>
HandshakeProtocolError vNumber -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall vNumber.
Show vNumber =>
Int -> HandshakeProtocolError vNumber -> ShowS
showsPrec :: Int -> HandshakeProtocolError vNumber -> ShowS
$cshow :: forall vNumber.
Show vNumber =>
HandshakeProtocolError vNumber -> String
show :: HandshakeProtocolError vNumber -> String
$cshowList :: forall vNumber.
Show vNumber =>
[HandshakeProtocolError vNumber] -> ShowS
showList :: [HandshakeProtocolError vNumber] -> ShowS
Show)
data HandshakeResult r vNumber vData
= HandshakeNegotiationResult r vNumber vData
| HandshakeQueryResult (Map vNumber (Either Text vData))
instance (Typeable vNumber, Show vNumber)
=> Exception (HandshakeProtocolError vNumber)