RWH Ch. 27 TCP Server Example

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

RWH Ch. 27 TCP Server Example

aditya siram-2
Hi all,
I am trying to understand the TCP Syslog Server example [1] from RWH.
Specifically my question is on this piece of code:
          -- | Process incoming messages
          procMessages :: MVar () -> Socket -> SockAddr -> IO ()
          procMessages lock connsock clientaddr =
              do connhdl <- socketToHandle connsock ReadMode
                 hSetBuffering connhdl LineBuffering
                 messages <- hGetContents connhdl
                 mapM_ (handle lock clientaddr) (lines messages)
                 hClose connhdl
                 handle lock clientaddr
                    "syslogtcpserver.hs: client disconnected"

How does control stay on "mapM_ (handle lock clientaddr) (lines
messages)" ? It would seem that the server would handle one message
and immediately close the handle and end - but it doesn't. I'm
guessing this has something to do with laziness, but I don't see how
to apply it.

thanks ..
-deech

[1] http://book.realworldhaskell.org/read/sockets-and-syslog.html
Reply | Threaded
Open this post in threaded view
|

RWH Ch. 27 TCP Server Example

Francesco Bochicchio
2009/4/14 aditya siram <[hidden email]>

> Hi all,
> I am trying to understand the TCP Syslog Server example [1] from RWH.
> Specifically my question is on this piece of code:
>          -- | Process incoming messages
>          procMessages :: MVar () -> Socket -> SockAddr -> IO ()
>          procMessages lock connsock clientaddr =
>              do connhdl <- socketToHandle connsock ReadMode
>                 hSetBuffering connhdl LineBuffering
>                 messages <- hGetContents connhdl
>                 mapM_ (handle lock clientaddr) (lines messages)
>                 hClose connhdl
>                 handle lock clientaddr
>                    "syslogtcpserver.hs: client disconnected"
>
> How does control stay on "mapM_ (handle lock clientaddr) (lines
> messages)" ? It would seem that the server would handle one message
> and immediately close the handle and end - but it doesn't. I'm
> guessing this has something to do with laziness, but I don't see how
> to apply it.



I think you are right thinking about laziness. The function hGetContents
produces a lazy
result (messages), therefore  any function that consumes messages could be
considered as 'iteratively calling' hGetContents to get new elements of
messages ...

Laziness is a Very Cool Feature ... when it does not bite you with thunk
accumulation problems  :-)

Ciao
------
FB

>
>
> thanks ..
> -deech
>
> [1] http://book.realworldhaskell.org/read/sockets-and-syslog.html
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090415/66a73b82/attachment.htm