<div dir="ltr">Didn't see your response, my gmail auto filter marks my haskell messages as read and puts them in a folder so I have to explicitly check to see. Anyway:<div><br></div><div>The bit of code I'm stuck on is:</div>

<div><div>startServer :: NodeSettings -> IO ()</div><div>startServer conf = do</div><div>  print "Bootstrapping, trying to reach configured seed nodes"</div><div>  let cluster = G.bootstrapNode $ seedNodes conf</div>

<div>  print "Initializing Gossip"</div><div>  gossip cluster conf</div><div>  print "Listening for client connections"</div><div>  listenForClients cluster conf</div><div>  print "Server shutting down..."</div>

</div><div><br></div><div>The recursive version I had that was similar to yours has long since gone because I couldn't get it to work.</div><div>bootstrapNode returns [RemoteNode].<br></div><div>So at the moment the cluster info is fetched once and that's it, where it falls apart in my head is when I try to change this so that cluster is updated every n seconds.</div>

<div><br></div><div>listenForClients is on the main thread with each accepted connection run with forkIO.</div><div><br></div><div>All I came up with was after "gossip conf" , I could do</div><div>forkIO someFn where</div>

<div><br></div><div>someFn = do</div><div> threadDelay n</div><div> let cluster = G.bootstrapNode $ seedNodes conf</div><div><br></div><div>but obviously this doesn't work because my "gossip"  and "listenForClients" functions already have an immutable version of cluster. So I'm not sure how to get the updated version to those fns.</div>

<div><br></div><div>Bare in mind that listenForClients and gossip are doing something similar to</div><div><br></div><div><div>  withSocketsDo $ do</div><div>  sock <- listenOn $ PortNumber(fromInteger gossipPort)</div>

</div><div><br></div><div>so only accepting a new connection causes those threads to do anything, but when a connection is accepted, if the request demands the cluster data those fns shouldn't go off gathering the data and shouldn't send the (probably) out dated one from the initialization but instead the one that's been updated every n in the background.</div>

<div><br></div><div>Thanks</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jan 3, 2014 at 6:17 PM, Bob Ippolito <span dir="ltr"><<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">I wouldn't recommend going down the path of using IORef or MVar for everything, it's not easy to build robust systems that way. Do you mind showing the code that you tried that "fell apart"? I'm sure there's a slightly different way to structure it that would work just fine, probably using some kind of message passing.</div>

<div class=""><div class="h5">
<div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jan 3, 2014 at 9:57 AM, Courtney Robinson <span dir="ltr"><<a href="mailto:courtney@crlog.info" target="_blank">courtney@crlog.info</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Thanks to both of you for your reply.<div>

I have something similar to your example Bob, wasn't sure if it was a good way forward. Plus it fell apart when I tried contacting multiple hosts on different threads using forkIO. But with Daniel's response I'll look into MVars.</div>




<div><br></div><div>Thanks again</div></div><div class="gmail_extra"><div><div><br><br><div class="gmail_quote">On Fri, Jan 3, 2014 at 5:16 PM, Bob Ippolito <span dir="ltr"><<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>></span> wrote:<br>




<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Generally speaking, state lives on the call stack in functional programming languages that have tail call elimination. Modification of the state is done by recursion with a new value for the state. This is more or less equivalent to a "do while" loop in imperative programming.<div>





<br></div><div>myServer :: State -> IO ()</div><div>myServer state = do</div><div>  state' <- updateState state</div><div>  myServer state'<br><div><br></div><div>For the concurrency, Control.Concurrent or Cloud Haskell (for a higher level Erlang-like approach) is probably the way to go here. Parallel and Concurrent Programming in Haskell is a great resource: <a href="http://chimera.labs.oreilly.com/books/1230000000929" target="_blank">http://chimera.labs.oreilly.com/books/1230000000929</a></div>





</div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div>On Fri, Jan 3, 2014 at 8:45 AM, Courtney Robinson <span dir="ltr"><<a href="mailto:courtney@crlog.info" target="_blank">courtney@crlog.info</a>></span> wrote:<br>





</div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div><div dir="ltr">I'm trying to take the training wheels of and moving more of my code base to Haskell from C++ but finding it increasingly tricky.<div>





<br></div><div>I have a subset of a gossip protocol written in C++.</div>

<div>When a server comes online it connects to 1 or more nodes already in the cluster and get data from them about other nodes they know of.</div><div><br></div><div>The new node merges the information and keeps a copy of the merged view. Every so often it contacts the nodes it knows about and refreshes the merged view. It also must have the up to date view ready to be sent in response to a new node joining.</div>







<div><br></div><div>I currently can't wrap my head around how to maintain this state. How would a more experienced Haskeller approach this problem? Code is OK if it demonstrates a particular point but I'm more interested in the line of thought that would go into designing a solution as I suspect that'll be more useful as I get further into the migration.
</div><div><br></div><div>As a gauge to you for my current level in Haskell. I read and understand most Haskell programs fine. I write some but currently heavily rely on hackage/hoogle docs for APIs, even some common ones.</div>







<div><br></div><div>Thanks</div></div>
<br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div></div></div><span><font color="#888888">-- <br>Courtney Robinson<br><a href="mailto:courtney@crlog.info" target="_blank">courtney@crlog.info</a><br>
<div><div><a href="http://crlog.info/" target="_blank">http://crlog.info</a></div>

<div><a href="tel:07535691628" value="+447535691628" target="_blank">07535691628</a> (No private #s)</div></div>
</font></span></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginn</a><br></blockquote></div>
</div></div>