Update syncing (WIP)

This commit is contained in:
MrLetsplay 2024-05-21 20:23:34 +02:00
parent 856171acff
commit 1a8a559697
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 21 additions and 9 deletions

View File

@ -26,8 +26,6 @@
</locations>
<environment>
<arch>x86_64</arch>
<os>linux</os>
<ws>gtk</ws>
<nl>en_US</nl>
</environment>
<launcherArgs>

View File

@ -2,6 +2,7 @@ package me.mrletsplay.shareclient;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
@ -227,10 +228,15 @@ public class ShareClient extends AbstractUIPlugin implements MessageListener, Di
Files.createFile(filePath);
}
String content = sync.content().stream()
.map(c -> String.valueOf(c.value()))
.collect(Collectors.joining());
SharedDocument sharedDocument = activeSession.getOrCreateSharedDocument(path, () -> content);
// TODO: update sharedDocuments
if(listener != null) listener.setIgnoreChanges(true);
Files.write(filePath, sync.content());
Files.write(filePath, content.getBytes(StandardCharsets.UTF_8)); // TODO: may cause problems with binary files? but they shouldn't be shared in the first place, so...
sharedProject.getLocal().refreshLocal(IResource.DEPTH_INFINITE, null);
if(listener != null) listener.setIgnoreChanges(false);
} catch (IOException | CoreException e) {
@ -246,7 +252,6 @@ public class ShareClient extends AbstractUIPlugin implements MessageListener, Di
// Sync entire (shared) workspace
for (SharedProject project : activeSession.getSharedProjects()) {
var files = getProjectFiles(project);
System.out.println(files);
if (files == null) return;
paths.putAll(files);
}
@ -275,6 +280,7 @@ public class ShareClient extends AbstractUIPlugin implements MessageListener, Di
}
RemoteConnection connection = activeSession.getConnection();
System.out.println(paths);
for (var en : paths.entrySet()) {
if(!sendFullSyncOrChecksum(connection, req.siteID(), en.getKey(), en.getValue(), false)) return;
}
@ -321,7 +327,15 @@ public class ShareClient extends AbstractUIPlugin implements MessageListener, Di
RemoteConnection connection = activeSession.getConnection();
for(Map.Entry<ProjectRelativePath, Path> en : getProjectFiles(shared).entrySet()) {
// TODO: add new document to sharedDocuments
activeSession.getOrCreateSharedDocument(en.getKey(), () -> {
try {
return Files.readString(en.getValue(), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
});
if(!sendFullSyncOrChecksum(connection, AddressableMessage.BROADCAST_SITE_ID, en.getKey(), en.getValue(), false)) return;
}
}
@ -330,11 +344,11 @@ public class ShareClient extends AbstractUIPlugin implements MessageListener, Di
if (!Files.isRegularFile(filePath)) return true;
try {
byte[] bytes = Files.readAllBytes(filePath);
if(!checksum) {
connection.send(new FullSyncMessage(siteID, relativePath.toString(), bytes));
SharedDocument document = activeSession.getSharedDocument(relativePath);
connection.send(new FullSyncMessage(siteID, relativePath.toString(), document.toList()));
}else {
byte[] bytes = Files.readAllBytes(filePath);
connection.send(new ChecksumMessage(siteID, relativePath.toString(), ChecksumUtil.generateSHA256(bytes)));
}
} catch (IOException | ConnectionException e) {
@ -359,6 +373,7 @@ public class ShareClient extends AbstractUIPlugin implements MessageListener, Di
try {
Path projectLocation = project.getLocal().getLocation().toPath();
return Files.walk(projectLocation)
.filter(Files::isRegularFile)
.collect(Collectors.toMap(
p -> new ProjectRelativePath(project.getRemoteName(), projectLocation.relativize(p).toString()),
Function.identity()));

View File

@ -78,7 +78,6 @@ public class ShareSession {
return sharedDocuments;
}
public SharedDocument getSharedDocument(ProjectRelativePath path) {
return sharedDocuments.get(path);
}