@Test public void testReceiveOffer() throws Exception { MessageId messageId1 = new MessageId(getRandomId()); MessageId messageId2 = new MessageId(getRandomId()); MessageId messageId3 = new MessageId(getRandomId()); context.checking(new Expectations() {{ oneOf(database).startTransaction(); will(returnValue(txn)); oneOf(database).containsContact(txn, contactId); will(returnValue(true)); oneOf(database).countOfferedMessages(txn, contactId); will(returnValue(MAX_OFFERED_MESSAGES - 2)); oneOf(database).containsVisibleMessage(txn, contactId, messageId); will(returnValue(false)); oneOf(database).addOfferedMessage(txn, contactId, messageId); oneOf(database).containsVisibleMessage(txn, contactId, messageId1); will(returnValue(true)); oneOf(database).raiseSeenFlag(txn, contactId, messageId1); oneOf(database).raiseAckFlag(txn, contactId, messageId1); oneOf(database).containsVisibleMessage(txn, contactId, messageId2); will(returnValue(false)); oneOf(database).addOfferedMessage(txn, contactId, messageId2); oneOf(database).containsVisibleMessage(txn, contactId, messageId3); will(returnValue(false)); oneOf(database).commitTransaction(txn); oneOf(eventBus).broadcast(with(any(MessageToAckEvent.class))); oneOf(eventBus).broadcast(with(any(MessageToRequestEvent.class))); }}); DatabaseComponent db = createDatabaseComponent(database, eventBus, shutdown); Transaction transaction = db.startTransaction(false); try { Offer o = new Offer(Arrays.asList(messageId, messageId1, messageId2, messageId3)); db.receiveOffer(transaction, contactId, o); db.commitTransaction(transaction); } finally { db.endTransaction(transaction); } }
@Override public void receiveOffer(Transaction transaction, ContactId c, Offer o) throws DbException { if (transaction.isReadOnly()) throw new IllegalArgumentException(); T txn = unbox(transaction); if (!db.containsContact(txn, c)) throw new NoSuchContactException(); boolean ack = false, request = false; int count = db.countOfferedMessages(txn, c); for (MessageId m : o.getMessageIds()) { if (db.containsVisibleMessage(txn, c, m)) { db.raiseSeenFlag(txn, c, m); db.raiseAckFlag(txn, c, m); ack = true; } else if (count < MAX_OFFERED_MESSAGES) { db.addOfferedMessage(txn, c, m); request = true; count++; } } if (ack) transaction.attach(new MessageToAckEvent(c)); if (request) transaction.attach(new MessageToRequestEvent(c)); }
DatabaseComponentImpl implements DatabaseComponent { @Override public void receiveOffer(Transaction transaction, ContactId c, Offer o) throws DbException { if (transaction.isReadOnly()) throw new IllegalArgumentException(); T txn = unbox(transaction); if (!db.containsContact(txn, c)) throw new NoSuchContactException(); boolean ack = false, request = false; int count = db.countOfferedMessages(txn, c); for (MessageId m : o.getMessageIds()) { if (db.containsVisibleMessage(txn, c, m)) { db.raiseSeenFlag(txn, c, m); db.raiseAckFlag(txn, c, m); ack = true; } else if (count < MAX_OFFERED_MESSAGES) { db.addOfferedMessage(txn, c, m); request = true; count++; } } if (ack) transaction.attach(new MessageToAckEvent(c)); if (request) transaction.attach(new MessageToRequestEvent(c)); } }
DatabaseComponentImpl implements DatabaseComponent { @Override public void receiveOffer(Transaction transaction, ContactId c, Offer o) throws DbException { if (transaction.isReadOnly()) throw new IllegalArgumentException(); T txn = unbox(transaction); if (!db.containsContact(txn, c)) throw new NoSuchContactException(); boolean ack = false, request = false; int count = db.countOfferedMessages(txn, c); for (MessageId m : o.getMessageIds()) { if (db.containsVisibleMessage(txn, c, m)) { db.raiseSeenFlag(txn, c, m); db.raiseAckFlag(txn, c, m); ack = true; } else if (count < MAX_OFFERED_MESSAGES) { db.addOfferedMessage(txn, c, m); request = true; count++; } } if (ack) transaction.attach(new MessageToAckEvent(c)); if (request) transaction.attach(new MessageToRequestEvent(c)); } @Inject  DatabaseComponentImpl(Database<T> db, Class<T> txnClass, EventBus eventBus,
			ShutdownManager shutdown); }
DatabaseComponentImpl implements DatabaseComponent { @Override public void receiveOffer(Transaction transaction, ContactId c, Offer o) throws DbException { if (transaction.isReadOnly()) throw new IllegalArgumentException(); T txn = unbox(transaction); if (!db.containsContact(txn, c)) throw new NoSuchContactException(); boolean ack = false, request = false; int count = db.countOfferedMessages(txn, c); for (MessageId m : o.getMessageIds()) { if (db.containsVisibleMessage(txn, c, m)) { db.raiseSeenFlag(txn, c, m); db.raiseAckFlag(txn, c, m); ack = true; } else if (count < MAX_OFFERED_MESSAGES) { db.addOfferedMessage(txn, c, m); request = true; count++; } } if (ack) transaction.attach(new MessageToAckEvent(c)); if (request) transaction.attach(new MessageToRequestEvent(c)); } @Inject  DatabaseComponentImpl(Database<T> db, Class<T> txnClass, EventBus eventBus,
			ShutdownManager shutdown); @Override boolean open(SecretKey key, @Nullable MigrationListener listener); @Override void close(); @Override Transaction startTransaction(boolean readOnly); @Override void commitTransaction(Transaction transaction); @Override void endTransaction(Transaction transaction); @Override ContactId addContact(Transaction transaction, Author remote,
			AuthorId local, boolean verified, boolean active); @Override void addGroup(Transaction transaction, Group g); @Override void addLocalAuthor(Transaction transaction, LocalAuthor a); @Override void addLocalMessage(Transaction transaction, Message m,
			Metadata meta, boolean shared); @Override void addTransport(Transaction transaction, TransportId t,
			int maxLatency); @Override KeySetId addTransportKeys(Transaction transaction, ContactId c,
			TransportKeys k); @Override boolean containsContact(Transaction transaction, AuthorId remote,
			AuthorId local); @Override boolean containsGroup(Transaction transaction, GroupId g); @Override boolean containsLocalAuthor(Transaction transaction, AuthorId local); @Override void deleteMessage(Transaction transaction, MessageId m); @Override void deleteMessageMetadata(Transaction transaction, MessageId m); @Nullable @Override Ack generateAck(Transaction transaction, ContactId c,
			int maxMessages); @Nullable @Override Collection<byte[]> generateBatch(Transaction transaction,
			ContactId c, int maxLength, int maxLatency); @Nullable @Override Offer generateOffer(Transaction transaction, ContactId c,
			int maxMessages, int maxLatency); @Nullable @Override Request generateRequest(Transaction transaction, ContactId c,
			int maxMessages); @Nullable @Override Collection<byte[]> generateRequestedBatch(Transaction transaction,
			ContactId c, int maxLength, int maxLatency); @Override Contact getContact(Transaction transaction, ContactId c); @Override Collection<Contact> getContacts(Transaction transaction); @Override Collection<Contact> getContactsByAuthorId(Transaction transaction,
			AuthorId remote); @Override Collection<ContactId> getContacts(Transaction transaction,
			AuthorId a); @Override Group getGroup(Transaction transaction, GroupId g); @Override Metadata getGroupMetadata(Transaction transaction, GroupId g); @Override Collection<Group> getGroups(Transaction transaction, ClientId c,
			int majorVersion); @Override Visibility getGroupVisibility(Transaction transaction, ContactId c,
			GroupId g); @Override LocalAuthor getLocalAuthor(Transaction transaction, AuthorId a); @Override Collection<LocalAuthor> getLocalAuthors(Transaction transaction); @Override Collection<MessageId> getMessageIds(Transaction transaction,
			GroupId g); @Override Collection<MessageId> getMessagesToValidate(Transaction transaction); @Override Collection<MessageId> getPendingMessages(Transaction transaction); @Override Collection<MessageId> getMessagesToShare(Transaction transaction); @Nullable @Override byte[] getRawMessage(Transaction transaction, MessageId m); @Override Map<MessageId, Metadata> getMessageMetadata(Transaction transaction,
			GroupId g); @Override Map<MessageId, Metadata> getMessageMetadata(Transaction transaction,
			GroupId g, Metadata query); @Override Metadata getMessageMetadata(Transaction transaction, MessageId m); @Override Metadata getMessageMetadataForValidator(Transaction transaction,
			MessageId m); @Override State getMessageState(Transaction transaction, MessageId m); @Override Collection<MessageStatus> getMessageStatus(Transaction transaction,
			ContactId c, GroupId g); @Override MessageStatus getMessageStatus(Transaction transaction, ContactId c,
			MessageId m); @Override Map<MessageId, State> getMessageDependencies(Transaction transaction,
			MessageId m); @Override Map<MessageId, State> getMessageDependents(Transaction transaction,
			MessageId m); @Override long getNextSendTime(Transaction transaction, ContactId c); @Override Settings getSettings(Transaction transaction, String namespace); @Override Collection<KeySet> getTransportKeys(Transaction transaction,
			TransportId t); @Override void incrementStreamCounter(Transaction transaction, TransportId t,
			KeySetId k); @Override void mergeGroupMetadata(Transaction transaction, GroupId g,
			Metadata meta); @Override void mergeMessageMetadata(Transaction transaction, MessageId m,
			Metadata meta); @Override void mergeSettings(Transaction transaction, Settings s,
			String namespace); @Override void receiveAck(Transaction transaction, ContactId c, Ack a); @Override void receiveMessage(Transaction transaction, ContactId c, Message m); @Override void receiveOffer(Transaction transaction, ContactId c, Offer o); @Override void receiveRequest(Transaction transaction, ContactId c, Request r); @Override void removeContact(Transaction transaction, ContactId c); @Override void removeGroup(Transaction transaction, Group g); @Override void removeLocalAuthor(Transaction transaction, AuthorId a); @Override void removeMessage(Transaction transaction, MessageId m); @Override void removeTransport(Transaction transaction, TransportId t); @Override void removeTransportKeys(Transaction transaction,
			TransportId t, KeySetId k); @Override void setContactVerified(Transaction transaction, ContactId c); @Override void setContactActive(Transaction transaction, ContactId c,
			boolean active); @Override void setGroupVisibility(Transaction transaction, ContactId c,
			GroupId g, Visibility v); @Override void setMessageShared(Transaction transaction, MessageId m); @Override void setMessageState(Transaction transaction, MessageId m,
			State state); @Override void addMessageDependencies(Transaction transaction,
			Message dependent, Collection<MessageId> dependencies); @Override void setReorderingWindow(Transaction transaction, KeySetId k,
			TransportId t, long rotationPeriod, long base, byte[] bitmap); @Override void setTransportKeysActive(Transaction transaction, TransportId t,
			KeySetId k); @Override void updateTransportKeys(Transaction transaction,
			Collection<KeySet> keys); }
DatabaseComponentImpl implements DatabaseComponent { @Override public void receiveOffer(Transaction transaction, ContactId c, Offer o) throws DbException { if (transaction.isReadOnly()) throw new IllegalArgumentException(); T txn = unbox(transaction); if (!db.containsContact(txn, c)) throw new NoSuchContactException(); boolean ack = false, request = false; int count = db.countOfferedMessages(txn, c); for (MessageId m : o.getMessageIds()) { if (db.containsVisibleMessage(txn, c, m)) { db.raiseSeenFlag(txn, c, m); db.raiseAckFlag(txn, c, m); ack = true; } else if (count < MAX_OFFERED_MESSAGES) { db.addOfferedMessage(txn, c, m); request = true; count++; } } if (ack) transaction.attach(new MessageToAckEvent(c)); if (request) transaction.attach(new MessageToRequestEvent(c)); } @Inject  DatabaseComponentImpl(Database<T> db, Class<T> txnClass, EventBus eventBus,
			ShutdownManager shutdown); @Override boolean open(SecretKey key, @Nullable MigrationListener listener); @Override void close(); @Override Transaction startTransaction(boolean readOnly); @Override void commitTransaction(Transaction transaction); @Override void endTransaction(Transaction transaction); @Override ContactId addContact(Transaction transaction, Author remote,
			AuthorId local, boolean verified, boolean active); @Override void addGroup(Transaction transaction, Group g); @Override void addLocalAuthor(Transaction transaction, LocalAuthor a); @Override void addLocalMessage(Transaction transaction, Message m,
			Metadata meta, boolean shared); @Override void addTransport(Transaction transaction, TransportId t,
			int maxLatency); @Override KeySetId addTransportKeys(Transaction transaction, ContactId c,
			TransportKeys k); @Override boolean containsContact(Transaction transaction, AuthorId remote,
			AuthorId local); @Override boolean containsGroup(Transaction transaction, GroupId g); @Override boolean containsLocalAuthor(Transaction transaction, AuthorId local); @Override void deleteMessage(Transaction transaction, MessageId m); @Override void deleteMessageMetadata(Transaction transaction, MessageId m); @Nullable @Override Ack generateAck(Transaction transaction, ContactId c,
			int maxMessages); @Nullable @Override Collection<byte[]> generateBatch(Transaction transaction,
			ContactId c, int maxLength, int maxLatency); @Nullable @Override Offer generateOffer(Transaction transaction, ContactId c,
			int maxMessages, int maxLatency); @Nullable @Override Request generateRequest(Transaction transaction, ContactId c,
			int maxMessages); @Nullable @Override Collection<byte[]> generateRequestedBatch(Transaction transaction,
			ContactId c, int maxLength, int maxLatency); @Override Contact getContact(Transaction transaction, ContactId c); @Override Collection<Contact> getContacts(Transaction transaction); @Override Collection<Contact> getContactsByAuthorId(Transaction transaction,
			AuthorId remote); @Override Collection<ContactId> getContacts(Transaction transaction,
			AuthorId a); @Override Group getGroup(Transaction transaction, GroupId g); @Override Metadata getGroupMetadata(Transaction transaction, GroupId g); @Override Collection<Group> getGroups(Transaction transaction, ClientId c,
			int majorVersion); @Override Visibility getGroupVisibility(Transaction transaction, ContactId c,
			GroupId g); @Override LocalAuthor getLocalAuthor(Transaction transaction, AuthorId a); @Override Collection<LocalAuthor> getLocalAuthors(Transaction transaction); @Override Collection<MessageId> getMessageIds(Transaction transaction,
			GroupId g); @Override Collection<MessageId> getMessagesToValidate(Transaction transaction); @Override Collection<MessageId> getPendingMessages(Transaction transaction); @Override Collection<MessageId> getMessagesToShare(Transaction transaction); @Nullable @Override byte[] getRawMessage(Transaction transaction, MessageId m); @Override Map<MessageId, Metadata> getMessageMetadata(Transaction transaction,
			GroupId g); @Override Map<MessageId, Metadata> getMessageMetadata(Transaction transaction,
			GroupId g, Metadata query); @Override Metadata getMessageMetadata(Transaction transaction, MessageId m); @Override Metadata getMessageMetadataForValidator(Transaction transaction,
			MessageId m); @Override State getMessageState(Transaction transaction, MessageId m); @Override Collection<MessageStatus> getMessageStatus(Transaction transaction,
			ContactId c, GroupId g); @Override MessageStatus getMessageStatus(Transaction transaction, ContactId c,
			MessageId m); @Override Map<MessageId, State> getMessageDependencies(Transaction transaction,
			MessageId m); @Override Map<MessageId, State> getMessageDependents(Transaction transaction,
			MessageId m); @Override long getNextSendTime(Transaction transaction, ContactId c); @Override Settings getSettings(Transaction transaction, String namespace); @Override Collection<KeySet> getTransportKeys(Transaction transaction,
			TransportId t); @Override void incrementStreamCounter(Transaction transaction, TransportId t,
			KeySetId k); @Override void mergeGroupMetadata(Transaction transaction, GroupId g,
			Metadata meta); @Override void mergeMessageMetadata(Transaction transaction, MessageId m,
			Metadata meta); @Override void mergeSettings(Transaction transaction, Settings s,
			String namespace); @Override void receiveAck(Transaction transaction, ContactId c, Ack a); @Override void receiveMessage(Transaction transaction, ContactId c, Message m); @Override void receiveOffer(Transaction transaction, ContactId c, Offer o); @Override void receiveRequest(Transaction transaction, ContactId c, Request r); @Override void removeContact(Transaction transaction, ContactId c); @Override void removeGroup(Transaction transaction, Group g); @Override void removeLocalAuthor(Transaction transaction, AuthorId a); @Override void removeMessage(Transaction transaction, MessageId m); @Override void removeTransport(Transaction transaction, TransportId t); @Override void removeTransportKeys(Transaction transaction,
			TransportId t, KeySetId k); @Override void setContactVerified(Transaction transaction, ContactId c); @Override void setContactActive(Transaction transaction, ContactId c,
			boolean active); @Override void setGroupVisibility(Transaction transaction, ContactId c,
			GroupId g, Visibility v); @Override void setMessageShared(Transaction transaction, MessageId m); @Override void setMessageState(Transaction transaction, MessageId m,
			State state); @Override void addMessageDependencies(Transaction transaction,
			Message dependent, Collection<MessageId> dependencies); @Override void setReorderingWindow(Transaction transaction, KeySetId k,
			TransportId t, long rotationPeriod, long base, byte[] bitmap); @Override void setTransportKeysActive(Transaction transaction, TransportId t,
			KeySetId k); @Override void updateTransportKeys(Transaction transaction,
			Collection<KeySet> keys);  }