{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric      #-}
-- |
-- Module      :  Pinch.Internal.Message
-- Copyright   :  (c) Abhinav Gupta 2015
-- License     :  BSD3
--
-- Maintainer  :  Abhinav Gupta <mail@abhinavg.net>
-- Stability   :  experimental
--
-- Message wrapper for Thrift payloads. Normal Thrift requests sent over the
-- wire are wrapped inside a message envelope that contains information about
-- the method being called, the type of message, etc. This information is
-- essential for the RPC system to function.
module Pinch.Internal.Message
    ( Message(..)
    , MessageType(..)
    ) where

import Control.DeepSeq (NFData)
import Data.Data       (Data)
import Data.Int        (Int32)
import Data.Text       (Text)
import Data.Typeable   (Typeable)
import GHC.Generics    (Generic)

import Pinch.Internal.TType (TStruct)
import Pinch.Internal.Value (Value)

-- | Type of message being sent.
data MessageType
    = Call
    -- ^ A call to a specific method.
    --
    -- The message body is the request arguments struct.
    | Reply
    -- ^ Response to a call.
    --
    -- The message body is the response union.
    | Exception
    -- ^ Failure to make a call.
    --
    -- Note: This message type is /not/ used for exceptions that are defined
    -- under the @throws@ clause of a method. Those exceptions are part of the
    -- response union of the method and are received in a @Reply@. This
    -- message type is used for Thrift-level failures.
    | Oneway
    -- ^ One-way call that expects no response.
  deriving (Int -> MessageType -> ShowS
[MessageType] -> ShowS
MessageType -> String
(Int -> MessageType -> ShowS)
-> (MessageType -> String)
-> ([MessageType] -> ShowS)
-> Show MessageType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MessageType] -> ShowS
$cshowList :: [MessageType] -> ShowS
show :: MessageType -> String
$cshow :: MessageType -> String
showsPrec :: Int -> MessageType -> ShowS
$cshowsPrec :: Int -> MessageType -> ShowS
Show, MessageType -> MessageType -> Bool
(MessageType -> MessageType -> Bool)
-> (MessageType -> MessageType -> Bool) -> Eq MessageType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MessageType -> MessageType -> Bool
$c/= :: MessageType -> MessageType -> Bool
== :: MessageType -> MessageType -> Bool
$c== :: MessageType -> MessageType -> Bool
Eq, Typeable MessageType
DataType
Constr
Typeable MessageType
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> MessageType -> c MessageType)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c MessageType)
-> (MessageType -> Constr)
-> (MessageType -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c MessageType))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c MessageType))
-> ((forall b. Data b => b -> b) -> MessageType -> MessageType)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> MessageType -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> MessageType -> r)
-> (forall u. (forall d. Data d => d -> u) -> MessageType -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> MessageType -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> MessageType -> m MessageType)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> MessageType -> m MessageType)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> MessageType -> m MessageType)
-> Data MessageType
MessageType -> DataType
MessageType -> Constr
(forall b. Data b => b -> b) -> MessageType -> MessageType
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MessageType -> c MessageType
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MessageType
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> MessageType -> u
forall u. (forall d. Data d => d -> u) -> MessageType -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MessageType -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MessageType -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> MessageType -> m MessageType
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MessageType -> m MessageType
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MessageType
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MessageType -> c MessageType
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MessageType)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MessageType)
$cOneway :: Constr
$cException :: Constr
$cReply :: Constr
$cCall :: Constr
$tMessageType :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> MessageType -> m MessageType
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MessageType -> m MessageType
gmapMp :: (forall d. Data d => d -> m d) -> MessageType -> m MessageType
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MessageType -> m MessageType
gmapM :: (forall d. Data d => d -> m d) -> MessageType -> m MessageType
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> MessageType -> m MessageType
gmapQi :: Int -> (forall d. Data d => d -> u) -> MessageType -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> MessageType -> u
gmapQ :: (forall d. Data d => d -> u) -> MessageType -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> MessageType -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MessageType -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MessageType -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MessageType -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MessageType -> r
gmapT :: (forall b. Data b => b -> b) -> MessageType -> MessageType
$cgmapT :: (forall b. Data b => b -> b) -> MessageType -> MessageType
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MessageType)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MessageType)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c MessageType)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MessageType)
dataTypeOf :: MessageType -> DataType
$cdataTypeOf :: MessageType -> DataType
toConstr :: MessageType -> Constr
$ctoConstr :: MessageType -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MessageType
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MessageType
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MessageType -> c MessageType
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MessageType -> c MessageType
$cp1Data :: Typeable MessageType
Data, Typeable, (forall x. MessageType -> Rep MessageType x)
-> (forall x. Rep MessageType x -> MessageType)
-> Generic MessageType
forall x. Rep MessageType x -> MessageType
forall x. MessageType -> Rep MessageType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MessageType x -> MessageType
$cfrom :: forall x. MessageType -> Rep MessageType x
Generic)

instance NFData MessageType


-- | Message envelope for Thrift payloads.
data Message = Message
    { Message -> Text
messageName    :: !Text
    -- ^ Name of the method to which this message is targeted.
    , Message -> MessageType
messageType    :: !MessageType
    -- ^ Type of the message.
    , Message -> Int32
messageId      :: !Int32
    -- ^ Sequence ID of the message.
    --
    -- If the clients expect to receive out-of-order responses, they may use
    -- the message ID to map responses back to their corresponding requests.
    -- If the client does not expect out-of-order responses, they are free to
    -- use the same message ID for all messages.
    --
    -- The server's contract regarding message IDs is that all responses must
    -- have the same message ID as their corresponding requests.
    , Message -> Value TStruct
messagePayload :: !(Value TStruct)
    -- ^ Contents of the message.
    }
  deriving (Int -> Message -> ShowS
[Message] -> ShowS
Message -> String
(Int -> Message -> ShowS)
-> (Message -> String) -> ([Message] -> ShowS) -> Show Message
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Message] -> ShowS
$cshowList :: [Message] -> ShowS
show :: Message -> String
$cshow :: Message -> String
showsPrec :: Int -> Message -> ShowS
$cshowsPrec :: Int -> Message -> ShowS
Show, Message -> Message -> Bool
(Message -> Message -> Bool)
-> (Message -> Message -> Bool) -> Eq Message
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Message -> Message -> Bool
$c/= :: Message -> Message -> Bool
== :: Message -> Message -> Bool
$c== :: Message -> Message -> Bool
Eq, Typeable, (forall x. Message -> Rep Message x)
-> (forall x. Rep Message x -> Message) -> Generic Message
forall x. Rep Message x -> Message
forall x. Message -> Rep Message x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Message x -> Message
$cfrom :: forall x. Message -> Rep Message x
Generic)

instance NFData Message