Archive for October 2009

Playing with sockets in Haskell

This is another one of those posts that I make mostly for myself, you know for organising and help my memory :-)

There are as far as I can see three ways to deal with sockets in Haskell. There’s the type Socket which is used throughout Network.Socket. From that it’s possible to get to the underlying filedescriptor, and it in turn can be converted to a Handle.

When coupled with fork+exec it’s crucial to make sure the child process can find the socket Leaving it in a predictable place seems to be the easiest way to do that, and as far as I can see that requires using dupTo from System.Posix.IO. So, on the child-side it’s necessary to find a way to turn an integer (CInt) into something that can be treated as a socket (i.e. a Socket, a Handle, or a filedescriptor).

A basic parent-child which obviously won’t work since the child’s socket is represented as a Socket:

import Control.Concurrent
import System.Posix.Process
import Network.Socket
 
childFunc s = send s "Ping from child" >> return ()
 
main = do
    (childSock, parentSock) <- socketPair AF_UNIX Stream defaultProtocol
    print (childSock, parentSock)
    child <- forkProcess $ childFunc childSock
    recv parentSock 10 >>= print

Let the child take a CInt and turn it into a filedescriptor:

import Control.Concurrent
import Control.Concurrent.MVar
import System.Posix.Process
import System.Posix.IO
import System.Posix.Types
import Network.Socket
 
childFunc sInt = do
    let fd = Fd sInt
    fdWrite fd "Ping from child" >> return ()
 
main = do
    (childSock, parentSock) <- socketPair AF_UNIX Stream defaultProtocol
    let childInt = fdSocket childSock
    print (childInt, parentSock)
    child <- forkProcess $ childFunc childInt
    recv parentSock 10 >>= print

Let the child take a CInt and turn it into a Handle:

import Control.Concurrent
import System.Posix.Process
import System.Posix.IO
import System.Posix.Types
import Network.Socket
import System.IO
 
childFunc sInt = do
    h <- fdToHandle $ Fd sInt
    hPutStr h "Ping from child"
    hFlush h
 
main = do
    (childSock, parentSock) <- socketPair AF_UNIX Stream defaultProtocol
    let childInt = fdSocket childSock
    print (childSock, parentSock)
    child <- forkProcess $ childFunc childInt
    recv parentSock 10 >>= print

Let the child take a CInt and turn it into a Socketi:

import Control.Concurrent
import Control.Concurrent.MVar
import System.Posix.Process
import System.Posix.IO
import System.Posix.Types
import Network.Socket
 
childFunc sInt = do
    s <- mkSocket sInt AF_UNIX Stream defaultProtocol Connected
    send s "Ping from child" >> return ()
 
main = do
    (childSock, parentSock) <- socketPair AF_UNIX Stream defaultProtocol
    let childInt = fdSocket childSock
    print (childInt, parentSock)
    child <- forkProcess $ childFunc childInt
    recv parentSock 10 >>= print
  1. It seems the socket is in the Connected state after socketPair succeeds.[back]

Dents for 2009-10-23

Epilicious is dead, say hello to BMS

With Python being dropped as a language for extensions in epiphany 2.28 I needed to replace epilicious. I tried writing it in JavaScript (seed was integrated in 2.28), but I gave up due to hitting too many hurdles on the way. Instead I decided to rewrite epilicious using Vala and a minimal layer of C.

It turned out to be very doable, despite epiphany’s extension API lacking Vala bindings (Cosimo Cecchi, I’m looking at you :-) ). Basically I did the following setup:

  1. Add the extension following the instructions in Writing Epiphany Extensions by Adam Hooper.
  2. Add a check for valac in configure.ac.
  3. Add a rule in the extension’s Makefile.am to generate a C header file for all the Vala code, for use from C.

Then I started writing the actual extension. I did the minimal amount of work in C, trying to escape as soon as possible into Vala. In the few places I needed to call from Vala into C I would declare a delegate in Vala, and pass a function from Ci.

I call this new version BMS, for bookmark synchronisation. I have a patch for BMS that applies to epiphany 2.28.1. (The file also contain a PKGBUILD in order to delight Arch users :-) )

I should probably point out that while epilicious never could be called polished, BMS is even less so. I might find the time to make it multi-threaded, so that it’s possible to do some sort of progress dialogue, but don’t hold your breath. In the back of my mind is also the thought of rewriting it yet again, in JavaScript/seed.

  1. The format of .vapi files are unknown to me, and I haven’t managed to find much documentation. Using function pointers seemed like an easier way, especially given that I needed this in exactly 3 places.[back]

Dents for 2009-10-22

  • #identi.ca I’m confused, does Twitter config option “Import my Friends Timeline.” not mean what I think it does? AFAICT nothing’s imported. #
  • Dear CNNMoney.com, you need a meta tag for Content-Type on your printer-friendly pages to make sure the character set is set correctly. #
  • I hate shell scripting! #
  • @drewfitzsimmons I too want an N900, still slightly confused as to what sort of suitable PAYG/subscription different operators offer though. #

Powered by modified Twitter Tools.

Twitter Weekly Updates for 2009-10-18

  • Viv seemed to enjoy her first night in her new, non-cot, bed. #
  • Almost at a fully working vimoutliner now. #
  • Hmm, was that a "Programming Perl" on Chuck's bedside table? #
  • Ah! Gnome 2.28 has hit Arch. Joy! #
  • How to change peoples' behaviour: http://tinyurl.com/y8aa9us #
  • Some ups and downs with new Gnome 2.28, mostly ups, as expected. #
  • Back on xmonad at home again. What a relief! #
  • Epiphany bookmark sync slowly getting getting there, all that's left is dealing with removed bookmarks. #
  • Impressed with Thunderbird 3 so far, the real test will be tomorrow when I try it out at work. #
  • Implementation of algorithm for bookmark sync is done, now it's only the boring usability stuff left. #
  • There are some fields where ALL bugs should be fixed, and bugfixes issued and applied ASAP: http://tinyurl.com/yfhu3×6 #
  • Times Online now requires logging in to comment. No more commenting from me until they start using #OpenID. #
  • FLOSS weekly on Boycott Novell: the first episode I couldn't stand listening to in its entirety. #
  • Catching up on subscriptions, it seems there are quite a few CLUGgers on identi.ca :-) #
  • Ah, damn it! Stuck on a bug in a vapi file, and that format is almost completely undocumented. Well, off to relax with some telly instead. #
  • Playing with Gnome Keyring, in C, since the Vala binding has some bugs. #
  • How irritating. Can't find the drill. Has someone borrowed it? #
  • Drill reserved at Argos for tomorrow. Picking up at lunch unless the old one surfaces. #

Powered by Twitter Tools

Twitter Weekly Updates for 2009-10-11

  • Ha! Twitter Tools did put together a weekly "digest". Great. #
  • Aargh! I suspect haskell has damaged me permanently. #
  • Writing Epiphany extensions in Vala is all about jumping through hoops to avoid C. #
  • Nice to see that dataenc is getting used by hi-profile Haskell projects… just received Q from darcs devs re requirements. #
  • Guide to Viv's language: yanna = lion, moomi = hippo :-) #
  • Ah, my Google Wave invite just arrived. That'll make for some fun today. #
  • Where else but in Britain? http://tinyurl.com/y9teum7 #
  • Vim could really do with an outliner that is powerful AND easy to install. #
  • Thanks El Siglirismo: http://tinyurl.com/yll25m8 #
  • Enjoying Micro Men, thanks @jsk #
  • Enjoying Swedish Fish and assembling IEKA furniture. Feeling VERY Swedish today. #
  • Electrical tools greatly improves the IKEA post-shopping experience. #

Powered by Twitter Tools

Twitter Weekly Updates for 2009-10-04

  • Now I'm jelous, Dita has been invited to a genuine English hen night. #
  • On my Windows machine at work: Windows Update tells me I need MS Office SP2, but I don't have MS Office installed. Confused! #
  • Beginning the day with headstand turned out to be a good. #yoga #
  • Myke Bartlett is at it again, more Salmon and Dusk. Brilliant! #
  • I just composed the third letter for Ryanair, I still have hope they'll refund the full £20 they owe us for check-in fees. #
  • How strange, no åäö for GP's RSS feeds in Google Reader. #
  • Identica Tools would be more useful if they could co-exist with Twitter Tools. http://tinyurl.com/5edgxn #
  • I actually agree with this, having Blair as an EU president would likely be catastrophic: http://tinyurl.com/yebe5h2 #
  • To bad I've never worked in a place that appreciates the employees like this: http://tinyurl.com/yd7llpo #
  • We had a wonderful kanelbullens dag today. I hope you did too! #

Powered by Twitter Tools