These are chat archives for vu3rdd/functorrent

27th
Jul 2015
Jaseem Abid
@jaseemabid
Jul 27 2015 05:23
A state machine being a very common thing, there might be good implementations of it already.
there might be libraries, or good ways to do it ourself
Jaseem Abid
@jaseemabid
Jul 27 2015 05:31
These days I'm mostly programming in Haskell, having spent the past 5 years going through that painful but eye-opening learning curve. Most of the code I maintain is Perl, C, and shell script, but I'd just as soon use Haskell for anything new. I've gradually became a big fan of Haskell; I feel it makes me write much more solid code that is much easier to maintain and refactor. But the best part is there are seemingly no end of things to learn in the Haskell space.
Ramakrishnan Muthukrishnan
@vu3rdd
Jul 27 2015 05:50
yup. Seen that. Joey was till recently a Debian developer as well.
Jaseem Abid
@jaseemabid
Jul 27 2015 05:54
That's how I know of him. And git-annex
I went though quite a bit of concurrency patters. This is really interesting stuff.
As far as I know now, the good way to do functorrent is to move off tasks to threads, and protect critical regions with the bracket function. Don't poll from the thread, but throw asynchronous exceptions to it from the parent.
For an example, see the definition of timeout. Its exceptionally well done and simple
Ramakrishnan Muthukrishnan
@vu3rdd
Jul 27 2015 05:58
Yes. Start hacking away and play with the code. One reason why I kept all the peer state in one record is that it makes life easy when we talk to peers concurrently. No global state to deal with.
Now, whatever worked for one peer should just work for multiple peers as well. That said, a few things in the current code assumes one peer, so that needs to be dealt with.
Jaseem Abid
@jaseemabid
Jul 27 2015 05:59
You do the computation in the main thread, but spin off a timer thread with a sleep. You kill the parent thread after the sleep, which kills the specific activity. In case the activity completes first, you kill off the timer thread. Specific race conditions that can occur handle well with the bracket.
Ramakrishnan Muthukrishnan
@vu3rdd
Jul 27 2015 05:59
bit torrent protocol is amazing.
Bram Cohen is a true genious.
genius*
Jaseem Abid
@jaseemabid
Jul 27 2015 06:00
This is a really really profound idea. You don't have to build complicated timeout machinery into your network or disk IO code at all. I was quite mindblown
I was having a feeling that Simon Marlow is one hell of a genius.
Ramakrishnan Muthukrishnan
@vu3rdd
Jul 27 2015 06:01
anyone hacking compilers are, I think. Not just Haskell ones.
Jaseem Abid
@jaseemabid
Jul 27 2015 06:01
Not for his compiler skills, his knowledge about concurrency and parallelism
And admittedly as a book author
Ramakrishnan Muthukrishnan
@vu3rdd
Jul 27 2015 06:03
Frankly I didn't like the book. It is more of a report than a book.
It apparently was a report, spruced up into a book.
and that shows.
Unfortunately, ther e are no good books in Haskell. I don't like any of the books I have read so far. RWH is so disorganized. So much of information squeezed in and jumping from one topic to another in no particular sequence.
Marlow's one is probably one of the best. The rest are too academic, not leaving the pure functional world. Writing sudoku solvers and so on.
Jaseem Abid
@jaseemabid
Jul 27 2015 09:28
Have you seen this letter to UT Austin by Edsger W.Dijkstra ?
http://www.cs.utexas.edu/users/EWD/transcriptions/OtherDocs/Haskell.html
Ramakrishnan Muthukrishnan
@vu3rdd
Jul 27 2015 11:09
Yes
Ramakrishnan Muthukrishnan
@vu3rdd
Jul 27 2015 13:08
See this:
handShake :: Peer -> ByteString -> String -> IO Handle
handShake peer@(Peer _ ip port) infoHash peerid = do
let hs = genHandShakeMsg infoHash peerid
h <- connectTo ip (PortNumber (fromIntegral port))
hSetBuffering h LineBuffering
hPut h hs
putStrLn $ "--> handhake to peer: " ++ show peer
_ <- hGet h (length (unpack hs))
putStrLn $ "<-- handshake from peer: " ++ show peer
return h
this function lumps together sending a handshake and opening the connection.
this should be split up.
connection handle should be given to the handShake function instead.
Jaseem Abid
@jaseemabid
Jul 27 2015 13:30
You can indent the code with 4 spaces here to get code highlight
Ramakrishnan Muthukrishnan
@vu3rdd
Jul 27 2015 15:05
msgLoop refactored a bit. Still not satisfied.