1

I found a nice answer of S.Lott about what I've been searching for:

Client-server synchronization pattern / algorithm?

But my question is now, what if the client has a wrong time?

Here's my problem:

Let's say the time of the client is 1 hour behind the servers, then the client changes a file, so the last write time is now 1 hour behind the servers. When the user starts his program which synchronizes the file, the server says to the changed file: "Oh, that file you have there is 1 hour older than mine, so let's replace it", but that's wrong, because the users file is actually newer, so it should be uploaded to the server.

I need a system that checks if the file is newer on server or on the client, and that doesn't work if the time is wrong or different.

Any ideas?

By the way, I am trying to write a cloud program.

Community
  • 1
  • 1
schacker22
  • 439
  • 2
  • 5
  • 12

1 Answers1

0

If you're resolving conflicts manually (which would make sense for most applications), this can probably be done better with versioning rather than timestamps. When a client modifies a file, set a flag. When synchronizing, check the flag and versions.

  • If the client flag is set and the client and server versions are the same, send the client file to the server.

  • If the client flag is not set and the server version is newer, send the server file to the client.

  • If the client flag is set and the server version is newer, a conflict occurred and should be resolved.

The versions are per-file and should be sent along with the files.

Reset all client flags after synchronization.


This 'flag' can just be a check whether the last modified time on the file is different from the time that file was received from the server (we can store this time separately right after getting the file from the server).


Alternatively, you could sync the time.

Here's one possible solution:

When receiving files from the server, first get the current time from the server, then offset the timestamp of each file received on the client side by the difference between the server and client time. When sending files to the server, you can do something similar by offsetting by the client time.

But this seems more complex than necessary.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • Thanks for your answer.But what if the Program doesn't run at time the user changes something, no flag can be set – schacker22 Apr 11 '14 at 17:52
  • The 'flag' can just be a check whether the last modified time on the file is different from the time that file was received from the server - this then won't require that the program is constantly running. – Bernhard Barker Apr 11 '14 at 18:50