<br><br><div class="gmail_quote">2012/2/3 Michael Snoyman <span dir="ltr"><<a href="mailto:michael@snoyman.com">michael@snoyman.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
2012/2/3 Ertugrul Söylemez <<a href="mailto:es@ertes.de">es@ertes.de</a>>:<br>
<div><div class="h5">> Hello there,<br>
><br>
> I'm trying to build a server for testing the conduit and network-conduit<br>
> packages. As a contrived example the goal is to pick the first three<br>
> lines from the client and send them back without the line feeds. After<br>
> that, I'd like to switch to a simple echo server. This is the code:<br>
><br>
> module Main where<br>
><br>
> import Data.Conduit<br>
> import Data.Conduit.Binary as Cb<br>
> import Data.Conduit.List as Cl<br>
> import Data.Conduit.Network<br>
><br>
> handleClient :: Application<br>
> handleClient src snk =<br>
> src $$ do<br>
> (Cb.lines =$= Cl.isolate 3) =$ snk<br>
> snk<br>
><br>
> main :: IO ()<br>
> main = runTCPServer (ServerSettings 4000 Nothing) handleClient<br>
><br>
> I'm not sure whether it is correct to use the 'snk' sink multiple times,<br>
> and intuitively I'd say that this is wrong. What would be the proper<br>
> way to do this?<br>
><br>
><br>
> Greets,<br>
> Ertugrul<br>
<br>
</div></div>In this particular case, it will work due to the implementation of<br>
snk. In general, however, you're correct: you should not use the same<br>
sink twice.<br></blockquote><div><br></div><div>Since Sink works in a CPS fashion, by which i mean every step it return a new push close pair, i think it can be used multiple time.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
I haven't thought about it much yet, but my initial recommendation<br>
would be to create a new Conduit using SequencedSink, which takes the<br>
three lines and then switches over to a passthrough conduit. The<br>
result looks like this:<br>
<div class="im"><br>
<br>
module Main where<br>
<br>
import Data.Conduit<br>
import Data.Conduit.Binary as Cb<br>
import Data.Conduit.List as Cl<br>
import Data.Conduit.Network<br>
<br>
handleClient :: Application<br>
</div> handleClient src snk = src $$ myConduit =$ snk<br>
<div class="im"><br>
main :: IO ()<br>
main = runTCPServer (ServerSettings 4000 Nothing) handleClient<br>
<br>
</div> myConduit =<br>
sequenceSink 3 go<br>
where<br>
go 0 = return $ StartConduit $ Cl.map id<br>
go count = do<br>
mx <- Cb.lines =$ Cl.head<br>
case mx of<br>
Nothing -> return Stop<br>
Just x -> return $ Emit (count - 1) [x]<br>
<br>
Michael<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><a href="http://www.yi-programmer.com/blog/">http://www.yi-programmer.com/blog/</a><br>