I have a small application that I’ve written and it talks to another application through a python server that can send json over a network.
What I would really like to do is to be able to have to objects “synchronize” with each other through json. Now I’ve already looked into the gson library, and it seems to be great for serializing an object into json. However, it does not seems (or maybe I just can’t figure it out), to be very helpful if I just want two objects to update with each other.
For example:
Gson is great at
Gson g = new Gson();
Object o = new Object();
String serialization = g.toJson(o, o.class);
However, let’s say that I have object o on two separate clients. If at client 1, they call
o.doSomeMethod(value);
I would like the resulting change to be update to object o on client 2’s computer. Right now I can do a little bit of fanciness and just replace the object with the new serialized version, but that does not seem to be a very efficient method of updating the two objects.
Thanks for any help in advance!
If I were you, what I’d do in your o.doSomeMethod is include a call out to the network that informs client2 about the change. On client2’s side of things, have a method listening for potential updates. When an update comes in, it makes the change to the object on client 2. What you’ll have to do is implement some sort of protocol for sending this sync updates. For instance, let’s say you have an attribute called att1 and client1 makes a change to that attribute on object o. Let’s also say object o has some sort of unique identifier (id). When you call the doSomeMethod, it sends out a message on the netwoks saying “This object has changed the the value for att1, here’s the new value.”. Does that make sense?