<br><br><div class="gmail_quote">2012/2/3 Michael Snoyman <span dir="ltr">&lt;<a href="mailto:michael@snoyman.com">michael@snoyman.com</a>&gt;</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 &lt;<a href="mailto:es@ertes.de">es@ertes.de</a>&gt;:<br>
<div><div class="h5">&gt; Hello there,<br>
&gt;<br>
&gt; I&#39;m trying to build a server for testing the conduit and network-conduit<br>
&gt; packages.  As a contrived example the goal is to pick the first three<br>
&gt; lines from the client and send them back without the line feeds.  After<br>
&gt; that, I&#39;d like to switch to a simple echo server.  This is the code:<br>
&gt;<br>
&gt;    module Main where<br>
&gt;<br>
&gt;    import Data.Conduit<br>
&gt;    import Data.Conduit.Binary as Cb<br>
&gt;    import Data.Conduit.List as Cl<br>
&gt;    import Data.Conduit.Network<br>
&gt;<br>
&gt;    handleClient :: Application<br>
&gt;    handleClient src snk =<br>
&gt;        src $$ do<br>
&gt;            (Cb.lines =$= Cl.isolate 3) =$ snk<br>
&gt;            snk<br>
&gt;<br>
&gt;    main :: IO ()<br>
&gt;    main = runTCPServer (ServerSettings 4000 Nothing) handleClient<br>
&gt;<br>
&gt; I&#39;m not sure whether it is correct to use the &#39;snk&#39; sink multiple times,<br>
&gt; and intuitively I&#39;d say that this is wrong.  What would be the proper<br>
&gt; way to do this?<br>
&gt;<br>
&gt;<br>
&gt; Greets,<br>
&gt; Ertugrul<br>
<br>
</div></div>In this particular case, it will work due to the implementation of<br>
snk. In general, however, you&#39;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&#39;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 &lt;- Cb.lines =$ Cl.head<br>
            case mx of<br>
                Nothing -&gt; return Stop<br>
                Just x -&gt; 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>