nanomsg-haskell-0.2.3: Bindings to the nanomsg library

Copyright(c) 2013 Ivar Nymoen
LicenseMIT
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Nanomsg

Contents

Description

This is a Haskell binding for the nanomsg library: http://nanomsg.org/.

There's support for (evented) blocking send and recv, a non-blocking receive, and for all the socket types and the functions you need to wire them up and tear them down again.

Most socket options are available through accessor and mutator functions. Sockets are typed, transports are not.

The documentation is adapted or quoted verbatim from the nanomsg manual, please refer to nanomsg.org for authoritative info. There's a simple code example in README.md.

Synopsis

Types

Socket types

data Pair Source

Socket for communication with exactly one peer. Each party can send messages at any time. If the peer is not available or the send buffer is full, subsequent calls will block until it’s possible to send the message.

Constructors

Pair 

data Req Source

Request socket. Pairs with Rep sockets.

The socket will resend requests automatically if there's no reply within a given time. The default timeout is 1 minute.

See also Rep, setRequestResendInterval.

Constructors

Req 

data Rep Source

Reply socket.

See also Req.

Constructors

Rep 

data Pub Source

Publish socket. Pairs with subscribe sockets.

See also Sub.

Constructors

Pub 

data Sub Source

Subscribe socket.

Only messages that the socket is subscribed to are received. When the socket is created there are no subscriptions and thus no messages will be received.

See also Pub, subscribe and unsubscribe.

Constructors

Sub 

data Surveyor Source

Surveyor and respondent are used to broadcast a survey to multiple locations and gather the responses.

This socket is used to send a survey. The survey is delivered to all onnected respondents. Once the query is sent, the socket can be used to receive the responses.

When the survey deadline expires, receive will throw an NNException.

See also Respondent, setSurveyorDeadline.

Constructors

Surveyor 

data Respondent Source

Used to respond to a survey. Survey is received using receive, response is sent using send. This socket can be connected to at most one peer.

See also Surveyor.

Constructors

Respondent 

data Push Source

Push and Pull sockets fair queue messages from one processing step, load balancing them among instances of the next processing step.

See also Pull.

Constructors

Push 

data Pull Source

Pull socket.

See also Push.

Constructors

Pull 

data Bus Source

Broadcasts messages from any node to all other nodes in the topology. The socket should never receives messages that it sent itself.

Constructors

Bus 

Other

data Socket a Source

Sockets are created by socket and connections are established with connect or bind.

Free sockets using close.

Instances

Eq a => Eq (Socket a) Source 
Show a => Show (Socket a) Source 

data Endpoint Source

Endpoint identifier. Created by connect or bind.

Close connections using shutdown.

data NNException Source

Pretty much any error condition throws this exception.

class SocketType a => Sender a Source

Typeclass restricting which sockets can use the send function.

Operations

General operations

socket :: SocketType a => a -> IO (Socket a) Source

Creates a socket. Connections are formed using bind or connect.

See also: close.

withSocket :: SocketType a => a -> (Socket a -> IO b) -> IO b Source

Creates a socket and runs your action with it.

E.g. collecting 10 messages:

withSocket Sub $ \sub -> do
    _ <- connect sub "tcp://localhost:5560"
    subscribe sub (C.pack "")
    replicateM 10 (recv sub)

Ensures the socket is closed when your action is done.

bind :: Socket a -> String -> IO Endpoint Source

Binds the socket to a local interface.

See the nanomsg documentation for specifics on transports. Note that host names do not work for tcp. Some examples are:

bind sock "tcp://*:5560"
bind sock "tcp://eth0:5560"
bind sock "tcp://127.0.0.1:5560"
bind sock "inproc://test"
bind sock "ipc:///tmp/test.ipc"

This function returns an Endpoint, which can be supplied to shutdown to remove a connection.

See also: connect, shutdown.

connect :: Socket a -> String -> IO Endpoint Source

Connects the socket to an endpoint.

e.g. :

connect sock "tcp://localhost:5560"
connect sock "inproc://test"

See also: bind, shutdown.

send :: Sender a => Socket a -> ByteString -> IO () Source

Blocking function for sending a message

See also: recv, recv'.

recv :: Receiver a => Socket a -> IO ByteString Source

Blocking receive.

recv' :: Receiver a => Socket a -> IO (Maybe ByteString) Source

Nonblocking receive function.

subscribe :: Socket Sub -> ByteString -> IO () Source

Subscribe to a given subject string.

unsubscribe :: Socket Sub -> ByteString -> IO () Source

Unsubscribes from a subject.

shutdown :: Socket a -> Endpoint -> IO () Source

Removes an endpoint from a socket.

See also: bind, connect.

close :: Socket a -> IO () Source

Closes the socket. Any buffered inbound messages that were not yet received by the application will be discarded. The library will try to deliver any outstanding outbound messages for the time specified by NN_LINGER socket option. The call will block in the meantime.

term :: IO () Source

Switches nanomsg into shutdown modus and interrupts any waiting function calls.

Socket option settings

linger :: Socket a -> IO Int Source

Specifies how long the socket should try to send pending outbound messages after close has been called, in milliseconds.

Negative value means infinite linger. Default value is 1000 (1 second).

setLinger :: Socket a -> Int -> IO () Source

Specifies how long should the socket try to send pending outbound messages after close has been called, in milliseconds.

Negative value means infinite linger. Default value is 1000 (1 second).

sndBuf :: Socket a -> IO Int Source

Size of the send buffer, in bytes. To prevent blocking for messages larger than the buffer, exactly one message may be buffered in addition to the data in the send buffer.

Default value is 128kB.

setSndBuf :: Socket a -> Int -> IO () Source

Size of the send buffer, in bytes. To prevent blocking for messages larger than the buffer, exactly one message may be buffered in addition to the data in the send buffer.

Default value is 128kB.

rcvBuf :: Socket a -> IO Int Source

Size of the receive buffer, in bytes. To prevent blocking for messages larger than the buffer, exactly one message may be buffered in addition to the data in the receive buffer.

Default value is 128kB.

setRcvBuf :: Socket a -> Int -> IO () Source

Size of the receive buffer, in bytes. To prevent blocking for messages larger than the buffer, exactly one message may be buffered in addition to the data in the receive buffer.

Default value is 128kB.

reconnectInterval :: Socket a -> IO Int Source

For connection-based transports such as TCP, this option specifies how long to wait, in milliseconds, when connection is broken before trying to re-establish it.

Note that actual reconnect interval may be randomised to some extent to prevent severe reconnection storms.

Default value is 100 (0.1 second).

setReconnectInterval :: Socket a -> Int -> IO () Source

For connection-based transports such as TCP, this option specifies how long to wait, in milliseconds, when connection is broken before trying to re-establish it.

Note that actual reconnect interval may be randomised to some extent to prevent severe reconnection storms.

Default value is 100 (0.1 second).

reconnectIntervalMax :: Socket a -> IO Int Source

This option is to be used only in addition to NN_RECONNECT_IVL option. It specifies maximum reconnection interval. On each reconnect attempt, the previous interval is doubled until NN_RECONNECT_IVL_MAX is reached.

Value of zero means that no exponential backoff is performed and reconnect interval is based only on NN_RECONNECT_IVL. If NN_RECONNECT_IVL_MAX is less than NN_RECONNECT_IVL, it is ignored.

Default value is 0.

setReconnectIntervalMax :: Socket a -> Int -> IO () Source

This option is to be used only in addition to NN_RECONNECT_IVL option. It specifies maximum reconnection interval. On each reconnect attempt, the previous interval is doubled until NN_RECONNECT_IVL_MAX is reached.

Value of zero means that no exponential backoff is performed and reconnect interval is based only on NN_RECONNECT_IVL. If NN_RECONNECT_IVL_MAX is less than NN_RECONNECT_IVL, it is ignored.

Default value is 0.

sndPrio :: Socket a -> IO Int Source

Sets outbound priority for endpoints subsequently added to the socket. This option has no effect on socket types that send messages to all the peers. However, if the socket type sends each message to a single peer (or a limited set of peers), peers with high priority take precedence over peers with low priority.

Highest priority is 1, lowest priority is 16. Default value is 8.

setSndPrio :: Socket a -> Int -> IO () Source

Sets outbound priority for endpoints subsequently added to the socket. This option has no effect on socket types that send messages to all the peers. However, if the socket type sends each message to a single peer (or a limited set of peers), peers with high priority take precedence over peers with low priority.

Highest priority is 1, lowest priority is 16. Default value is 8.

ipv4Only :: Socket a -> IO Int Source

If set to 1, only IPv4 addresses are used. If set to 0, both IPv4 and IPv6 addresses are used.

Default value is 1.

setIpv4Only :: Socket a -> Int -> IO () Source

If set to 1, only IPv4 addresses are used. If set to 0, both IPv4 and IPv6 addresses are used.

Default value is 1.

requestResendInterval :: Socket Req -> IO Int Source

This option is defined on the full REQ socket. If reply is not received in specified amount of milliseconds, the request will be automatically resent.

Default value is 60000 (1 minute).

setRequestResendInterval :: Socket Req -> Int -> IO () Source

This option is defined on the full REQ socket. If reply is not received in specified amount of milliseconds, the request will be automatically resent.

Default value is 60000 (1 minute).

surveyorDeadline :: Socket Surveyor -> IO Int Source

Get timeout for Surveyor sockets

setSurveyorDeadline :: Socket Surveyor -> Int -> IO () Source

Set timeout for Surveyor sockets

tcpNoDelay :: Socket a -> IO Int Source

This option, when set to 1, disables Nagle's algorithm.

Default value is 0.

setTcpNoDelay :: Socket a -> Int -> IO () Source

This option, when set to 1, disables Nagle's algorithm.

Default value is 0.