-
Notifications
You must be signed in to change notification settings - Fork 97
Network.Transport
Network.Transport
is a Network Abstraction Layer which provides the following high-level concepts:
- Nodes in the network are represented by
EndPoint
s -
Connection
s betweenEndPoint
s are unidirectional (they offersend
but no per-connection receive) -
EndPoint
s are notified ofEvent
s such as new or broken incoming connections and incoming messages.
This design was heavily influenced by the design of the Common Communication Interface (CCI). Important design goals are:
- Connections should be lightweight: it should be no problem to create thousands of connections between endpoints.
- Error handling is explicit: every function declares as part of its type which errors it can return (no exceptions are thrown)
- Error handling is "abstract": errors that originate from implementation specific problems (such as "no more sockets" in the TCP implementation) get mapped to generic errors ("insufficient resources") at the Transport level.
It is intended that Network.Transport
can be instantiated to use many different protocols for message passing: TCP/IP, UDP, MPI, CCI, ZeroMQ, ssh, MVars, Unix pipes and more. Currently, we offer a TCP/IP transport and (mostly for demonstration purposes) an in-memory Chan
-based transport.
For a flavour of what programming with Network.Transport
looks like, here is a tiny self-contained example.
import Network.Transport
import Network.Transport.TCP (createTransport)
import Control.Concurrent
import Control.Monad
import Data.String
main :: IO ()
main = do
serverAddr <- newEmptyMVar
clientDone <- newEmptyMVar
Right transport <- createTransport "127.0.0.1" "10080"
-- "Server"
forkIO $ do
Right endpoint <- newEndPoint transport
putMVar serverAddr (address endpoint)
forever $ do
event <- receive endpoint
case event of
Received _ msg -> print msg
_ -> return () -- ignore
-- "Client"
forkIO $ do
Right endpoint <- newEndPoint transport
Right conn <- do addr <- readMVar serverAddr
connect endpoint addr ReliableOrdered
send conn [fromString "Hello world"]
putMVar clientDone ()
-- Wait for the client to finish
takeMVar clientDone
We create a "server" and a "client" (each represented by an EndPoint
). The server waits for Event
s and whenever it receives a message it just prints it to the console; it ignores all other messages. The client sets up a connection to the server, sends a single message, and then signals to the main process that it is done.
-
Programming with Network.Transport introduces
Network.Transport
from an application developer's point of view. -
Creating New Transports describes how to design new instantiations of
Network.Transport
for other messaging protocols, and describes the TCP transport in some detail as a guiding example. - New backend and transport design has some notes about the design of the transport layer. Note however that this page is currently out of date.