3

I have uploaded a Java Game Server to github. I would like to provide the following functionality to users. When the game state changes, only transmit the delta to the connected game clients, thereby reducing network load.

I have the below idea to do it.... which is pretty dump as far as I can see.

1) Serialize object before modification
2) Serialize object after modification
3) Convert both to String and find diff (not sure how, but sure some libraries will be there to do that)
4) Transmit diff to interested clients.

How are these kind of requirements normally handled in enterprise?

John McDonald
  • 1,790
  • 13
  • 20
Abe
  • 8,623
  • 10
  • 50
  • 74
  • It sounds like you're planning to work with only portions of a serialized object. I can't really imagine how that would work. – Dave Mar 15 '12 at 17:20
  • @Dave at the client side I would serialize existing object, apply the delta(God knows how!) and convert it back... b.t.w do you know whether enterprise servers do this sort of thing? If not I wouldnt bother trying at all. – Abe Mar 15 '12 at 17:28
  • 1
    I've not heard of doing it that way, but that means nothing. Unless the objects are really big, why not just send the whole thing? Much easier and far less error prone. – Dave Mar 15 '12 at 17:31
  • Already doing it that way...:) – Abe Mar 16 '12 at 02:18

4 Answers4

2

It would be simpler to produce the delta first and serialize that. You don't need serialization at all to produce the delta. You could get a long way with it just using the Bean Introspector on object properties, if your objects are bean-ish enough.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

I would use a library like kryo (https://code.google.com/p/kryo/wiki/V1Documentation) or Sqisher java-object-diff from Daniel Bechler. The latter is suitable for Beans, that is, you require get and set methods for each variable. kryo is more flexible and very fast (https://github.com/eishay/jvm-serializers/wiki). Search for kryo and Delta to find more information. To make use of the delta functionality you have to use kryo version 1 and kryonet.

Harun
  • 24
  • 1
  • That kryo documentation is before they moved over to git. The latest documentation for 2.22+ should be here: https://github.com/EsotericSoftware/kryo – Marcus Dec 05 '13 at 22:51
1

You might want to investigate badiff for this. badiff is a pure-java binary differ with an emphasis on small diffs and parallelization. See the website at http://badiff.org/ .

Since the serialized form of an object is deterministic, your approach should work. Just serialize the object to a ByteArrayInputStream, do your modifications, serialize it to another ByteArrayInputStream, and use badiff to compute the diff.

This is not an appropriate solution for gaming applications where diffs might arrive out of order. In those cases, if you still want to send diffs, consider sending "key" serialized objects every now and then, which you can compute diffs from, such that keys will always be in order.

esialb
  • 51
  • 1
1

Well as for diff a couple options exists; a few are pointed out here: How to perform string Diffs in Java?

Another way might be to serialize the object to XML and use a XML diff tool to produce the delta. XML as the advantage of offering a structure where your binary serialized instances won't. However you should make sure to compress your messages to minizime traffic if you use this strategy.

Community
  • 1
  • 1
Etienne
  • 901
  • 6
  • 15
  • Sounds good, but XML would in all probability be heavy on the bandwidth. – Abe Mar 15 '12 at 17:31
  • Serializing to XML (even compressed) is going to completely invalidate the reasons behind only sending updates that I don't even.... dailywtf.com – thedaian Mar 15 '12 at 18:00
  • 1
    Yeah well XML is indeed not a good solution in terms of bandwidth; my point is that I think you need some "structured" representation of your data to build a meaningful diff. Trying to diff binary outputs from object serialization will probably give you more than you would think... a small change in the initial object could mean a big difference in the serialized output. But that could also depend on what mechanism you use for serialization. – Etienne Mar 15 '12 at 18:33