diff --git a/briar-api/src/org/briarproject/api/introduction/IntroductionManager.java b/briar-api/src/org/briarproject/api/introduction/IntroductionManager.java index 18c5da1c691ef626deb5634194cde763a1730b1f..f02b9b635cc3a40cd655074f1e3c8e8ff1bdb447 100644 --- a/briar-api/src/org/briarproject/api/introduction/IntroductionManager.java +++ b/briar-api/src/org/briarproject/api/introduction/IntroductionManager.java @@ -45,22 +45,4 @@ public interface IntroductionManager { Collection<IntroductionMessage> getIntroductionMessages(ContactId contactId) throws DbException; - /** Marks an introduction message as read or unread. */ - void setReadFlag(MessageId m, boolean read) throws DbException; - - - /** Get the session state for the given session ID */ - BdfDictionary getSessionState(Transaction txn, GroupId groupId, - byte[] sessionId) throws DbException, FormatException; - - /** Gets the group used for introductions with Contact c */ - Group getIntroductionGroup(Contact c); - - /** Get the local group used to store session states */ - Group getLocalGroup(); - - /** Send an introduction message */ - void sendMessage(Transaction txn, BdfDictionary message) - throws DbException, FormatException; - } diff --git a/briar-core/src/org/briarproject/introduction/IntroduceeManager.java b/briar-core/src/org/briarproject/introduction/IntroduceeManager.java index 64af33b44d59cd2f8ff89d194cefb9fb7e97ae7d..b25f299e024870824fbbcd2f8329ae530b1dd085 100644 --- a/briar-core/src/org/briarproject/introduction/IntroduceeManager.java +++ b/briar-core/src/org/briarproject/introduction/IntroduceeManager.java @@ -24,11 +24,8 @@ import org.briarproject.api.event.IntroductionSucceededEvent; import org.briarproject.api.identity.Author; import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.identity.AuthorId; -import org.briarproject.api.introduction.IntroductionManager; -import org.briarproject.api.introduction.SessionId; import org.briarproject.api.properties.TransportProperties; import org.briarproject.api.properties.TransportPropertyManager; -import org.briarproject.api.sync.Group; import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; @@ -40,6 +37,8 @@ import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; +import javax.inject.Inject; + import static java.util.logging.Level.WARNING; import static org.briarproject.api.introduction.IntroduceeProtocolState.AWAIT_REQUEST; import static org.briarproject.api.introduction.IntroductionConstants.ACCEPT; @@ -76,33 +75,36 @@ import static org.briarproject.api.introduction.IntroductionConstants.TYPE_RESPO class IntroduceeManager { - private static final Logger LOG = Logger.getLogger(IntroduceeManager.class.getName()); + private final MessageSender messageSender; private final DatabaseComponent db; - private final IntroductionManager introductionManager; private final ClientHelper clientHelper; private final Clock clock; private final CryptoComponent cryptoComponent; private final TransportPropertyManager transportPropertyManager; private final AuthorFactory authorFactory; private final ContactManager contactManager; + private final IntroductionGroupFactory introductionGroupFactory; - IntroduceeManager(DatabaseComponent db, - IntroductionManager introductionManager, ClientHelper clientHelper, - Clock clock, CryptoComponent cryptoComponent, + @Inject + IntroduceeManager(MessageSender messageSender, DatabaseComponent db, + ClientHelper clientHelper, Clock clock, + CryptoComponent cryptoComponent, TransportPropertyManager transportPropertyManager, - AuthorFactory authorFactory, ContactManager contactManager) { + AuthorFactory authorFactory, ContactManager contactManager, + IntroductionGroupFactory introductionGroupFactory) { + this.messageSender = messageSender; this.db = db; - this.introductionManager = introductionManager; this.clientHelper = clientHelper; this.clock = clock; this.cryptoComponent = cryptoComponent; this.transportPropertyManager = transportPropertyManager; this.authorFactory = authorFactory; this.contactManager = contactManager; + this.introductionGroupFactory = introductionGroupFactory; } public BdfDictionary initialize(Transaction txn, GroupId groupId, @@ -113,9 +115,9 @@ class IntroduceeManager { Bytes salt = new Bytes(new byte[64]); cryptoComponent.getSecureRandom().nextBytes(salt.getBytes()); - Message localMsg = clientHelper - .createMessage(introductionManager.getLocalGroup().getId(), now, - BdfList.of(salt)); + Message localMsg = clientHelper.createMessage( + introductionGroupFactory.createLocalGroup().getId(), now, + BdfList.of(salt)); MessageId storageId = localMsg.getId(); // find out who is introducing us @@ -133,7 +135,7 @@ class IntroduceeManager { d.put(INTRODUCER, introducer.getAuthor().getName()); d.put(CONTACT_ID_1, introducer.getId().getInt()); d.put(LOCAL_AUTHOR_ID, introducer.getLocalAuthorId().getBytes()); - d.put(NOT_OUR_RESPONSE, new byte[0]); + d.put(NOT_OUR_RESPONSE, storageId); d.put(ANSWERED, false); // check if the contact we are introduced to does already exist @@ -147,7 +149,7 @@ class IntroduceeManager { // save local state to database clientHelper.addLocalMessage(txn, localMsg, - introductionManager.getClientId(), d, false); + IntroductionManagerImpl.CLIENT_ID, d, false); return d; } @@ -159,16 +161,10 @@ class IntroduceeManager { processStateUpdate(txn, engine.onMessageReceived(state, message)); } - public void acceptIntroduction(Transaction txn, final ContactId contactId, - final SessionId sessionId, final long timestamp) + public void acceptIntroduction(Transaction txn, BdfDictionary state, + final long timestamp) throws DbException, FormatException { - Contact c = db.getContact(txn, contactId); - Group g = introductionManager.getIntroductionGroup(c); - - BdfDictionary state = introductionManager - .getSessionState(txn, g.getId(), sessionId.getBytes()); - // get data to connect and derive a shared secret later long now = clock.currentTimeMillis(); KeyPair keyPair = cryptoComponent.generateAgreementKeyPair(); @@ -195,16 +191,10 @@ class IntroduceeManager { processStateUpdate(txn, engine.onLocalAction(state, localAction)); } - public void declineIntroduction(Transaction txn, final ContactId contactId, - final SessionId sessionId, final long timestamp) + public void declineIntroduction(Transaction txn, BdfDictionary state, + final long timestamp) throws DbException, FormatException { - Contact c = db.getContact(txn, contactId); - Group g = introductionManager.getIntroductionGroup(c); - - BdfDictionary state = introductionManager - .getSessionState(txn, g.getId(), sessionId.getBytes()); - // update session state state.put(ACCEPT, false); @@ -233,7 +223,7 @@ class IntroduceeManager { // send messages for (BdfDictionary d : result.toSend) { - introductionManager.sendMessage(txn, d); + messageSender.sendMessage(txn, d); } // broadcast events diff --git a/briar-core/src/org/briarproject/introduction/IntroducerManager.java b/briar-core/src/org/briarproject/introduction/IntroducerManager.java index 93481367df01a89508d541d4ca6be4b21072288d..891c4b7b599e3eb2bf679a8eeadd49c237c8d98a 100644 --- a/briar-core/src/org/briarproject/introduction/IntroducerManager.java +++ b/briar-core/src/org/briarproject/introduction/IntroducerManager.java @@ -10,7 +10,6 @@ import org.briarproject.api.data.BdfList; import org.briarproject.api.db.DbException; import org.briarproject.api.db.Transaction; import org.briarproject.api.event.Event; -import org.briarproject.api.introduction.IntroductionManager; import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Message; import org.briarproject.api.sync.MessageId; @@ -20,6 +19,8 @@ import org.briarproject.util.StringUtils; import java.io.IOException; import java.util.logging.Logger; +import javax.inject.Inject; + import static java.util.logging.Level.WARNING; import static org.briarproject.api.introduction.IntroducerProtocolState.PREPARE_REQUESTS; import static org.briarproject.api.introduction.IntroductionConstants.AUTHOR_ID_1; @@ -39,7 +40,6 @@ import static org.briarproject.api.introduction.IntroductionConstants.ROLE_INTRO import static org.briarproject.api.introduction.IntroductionConstants.SESSION_ID; import static org.briarproject.api.introduction.IntroductionConstants.STATE; import static org.briarproject.api.introduction.IntroductionConstants.STORAGE_ID; -import static org.briarproject.api.introduction.IntroductionConstants.TIME; import static org.briarproject.api.introduction.IntroductionConstants.TYPE; import static org.briarproject.api.introduction.IntroductionConstants.TYPE_ABORT; import static org.briarproject.api.introduction.IntroductionConstants.TYPE_REQUEST; @@ -49,19 +49,22 @@ class IntroducerManager { private static final Logger LOG = Logger.getLogger(IntroducerManager.class.getName()); - private final IntroductionManager introductionManager; + private final MessageSender messageSender; private final ClientHelper clientHelper; private final Clock clock; private final CryptoComponent cryptoComponent; + private final IntroductionGroupFactory introductionGroupFactory; - IntroducerManager(IntroductionManager introductionManager, - ClientHelper clientHelper, Clock clock, - CryptoComponent cryptoComponent) { + @Inject + IntroducerManager(MessageSender messageSender, ClientHelper clientHelper, + Clock clock, CryptoComponent cryptoComponent, + IntroductionGroupFactory introductionGroupFactory) { - this.introductionManager = introductionManager; + this.messageSender = messageSender; this.clientHelper = clientHelper; this.clock = clock; this.cryptoComponent = cryptoComponent; + this.introductionGroupFactory = introductionGroupFactory; } public BdfDictionary initialize(Transaction txn, Contact c1, Contact c2) @@ -72,13 +75,13 @@ class IntroducerManager { Bytes salt = new Bytes(new byte[64]); cryptoComponent.getSecureRandom().nextBytes(salt.getBytes()); - Message m = clientHelper - .createMessage(introductionManager.getLocalGroup().getId(), now, - BdfList.of(salt)); + Message m = clientHelper.createMessage( + introductionGroupFactory.createLocalGroup().getId(), now, + BdfList.of(salt)); MessageId sessionId = m.getId(); - Group g1 = introductionManager.getIntroductionGroup(c1); - Group g2 = introductionManager.getIntroductionGroup(c2); + Group g1 = introductionGroupFactory.createIntroductionGroup(c1); + Group g2 = introductionGroupFactory.createIntroductionGroup(c2); BdfDictionary d = new BdfDictionary(); d.put(SESSION_ID, sessionId); @@ -95,7 +98,9 @@ class IntroducerManager { d.put(AUTHOR_ID_2, c2.getAuthor().getId()); // save local state to database - clientHelper.addLocalMessage(txn, m, introductionManager.getClientId(), d, false); + clientHelper + .addLocalMessage(txn, m, IntroductionManagerImpl.CLIENT_ID, d, + false); return d; } @@ -143,7 +148,7 @@ class IntroducerManager { // send messages for (BdfDictionary d : result.toSend) { - introductionManager.sendMessage(txn, d); + messageSender.sendMessage(txn, d); } // broadcast events diff --git a/briar-core/src/org/briarproject/introduction/IntroductionGroupFactory.java b/briar-core/src/org/briarproject/introduction/IntroductionGroupFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..a255c8114538c0ad464e8a52eb6ecfee21400240 --- /dev/null +++ b/briar-core/src/org/briarproject/introduction/IntroductionGroupFactory.java @@ -0,0 +1,30 @@ +package org.briarproject.introduction; + +import org.briarproject.api.clients.PrivateGroupFactory; +import org.briarproject.api.contact.Contact; +import org.briarproject.api.sync.Group; + +import javax.inject.Inject; + +class IntroductionGroupFactory { + + final private PrivateGroupFactory privateGroupFactory; + final private Group localGroup; + + @Inject + IntroductionGroupFactory(PrivateGroupFactory privateGroupFactory) { + this.privateGroupFactory = privateGroupFactory; + localGroup = privateGroupFactory + .createLocalGroup(IntroductionManagerImpl.CLIENT_ID); + } + + public Group createIntroductionGroup(Contact c) { + return privateGroupFactory + .createPrivateGroup(IntroductionManagerImpl.CLIENT_ID, c); + } + + public Group createLocalGroup() { + return localGroup; + } + +} diff --git a/briar-core/src/org/briarproject/introduction/IntroductionManagerImpl.java b/briar-core/src/org/briarproject/introduction/IntroductionManagerImpl.java index edec27bef53a4d1042639557c96d65f03f4bca06..3550d0148960c92e4677dea85e9180fa6aca70c5 100644 --- a/briar-core/src/org/briarproject/introduction/IntroductionManagerImpl.java +++ b/briar-core/src/org/briarproject/introduction/IntroductionManagerImpl.java @@ -3,25 +3,17 @@ package org.briarproject.introduction; import org.briarproject.api.FormatException; import org.briarproject.api.clients.Client; import org.briarproject.api.clients.ClientHelper; -import org.briarproject.api.clients.MessageQueueManager; -import org.briarproject.api.clients.PrivateGroupFactory; import org.briarproject.api.contact.Contact; import org.briarproject.api.contact.ContactId; -import org.briarproject.api.contact.ContactManager; import org.briarproject.api.contact.ContactManager.AddContactHook; import org.briarproject.api.contact.ContactManager.RemoveContactHook; -import org.briarproject.api.crypto.CryptoComponent; import org.briarproject.api.data.BdfDictionary; -import org.briarproject.api.data.BdfEntry; import org.briarproject.api.data.BdfList; -import org.briarproject.api.data.MetadataEncoder; import org.briarproject.api.data.MetadataParser; import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DbException; -import org.briarproject.api.db.Metadata; import org.briarproject.api.db.NoSuchMessageException; import org.briarproject.api.db.Transaction; -import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.identity.AuthorId; import org.briarproject.api.introduction.IntroducerProtocolState; import org.briarproject.api.introduction.IntroductionManager; @@ -29,7 +21,6 @@ import org.briarproject.api.introduction.IntroductionMessage; import org.briarproject.api.introduction.IntroductionRequest; import org.briarproject.api.introduction.IntroductionResponse; import org.briarproject.api.introduction.SessionId; -import org.briarproject.api.properties.TransportPropertyManager; import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.Group; import org.briarproject.api.sync.GroupId; @@ -94,37 +85,22 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook Logger.getLogger(IntroductionManagerImpl.class.getName()); private final DatabaseComponent db; - private final MessageQueueManager messageQueueManager; - private final PrivateGroupFactory privateGroupFactory; - private final MetadataEncoder metadataEncoder; private final IntroducerManager introducerManager; private final IntroduceeManager introduceeManager; - private final Group localGroup; + private final IntroductionGroupFactory introductionGroupFactory; @Inject - IntroductionManagerImpl(DatabaseComponent db, - MessageQueueManager messageQueueManager, - ClientHelper clientHelper, PrivateGroupFactory privateGroupFactory, - MetadataEncoder metadataEncoder, MetadataParser metadataParser, - CryptoComponent cryptoComponent, - TransportPropertyManager transportPropertyManager, - AuthorFactory authorFactory, ContactManager contactManager, - Clock clock) { + IntroductionManagerImpl(DatabaseComponent db, ClientHelper clientHelper, + MetadataParser metadataParser, Clock clock, + IntroducerManager introducerManager, + IntroduceeManager introduceeManager, + IntroductionGroupFactory introductionGroupFactory) { super(clientHelper, metadataParser, clock); this.db = db; - this.messageQueueManager = messageQueueManager; - this.privateGroupFactory = privateGroupFactory; - this.metadataEncoder = metadataEncoder; - // TODO: Inject these dependencies for easier testing - this.introducerManager = - new IntroducerManager(this, clientHelper, clock, - cryptoComponent); - this.introduceeManager = - new IntroduceeManager(db, this, clientHelper, clock, - cryptoComponent, transportPropertyManager, - authorFactory, contactManager); - localGroup = privateGroupFactory.createLocalGroup(CLIENT_ID); + this.introducerManager = introducerManager; + this.introduceeManager = introduceeManager; + this.introductionGroupFactory = introductionGroupFactory; } @Override @@ -134,7 +110,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook @Override public void createLocalState(Transaction txn) throws DbException { - db.addGroup(txn, localGroup); + db.addGroup(txn, introductionGroupFactory.createLocalGroup()); // Ensure we've set things up for any pre-existing contacts for (Contact c : db.getContacts(txn)) addingContact(txn, c); } @@ -143,7 +119,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook public void addingContact(Transaction txn, Contact c) throws DbException { try { // Create an introduction group for sending introduction messages - Group g = getIntroductionGroup(c); + Group g = introductionGroupFactory.createIntroductionGroup(c); // Return if we've already set things up for this contact if (db.containsGroup(txn, g.getId())) return; // Store the group and share it with the contact @@ -164,7 +140,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook Long id = (long) c.getId().getInt(); try { Map<MessageId, BdfDictionary> map = clientHelper - .getMessageMetadataAsDictionary(txn, localGroup.getId()); + .getMessageMetadataAsDictionary(txn, + introductionGroupFactory.createLocalGroup().getId()); for (Map.Entry<MessageId, BdfDictionary> entry : map.entrySet()) { BdfDictionary d = entry.getValue(); long role = d.getLong(ROLE, -1L); @@ -185,7 +162,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook // remove the group (all messages will be removed with it) // this contact won't get our abort message, but the other will - db.removeGroup(txn, getIntroductionGroup(c)); + db.removeGroup(txn, introductionGroupFactory.createIntroductionGroup(c)); } /** @@ -287,8 +264,12 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook Transaction txn = db.startTransaction(false); try { - introduceeManager - .acceptIntroduction(txn, contactId, sessionId, timestamp); + Contact c = db.getContact(txn, contactId); + Group g = introductionGroupFactory.createIntroductionGroup(c); + BdfDictionary state = + getSessionState(txn, g.getId(), sessionId.getBytes()); + + introduceeManager.acceptIntroduction(txn, state, timestamp); txn.setComplete(); } finally { db.endTransaction(txn); @@ -302,8 +283,12 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook Transaction txn = db.startTransaction(false); try { - introduceeManager - .declineIntroduction(txn, contactId, sessionId, timestamp); + Contact c = db.getContact(txn, contactId); + Group g = introductionGroupFactory.createIntroductionGroup(c); + BdfDictionary state = + getSessionState(txn, g.getId(), sessionId.getBytes()); + + introduceeManager.declineIntroduction(txn, state, timestamp); txn.setComplete(); } finally { db.endTransaction(txn); @@ -322,8 +307,9 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook Transaction txn = db.startTransaction(true); try { // get messages and their status - GroupId g = - getIntroductionGroup(db.getContact(txn, contactId)).getId(); + GroupId g = introductionGroupFactory + .createIntroductionGroup(db.getContact(txn, contactId)) + .getId(); metadata = clientHelper.getMessageMetadataAsDictionary(txn, g); statuses = db.getMessageStatus(txn, contactId, g); @@ -444,17 +430,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook } } - @Override - public void setReadFlag(MessageId m, boolean read) throws DbException { - try { - BdfDictionary meta = BdfDictionary.of(new BdfEntry(READ, read)); - clientHelper.mergeMessageMetadata(m, meta); - } catch (FormatException e) { - throw new RuntimeException(e); - } - } - - public BdfDictionary getSessionState(Transaction txn, GroupId groupId, + private BdfDictionary getSessionState(Transaction txn, GroupId groupId, byte[] sessionId) throws DbException, FormatException { try { @@ -473,7 +449,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook // to find state for introducee Map<MessageId, BdfDictionary> map = clientHelper .getMessageMetadataAsDictionary(txn, - localGroup.getId()); + introductionGroupFactory.createLocalGroup().getId()); for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) { if (Arrays.equals(m.getValue().getRaw(SESSION_ID), sessionId)) { BdfDictionary state = m.getValue(); @@ -490,35 +466,11 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook } } - public Group getIntroductionGroup(Contact c) { - return privateGroupFactory.createPrivateGroup(CLIENT_ID, c); - } - - public Group getLocalGroup() { - return localGroup; - } - - public void sendMessage(Transaction txn, BdfDictionary message) - throws DbException, FormatException { - - BdfList bdfList = MessageEncoder.encodeMessage(message); - byte[] body = clientHelper.toByteArray(bdfList); - GroupId groupId = new GroupId(message.getRaw(GROUP_ID)); - Group group = db.getGroup(txn, groupId); - long timestamp = - message.getLong(MESSAGE_TIME, System.currentTimeMillis()); - message.put(MESSAGE_TIME, timestamp); - - Metadata metadata = metadataEncoder.encode(message); - - messageQueueManager - .sendMessage(txn, group, timestamp, body, metadata); - } - private void deleteMessage(Transaction txn, MessageId messageId) throws DbException { db.deleteMessage(txn, messageId); db.deleteMessageMetadata(txn, messageId); } + } diff --git a/briar-core/src/org/briarproject/introduction/IntroductionModule.java b/briar-core/src/org/briarproject/introduction/IntroductionModule.java index 4fb0e493df2349fbe75da4acbe4a25f5932e18ea..633d14c15bbf8c579b259eca808a19b65ee7bee1 100644 --- a/briar-core/src/org/briarproject/introduction/IntroductionModule.java +++ b/briar-core/src/org/briarproject/introduction/IntroductionModule.java @@ -3,9 +3,13 @@ package org.briarproject.introduction; import org.briarproject.api.clients.ClientHelper; import org.briarproject.api.clients.MessageQueueManager; import org.briarproject.api.contact.ContactManager; +import org.briarproject.api.crypto.CryptoComponent; import org.briarproject.api.data.MetadataEncoder; +import org.briarproject.api.db.DatabaseComponent; +import org.briarproject.api.identity.AuthorFactory; import org.briarproject.api.introduction.IntroductionManager; import org.briarproject.api.lifecycle.LifecycleManager; +import org.briarproject.api.properties.TransportPropertyManager; import org.briarproject.api.system.Clock; import javax.inject.Inject; diff --git a/briar-core/src/org/briarproject/introduction/MessageEncoder.java b/briar-core/src/org/briarproject/introduction/MessageSender.java similarity index 52% rename from briar-core/src/org/briarproject/introduction/MessageEncoder.java rename to briar-core/src/org/briarproject/introduction/MessageSender.java index 12153122cd51323d3975cfcbcc38b48fac39c15a..7152a1344ea90ea0b6a622061fe299c0c8f82788 100644 --- a/briar-core/src/org/briarproject/introduction/MessageEncoder.java +++ b/briar-core/src/org/briarproject/introduction/MessageSender.java @@ -1,11 +1,25 @@ package org.briarproject.introduction; import org.briarproject.api.FormatException; +import org.briarproject.api.clients.ClientHelper; +import org.briarproject.api.clients.MessageQueueManager; import org.briarproject.api.data.BdfDictionary; import org.briarproject.api.data.BdfList; +import org.briarproject.api.data.MetadataEncoder; +import org.briarproject.api.db.DatabaseComponent; +import org.briarproject.api.db.DbException; +import org.briarproject.api.db.Metadata; +import org.briarproject.api.db.Transaction; +import org.briarproject.api.sync.Group; +import org.briarproject.api.sync.GroupId; +import org.briarproject.api.system.Clock; + +import javax.inject.Inject; import static org.briarproject.api.introduction.IntroductionConstants.ACCEPT; import static org.briarproject.api.introduction.IntroductionConstants.E_PUBLIC_KEY; +import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID; +import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_TIME; import static org.briarproject.api.introduction.IntroductionConstants.MSG; import static org.briarproject.api.introduction.IntroductionConstants.NAME; import static org.briarproject.api.introduction.IntroductionConstants.PUBLIC_KEY; @@ -18,9 +32,43 @@ import static org.briarproject.api.introduction.IntroductionConstants.TYPE_ACK; import static org.briarproject.api.introduction.IntroductionConstants.TYPE_REQUEST; import static org.briarproject.api.introduction.IntroductionConstants.TYPE_RESPONSE; -public class MessageEncoder { +class MessageSender { + + final private DatabaseComponent db; + final private ClientHelper clientHelper; + final private Clock clock; + final private MetadataEncoder metadataEncoder; + final private MessageQueueManager messageQueueManager; + + @Inject + MessageSender(DatabaseComponent db, ClientHelper clientHelper, Clock clock, + MetadataEncoder metadataEncoder, + MessageQueueManager messageQueueManager) { + + this.db = db; + this.clientHelper = clientHelper; + this.clock = clock; + this.metadataEncoder = metadataEncoder; + this.messageQueueManager = messageQueueManager; + } + + public void sendMessage(Transaction txn, BdfDictionary message) + throws DbException, FormatException { + + BdfList bdfList = encodeMessage(message); + byte[] body = clientHelper.toByteArray(bdfList); + GroupId groupId = new GroupId(message.getRaw(GROUP_ID)); + Group group = db.getGroup(txn, groupId); + long timestamp = clock.currentTimeMillis(); + + message.put(MESSAGE_TIME, timestamp); + Metadata metadata = metadataEncoder.encode(message); + + messageQueueManager + .sendMessage(txn, group, timestamp, body, metadata); + } - public static BdfList encodeMessage(BdfDictionary d) + private BdfList encodeMessage(BdfDictionary d) throws FormatException { BdfList body; @@ -39,7 +87,7 @@ public class MessageEncoder { return body; } - private static BdfList encodeRequest(BdfDictionary d) + private BdfList encodeRequest(BdfDictionary d) throws FormatException { BdfList list = BdfList.of(TYPE_REQUEST, d.getRaw(SESSION_ID), d.getString(NAME), d.getRaw(PUBLIC_KEY)); @@ -50,7 +98,7 @@ public class MessageEncoder { return list; } - private static BdfList encodeResponse(BdfDictionary d) + private BdfList encodeResponse(BdfDictionary d) throws FormatException { BdfList list = BdfList.of(TYPE_RESPONSE, d.getRaw(SESSION_ID), d.getBoolean(ACCEPT)); @@ -64,11 +112,11 @@ public class MessageEncoder { return list; } - private static BdfList encodeAck(BdfDictionary d) throws FormatException { + private BdfList encodeAck(BdfDictionary d) throws FormatException { return BdfList.of(TYPE_ACK, d.getRaw(SESSION_ID)); } - private static BdfList encodeAbort(BdfDictionary d) throws FormatException { + private BdfList encodeAbort(BdfDictionary d) throws FormatException { return BdfList.of(TYPE_ABORT, d.getRaw(SESSION_ID)); }