mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Use client versioning for introductions.
This commit is contained in:
@@ -19,7 +19,10 @@ import org.briarproject.bramble.api.identity.IdentityManager;
|
|||||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.sync.Client;
|
import org.briarproject.bramble.api.sync.Client;
|
||||||
|
import org.briarproject.bramble.api.sync.ClientVersioningManager;
|
||||||
|
import org.briarproject.bramble.api.sync.ClientVersioningManager.ClientVersioningHook;
|
||||||
import org.briarproject.bramble.api.sync.Group;
|
import org.briarproject.bramble.api.sync.Group;
|
||||||
|
import org.briarproject.bramble.api.sync.Group.Visibility;
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.bramble.api.sync.Message;
|
import org.briarproject.bramble.api.sync.Message;
|
||||||
import org.briarproject.bramble.api.sync.MessageId;
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
@@ -39,11 +42,13 @@ import java.util.Collection;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static java.util.logging.Level.INFO;
|
||||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||||
import static org.briarproject.briar.api.introduction.Role.INTRODUCEE;
|
import static org.briarproject.briar.api.introduction.Role.INTRODUCEE;
|
||||||
import static org.briarproject.briar.api.introduction.Role.INTRODUCER;
|
import static org.briarproject.briar.api.introduction.Role.INTRODUCER;
|
||||||
@@ -59,8 +64,13 @@ import static org.briarproject.briar.introduction.MessageType.REQUEST;
|
|||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class IntroductionManagerImpl extends ConversationClientImpl
|
class IntroductionManagerImpl extends ConversationClientImpl
|
||||||
implements IntroductionManager, Client, ContactHook {
|
implements IntroductionManager, Client, ContactHook,
|
||||||
|
ClientVersioningHook {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
Logger.getLogger(IntroductionManagerImpl.class.getName());
|
||||||
|
|
||||||
|
private final ClientVersioningManager clientVersioningManager;
|
||||||
private final ContactGroupFactory contactGroupFactory;
|
private final ContactGroupFactory contactGroupFactory;
|
||||||
private final ContactManager contactManager;
|
private final ContactManager contactManager;
|
||||||
private final MessageParser messageParser;
|
private final MessageParser messageParser;
|
||||||
@@ -74,11 +84,8 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
private final Group localGroup;
|
private final Group localGroup;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
IntroductionManagerImpl(
|
IntroductionManagerImpl(DatabaseComponent db, ClientHelper clientHelper,
|
||||||
DatabaseComponent db,
|
ClientVersioningManager clientVersioningManager,MetadataParser metadataParser, MessageTracker messageTracker,
|
||||||
ClientHelper clientHelper,
|
|
||||||
MetadataParser metadataParser,
|
|
||||||
MessageTracker messageTracker,
|
|
||||||
ContactGroupFactory contactGroupFactory,
|
ContactGroupFactory contactGroupFactory,
|
||||||
ContactManager contactManager,
|
ContactManager contactManager,
|
||||||
MessageParser messageParser,
|
MessageParser messageParser,
|
||||||
@@ -89,6 +96,7 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
IntroductionCrypto crypto,
|
IntroductionCrypto crypto,
|
||||||
IdentityManager identityManager) {
|
IdentityManager identityManager) {
|
||||||
super(db, clientHelper, metadataParser, messageTracker);
|
super(db, clientHelper, metadataParser, messageTracker);
|
||||||
|
this.clientVersioningManager = clientVersioningManager;
|
||||||
this.contactGroupFactory = contactGroupFactory;
|
this.contactGroupFactory = contactGroupFactory;
|
||||||
this.contactManager = contactManager;
|
this.contactManager = contactManager;
|
||||||
this.messageParser = messageParser;
|
this.messageParser = messageParser;
|
||||||
@@ -112,13 +120,17 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
// TODO adapt to use upcoming ClientVersioning client
|
|
||||||
public void addingContact(Transaction txn, Contact c) throws DbException {
|
public void addingContact(Transaction txn, Contact c) throws DbException {
|
||||||
// Create a group to share with the contact
|
// Create a group to share with the contact
|
||||||
Group g = getContactGroup(c);
|
Group g = getContactGroup(c);
|
||||||
// Store the group and share it with the contact
|
// Store the group and share it with the contact
|
||||||
db.addGroup(txn, g);
|
db.addGroup(txn, g);
|
||||||
db.setGroupVisibility(txn, c.getId(), g.getId(), SHARED);
|
// Apply the client's visibility to the contact group
|
||||||
|
Visibility client = clientVersioningManager.getClientVisibility(txn,
|
||||||
|
c.getId(), CLIENT_ID, CLIENT_VERSION);
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Applying visibility " + client + " to new contact group");db.setGroupVisibility(txn, c.getId(), g.getId(), client);
|
||||||
|
db.setGroupVisibility(txn, c.getId(), g.getId(), client);
|
||||||
// Attach the contact ID to the group
|
// Attach the contact ID to the group
|
||||||
BdfDictionary meta = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
meta.put(GROUP_KEY_CONTACT_ID, c.getId().getInt());
|
meta.put(GROUP_KEY_CONTACT_ID, c.getId().getInt());
|
||||||
@@ -135,9 +147,20 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
abortOrRemoveSessionWithIntroducee(txn, c);
|
abortOrRemoveSessionWithIntroducee(txn, c);
|
||||||
|
|
||||||
// Remove the contact group (all messages will be removed with it)
|
// Remove the contact group (all messages will be removed with it)
|
||||||
|
|
||||||
db.removeGroup(txn, getContactGroup(c));
|
db.removeGroup(txn, getContactGroup(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClientVisibilityChanging(Transaction txn, Contact c,
|
||||||
|
Visibility v) throws DbException {
|
||||||
|
// Apply the client's visibility to the contact group
|
||||||
|
Group g = getContactGroup(c);
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Applying visibility " + v + " to contact group");
|
||||||
|
db.setGroupVisibility(txn, c.getId(), g.getId(), v);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Group getContactGroup(Contact c) {
|
public Group getContactGroup(Contact c) {
|
||||||
return contactGroupFactory
|
return contactGroupFactory
|
||||||
@@ -147,10 +170,11 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
@Override
|
@Override
|
||||||
protected boolean incomingMessage(Transaction txn, Message m, BdfList body,
|
protected boolean incomingMessage(Transaction txn, Message m, BdfList body,
|
||||||
BdfDictionary bdfMeta) throws DbException, FormatException {
|
BdfDictionary bdfMeta) throws DbException, FormatException {
|
||||||
// Parse the metadata
|
// Parse the metadata
|
||||||
MessageMetadata meta = messageParser.parseMetadata(bdfMeta);
|
MessageMetadata meta = messageParser.parseMetadata(bdfMeta);
|
||||||
// Look up the session, if there is one
|
// Look up the session, if there is one
|
||||||
SessionId sessionId = meta.getSessionId();
|
SessionId sessionId = meta.getSessionId();
|
||||||
|
|
||||||
IntroduceeSession newIntroduceeSession = null;
|
IntroduceeSession newIntroduceeSession = null;
|
||||||
if (sessionId == null) {
|
if (sessionId == null) {
|
||||||
if (meta.getMessageType() != REQUEST) throw new AssertionError();
|
if (meta.getMessageType() != REQUEST) throw new AssertionError();
|
||||||
@@ -161,7 +185,7 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
// Handle the message
|
// Handle the message
|
||||||
Session session;
|
Session session;
|
||||||
MessageId storageId;
|
MessageId storageId;
|
||||||
if (ss == null) {
|
if (ss == null){
|
||||||
if (meta.getMessageType() != REQUEST) throw new FormatException();
|
if (meta.getMessageType() != REQUEST) throw new FormatException();
|
||||||
if (newIntroduceeSession == null) throw new AssertionError();
|
if (newIntroduceeSession == null) throw new AssertionError();
|
||||||
storageId = createStorageId(txn);
|
storageId = createStorageId(txn);
|
||||||
@@ -172,12 +196,12 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
Role role = sessionParser.getRole(ss.bdfSession);
|
Role role = sessionParser.getRole(ss.bdfSession);
|
||||||
if (role == INTRODUCER) {
|
if (role == INTRODUCER) {
|
||||||
session = handleMessage(txn, m, body, meta.getMessageType(),
|
session = handleMessage(txn, m, body, meta.getMessageType(),
|
||||||
sessionParser.parseIntroducerSession(ss.bdfSession),
|
sessionParser.parseIntroducerSession(ss.bdfSession),
|
||||||
introducerEngine);
|
introducerEngine);
|
||||||
} else if (role == INTRODUCEE) {
|
} else if (role == INTRODUCEE) {
|
||||||
session = handleMessage(txn, m, body, meta.getMessageType(),
|
session = handleMessage(txn, m, body, meta.getMessageType(),
|
||||||
sessionParser.parseIntroduceeSession(m.getGroupId(),
|
sessionParser.parseIntroduceeSession(m.getGroupId(),
|
||||||
ss.bdfSession), introduceeEngine);
|
ss.bdfSession), introduceeEngine);
|
||||||
} else throw new AssertionError();
|
} else throw new AssertionError();
|
||||||
}
|
}
|
||||||
// Store the updated session
|
// Store the updated session
|
||||||
@@ -388,13 +412,14 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
List<IntroductionMessage> messages;
|
List<IntroductionMessage> messages;
|
||||||
Transaction txn = db.startTransaction(true);
|
Transaction txn = db.startTransaction(true);
|
||||||
try {
|
try {
|
||||||
Contact contact = db.getContact(txn, c);
|
Contact contact =db.getContact(txn, c);
|
||||||
GroupId contactGroupId = getContactGroup(contact).getId();
|
GroupId contactGroupId = getContactGroup(contact).getId();
|
||||||
BdfDictionary query = messageParser.getMessagesVisibleInUiQuery();
|
BdfDictionary query = messageParser.getMessagesVisibleInUiQuery();
|
||||||
Map<MessageId, BdfDictionary> results = clientHelper
|
Map<MessageId, BdfDictionary> results = clientHelper.getMessageMetadataAsDictionary(txn, contactGroupId, query);
|
||||||
.getMessageMetadataAsDictionary(txn, contactGroupId, query);
|
messages = new ArrayList<>(results.size());
|
||||||
messages = new ArrayList<>(results.size());
|
for (Entry<
|
||||||
for (Entry<MessageId, BdfDictionary> e : results.entrySet()) {
|
MessageId ,
|
||||||
|
BdfDictionary > e : results.entrySet()) {
|
||||||
MessageId m = e.getKey();
|
MessageId m = e.getKey();
|
||||||
MessageMetadata meta =
|
MessageMetadata meta =
|
||||||
messageParser.parseMetadata(e.getValue());
|
messageParser.parseMetadata(e.getValue());
|
||||||
@@ -405,7 +430,7 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
if (type == REQUEST) {
|
if (type == REQUEST) {
|
||||||
messages.add(
|
messages.add(
|
||||||
parseInvitationRequest(txn, contactGroupId, m,
|
parseInvitationRequest(txn, contactGroupId, m,
|
||||||
meta, status, ss.bdfSession));
|
meta, status, ss.bdfSession));
|
||||||
} else if (type == ACCEPT) {
|
} else if (type == ACCEPT) {
|
||||||
messages.add(
|
messages.add(
|
||||||
parseInvitationResponse(contactGroupId, m, meta,
|
parseInvitationResponse(contactGroupId, m, meta,
|
||||||
@@ -425,8 +450,7 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IntroductionRequest parseInvitationRequest(Transaction txn,
|
private IntroductionRequest parseInvitationRequest(Transaction txn, GroupId contactGroupId, MessageId m, MessageMetadata meta,
|
||||||
GroupId contactGroupId, MessageId m, MessageMetadata meta,
|
|
||||||
MessageStatus status, BdfDictionary bdfSession)
|
MessageStatus status, BdfDictionary bdfSession)
|
||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
Role role = sessionParser.getRole(bdfSession);
|
Role role = sessionParser.getRole(bdfSession);
|
||||||
@@ -497,9 +521,9 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
Map<MessageId, BdfDictionary> sessions;
|
Map<MessageId, BdfDictionary> sessions;
|
||||||
try {
|
try {
|
||||||
sessions = clientHelper
|
sessions = clientHelper
|
||||||
.getMessageMetadataAsDictionary(txn, localGroup.getId(),
|
.getMessageMetadataAsDictionary(txn,localGroup.getId(),
|
||||||
query);
|
query);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e){
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
}
|
}
|
||||||
for (MessageId id : sessions.keySet()) {
|
for (MessageId id : sessions.keySet()) {
|
||||||
@@ -510,20 +534,20 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
|||||||
private void abortOrRemoveSessionWithIntroducee(Transaction txn,
|
private void abortOrRemoveSessionWithIntroducee(Transaction txn,
|
||||||
Contact c) throws DbException {
|
Contact c) throws DbException {
|
||||||
BdfDictionary query = sessionEncoder.getIntroducerSessionsQuery();
|
BdfDictionary query = sessionEncoder.getIntroducerSessionsQuery();
|
||||||
Map<MessageId, BdfDictionary> sessions;
|
Map<MessageId, BdfDictionary> sessions;
|
||||||
try {
|
try {
|
||||||
sessions = clientHelper
|
sessions = clientHelper
|
||||||
.getMessageMetadataAsDictionary(txn, localGroup.getId(),
|
.getMessageMetadataAsDictionary(txn,
|
||||||
query);
|
localGroup.getId(),
|
||||||
|
query);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new DbException();
|
throw new DbException();
|
||||||
}
|
}
|
||||||
LocalAuthor localAuthor = identityManager.getLocalAuthor(txn);
|
LocalAuthor localAuthor = identityManager.getLocalAuthor(txn);for (Entry<MessageId, BdfDictionary> session : sessions.entrySet()) {
|
||||||
for (Entry<MessageId, BdfDictionary> session : sessions.entrySet()) {
|
|
||||||
IntroducerSession s;
|
IntroducerSession s;
|
||||||
try {
|
try {
|
||||||
s = sessionParser.parseIntroducerSession(session.getValue());
|
s = sessionParser.parseIntroducerSession(session.getValue());
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e){
|
||||||
throw new DbException();
|
throw new DbException();
|
||||||
}
|
}
|
||||||
if (s.getIntroduceeA().author.equals(c.getAuthor())) {
|
if (s.getIntroduceeA().author.equals(c.getAuthor())) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.client.ClientHelper;
|
|||||||
import org.briarproject.bramble.api.contact.ContactManager;
|
import org.briarproject.bramble.api.contact.ContactManager;
|
||||||
import org.briarproject.bramble.api.data.MetadataEncoder;
|
import org.briarproject.bramble.api.data.MetadataEncoder;
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
|
import org.briarproject.bramble.api.sync.ClientVersioningManager;
|
||||||
import org.briarproject.bramble.api.sync.ValidationManager;
|
import org.briarproject.bramble.api.sync.ValidationManager;
|
||||||
import org.briarproject.bramble.api.system.Clock;
|
import org.briarproject.bramble.api.system.Clock;
|
||||||
import org.briarproject.briar.api.introduction.IntroductionManager;
|
import org.briarproject.briar.api.introduction.IntroductionManager;
|
||||||
@@ -47,12 +48,16 @@ public class IntroductionModule {
|
|||||||
LifecycleManager lifecycleManager, ContactManager contactManager,
|
LifecycleManager lifecycleManager, ContactManager contactManager,
|
||||||
ValidationManager validationManager,
|
ValidationManager validationManager,
|
||||||
ConversationManager conversationManager,
|
ConversationManager conversationManager,
|
||||||
|
ClientVersioningManager clientVersioningManager,
|
||||||
IntroductionManagerImpl introductionManager) {
|
IntroductionManagerImpl introductionManager) {
|
||||||
lifecycleManager.registerClient(introductionManager);
|
lifecycleManager.registerClient(introductionManager);
|
||||||
contactManager.registerContactHook(introductionManager);
|
contactManager.registerContactHook(introductionManager);
|
||||||
validationManager.registerIncomingMessageHook(CLIENT_ID,
|
validationManager.registerIncomingMessageHook(CLIENT_ID,
|
||||||
CLIENT_VERSION, introductionManager);
|
CLIENT_VERSION, introductionManager);
|
||||||
conversationManager.registerConversationClient(introductionManager);
|
conversationManager.registerConversationClient(introductionManager);
|
||||||
|
clientVersioningManager.registerClient(CLIENT_ID, CLIENT_VERSION);
|
||||||
|
clientVersioningManager.registerClientVersioningHook(CLIENT_ID,
|
||||||
|
CLIENT_VERSION, introductionManager);
|
||||||
return introductionManager;
|
return introductionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user