<div><font face="courier new,monospace">Hi all,</font><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">Can anyone help me with some Haskell de/serialisation woes I'm encountering? What I'm trying to do it implement a proprietary network protocol which will enable me to talk to a network Java/Tuple Space.</font></div>
<div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">Specifically, I'm trying to serialise a "TypeStructurePreamble" and send that down the Handle (my network stream), I'm then expecting the Space to send me a TypeStructurePreamble back again, but with different data. So I need to de/serialise this type. I've not got all the code together yet, because I'm trying to figure out the concepts rather than any particular coding problem.</font></div>
<div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">Here's some code I have so far;</font></div><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">import System.IO (hGetLine,hClose,hPutStrLn,hSetBuffering,BufferMode(..),Handle,stdout)<br>
</font></div><div><font face="courier new,monospace">import Data.Binary.Get<br>import qualified Data.ByteString.Lazy        as L<br></font></div><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">type TypeStructurePreamble = (String,Int,[String])</font></div>
<div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">-- This function I know works because it was spoon-fed to me by someone on this list! :-)</font></div><div><font face="courier new,monospace">readStrings :: Int -> Get [String]</font></div>
<div><font face="courier new,monospace">readStrings n = replicateM n $ do</font></div><div><font face="courier new,monospace"> len <- getWord32be<br></font></div><div><font face="courier new,monospace"> name <- getByteString $ fromIntegral len</font></div>
<div><font face="courier new,monospace"> </font><font face="courier new,monospace"> return $ UTF.toString name </font><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace">deserialisePreamble :: Get(String, Int, [String])<br></font></div><div><font face="courier new,monospace">deserialisePreamble = do<br></font></div><div><font face="courier new,monospace"> len <- getWord32be</font><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace"> typename <- getByteString $ fromIntegral len</font></div><div><font face="courier new,monospace"> c <- getWord32be</font></div>
<div><font face="courier new,monospace"> numFields <- getWord32be</font></div><div><font face="courier new,monospace"> fields <- readStrings (fromIntegral numfields)</font></div>
<div><font face="courier new,monospace"> </font><font face="courier new,monospace"> return (typename,c,numFields) </font><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace">-- not sure what this type should be</font><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">serialisePreamble :: Handle -> TypeStructurePreamble -> ??</font></div>
<div><font face="courier new,monospace">serialisePreamble h (n,c,fs) = do</font></div><div><font face="courier new,monospace"> L.hPut h (encode (0xFAB1000A :: Word32))<br></font></div><div>
<font face="courier new,monospace"> L.hPut h (encode (length n))</font><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace"> L.hPut h (encode n)</font><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace"> L.hPut h (encode (c :: Word32))</font><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace"> L.hPut h (encode (length fs))</font><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace"> -- not sure how I would L.hPut out every String in fs</font><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace">It is my (maybe wrong) understanding that these de/serialise functions don't actually do the work, they basically just form a description of what must be done in order to get the work done, so I'm slightly confused on how I should use it.<br>
</font></div><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">For example;<br></font></div><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">main = do<br>
</font></div><div><font face="courier new,monospace"> -- set up Handle called h here, I know how to do this</font><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace"> serialisePreamble h </font><font face="courier new,monospace">("MyType",0,["field1","field2"])</font><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace"> --now I'm expecting to be able to read a modified TypeStructurePreamble from h<br></font></div><div><font face="courier new,monospace"> (n,c,fs) <- deserialisePreamble -- but according to the type of deserialisePreamble, how does the h get into it?<br>
</font></div><div><font face="courier new,monospace"> print "c was "+c -- can I just slot sout style statements into here?<br></font></div><div><font face="courier new,monospace"> evaluate (n,c,fs)</font><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace"> hClose h<br></font></div><div><font face="courier new,monospace"> return (n,c,fs)<br></font></div><div><font face="courier new,monospace"><br></font></div>
<div><font face="courier new,monospace">Does this make sense? Can someone help me fill in the blanks of serialisePreamble and main? I've read the tutorials on the Get monad which google turns up, am I missing some really good ones? Is there some other resource which I'm missing? I'm been thrown a lot of the Hackage documentation on these things but (assuming I'm looking in the right place) they seem to work more like JavaDocs and give the "what it does", rather than the "how it should be used".<br>
</font></div><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">Many thanks,<br></font></div><div><font face="courier new,monospace"><br></font></div><div><font face="courier new,monospace">Tom<br>
</font></div>