netspec-0.2.0.0: Simplify static Networking tasks

Safe HaskellSafe-Infered

Network.NetSpec

Contents

Description

Simplify static Networking tasks.

Synopsis

Types and Constructors

data NetSpec t s Source

Define the specification of your networking task as a begin, loop, and end proceedure. Run your NetSpec with runSpec.

t indicates the Traversable structure used. [] is recommended for simplicity, but you are at liberty to use any Traversable you see fit.

s indicates the type used for state. Use () for a stateless specification.

A server must specify which ports to listen on, while a client instead specifies tuples of (hostname, port) to connect to.

Constructors

ServerSpec 

Fields

_ports :: t PortID
 
_begin :: t Handle -> IO s
 
_loop :: t Handle -> s -> IO (SpecState s)
 
_end :: t Handle -> s -> IO ()
 
ClientSpec 

Fields

_conns :: t (String, PortID)
 
_begin :: t Handle -> IO s
 
_loop :: t Handle -> s -> IO (SpecState s)
 
_end :: t Handle -> s -> IO ()
 

data SpecState s Source

Indicate whether to Continue or Stop with a given state

Constructors

Continue s 
Stop s 

Instances

Functions

Running a NetSpec

runSpec :: Traversable t => NetSpec t s -> IO ()Source

Run a NetSpec.

Running a spec will step through your Traversable of connection descriptions, and replace each one with a Handle, preserving the structure of the Traversable otherwise.

Regardless of exceptions, all Handles and Sockets opened by the spec will be closed at the end of the run; you should not need to close any of the Handles given to you by the spec.

(Note runSpec calls withSocketsDo for you)

Continue and Stop Combinators

continue :: Monad m => s -> m (SpecState s)Source

Continue with a given state

continue_ :: Monad m => m (SpecState ())Source

Continue (statless)

continueIf :: Monad m => (a -> s -> Bool) -> m (a, s) -> m (SpecState s)Source

Conditionally continue with a given state, based on that state and additional given information.

Recommended usage:

 _loop = \handles -> continueIf f .: runStateT $ do ...

continueIf' :: Monad m => (s -> Bool) -> m s -> m (SpecState s)Source

Conditionally continue with a given state, based solely on that state.

Recommended usage:

 _loop = \handles -> continueIf' f .: execStateT $ do ...

continueIf_ :: Monad m => (a -> Bool) -> m a -> m (SpecState ())Source

Conditionally continue statelessly, based on given information.

Recommended usage

 _loop = \handles () -> continueIf_ f $ do ...

stop :: Monad m => s -> m (SpecState s)Source

Stop with a given state

stop_ :: Monad m => m (SpecState ())Source

Stop (stateless)

stopIf :: Monad m => (a -> s -> Bool) -> m (a, s) -> m (SpecState s)Source

Conditionally stop with a given state, based on that state and additional given information.

stopIf' :: Monad m => (s -> Bool) -> m s -> m (SpecState s)Source

Conditionally stop with a given state, based solely on that state.

stopIf_ :: Monad m => (a -> Bool) -> m a -> m (SpecState ())Source

Conditionally stop statlessly, based on given information.

Convenience

Composition

(.:) :: (a -> b) -> (c -> d -> a) -> c -> d -> bSource

Compose two functions, similar to . from Prelude. If h = f .: g then h x y = f (g x y).

IO and Networking

Functors

State

stateT :: Monad m => (s -> (a, s)) -> StateT s m aSource

Lift a state function into a StateT monad stack