Browse Source

Typing and docummentation fixes for publishing.

Marcos 8 years ago
parent
commit
61c3a05a4e
4 changed files with 57 additions and 44 deletions
  1. 5 0
      src/System/IO/Uniform.hs
  2. 10 7
      src/System/IO/Uniform/Streamline.hs
  3. 41 36
      src/System/IO/Uniform/Targets.hs
  4. 1 1
      uniform-io.cabal

+ 5 - 0
src/System/IO/Uniform.hs

@@ -1,3 +1,8 @@
+-- |
+-- Uniform-IO provides a typeclass for uniform access of different types of targets,
+-- and implementations for abstracting standard streams, files and network connections.
+-- This module also provides TLS wraping over other IO targets.
 module System.IO.Uniform (module System.IO.Uniform.Targets) where
 
+
 import System.IO.Uniform.Targets

+ 10 - 7
src/System/IO/Uniform/Streamline.hs

@@ -33,40 +33,43 @@ defaultTimeout = 1000000 * 600
 readF :: MonadIO m => Data -> m ByteString
 readF cl = if echo cl
           then do
-            l <- liftIO $ S.fRead (str cl) blockSize
+            l <- liftIO $ S.uRead (str cl) blockSize
             liftIO $ BS.putStr "<"
             liftIO $ BS.putStr l
             return l
-          else liftIO $ S.fRead (str cl) blockSize
+          else liftIO $ S.uRead (str cl) blockSize
 
 writeF :: MonadIO m => Data -> ByteString -> m ()
 writeF cl l = if echo cl
              then do
                liftIO $ BS.putStr ">"
                liftIO $ BS.putStr l
-               liftIO $ S.fPut (str cl) l
-             else liftIO $ S.fPut (str cl) l
+               liftIO $ S.uPut (str cl) l
+             else liftIO $ S.uPut (str cl) l
 
 -- | withServer f serverIP port
+--
 --  Connects to the given server port, runs f, and closes the connection.
 withServer :: MonadIO m => Streamline m a -> IP -> Int -> m a
 withServer f host port = do
   ds <- liftIO $ S.connectTo host port
   (ret, _) <- withTarget' f $ Data (SomeIO ds) defaultTimeout "" False False
-  liftIO $ S.fClose ds
+  liftIO $ S.uClose ds
   return ret
 
 -- | withClient f boundPort
+--
 --  Accepts a connection at the bound port, runs f and closes the connection.
-withClient :: MonadIO m => (IP -> Int -> Streamline m a) -> S.BoundPort -> m a
+withClient :: MonadIO m => (IP -> Int -> Streamline m a) -> S.BoundedPort -> m a
 withClient f port = do
   ds <- liftIO $ S.accept port
   (peerIp, peerPort) <- liftIO $ S.getPeer ds
   (ret, _) <- withTarget' (f peerIp peerPort) $ Data (SomeIO ds) defaultTimeout "" False False
-  liftIO $ S.fClose ds
+  liftIO $ S.uClose ds
   return ret
 
 -- | withTarget f someIO
+--
 --  Runs f wrapped on a Streamline monad that does IO on nomeIO.
 withTarget :: (MonadIO m, UniformIO a) => Streamline m b -> a -> m b
 withTarget f s = do  

+ 41 - 36
src/System/IO/Uniform/Targets.hs

@@ -2,11 +2,7 @@
 {-# LANGUAGE ExistentialQuantification #-}
 {-# LANGUAGE ForeignFunctionInterface #-}
 
--- |
--- Filelike provides a typeclass for Unix style "everything is a file" IO,
--- and implementations for abstracting standard IO, files and network connections.
--- This module also provides TLS wraping over other filelike types.
-module System.IO.Uniform.Targets (TlsSettings(..), UniformIO(..), SocketIO, FileIO, TlsStream, BoundPort, SomeIO(..), connectTo, connectToHost, bindPort, accept, openFile, getPeer, closePort) where
+module System.IO.Uniform.Targets (TlsSettings(..), UniformIO(..), SocketIO, FileIO, TlsStream, BoundedPort, SomeIO(..), connectTo, connectToHost, bindPort, accept, openFile, getPeer, closePort) where
 
 import Foreign
 import Foreign.C.Types
@@ -29,24 +25,29 @@ instance Default TlsSettings where
   def = TlsSettings "" ""
 
 -- |
--- Typeclass for IO objects that behave like a Unix file (independent of seeking support).
+-- Typeclass for uniform IO targets.
 class UniformIO a where
-  -- | fRead fd n
-  --  Reads a block of at most n bytes of data from the filelike object fd.
+  -- | uRead fd n
+  --
+  --  Reads a block of at most n bytes of data from the IO target.
   --  Reading will block if there's no data available, but will return immediately
   --  if any amount of data is availble.
-  fRead  :: a -> Int -> IO ByteString
-  -- | fPut fd text
-  --  Writes all the bytes of text into the filelike object. Takes care of retrying if needed.
-  fPut   :: a -> ByteString -> IO ()
+  uRead  :: a -> Int -> IO ByteString
+  -- | uPut fd text
+  --
+  --  Writes all the bytes of text into the IO target. Takes care of retrying if needed.
+  uPut   :: a -> ByteString -> IO ()
   -- | fClose fd
-  --  Closes the filelike object, releasing any allocated resource. Resources may leak if not called
+  --
+  --  Closes the IO target, releasing any allocated resource. Resources may leak if not called
   --  for every oppened fd.
-  fClose :: a -> IO ()
+  uClose :: a -> IO ()
   -- | startTLS fd
-  --  Starts a TLS connection over the filelike object.
+  --
+  --  Starts a TLS connection over the IO target.
   startTls :: TlsSettings -> a -> IO TlsStream
   -- | isSecure fd
+  --
   --  Indicates whether the data written or read from fd is secure at transport.
   isSecure :: a -> Bool
   
@@ -54,15 +55,15 @@ class UniformIO a where
 data SomeIO = forall a. (UniformIO a) => SomeIO a
 
 instance UniformIO SomeIO where
-  fRead (SomeIO s) n = fRead s n
-  fPut (SomeIO s) t  = fPut s t
-  fClose (SomeIO s) = fClose s
+  uRead (SomeIO s) n = uRead s n
+  uPut (SomeIO s) t  = uPut s t
+  uClose (SomeIO s) = uClose s
   startTls set (SomeIO s) = startTls set s
   isSecure (SomeIO s) = isSecure s
 
 data Nethandler
--- | A bound IP port from where to accept SocketIO connections.
-newtype BoundPort = BoundPort {lis :: (Ptr Nethandler)}
+-- | A bounded IP port from where to accept SocketIO connections.
+newtype BoundedPort = BoundedPort {lis :: (Ptr Nethandler)}
 data SockDs
 newtype SocketIO = SocketIO {sock :: (Ptr SockDs)}
 data FileDs
@@ -72,21 +73,21 @@ newtype TlsStream = TlsStream {tls :: (Ptr TlsDs)}
 
 -- | UniformIO IP connections.
 instance UniformIO SocketIO where
-  fRead s n = allocaArray n (
+  uRead s n = allocaArray n (
     \b -> do
       count <- c_recvSock (sock s) b (fromIntegral n)
       if count < 0
         then throwErrno "could not read"
         else BS.packCStringLen (b, fromIntegral count)
     )
-  fPut s t = BS.useAsCStringLen t (
+  uPut s t = BS.useAsCStringLen t (
     \(str, n) -> do
       count <- c_sendSock (sock s) str $ fromIntegral n
       if count < 0
         then throwErrno "could not write"
         else return ()
     )
-  fClose s = c_closeSock (sock s)
+  uClose s = c_closeSock (sock s)
   startTls st s = withCString (tlsCertificateChainFile st) (
     \cert -> withCString (tlsPrivateKeyFile st) (
       \key -> do
@@ -100,46 +101,48 @@ instance UniformIO SocketIO where
   
 -- | UniformIO type for file IO.
 instance UniformIO FileIO where
-  fRead s n = allocaArray n (
+  uRead s n = allocaArray n (
     \b -> do
       count <- c_recvFile (fd s) b $ fromIntegral n
       if count < 0
         then throwErrno "could not read"
         else  BS.packCStringLen (b, fromIntegral count)
     )
-  fPut s t = BS.useAsCStringLen t (
+  uPut s t = BS.useAsCStringLen t (
     \(str, n) -> do
       count <- c_sendFile (fd s) str $ fromIntegral n
       if count < 0
         then throwErrno "could not write"
         else return ()
     )
-  fClose s = c_closeFile (fd s)
+  uClose s = c_closeFile (fd s)
   -- Not implemented yet.
   startTls _ _ = return . TlsStream $ nullPtr
   isSecure _ = False
   
--- | UniformIO wrapper that applies TLS to communication on filelike objects.
+-- | UniformIO wrapper that applies TLS to communication on IO target.
+-- This type is constructed by calling startTls on other targets.
 instance UniformIO TlsStream where
-  fRead s n = allocaArray n (
+  uRead s n = allocaArray n (
     \b -> do
       count <- c_recvTls (tls s) b $ fromIntegral n
       if count < 0
         then throwErrno "could not read"
         else BS.packCStringLen (b, fromIntegral count)
     )
-  fPut s t = BS.useAsCStringLen t (
+  uPut s t = BS.useAsCStringLen t (
     \(str, n) -> do
       count <- c_sendTls (tls s) str $ fromIntegral n
       if count < 0
         then throwErrno "could not write"
         else return ()
     )
-  fClose s = c_closeTls (tls s)
+  uClose s = c_closeTls (tls s)
   startTls _ s = return s
   isSecure _ = True
 
 -- | connectToHost hostName port
+--
 --  Connects to the given host and port.
 connectToHost :: String -> Int -> IO SocketIO
 connectToHost host port = do
@@ -158,6 +161,7 @@ connectToHost host port = do
 
 
 -- | ConnecctTo ipAddress port
+--
 --  Connects to the given port of the host at the given IP address.
 connectTo :: IP.IP -> Int -> IO SocketIO
 connectTo host port = do
@@ -187,17 +191,18 @@ connectTo host port = do
 -- | bindPort port
 --  Binds to the given IP port, becoming ready to accept connections on it.
 --  Binding to port numbers under 1024 will fail unless performed by the superuser,
---  once bound, a process can reduce its privileges and still accept clients on that port.
-bindPort :: Int -> IO BoundPort
+--  once bounded, a process can reduce its privileges and still accept clients on that port.
+bindPort :: Int -> IO BoundedPort
 bindPort port = do
-  r <- fmap BoundPort $ c_getPort $ fromIntegral port
+  r <- fmap BoundedPort $ c_getPort $ fromIntegral port
   if lis r == nullPtr
     then throwErrno "could not bind to port"
     else return r
   
 -- | accept port
+--
 --  Accept clients on a port previously bound with bindPort.
-accept :: BoundPort -> IO SocketIO
+accept :: BoundedPort -> IO SocketIO
 accept port = do
   r <- fmap SocketIO $ c_accept (lis port)
   if sock r == nullPtr
@@ -236,8 +241,8 @@ getPeer s = allocaArray 16 (
     )
   )
     
--- | Closes a BoundPort, and releases any resource used by it.
-closePort :: BoundPort -> IO ()
+-- | Closes a BoundedPort, and releases any resource used by it.
+closePort :: BoundedPort -> IO ()
 closePort p = c_closePort (lis p)
 
 foreign import ccall "getPort" c_getPort :: CInt -> IO (Ptr Nethandler)

+ 1 - 1
uniform-io.cabal

@@ -30,7 +30,7 @@ description:
 
 
 -- URL for the project homepage or repository.
-homepage:   http://sealgram.com/git/haskell/uniform-io
+homepage:   https://sealgram.com/git/haskell/uniform-io
 
 -- The license under which the package is released.
 license:    MIT