iterio-server (empty) → 0.0
raw patch · 4 files changed
+134/−0 lines, 4 filesdep +ListLikedep +basedep +bytestringsetup-changed
Dependencies added: ListLike, base, bytestring, iterIO, monadIO, network
Files
- Data/IterIO/Server/TCPServer.hs +71/−0
- LICENSE +29/−0
- Setup.hs +2/−0
- iterio-server.cabal +32/−0
+ Data/IterIO/Server/TCPServer.hs view
@@ -0,0 +1,71 @@+-- |Generic building blocks for creating TCP Servers based on 'IterIO'+module Data.IterIO.Server.TCPServer (+ TCPServer,+ runTCPServer,+ simpleHttpServer,+ echoServer+) where++import Control.Concurrent.MonadIO+import Control.Monad+import qualified Data.ByteString.Lazy as L+import qualified Network.Socket as Net+import System.IO+import Data.IterIO+import Data.IterIO.Http+import Data.ListLike.IO++sockListenTCP :: Net.PortNumber -> IO Net.Socket+sockListenTCP pn = do+ sock <- Net.socket Net.AF_INET Net.Stream Net.defaultProtocol+ Net.setSocketOption sock Net.ReuseAddr 1+ Net.bindSocket sock (Net.SockAddrInet pn Net.iNADDR_ANY)+ Net.listen sock Net.maxListenQueue+ return sock++-- |'TCPServer' holds all the information necessary to run+-- bind to a sock and respond to TCP requests from the network.+data TCPServer inp m out = TCPServer {+ serverPort :: Net.PortNumber+ , serverHandler :: Inum inp out m ()+}++instance Show (TCPServer inp m out) where+ show s = "TCPServer { serverPort: " ++ (show $ serverPort s) ++ " }"++-- |Runs a 'TCPServer' in a loop.+runTCPServer :: (ListLikeIO inp e, ListLikeIO out e,+ ChunkData inp, ChunkData out, HasFork m)+ => TCPServer inp m out+ -> m ()+runTCPServer server = do+ sock <- liftIO $ sockListenTCP $ serverPort server+ forever $ do+ (iter, enum) <- liftIO $ do+ (s, _) <- Net.accept sock+ h <- Net.socketToHandle s ReadWriteMode+ hSetBuffering h NoBuffering+ return (handleI h, enumHandle h)+ _ <- fork $ do+ enum |$ serverHandler server .| iter+ return ()++-- |Creates a simple HTTP server from an 'HTTPRequestHandler'.+simpleHttpServer :: (HasFork m)+ => Net.PortNumber+ -> HttpRequestHandler m ()+ -> TCPServer L.ByteString m L.ByteString+simpleHttpServer port reqHandler = TCPServer port httpAppHandler+ where httpAppHandler = mkInumM $ do+ req <- httpReqI+ resp <- liftI $ reqHandler req+ irun $ enumHttpResp resp Nothing+-- |Creates a 'TCPServer' that echoes each line from the client until EOF.+echoServer :: (HasFork m) => Net.PortNumber -> TCPServer String m String+echoServer port = TCPServer port echoAppHandler+ where echoAppHandler = mkInumM $ forever $ do+ input <- safeLineI+ case input of+ Just output -> irun $ enumPure $ output ++ "\r\n"+ Nothing -> irun $ enumPure []+
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (C) 2011 Amit Levy++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are+met:++ 1. Redistributions of source code must retain the above copyright+ notice, this list of conditions, and the following disclaimer.++ 2. Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions, and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ 3. The name of the author may not be used to endorse or promote+ products derived from this software without specific prior+ written permission.++THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING+IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE+POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ iterio-server.cabal view
@@ -0,0 +1,32 @@+Name: iterio-server+Version: 0.0+build-type: Simple+License: BSD3+License-File: LICENSE+Homepage: https://github.com/alevy/iterio-server+Author: Amit Levy+Maintainer: Amit Levy <levya at cs.stanford dot edu>+Stability: experimental+Synopsis: Library for building servers with IterIO+Category: Network, Enumerator+Cabal-Version: >= 1.6++Description:+ This module contains a set of generic building blocks for building servers with IterIO.++Source-repository head+ Type: git+ Location: git://github.com/alevy/iterio-server.git++Library+ Build-Depends: base >= 4 && < 5,+ bytestring >= 0.9 && < 1,+ ListLike >= 3 && < 4,+ monadIO >= 0.10 && < 0.20,+ iterIO >= 0.2 && < 0.3,+ network >= 2.3 && < 2.4++ ghc-options: -Wall++ Exposed-modules:+ Data.IterIO.Server.TCPServer