2 Aug 2006 18:00
Re: BytePickle examples?
Andrew Lentvorski <bsder <at> allcaps.org>
2006-08-02 16:00:16 GMT
2006-08-02 16:00:16 GMT
Philipp Haller wrote: >> The question I have is: How do I get an integer to always pickle as a >> 4 byte, network order thing? > > I attached some code with a pickler that always pickles integers as 4 > bytes. I don't know which order you need, though. If you think a pickler > for integers as 4 bytes in "network order" is generally useful, we might > add this to our pickler library. Well, at the very least, the example of how to do this needs to be somewhere. The library is probably as good a place as any. Having a way to serialize an int into 4 byte network order is probably a useful construct. I'm not sure how much further I would go, though. Probably I'd add 2 byte network order as well and stop there. After 4-byte and 2-byte (nat seems to work for 1 byte) ints the usefulness drops off really quickly. Two things still aren't very clear to me yet, though. First, I see some "polymorphic" behavior in the Actor library in TcpSerializerComb.scala, but I don't quite grok it. What I am trying to do is read a network packet and unpickle. The first four bytes are an integer, and are the packet type. I would like to read the first 4 bytes, figure out what the packet is, and then unpickle the rest of the packet to a case class which is the actual packet. Part of the complication is my weak understanding of pickling and part of the complication is my weak understanding of matching in Scala.(Continue reading)
> First, I see some "polymorphic" behavior in the Actor library in
> TcpSerializerComb.scala, but I don't quite grok it. What I am trying to
> do is read a network packet and unpickle. The first four bytes are an
> integer, and are the packet type. I would like to read the first 4
> bytes, figure out what the packet is, and then unpickle the rest of the
> packet to a case class which is the actual packet. Part of the
> complication is my weak understanding of pickling and part of the
> complication is my weak understanding of matching in Scala.
OK, so what you want is a datatype pickler. In the library there is a `data'
pickler which uses integers to tag the different alternatives. For your pickler,
you could define a pickler similar to `data' which uses the `int4' pickler, like
this:
def myData[a](tag: a => int, ps: List[()=>SPU[a]]): SPU[a] =
sequ(tag, int4, (x: int) => ps.apply(x)());
Usage: (example from my thesis <at>
RSS Feed