Example #1
0
 def test_file_modified_and_moved_on_both_devices(self):
     ownChanges = {
         "dirs_created": [],
         "dirs_deleted": [],
         "dirs_modified": [],
         "dirs_moved": [],
         "files_created": [],
         "files_deleted": [],
         "files_modified": ["moved_file_src"],
         "files_moved": [("moved_file_src", "moved_file_dest_own")],
     }
     theirChanges = {
         "dirs_created": [],
         "dirs_deleted": [],
         "dirs_modified": [],
         "dirs_moved": [],
         "files_created": [],
         "files_deleted": [],
         "files_modified": ["moved_file_src"],
         "files_moved": [("moved_file_src", "moved_file_dest_their")],
     }
     synchroniser = Synchroniser(ownChanges, theirChanges)
     ownActions, theirActions = synchroniser.getActions()
     expectedOwnActions = [("create", "moved_file_dest_their")]
     expectedTheirActions = [("create", "moved_file_dest_own")]
     self.assertCountEqual(ownActions, expectedOwnActions)
     self.assertCountEqual(theirActions, expectedTheirActions)
Example #2
0
 def test_file_created_on_own_device(self):
     ownChanges = {
         "dirs_created": [],
         "dirs_deleted": [],
         "dirs_modified": [],
         "dirs_moved": [],
         "files_created": ["created_file"],
         "files_deleted": [],
         "files_modified": [],
         "files_moved": [],
     }
     theirChanges = {
         "dirs_created": [],
         "dirs_deleted": [],
         "dirs_modified": [],
         "dirs_moved": [],
         "files_created": [],
         "files_deleted": [],
         "files_modified": [],
         "files_moved": [],
     }
     synchroniser = Synchroniser(ownChanges, theirChanges)
     ownActions, theirActions = synchroniser.getActions()
     expectedOwnActions = []
     expectedTheirActions = [("create", "created_file")]
     self.assertCountEqual(ownActions, expectedOwnActions)
     self.assertCountEqual(theirActions, expectedTheirActions)
Example #3
0
class SyncProtocol(Protocol):
    def __init__(self, factory):
        self.factory = factory
        self.actions = []
        self.data = {}

    def connectionLost(self, reason):
        if reason.check(ConnectionDone):
            Synchroniser.synchronise(self.actions, self.data,
                                     self.factory.fs_manager)
            self.factory.fs_manager.updateLastSyncState()

    def dataReceived(self, data):
        response = {"type": "error", "message": "Unknown error"}
        try:
            message = json.loads(data.decode())
            if message["type"] == "changes":
                response = self.handleChanges(message["changes"])
            elif message["type"] == "data":
                response = self.handleData(message["data"])
            elif message["type"] == "end":
                self.transport.loseConnection()
            else:
                response = {"type": "error", "message": "Invalid message type"}
        except json.JSONDecodeError:
            response = {"type": "error", "message": "Invalid message format"}
        finally:
            self.transport.write(json.dumps(response).encode())

    def handleChanges(self, theirChanges):
        ownChanges = self.factory.fs_manager.getChangesSinceLastSync()
        self.synchroniser = Synchroniser(ownChanges, theirChanges)
        ownActions, theirActions = self.synchroniser.getActions()
        self.ownRequiredFiles = self.synchroniser.getRequiredFiles(ownActions)
        self.theirRequiredFiles = self.synchroniser.getRequiredFiles(
            theirActions)
        self.actions = ownActions
        response = {
            "type": "actions",
            "actions": theirActions,
            "files": self.ownRequiredFiles,
        }
        return response

    def handleData(self, data):
        ownData, theirData = self.synchroniser.getData(data,
                                                       self.ownRequiredFiles,
                                                       self.theirRequiredFiles,
                                                       self.factory.fs_manager)
        self.data = ownData
        response = {
            "type": "data",
            "data": theirData,
        }
        return response