Copyright | (c) Serokell 2016 |
---|---|
License | GPL-3 (see the file LICENSE) |
Maintainer | Serokell <hi@serokell.io> |
Stability | experimental |
Portability | POSIX, GHC |
Safe Haskell | None |
Language | Haskell2010 |
This module defines Dialog
monad, which is add-on over Transfer
and allows to
send/receive whole messages, where serialization strategy could be defined in
arbitrary way.
For Dialog
, send
function is trivial; listen
function infinitelly captures
messages from input stream, processing them in separate thread then.
Mainly, following structure of message is currently supported: [header, name, content] where name uniquely defines type of message.
Given message could be deserealized as sum of header and raw data; then user can just send the message further, or deserialize and process message's content.
- class MonadTransfer s m => MonadDialog s p m | m -> p, m -> s where
- newtype Dialog p m a = Dialog {
- getDialog :: ReaderT (p, ForkStrategy MessageName) m a
- runDialog :: p -> Dialog p m a -> m a
- data ForkStrategy s = ForkStrategy {
- withForkStrategy :: forall m. (MonadIO m, MonadTimed m) => s -> m () -> m ()
- send :: (Packable p (WithHeaderData () (ContentData r)), MonadDialog s p m, MonadThrow m) => NetworkAddress -> r -> m ()
- sendH :: (Packable p (WithHeaderData h (ContentData r)), MonadDialog s p m, MonadThrow m) => NetworkAddress -> h -> r -> m ()
- sendR :: (Packable p (WithHeaderData h RawData), MonadDialog s p m, MonadThrow m) => NetworkAddress -> h -> RawData -> m ()
- listen :: (Unpackable p (WithHeaderData () RawData), Unpackable p NameData, MonadListener s m, MonadDialog s p m) => Binding -> [Listener s p m] -> m (m ())
- listenH :: (Unpackable p (WithHeaderData h RawData), Unpackable p NameData, MonadListener s m, MonadDialog s p m) => Binding -> [ListenerH s p h m] -> m (m ())
- listenR :: (Unpackable p (WithHeaderData h RawData), Unpackable p NameData, MonadListener s m, MonadDialog s p m) => Binding -> [ListenerH s p h m] -> ListenerR s h m -> m (m ())
- reply :: (Packable p (WithHeaderData () (ContentData r)), MonadDialog s p m, MonadResponse s m, MonadThrow m) => r -> m ()
- replyH :: (Packable p (WithHeaderData h (ContentData r)), MonadDialog s p m, MonadResponse s m, MonadThrow m) => h -> r -> m ()
- replyR :: (Packable p (WithHeaderData h RawData), MonadDialog s p m, MonadResponse s m, MonadThrow m) => h -> RawData -> m ()
- data Listener s p m = (Unpackable p (ContentData r), Message r) => Listener (r -> ResponseT s m ())
- data ListenerH s p h m = (Unpackable p (ContentData r), Message r) => ListenerH ((h, r) -> ResponseT s m ())
- type ListenerR s h m = (h, RawData) -> ResponseT s m Bool
- getListenerName :: Listener s p m -> MessageName
- getListenerNameH :: ListenerH s p h m -> MessageName
- type MonadListener s m = (MonadIO m, MonadMask m, MonadTimed m, MonadTransfer s m, WithLogger m)
MonadDialog
class MonadTransfer s m => MonadDialog s p m | m -> p, m -> s where Source #
Defines communication based on messages. It allows to specify service data (header) for use by overlying protocols.
packingType :: m p Source #
forkStrategy :: m (ForkStrategy MessageName) Source #
setForkStrategy :: ForkStrategy MessageName -> m a -> m a Source #
MonadDialog s p m => MonadDialog s p (LoggerNameBox m) Source # | |
MonadTransfer s m => MonadDialog s p (Dialog p m) Source # | |
MonadDialog s p m => MonadDialog s p (ResponseT s0 m) Source # | |
MonadDialog s p m => MonadDialog s p (ReaderT * r m) Source # | |
Dialog
Default implementation of MonadDialog
.
Keeps packing type in context, allowing to use the same serialization strategy
all over the code without extra boilerplate.
Dialog | |
|
ForkStrategy
data ForkStrategy s Source #
ForkStrategy | |
|
Communication methods
Functions differ by suffix, meanings are following:
- No suffix - operates with plain message (TODO: weaken
Packable
constraints) - H - operates with message with header
- R - operates with message in raw form with header
send :: (Packable p (WithHeaderData () (ContentData r)), MonadDialog s p m, MonadThrow m) => NetworkAddress -> r -> m () Source #
Send plain message
sendH :: (Packable p (WithHeaderData h (ContentData r)), MonadDialog s p m, MonadThrow m) => NetworkAddress -> h -> r -> m () Source #
Send message with header
sendR :: (Packable p (WithHeaderData h RawData), MonadDialog s p m, MonadThrow m) => NetworkAddress -> h -> RawData -> m () Source #
Send message given in raw form
listen :: (Unpackable p (WithHeaderData () RawData), Unpackable p NameData, MonadListener s m, MonadDialog s p m) => Binding -> [Listener s p m] -> m (m ()) Source #
Starts server with given set of listeners.
listenH :: (Unpackable p (WithHeaderData h RawData), Unpackable p NameData, MonadListener s m, MonadDialog s p m) => Binding -> [ListenerH s p h m] -> m (m ()) Source #
Starts server with given set of listeners, which allow to read both header and content of received message.
listenR :: (Unpackable p (WithHeaderData h RawData), Unpackable p NameData, MonadListener s m, MonadDialog s p m) => Binding -> [ListenerH s p h m] -> ListenerR s h m -> m (m ()) Source #
reply :: (Packable p (WithHeaderData () (ContentData r)), MonadDialog s p m, MonadResponse s m, MonadThrow m) => r -> m () Source #
Sends message to peer node.
replyH :: (Packable p (WithHeaderData h (ContentData r)), MonadDialog s p m, MonadResponse s m, MonadThrow m) => h -> r -> m () Source #
Sends message with given header to peer node.
replyR :: (Packable p (WithHeaderData h RawData), MonadDialog s p m, MonadResponse s m, MonadThrow m) => h -> RawData -> m () Source #
Sends message with given header and message content in raw form to peer node.
Listeners
Creates plain listener which accepts message.
(Unpackable p (ContentData r), Message r) => Listener (r -> ResponseT s m ()) |
data ListenerH s p h m Source #
Creates listener which accepts header and message.
(Unpackable p (ContentData r), Message r) => ListenerH ((h, r) -> ResponseT s m ()) |
type ListenerR s h m = (h, RawData) -> ResponseT s m Bool Source #
Creates listener which accepts header and raw data. Returns, whether message souhld then be deserialized and passed to typed listener.
getListenerName :: Listener s p m -> MessageName Source #
Gets name of message type, acceptable by this listener.
getListenerNameH :: ListenerH s p h m -> MessageName Source #
Gets name of message type, acceptable by this listener.
Misc
type MonadListener s m = (MonadIO m, MonadMask m, MonadTimed m, MonadTransfer s m, WithLogger m) Source #