diff --git a/briar-api/src/org/briarproject/api/properties/TransportPropertyManager.java b/briar-api/src/org/briarproject/api/properties/TransportPropertyManager.java index 0e8b76d68f9281b89391913a3324385db23ab431..9dc655543950ec88500749ffc6c0d68c177d0cb9 100644 --- a/briar-api/src/org/briarproject/api/properties/TransportPropertyManager.java +++ b/briar-api/src/org/briarproject/api/properties/TransportPropertyManager.java @@ -1,5 +1,6 @@ package org.briarproject.api.properties; +import org.briarproject.api.DeviceId; import org.briarproject.api.TransportId; import org.briarproject.api.contact.ContactId; import org.briarproject.api.db.DbException; @@ -8,6 +9,13 @@ import java.util.Map; public interface TransportPropertyManager { + /** + * Stores the given properties received while adding a contact - they will + * be superseded by any properties synced from the contact. + */ + void addRemoteProperties(ContactId c, DeviceId dev, + Map<TransportId, TransportProperties> props) throws DbException; + /** Returns the local transport properties for all transports. */ Map<TransportId, TransportProperties> getLocalProperties() throws DbException; diff --git a/briar-core/src/org/briarproject/properties/TransportPropertyManagerImpl.java b/briar-core/src/org/briarproject/properties/TransportPropertyManagerImpl.java index 9d42269ada92b2e8c8f643e261d617fd5570300b..1df75db183f034f588f0184a951ecc8143226020 100644 --- a/briar-core/src/org/briarproject/properties/TransportPropertyManagerImpl.java +++ b/briar-core/src/org/briarproject/properties/TransportPropertyManagerImpl.java @@ -103,8 +103,10 @@ class TransportPropertyManagerImpl implements TransportPropertyManager, // Copy the latest local properties into the group DeviceId dev = db.getDeviceId(); Map<TransportId, TransportProperties> local = getLocalProperties(); - for (Entry<TransportId, TransportProperties> e : local.entrySet()) - storeMessage(g.getId(), dev, e.getKey(), e.getValue(), 0); + for (Entry<TransportId, TransportProperties> e : local.entrySet()) { + storeMessage(g.getId(), dev, e.getKey(), e.getValue(), 1, true, + true); + } } catch (DbException e) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); } catch (FormatException e) { @@ -121,16 +123,16 @@ class TransportPropertyManagerImpl implements TransportPropertyManager, } private void storeMessage(GroupId g, DeviceId dev, TransportId t, - TransportProperties p, long version) throws DbException, - IOException { + TransportProperties p, long version, boolean local, boolean shared) + throws DbException, IOException { byte[] body = encodeProperties(dev, t, p, version); long now = clock.currentTimeMillis(); Message m = messageFactory.createMessage(g, now, body); BdfDictionary d = new BdfDictionary(); d.put("transportId", t.getString()); d.put("version", version); - d.put("local", true); - db.addLocalMessage(m, CLIENT_ID, metadataEncoder.encode(d), true); + d.put("local", local); + db.addLocalMessage(m, CLIENT_ID, metadataEncoder.encode(d), shared); } private byte[] encodeProperties(DeviceId dev, TransportId t, @@ -163,6 +165,23 @@ class TransportPropertyManagerImpl implements TransportPropertyManager, } } + @Override + public void addRemoteProperties(ContactId c, DeviceId dev, + Map<TransportId, TransportProperties> props) throws DbException { + lock.writeLock().lock(); + try { + Group g = getContactGroup(db.getContact(c)); + for (Entry<TransportId, TransportProperties> e : props.entrySet()) { + storeMessage(g.getId(), dev, e.getKey(), e.getValue(), 0, false, + false); + } + } catch (IOException e) { + throw new DbException(e); + } finally { + lock.writeLock().unlock(); + } + } + @Override public Map<TransportId, TransportProperties> getLocalProperties() throws DbException { @@ -292,14 +311,15 @@ class TransportPropertyManagerImpl implements TransportPropertyManager, } // Store the merged properties in the local group DeviceId dev = db.getDeviceId(); - long version = latest == null ? 0 : latest.version + 1; - storeMessage(localGroup.getId(), dev, t, merged, version); + long version = latest == null ? 1 : latest.version + 1; + storeMessage(localGroup.getId(), dev, t, merged, version, true, + false); // Store the merged properties in each contact's group for (Contact c : db.getContacts()) { Group g = getContactGroup(c); latest = findLatest(g.getId(), true).get(t); - version = latest == null ? 0 : latest.version + 1; - storeMessage(g.getId(), dev, t, merged, version); + version = latest == null ? 1 : latest.version + 1; + storeMessage(g.getId(), dev, t, merged, version, true, true); } } catch (IOException e) { throw new DbException(e);