Wednesday, January 9, 2008

More on the AMQP (RabbitMQ) haskell client (and an example)

I am building a AMQP client library to use along with some of the openbotlistprojects. The client library will need to be written in haskell and will be able to communicate with MQ servers like RabbitMQ. That is irrelevant because I am just wrapping my head around haskell networking.

The "Data.Binary" module is used here to have fully control over the size and endianess of all data sent across the network. There are several utilities for writing shorts, byte words, longs, 64 bit longs, etc.

Here is a data structure defined with the Binary.Put monad. Our task is write this data to the socket connection.


data AMQPData = AMQPData {
amqpHeaderA :: [Word8],
amqpHeaderB :: Word32
}

instance Binary AMQPData where
put amq = do
BinaryPut.putByteString (pack (amqpHeaderA amq))
BinaryPut.putWord32be (amqpHeaderB amq)

amqInstance :: IO AMQPData
amqInstance = return (AMQPData { amqpHeaderA = (unpack (C.pack amqpHeadA)),
amqpHeaderB = amqpHeadB
})


amq <- amqInstance
let bs_amq = encode amq
-- Through the use of lazy hPut, write out the data to the socket handle
LazyC.hPut h bs_amq


Download
You can download the haskell source for this entry at the following URL including a simple java server to mimic a AMQP node.

http://haskellnotebook.googlecode.com/svn/trunk/haskell/simplenetwork

Resources

(1) http://www.haskell.org/ghc/docs/6.8.2/html/libraries/network/Network.html
(2) http://hackage.haskell.org/packages/archive/binary/0.4.1/doc/html/Data-Binary.html
(3) http://hackage.haskell.org/packages/archive/bytestring/0.9.0.1/doc/html/Data-ByteString.html

6 comments:

alexis said...

Hi, are you subscribed to the RabbitMQ mailing list?

Berlin Brown said...

I am not yet, I was thinking about getting on.

You think this might be useful? hehe.

alexis said...

Yes, people would definitely be interested in your client, which would be useful for RabbitMQ. You might also get some feedback. Others have been looking at Haskell clients but you may be the first to actually build one ;-)

Berlin Brown said...

It isn't that bad, to be honest. I will definitely consider working on it.

alexis said...

you can join the list here:

http://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss

we welcome people to the discussion :-)

alexis said...

Hi, this may be of interest:

http://haskell.hivefire.com/articles/10830/amqp-client-haskell_proposals/

alexis