He studied in detail the work of sockets, up to the practical implementation of various ideas in several languages. Now there was a task to write a cloud text editor. (Joint text editing) Sel, wrote a server on which the txt file is located. When sending a GET request to the server, the server returns the contents of the document. For any other - the server saves the received data in the document.

Began to write an editor. Made the simplest logic: every second a file is synchronized with the server. But, there was a problem when the file on computer A changes, the server accepts it, updates the document on computer B and at the same time erases a part of the text that user B. wrote. Either way.

Developing an application in Xcode in c ++, I can redo it under swift, but this is not so important. So nevertheless how to implement logic of cloud editing?

    1 answer 1

    Apparently, you need to enter lsn - linear serial number, well, stupid counter. This concept is very common.
    What is the idea?
    1) Client A, when starting the editor, requests lsn from the server. Suppose lsn == 1 at this moment. Next, client A edits the document and sends a pair to the server: (new text, lsn).

    2) The server checks its lsn with the one sent by the client. If they match, the server increments lsn and saves the new text. Otherwise, no changes are accepted - the server sends a response to client A with an error and the lsn value that is currently installed in the server.
    Important note: the server must update lsn atomically.

    3) What does A do? If the server has sent "success", then everything is OK - you can edit further. If the server has sent an "error" and a new lsn - this means that the text on the server has been changed. It is necessary to download the new text from the server, merge this new text with the one that already exists in client A and try again to perform step 1.