summaryrefslogtreecommitdiff
path: root/src/Network/RPC.hs
blob: 727422fdd51ff0d3d2010c40e321217040cbea23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE ConstraintKinds        #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses  #-}
{-# LANGUAGE RankNTypes             #-}
{-# LANGUAGE ScopedTypeVariables    #-}
{-# LANGUAGE TypeFamilies           #-}
{-# LANGUAGE DeriveDataTypeable     #-}
module Network.RPC where

import Data.ByteString (ByteString)
import Data.Kind       (Constraint)
import Data.Data
import Network.Socket

data MessageClass = Error | Query | Response
 deriving (Eq,Ord,Enum,Bounded,Data,Show,Read)

class Envelope envelope where
    type TransactionID envelope
    type NodeId envelope

    envelopePayload     :: envelope a -> a
    envelopeTransaction :: envelope a -> TransactionID envelope
    envelopeClass       :: envelope a -> MessageClass

    -- | > buildReply self addr qry response
    --
    --    [ self ]     this node's id.
    --
    --    [ addr ]     SockAddr of query origin.
    --
    --    [ qry ]      received query message.
    --
    --    [ response ] response payload.
    --
    -- Returns: response message envelope
    buildReply :: NodeId envelope -> SockAddr -> envelope a -> b -> envelope b

class Envelope envelope => WireFormat raw envelope where
    type SerializableTo raw :: * -> Constraint
    type CipherContext raw envelope

    decodeHeaders :: CipherContext raw envelope -> ByteString -> Either String (envelope raw)
    decodePayload :: SerializableTo raw a => envelope raw -> Either String (envelope a)

    encodeHeaders :: CipherContext raw envelope -> envelope raw -> ByteString
    encodePayload :: SerializableTo raw a => envelope a -> envelope raw