Sync Algorithm

syncAlgorithm() {

    if ( C == L ) { // No local changes

        if ( S == L ) { // No remote changes
            doNothing() 
        }
        else {
                if ( S = Null ) { // Object doesnt exist
                    deleteObjectFromLocal()
                    updateRecord(Remove C, L)
                }
                else {
                downloadServerObject() // Result: L = S
                updateRecord( L = S )
            }
        }

    else { //Local changes found
        if ( S == L ) {
            if ( C = Null ) {
                deleteObjectFromServer()
                updateRecord(Remove L, S)
            }
            else{
                     uploadLocalObject() // Result: S = C, L = S
                    updateRecord( S = C )
                        }
        }
        else {
            if ( C == S ) { // (Identical Changes) Result: L = S
                doNothing()
            }
            else {
            if ( C = Null ) {
                deleteObjectFromServer()
                updateRecord(Remove L, S)
            }
            else{
                identifyAsConflict() // Manual action required
            }
        }
    }
    }

}

L: local state (stored state from last sync with the server)
C: current state (state computed right before sync)
S: server state