Potential Network SIG

taruti taruti at taruti.net
Tue Aug 25 03:39:03 EDT 2009


Excerpts from Thomas DuBuisson's message of Mon Aug 24 21:34:01 +0300 2009:
> I Object!  Code that uses addresses _might_ be very specific to IPv4
> or IPv6 - such code should be able to restrict itself to the proper
> category of addresses, headers etc without resorting to partial
> functions.  I propose a solution akin to:
> 
>     class Address a where ...
>     instance Address IPv4
>     instance Address IPv6

Also we could have

data ConnectOptions t where ...
instance Address t => Address (ConnectOptions t) where ...

> I understand the issue, but it would be a shame if a new network
> package failed to have functional Address parsing - its just such a
> basic thing.  I suppose this gives even more weight to a dual package
> solution.

IP-address parsing does not need Parsec. It is really quite simple.

Which type system extensions can we use? e.g. GADTs are attractive
but not supported by Hugs...

Here is a little bit of formulation for a possible high-level API.

Connections 

> type HostName = String
> type Port     = Word16

> data IPv4 = IPv4 HostName Port
> data IPv6 = IPv6 HostName Port
> data IP   = IP HostName Port
> data Unix = Unix FilePath
> data AddressOptions t where ...

> data Stream
> data Packet

> class Address t where
>   connectStream :: t -> IO (Socket Stream)
>   connectPacket :: t -> IO (Socket Packet)

> instance Connect IPv4 where ...
> instance Connect IPv6 where ...
> instance Connect IP   where ...
> instance Connect (AddressOptions t) where ...
> instance Connect Unix where ...
>
> withStream :: Address a => a -> (Socket Stream -> IO a) -> IO a
> withPacket :: Address a => a -> (Socket Packet -> IO a) -> IO a

> socketToHandle :: Socket Stream -> IO Handle

Sending and receiving

> -- | Send a bytestring
> send :: Socket t -> L.ByteString -> IO ()
> -- | Receive a bytestring with a maximum length
> recv :: Socket t -> Int -> IO L.ByteString
> -- | Receive from
> --   FIXME SomeAddress here is a kludge, what would be better?
> recvFrom :: Socket t -> Int -> IO (L.ByteString, SomeAddress)
> close :: Socket t -> IO ()
> sendTo :: Address a => Socket Packet -> a -> L.ByteString -> IO ()

Server API

> data ServerOptions t where ...
> -- FIXME
> server :: ServerOptions t -> (Socket t -> SomeAddress -> IO ()) -> IO Foobar

Miscellaneous highlevel api

> sleepForever :: IO ()
> getCurrentHost :: IO HostName
> nameToNumeric :: Address a => a -> IO a
> numericToName :: Address a => a -> IO a

- Taru Karttunen


More information about the Libraries mailing list