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

No comments: