diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/MyTeamSpeakLogin.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/MyTeamSpeakLogin.java index 758ce9c..43d17dd 100644 --- a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/MyTeamSpeakLogin.java +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/MyTeamSpeakLogin.java @@ -29,6 +29,7 @@ public final class MyTeamSpeakLogin { private MyTeamSpeak.Session session; private MyTeamSpeak.Credentials sessionCredentials; private final Object identityLoadLock = new Object(); + private final Object sessionLock = new Object(); private long revision; /** Changes whenever the signed-in account changes, including sign-out. */ @@ -147,12 +148,31 @@ public final class MyTeamSpeakLogin { } } - private synchronized MyTeamSpeak.Session session(MyTeamSpeak.Credentials current) throws IOException, MyTsException { - if (session != null && sessionCredentials == current) return session; - endSession(); - session = service.signIn(current); - sessionCredentials = current; - return session; + /** + * The kept session, signing in when there is none. One sign-in at a time, under its own lock: + * the requests behind it wait, but whoever only asks whether we are signed in does not. + */ + private MyTeamSpeak.Session session(MyTeamSpeak.Credentials current) throws IOException, MyTsException { + synchronized (sessionLock) { + MyTeamSpeak.Session stale; + synchronized (this) { + if (session != null && sessionCredentials == current) return session; + stale = session; + session = null; + sessionCredentials = null; + } + if (stale != null) service.signOut(stale.id()); + MyTeamSpeak.Session started = service.signIn(current); + synchronized (this) { + if (credentials == current) { + session = started; + sessionCredentials = current; + return started; + } + } + service.signOut(started.id()); + throw new MyTsException("Signed out of myTeamSpeak meanwhile", 0); + } } /** Ends the kept session, as on exit; the next request signs in again. Blocks for a request. */ diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/MyTeamSpeakSync.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/MyTeamSpeakSync.java index 31008b7..53de74d 100644 --- a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/MyTeamSpeakSync.java +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/MyTeamSpeakSync.java @@ -214,6 +214,8 @@ public final class MyTeamSpeakSync { Map cloudByUuid = new LinkedHashMap<>(); for (MyTeamSpeak.CloudItem item : cloud) cloudByUuid.put(item.uuid(), item); Set claimed = new HashSet<>(); + // What local items the account's items stand for, by match key. + Set linked = new HashSet<>(); for (SyncState.Entry entry : base) { MyTeamSpeak.CloudItem now = cloudByUuid.remove(entry.uuid()); @@ -223,8 +225,16 @@ public final class MyTeamSpeakSync { if (now != null) plan.add(Step.keep(kind, now, "")); continue; } + if (claimed.contains(entry.localId())) { + // Two account items on one local item: only the first is synchronised. + if (now != null) plan.add(Step.keep(kind, now, "")); + continue; + } SyncFields local = kind.localFields(entry.localId()); - if (local != null) claimed.add(entry.localId()); + if (local != null) { + claimed.add(entry.localId()); + linked.add(kind.matchKey(local)); + } SyncFields remote = now == null ? null : fieldsOf(kind, now.data()); if (now != null && remote == null) { plan.add(Step.keep(kind, now, "")); @@ -232,6 +242,8 @@ public final class MyTeamSpeakSync { } boolean localChanged = local != null && !local.sameAs(baseFields); boolean cloudChanged = now != null && !now.version().equals(entry.version()); + // Lost only its identity, deleted here: whether that stands is the identity's conflict. + Conflict restoring = now == null ? null : restoredWith(plan, kind, entry, local, baseFields); if (now == null && local == null) { plan.add(new Step(kind, Action.FORGET, entry.uuid(), entry.localId(), null, null)); @@ -248,7 +260,12 @@ public final class MyTeamSpeakSync { Step delete = new Step(kind, Action.PUSH_DELETE, entry.uuid(), entry.localId(), null, new MyTeamSpeak.CloudItem(kind.itemClass(), entry.uuid(), UUID.randomUUID().toString(), null)); if (!cloudChanged) plan.add(delete); - else plan.conflict(kind, null, remote, delete, Step.pull(kind, now, null)); + else plan.deletedHere.put(entry.uuid(), + plan.conflict(kind, null, remote, delete, Step.pull(kind, now, null))); + } else if (restoring != null) { + Step pull = Step.pull(kind, now, entry.localId()); + plan.contingent(restoring, cloudChanged ? pull : push(kind, entry.uuid(), entry.localId(), now, false), + pull); } else if (!localChanged && !cloudChanged) { plan.add(Step.keep(kind, now, entry.localId())); } else if (!cloudChanged) { @@ -276,12 +293,17 @@ public final class MyTeamSpeakSync { plan.add(Step.keep(kind, now, "")); continue; } - String localId = unpaired.remove(kind.matchKey(remote)); + String key = kind.matchKey(remote); + String localId = unpaired.remove(key); if (localId == null) { - plan.add(Step.pull(kind, now, null)); + // One local item per key: another account item already stands for it. + if (kind.onePerKey() && linked.contains(key)) plan.add(Step.keep(kind, now, "")); + else plan.add(Step.pull(kind, now, null)); + linked.add(key); continue; } paired.add(localId); + linked.add(key); SyncFields local = kind.localFields(localId); if (local.sameAs(remote)) { plan.add(Step.keep(kind, now, localId)); @@ -332,6 +354,18 @@ public final class MyTeamSpeakSync { new MyTeamSpeak.CloudItem(kind.itemClass(), uuid, version, item.toByteArray())); } + /** + * The conflict of the identity deleted here that the item was using, when losing it is all + * that changed about the item here. + */ + private static Conflict restoredWith(Plan plan, SyncKind kind, SyncState.Entry entry, SyncFields local, + SyncFields base) { + if (local == null) return null; + String identity = kind.identityUsed(entry.data()); + Conflict conflict = identity == null ? null : plan.deletedHere.get(identity); + return conflict != null && kind.lostIdentityOnly(local, base) ? conflict : null; + } + private static SyncFields fieldsOf(SyncKind kind, byte[] data) { SyncItem item = decode(data); return item == null ? null : kind.cloudFields(item); @@ -371,12 +405,18 @@ public final class MyTeamSpeakSync { } } + private record Contingent(Conflict on, Step ifHere, Step ifCloud) { + } + /** What a run will do, once its conflicts are settled. */ public static final class Plan { final Remote remote; final SyncKind.Links links; private final List steps = new ArrayList<>(); private final List conflicts = new ArrayList<>(); + private final List contingents = new ArrayList<>(); + /** Conflicts of identities deleted here but changed in the account, by item UUID. */ + final Map deletedHere = new HashMap<>(); /** The class revisions sent; set once {@link #upload} succeeded. */ volatile Map uploadedVersions; /** The account-wide cursor the upload left, {@code null} when nothing was sent. */ @@ -391,9 +431,16 @@ public final class MyTeamSpeakSync { steps.add(step); } - void conflict(SyncKind kind, SyncFields here, SyncFields cloud, Step keepHere, Step keepCloud) { - conflicts.add(new Conflict(kind.title(here != null ? here : cloud), - kind instanceof SyncKind.IdentityKind, here, cloud, keepHere, keepCloud)); + Conflict conflict(SyncKind kind, SyncFields here, SyncFields cloud, Step keepHere, Step keepCloud) { + Conflict c = new Conflict(kind.title(here != null ? here : cloud), + kind instanceof SyncKind.IdentityKind, here, cloud, keepHere, keepCloud); + conflicts.add(c); + return c; + } + + /** A step that follows how another item's conflict is settled. */ + void contingent(Conflict on, Step ifHere, Step ifCloud) { + contingents.add(new Contingent(on, ifHere, ifCloud)); } /** Every step either choice of a conflict could take, for the identity links. */ @@ -403,6 +450,10 @@ public final class MyTeamSpeakSync { out.add(c.keepHere); out.add(c.keepCloud); } + for (Contingent c : contingents) { + out.add(c.ifHere); + out.add(c.ifCloud); + } return out; } @@ -410,6 +461,7 @@ public final class MyTeamSpeakSync { List effectiveSteps() { List out = new ArrayList<>(steps); for (Conflict c : conflicts) out.add(c.choice == Conflict.Choice.HERE ? c.keepHere : c.keepCloud); + for (Contingent c : contingents) out.add(c.on.choice == Conflict.Choice.HERE ? c.ifHere : c.ifCloud); out.sort((a, b) -> Integer.compare(b.kind.itemClass(), a.kind.itemClass())); return out; } diff --git a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/SyncKind.java b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/SyncKind.java index 1b084e4..697b7e3 100644 --- a/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/SyncKind.java +++ b/ts3-client/core/src/main/java/eu/lixko/ts3j/client/myts/SyncKind.java @@ -8,6 +8,7 @@ import eu.lixko.ts3j.client.config.IdentityStore; import eu.lixko.ts3j.client.config.Settings; import eu.lixko.ts3j.client.proto.ProtobufMessage; import eu.lixko.ts3j.client.teamspeak.SyncItem; +import eu.lixko.ts3j.client.teamspeak.SyncItemDecoder; import java.io.IOException; import java.util.ArrayList; @@ -71,6 +72,21 @@ abstract sealed class SyncKind { abstract String title(SyncFields fields); + /** Whether the local store holds at most one item per {@link #matchKey}. */ + boolean onePerKey() { + return false; + } + + /** The UUID of the account's identity the item connects with, or {@code null}. */ + String identityUsed(byte[] data) { + return null; + } + + /** Whether all that differs from {@code base} is the identity, gone here. */ + boolean lostIdentityOnly(SyncFields local, SyncFields base) { + return false; + } + /** Writes the labelled fields of the local item into the content message. */ abstract void write(String localId, ProtobufMessage content, Set labels) throws IOException; @@ -150,6 +166,12 @@ abstract sealed class SyncKind { return fields.display(NAME); } + /** Adding an identity whose key is here already updates that one instead. */ + @Override + boolean onePerKey() { + return true; + } + @Override void write(String localId, ProtobufMessage content, Set labels) throws IOException { IdentityEntry e = identities.byId(localId); @@ -300,6 +322,24 @@ abstract sealed class SyncKind { return name.isBlank() ? fields.display(ADDRESS) : name; } + @Override + String identityUsed(byte[] data) { + SyncItem.Identity identity = null; + try { + if (SyncItemDecoder.decode(data) instanceof SyncItem.Bookmark b && !b.identityUuid().isEmpty()) { + identity = links.cloudIdentities.get(b.identityUuid()); + } + } catch (IOException | RuntimeException unreadable) { + return null; + } + return identity == null ? null : identity.uuid(); + } + + @Override + boolean lostIdentityOnly(SyncFields local, SyncFields base) { + return local.compare(IDENTITY).isEmpty() && local.changedFrom(base).equals(Set.of(IDENTITY)); + } + @Override void write(String localId, ProtobufMessage content, Set labels) { Bookmark b = bookmarks.find(localId); @@ -361,7 +401,18 @@ abstract sealed class SyncKind { to.identityId = ""; if (from.identityUuid().isEmpty()) return; SyncItem.Identity identity = links.cloudIdentities.get(from.identityUuid()); - String local = identity == null ? null : links.localByUuid.get(identity.uuid()); + if (identity == null) return; + String local = links.localByUuid.get(identity.uuid()); + if (local == null || local.isEmpty()) { + // An account item not synchronised on its own, as a second copy of a key is not. + try { + IdentityEntry same = identities.byUniqueId( + IdentityStore.parseIdentityString(identity.identity()).getUid().toBase64()); + local = same == null ? null : same.getId(); + } catch (IOException malformed) { + local = null; + } + } if (local != null) to.identityId = local; } diff --git a/ts3-client/core/src/test/java/eu/lixko/ts3j/client/myts/MyTeamSpeakLoginTest.java b/ts3-client/core/src/test/java/eu/lixko/ts3j/client/myts/MyTeamSpeakLoginTest.java index a2a8bf6..69e27ed 100644 --- a/ts3-client/core/src/test/java/eu/lixko/ts3j/client/myts/MyTeamSpeakLoginTest.java +++ b/ts3-client/core/src/test/java/eu/lixko/ts3j/client/myts/MyTeamSpeakLoginTest.java @@ -6,6 +6,9 @@ import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -64,4 +67,38 @@ class MyTeamSpeakLoginTest { assertFalse(login.isSignedIn()); assertFalse(file.exists()); } + + @Test + void aSlowSignInHoldsUpOnlyRequests(@TempDir File dir) throws Exception { + File file = new File(dir, "myteamspeak.properties"); + MyTeamSpeakTest.ReplayServer server = new MyTeamSpeakTest.ReplayServer(); + new MyTeamSpeakLogin(file, new MyTeamSpeak(server)).signIn(MyTsCryptoTest.EMAIL, MyTsCryptoTest.PASSWORD); + + CountDownLatch signingIn = new CountDownLatch(1); + CountDownLatch release = new CountDownLatch(1); + MyTeamSpeakLogin login = new MyTeamSpeakLogin(file, new MyTeamSpeak((endpoint, method, request) -> { + if (method.equals("login")) { + signingIn.countDown(); + try { + release.await(); + } catch (InterruptedException e) { + throw new java.io.InterruptedIOException(); + } + } + return server.call(endpoint, method, request); + })); + CompletableFuture fetch = CompletableFuture.supplyAsync(() -> { + try { + return login.fetch(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + assertTrue(signingIn.await(5, TimeUnit.SECONDS)); + + CompletableFuture asked = CompletableFuture.supplyAsync(() -> login.isSignedIn() + " " + login.email()); + assertEquals("true " + MyTsCryptoTest.EMAIL, asked.get(2, TimeUnit.SECONDS)); + release.countDown(); + assertEquals(1, fetch.get(5, TimeUnit.SECONDS).bookmarks()); + } } diff --git a/ts3-client/core/src/test/java/eu/lixko/ts3j/client/myts/MyTeamSpeakSyncTest.java b/ts3-client/core/src/test/java/eu/lixko/ts3j/client/myts/MyTeamSpeakSyncTest.java index e210526..21b619c 100644 --- a/ts3-client/core/src/test/java/eu/lixko/ts3j/client/myts/MyTeamSpeakSyncTest.java +++ b/ts3-client/core/src/test/java/eu/lixko/ts3j/client/myts/MyTeamSpeakSyncTest.java @@ -6,6 +6,7 @@ import eu.lixko.ts3j.client.config.Bookmarks; import eu.lixko.ts3j.client.config.IdentityEntry; import eu.lixko.ts3j.client.config.IdentityStore; import eu.lixko.ts3j.client.config.Settings; +import eu.lixko.ts3j.client.proto.ProtobufMessage; import eu.lixko.ts3j.client.proto.ProtobufReader; import eu.lixko.ts3j.client.proto.ProtobufWriter; import org.junit.jupiter.api.AfterEach; @@ -382,6 +383,85 @@ class MyTeamSpeakSyncTest { assertEquals(1, fresh.conflicts().size()); } + @Test + void aSecondCopyOfAnIdentityInTheAccountIsLeftAlone(@TempDir File dir) throws Exception { + Client a = new Client(new File(dir, "a")); + String[] workId = new String[1]; + a.in(() -> workId[0] = a.identities.generate("Work", 1).getId()); + a.sync(); + String first = cloud.items.get(1).keySet().iterator().next(); + // The official client can hold the same key twice, under another name. + ProtobufMessage copy = new ProtobufMessage(cloud.plain(1, first)); + ProtobufMessage content = new ProtobufMessage(copy.get(17).readBytes()).string(2, "Copy"); + String second = UUID.randomUUID().toString(); + copy.string(2, second).message(17, content); + cloud.seed(1, second, copy.toByteArray()); + String y = UUID.randomUUID().toString(); + cloud.seed(0, y, bookmarkItem(y, "Y", "y.example", second)); + + assertEquals(new MyTeamSpeakSync.Result(0, 1), a.sync(), "the bookmark, not the copy"); + assertEquals(1, a.identities.all().size()); + assertEquals("Work", a.identities.byId(workId[0]).getName()); + assertEquals(workId[0], a.bookmark("Y").identityId, "a bookmark using the copy connects with the key"); + assertEquals(new MyTeamSpeakSync.Result(0, 0), a.sync()); + assertEquals("Work", identityName(cloud.plain(1, first))); + assertEquals("Copy", identityName(cloud.plain(1, second))); + } + + @Test + void bookmarksFollowAnIdentityRestoredFromTheAccount(@TempDir File dir) throws Exception { + Client a = new Client(new File(dir, "a")); + Client b = new Client(new File(dir, "b")); + a.in(() -> { + IdentityEntry work = a.identities.generate("Work", 1); + Bookmark x = new Bookmark("X", "x.example", 9987, "", ""); + x.identityId = work.getId(); + a.bookmarks.add(x); + }); + a.sync(); + b.sync(); + IdentityEntry onB = b.identities.byId(b.bookmark("X").identityId); + b.in(() -> b.identities.setNicknames(onB, "changed", "")); + b.sync(); + + IdentityEntry onA = a.identities.byId(a.bookmark("X").identityId); + a.in(() -> a.identities.remove(onA, a.settings, a.bookmarks)); + MyTeamSpeakSync.Plan restore = a.plan(); + assertEquals(1, restore.conflicts().size(), "the identity; the bookmark follows it"); + restore.conflicts().get(0).choose(MyTeamSpeakSync.Conflict.Choice.CLOUD); + a.finish(restore); + IdentityEntry restored = a.identities.byId(a.bookmark("X").identityId); + assertNotNull(restored); + assertEquals(onA.getUniqueId(), restored.getUniqueId()); + b.sync(); + assertEquals(onB.getId(), b.bookmark("X").identityId); + assertEquals(new MyTeamSpeakSync.Result(0, 0), a.sync()); + + a.in(() -> a.identities.remove(restored, a.settings, a.bookmarks)); + b.in(() -> b.identities.setNicknames(onB, "again", "")); + b.sync(); + MyTeamSpeakSync.Plan delete = a.plan(); + delete.conflicts().get(0).choose(MyTeamSpeakSync.Conflict.Choice.HERE); + a.finish(delete); + b.sync(); + assertNull(b.identities.byId(onB.getId())); + assertEquals("", b.bookmark("X").identityId); + assertEquals(new MyTeamSpeakSync.Result(0, 0), a.sync()); + assertEquals(new MyTeamSpeakSync.Result(0, 0), b.sync()); + } + + private static byte[] bookmarkItem(String uuid, String name, String address, String identityUuid) { + ProtobufWriter content = new ProtobufWriter().string(1, name).string(2, address).varint(3, 9987) + .string(6, identityUuid); + return new ProtobufWriter().string(2, uuid).string(3, UUID.randomUUID().toString()).varint(6, 0) + .message(16, content).toByteArray(); + } + + private static String identityName(byte[] itemData) throws IOException { + return ((eu.lixko.ts3j.client.teamspeak.SyncItem.Identity) + eu.lixko.ts3j.client.teamspeak.SyncItemDecoder.decode(itemData)).name(); + } + @Test void signingOutForgetsWhatWasSynchronised(@TempDir File dir) throws Exception { Client a = new Client(new File(dir, "a"));