Verified Commit 05deaf42 authored by akwizgran's avatar akwizgran

Store and exchange client minor versions.

These don't affect client visibility.
parent 2e570ba5
......@@ -22,6 +22,11 @@ public interface TransportPropertyManager {
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the transport property client.
*/
int MINOR_VERSION = 0;
/**
* Stores the given properties received while adding a contact - they will
* be superseded by any properties synced from the contact.
......
......@@ -25,7 +25,7 @@ public interface ClientVersioningManager {
* Registers a client that will be advertised to contacts. This method
* should be called before {@link LifecycleManager#startServices(String)}.
*/
void registerClient(ClientId clientId, int majorVersion);
void registerClient(ClientId clientId, int majorVersion, int minorVersion);
/**
* Registers a hook that will be called when the visibility of the given
......
......@@ -17,6 +17,7 @@ import dagger.Provides;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MAJOR_VERSION;
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MINOR_VERSION;
@Module
public class PropertiesModule {
......@@ -51,7 +52,8 @@ public class PropertiesModule {
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
transportPropertyManager);
contactManager.registerContactHook(transportPropertyManager);
clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION);
clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION,
MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(CLIENT_ID,
MAJOR_VERSION, transportPropertyManager);
return transportPropertyManager;
......
......@@ -33,15 +33,10 @@ class ClientVersion implements Comparable<ClientVersion> {
}
@Override
public int compareTo(ClientVersion c) {
int compare = clientId.compareTo(c.clientId);
public int compareTo(ClientVersion cv) {
int compare = clientId.compareTo(cv.clientId);
if (compare != 0) return compare;
return majorVersion - c.majorVersion;
}
@Override
public String toString() {
return clientId.getString() + ":" + majorVersion;
return majorVersion - cv.majorVersion;
}
}
......@@ -61,7 +61,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
private final Clock clock;
private final Group localGroup;
private final Collection<ClientVersion> clients =
private final List<ClientMinorVersion> clients =
new CopyOnWriteArrayList<>();
private final Map<ClientVersion, ClientVersioningHook> hooks =
new ConcurrentHashMap<>();
......@@ -79,8 +79,10 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
}
@Override
public void registerClient(ClientId clientId, int majorVersion) {
clients.add(new ClientVersion(clientId, majorVersion));
public void registerClient(ClientId clientId, int majorVersion,
int minorVersion) {
clients.add(new ClientMinorVersion(clientId, majorVersion,
minorVersion));
}
@Override
......@@ -124,7 +126,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
@Override
public void startService() throws ServiceException {
List<ClientVersion> versions = new ArrayList<>(clients);
List<ClientMinorVersion> versions = new ArrayList<>(clients);
Collections.sort(versions);
try {
Transaction txn = db.startTransaction(false);
......@@ -161,7 +163,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
throw new AssertionError(e);
}
// Create and store the first local update
List<ClientVersion> versions = new ArrayList<>(clients);
List<ClientMinorVersion> versions = new ArrayList<>(clients);
Collections.sort(versions);
storeFirstUpdate(txn, g.getId(), versions);
}
......@@ -230,7 +232,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
}
private void storeClientVersions(Transaction txn,
List<ClientVersion> versions) throws DbException {
List<ClientMinorVersion> versions) throws DbException {
long now = clock.currentTimeMillis();
BdfList body = encodeClientVersions(versions);
try {
......@@ -242,30 +244,35 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
}
}
private BdfList encodeClientVersions(List<ClientVersion> versions) {
private BdfList encodeClientVersions(List<ClientMinorVersion> versions) {
BdfList encoded = new BdfList();
for (ClientVersion cv : versions)
encoded.add(BdfList.of(cv.clientId.getString(), cv.majorVersion));
for (ClientMinorVersion cm : versions)
encoded.add(encodeClientVersion(cm));
return encoded;
}
private BdfList encodeClientVersion(ClientMinorVersion cm) {
return BdfList.of(cm.version.clientId.getString(),
cm.version.majorVersion, cm.minorVersion);
}
private boolean updateClientVersions(Transaction txn,
List<ClientVersion> newVersions) throws DbException {
List<ClientMinorVersion> newVersions) throws DbException {
Collection<MessageId> ids = db.getMessageIds(txn, localGroup.getId());
if (ids.isEmpty()) {
storeClientVersions(txn, newVersions);
return true;
}
MessageId m = ids.iterator().next();
List<ClientVersion> oldVersions = loadClientVersions(txn, m);
List<ClientMinorVersion> oldVersions = loadClientVersions(txn, m);
if (oldVersions.equals(newVersions)) return false;
db.removeMessage(txn, m);
storeClientVersions(txn, newVersions);
return true;
}
private List<ClientVersion> loadClientVersions(Transaction txn, MessageId m)
throws DbException {
private List<ClientMinorVersion> loadClientVersions(Transaction txn,
MessageId m) throws DbException {
try {
BdfList body = clientHelper.getMessageAsList(txn, m);
if (body == null) throw new DbException();
......@@ -275,21 +282,23 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
}
}
private List<ClientVersion> parseClientVersions(BdfList body)
private List<ClientMinorVersion> parseClientVersions(BdfList body)
throws FormatException {
int size = body.size();
List<ClientVersion> parsed = new ArrayList<>(size);
List<ClientMinorVersion> parsed = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
BdfList cv = body.getList(i);
ClientId clientId = new ClientId(cv.getString(0));
int majorVersion = cv.getLong(1).intValue();
parsed.add(new ClientVersion(clientId, majorVersion));
int minorVersion = cv.getLong(2).intValue();
parsed.add(new ClientMinorVersion(clientId, majorVersion,
minorVersion));
}
return parsed;
}
private void clientVersionsUpdated(Transaction txn, Contact c,
List<ClientVersion> versions) throws DbException {
List<ClientMinorVersion> versions) throws DbException {
try {
// Find the latest local and remote updates
Group g = getContactGroup(c);
......@@ -372,11 +381,12 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
private ClientState parseClientState(BdfList clientState)
throws FormatException {
// Client ID, major version, active
// Client ID, major version, minor version, active
ClientId clientId = new ClientId(clientState.getString(0));
int majorVersion = clientState.getLong(1).intValue();
boolean active = clientState.getBoolean(2);
return new ClientState(clientId, majorVersion, active);
int minorVersion = clientState.getLong(2).intValue();
boolean active = clientState.getBoolean(3);
return new ClientState(clientId, majorVersion, minorVersion, active);
}
private long parseUpdateVersion(BdfList body) throws FormatException {
......@@ -385,14 +395,15 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
}
private List<ClientState> updateStatesFromLocalVersions(
List<ClientState> oldStates, List<ClientVersion> newVersions) {
List<ClientState> oldStates, List<ClientMinorVersion> newVersions) {
Map<ClientVersion, ClientState> oldMap = new HashMap<>();
for (ClientState cs : oldStates) oldMap.put(cs.version, cs);
List<ClientState> newStates = new ArrayList<>(newVersions.size());
for (ClientVersion newVersion : newVersions) {
ClientState oldState = oldMap.get(newVersion);
for (ClientMinorVersion newVersion : newVersions) {
ClientState oldState = oldMap.get(newVersion.version);
boolean active = oldState != null && oldState.active;
newStates.add(new ClientState(newVersion, active));
newStates.add(new ClientState(newVersion.version,
newVersion.minorVersion, active));
}
return newStates;
}
......@@ -420,7 +431,7 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
private BdfList encodeClientState(ClientState cs) {
return BdfList.of(cs.version.clientId.getString(),
cs.version.majorVersion, cs.active);
cs.version.majorVersion, cs.minorVersion, cs.active);
}
private Map<ClientVersion, Visibility> getVisibilities(
......@@ -461,10 +472,10 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
}
private void storeFirstUpdate(Transaction txn, GroupId g,
List<ClientVersion> versions) throws DbException {
List<ClientMinorVersion> versions) throws DbException {
List<ClientState> states = new ArrayList<>(versions.size());
for (ClientVersion cv : versions)
states.add(new ClientState(cv, false));
for (ClientMinorVersion cm : versions)
states.add(new ClientState(cm.version, cm.minorVersion, false));
storeUpdate(txn, g, states, 1);
}
......@@ -487,7 +498,8 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
new ArrayList<>(oldLocalStates.size());
for (ClientState oldState : oldLocalStates) {
boolean active = remoteSet.contains(oldState.version);
newLocalStates.add(new ClientState(oldState.version, active));
newLocalStates.add(new ClientState(oldState.version,
oldState.minorVersion, active));
}
return newLocalStates;
}
......@@ -526,26 +538,71 @@ class ClientVersioningManagerImpl implements ClientVersioningManager, Client,
}
}
private static class ClientMinorVersion
implements Comparable<ClientMinorVersion> {
private final ClientVersion version;
private final int minorVersion;
private ClientMinorVersion(ClientVersion version, int minorVersion) {
this.version = version;
this.minorVersion = minorVersion;
}
private ClientMinorVersion(ClientId clientId, int majorVersion,
int minorVersion) {
this(new ClientVersion(clientId, majorVersion), minorVersion);
}
@Override
public boolean equals(Object o) {
if (o instanceof ClientMinorVersion) {
ClientMinorVersion cm = (ClientMinorVersion) o;
return version.equals(cm.version)
&& minorVersion == cm.minorVersion;
}
return false;
}
@Override
public int hashCode() {
return version.hashCode();
}
@Override
public int compareTo(ClientMinorVersion cm) {
int compare = version.compareTo(cm.version);
if (compare != 0) return compare;
return minorVersion - cm.minorVersion;
}
}
private static class ClientState {
private final ClientVersion version;
private final int minorVersion;
private final boolean active;
private ClientState(ClientVersion version, boolean active) {
private ClientState(ClientVersion version, int minorVersion,
boolean active) {
this.version = version;
this.minorVersion = minorVersion;
this.active = active;
}
private ClientState(ClientId clientId, int majorVersion,
boolean active) {
this(new ClientVersion(clientId, majorVersion), active);
int minorVersion, boolean active) {
this(new ClientVersion(clientId, majorVersion), minorVersion,
active);
}
@Override
public boolean equals(Object o) {
if (o instanceof ClientState) {
ClientState cs = (ClientState) o;
return version.equals(cs.version) && active == cs.active;
return version.equals(cs.version)
&& minorVersion == cs.minorVersion
&& active == cs.active;
}
return false;
}
......
......@@ -39,13 +39,15 @@ class ClientVersioningValidator extends BdfMessageValidator {
int size = states.size();
for (int i = 0; i < size; i++) {
BdfList clientState = states.getList(i);
// Client ID, major version, active
checkSize(clientState, 3);
// Client ID, major version, minor version, active
checkSize(clientState, 4);
String clientId = clientState.getString(0);
checkLength(clientId, 1, MAX_CLIENT_ID_LENGTH);
int majorVersion = clientState.getLong(1).intValue();
if (majorVersion < 0) throw new FormatException();
clientState.getBoolean(2);
int minorVersion = clientState.getLong(2).intValue();
if (minorVersion < 0) throw new FormatException();
clientState.getBoolean(3);
}
// Update version
long updateVersion = body.getLong(1);
......
......@@ -26,6 +26,11 @@ public interface BlogManager {
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the blog client.
*/
int MINOR_VERSION = 0;
/**
* Adds the given {@link Blog).}
*/
......
......@@ -14,4 +14,9 @@ public interface BlogSharingManager extends SharingManager<Blog> {
* The current major version of the blog sharing client.
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the blog sharing client.
*/
int MINOR_VERSION = 0;
}
......@@ -27,6 +27,11 @@ public interface ForumManager {
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the forum client.
*/
int MINOR_VERSION = 0;
/**
* Subscribes to a forum.
*/
......
......@@ -14,4 +14,9 @@ public interface ForumSharingManager extends SharingManager<Forum> {
* The current major version of the forum sharing client.
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the forum sharing client.
*/
int MINOR_VERSION = 0;
}
......@@ -30,6 +30,11 @@ public interface IntroductionManager extends ConversationClient {
*/
boolean canIntroduce(Contact c1, Contact c2) throws DbException;
/**
* The current minor version of the introduction client.
*/
int MINOR_VERSION = 0;
/**
* Sends two initial introduction messages.
*/
......
......@@ -23,6 +23,11 @@ public interface MessagingManager extends ConversationClient {
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the messaging client.
*/
int MINOR_VERSION = 0;
/**
* Stores a local private message.
*/
......
......@@ -26,6 +26,11 @@ public interface PrivateGroupManager {
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the private group client.
*/
int MINOR_VERSION = 0;
/**
* Adds a new private group and joins it.
*
......
......@@ -30,6 +30,11 @@ public interface GroupInvitationManager extends ConversationClient {
*/
int MAJOR_VERSION = 0;
/**
* The current minor version of the private group invitation client.
*/
int MINOR_VERSION = 0;
/**
* Sends an invitation to share the given private group with the given
* contact, including an optional message.
......
......@@ -18,6 +18,7 @@ import dagger.Provides;
import static org.briarproject.briar.api.introduction.IntroductionManager.CLIENT_ID;
import static org.briarproject.briar.api.introduction.IntroductionManager.MAJOR_VERSION;
import static org.briarproject.briar.api.introduction.IntroductionManager.MINOR_VERSION;
@Module
public class IntroductionModule {
......@@ -55,7 +56,8 @@ public class IntroductionModule {
validationManager.registerIncomingMessageHook(CLIENT_ID,
MAJOR_VERSION, introductionManager);
conversationManager.registerConversationClient(introductionManager);
clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION);
clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION,
MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(CLIENT_ID,
MAJOR_VERSION, introductionManager);
return introductionManager;
......
......@@ -19,6 +19,7 @@ import dagger.Provides;
import static org.briarproject.briar.api.messaging.MessagingManager.CLIENT_ID;
import static org.briarproject.briar.api.messaging.MessagingManager.MAJOR_VERSION;
import static org.briarproject.briar.api.messaging.MessagingManager.MINOR_VERSION;
@Module
public class MessagingModule {
......@@ -62,7 +63,8 @@ public class MessagingModule {
validationManager.registerIncomingMessageHook(CLIENT_ID, MAJOR_VERSION,
messagingManager);
conversationManager.registerConversationClient(messagingManager);
clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION);
clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION,
MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(CLIENT_ID,
MAJOR_VERSION, messagingManager);
return messagingManager;
......
......@@ -21,6 +21,7 @@ import dagger.Provides;
import static org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager.CLIENT_ID;
import static org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager.MAJOR_VERSION;
import static org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager.MINOR_VERSION;
@Module
public class GroupInvitationModule {
......@@ -47,13 +48,15 @@ public class GroupInvitationModule {
contactManager.registerContactHook(groupInvitationManager);
privateGroupManager.registerPrivateGroupHook(groupInvitationManager);
conversationManager.registerConversationClient(groupInvitationManager);
clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION);
clientVersioningManager.registerClient(CLIENT_ID, MAJOR_VERSION,
MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(CLIENT_ID,
MAJOR_VERSION, groupInvitationManager);
// The group invitation manager handles client visibility changes for
// the private group manager
clientVersioningManager.registerClient(PrivateGroupManager.CLIENT_ID,
PrivateGroupManager.MAJOR_VERSION);
PrivateGroupManager.MAJOR_VERSION,
PrivateGroupManager.MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(
PrivateGroupManager.CLIENT_ID,
PrivateGroupManager.MAJOR_VERSION,
......
......@@ -84,14 +84,15 @@ public class SharingModule {
conversationManager.registerConversationClient(blogSharingManager);
blogManager.registerRemoveBlogHook(blogSharingManager);
clientVersioningManager.registerClient(BlogSharingManager.CLIENT_ID,
BlogSharingManager.MAJOR_VERSION);
BlogSharingManager.MAJOR_VERSION,
BlogSharingManager.MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(
BlogSharingManager.CLIENT_ID, BlogSharingManager.MAJOR_VERSION,
blogSharingManager);
// The blog sharing manager handles client visibility changes for the
// blog manager
clientVersioningManager.registerClient(BlogManager.CLIENT_ID,
BlogManager.MAJOR_VERSION);
BlogManager.MAJOR_VERSION, BlogManager.MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(
BlogManager.CLIENT_ID, BlogManager.MAJOR_VERSION,
blogSharingManager.getShareableClientVersioningHook());
......@@ -147,14 +148,15 @@ public class SharingModule {
conversationManager.registerConversationClient(forumSharingManager);
forumManager.registerRemoveForumHook(forumSharingManager);
clientVersioningManager.registerClient(ForumSharingManager.CLIENT_ID,
ForumSharingManager.MAJOR_VERSION);
ForumSharingManager.MAJOR_VERSION,
ForumSharingManager.MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(
ForumSharingManager.CLIENT_ID,
ForumSharingManager.MAJOR_VERSION, forumSharingManager);
// The forum sharing manager handles client visibility changes for the
// forum manager
clientVersioningManager.registerClient(ForumManager.CLIENT_ID,
ForumManager.MAJOR_VERSION);
ForumManager.MAJOR_VERSION, ForumManager.MINOR_VERSION);
clientVersioningManager.registerClientVersioningHook(
ForumManager.CLIENT_ID, ForumManager.MAJOR_VERSION,
forumSharingManager.getShareableClientVersioningHook());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment