views:

119

answers:

3

I'm making a small peer-to-peer app that holds a common collection of objects. This isnt a question about the socket comms to transfer the objects as I have that sorted.

To start with I dont have to worry about conflicts as the clients can only add to the object collection. But I'm struggling to work out in my head how the clents negotiate which objects they need to transfer to each other.

I would guess this has been done many times before, and there must be some sort of sync algorithm out there somewhere...

Any ideas?

UPDATE:

I guess I'm asking if there is a way to sync without having to cycle through all the objects on each peer and check that they exist at the other end

A: 

assign a guid to each object and when you merge them you don't have problems distinguishing them apart. or am i misunderstanding your question?

Mladen Prajdic
The main issue is that when I connect to another peer, will I have to cycle through every single object to check that they exist on both?
Tim
yes, you'll have to cycle through the smaller collection.if you can put collection you're searching into a dictionary the check can go from O(n) to O(1).
Mladen Prajdic
+1  A: 

If you use an ObservableCollection of serializable objects implementing INotifyPropertyChanged, you can queue the objects for transmission as they are altered. If you keep the objects that need synchronization in a HashSet, you can avoid double-entries in the queue at the expense of linear ordering. If you carefully override GetHashCode(), or even better create a method that returns a stronger hash, you can filter out items that changed equally on both ends.

Edit: For the initial sync, build a packet that lists the generated hashes of all the current objects as a binary block. That way the exchange is quick.

280Z28
What would be the difference between the initial sync and later syncs?
Tim
It depends on how you look at it. Without any special work, it's the same, but the set of generated hashes should have many more matches. :)
280Z28
A: 

How about the "Microsoft Sync Framework"

Looks like it can sync files, feeds and data stores by using custom providers.

By using Sync Framework, developers can build synchronization ecosystems that integrate any application with data from any store, by using any protocol over any network.

http://msdn.microsoft.com/en-us/library/bb902854(SQL.105).aspx

Tim