mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Added names to contacts, created Contact class in API.
This commit is contained in:
@@ -4,6 +4,7 @@ import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.briar.api.Contact;
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.Rating;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
@@ -77,11 +78,12 @@ interface Database<T> {
|
||||
void commitTransaction(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Adds a new contact to the database and returns an ID for the contact.
|
||||
* Adds a contact with the given name to the database and returns an ID for
|
||||
* the contact.
|
||||
* <p>
|
||||
* Locking: contact write, subscription write.
|
||||
*/
|
||||
ContactId addContact(T txn) throws DbException;
|
||||
ContactId addContact(T txn, String name) throws DbException;
|
||||
|
||||
/**
|
||||
* Adds an endpoint to the database.
|
||||
@@ -203,7 +205,14 @@ interface Database<T> {
|
||||
* <p>
|
||||
* Locking: contact read.
|
||||
*/
|
||||
Collection<ContactId> getContacts(T txn) throws DbException;
|
||||
Collection<ContactId> getContactIds(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all contacts.
|
||||
* <p>
|
||||
* Locking: contact read.
|
||||
*/
|
||||
Collection<Contact> getContacts(T txn) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns all endpoints.
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.api.Contact;
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.Rating;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
@@ -171,7 +172,7 @@ DatabaseCleaner.Callback {
|
||||
listeners.remove(d);
|
||||
}
|
||||
|
||||
public ContactId addContact() throws DbException {
|
||||
public ContactId addContact(String name) throws DbException {
|
||||
ContactId c;
|
||||
contactLock.writeLock().lock();
|
||||
try {
|
||||
@@ -179,7 +180,7 @@ DatabaseCleaner.Callback {
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
c = db.addContact(txn);
|
||||
c = db.addContact(txn, name);
|
||||
db.commitTransaction(txn);
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
@@ -279,7 +280,7 @@ DatabaseCleaner.Callback {
|
||||
if(sender != null) db.addStatus(txn, sender, id, true);
|
||||
if(stored) {
|
||||
// Mark the message as unseen by other contacts
|
||||
for(ContactId c : db.getContacts(txn)) {
|
||||
for(ContactId c : db.getContactIds(txn)) {
|
||||
if(!c.equals(sender)) db.addStatus(txn, c, id, false);
|
||||
}
|
||||
// Calculate and store the message's sendability
|
||||
@@ -801,12 +802,12 @@ DatabaseCleaner.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<ContactId> getContacts() throws DbException {
|
||||
public Collection<Contact> getContacts() throws DbException {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
Collection<ContactId> contacts = db.getContacts(txn);
|
||||
Collection<Contact> contacts = db.getContacts(txn);
|
||||
db.commitTransaction(txn);
|
||||
return contacts;
|
||||
} catch(DbException e) {
|
||||
@@ -1691,7 +1692,7 @@ DatabaseCleaner.Callback {
|
||||
HashSet<ContactId> oldVisible =
|
||||
new HashSet<ContactId>(db.getVisibility(txn, g));
|
||||
// Set the group's visibility for each current contact
|
||||
for(ContactId c : db.getContacts(txn)) {
|
||||
for(ContactId c : db.getContactIds(txn)) {
|
||||
boolean then = oldVisible.contains(c);
|
||||
boolean now = newVisible.contains(c);
|
||||
if(!then && now) {
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.api.Contact;
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.Rating;
|
||||
import net.sf.briar.api.TransportConfig;
|
||||
@@ -58,7 +59,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
// Locking: contact
|
||||
private static final String CREATE_CONTACTS =
|
||||
"CREATE TABLE contacts (contactId COUNTER)";
|
||||
"CREATE TABLE contacts "
|
||||
+ " (contactId COUNTER,"
|
||||
+ " name VARCHAR NOT NULL,"
|
||||
+ " PRIMARY KEY (contactId))";
|
||||
|
||||
// Locking: subscription
|
||||
private static final String CREATE_GROUPS =
|
||||
@@ -492,13 +496,15 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
if(interrupted) Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
public ContactId addContact(Connection txn) throws DbException {
|
||||
public ContactId addContact(Connection txn, String name)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
// Create a contact row
|
||||
String sql = "INSERT INTO contacts DEFAULT VALUES";
|
||||
String sql = "INSERT INTO contacts (name) VALUES (?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, name);
|
||||
int affected = ps.executeUpdate();
|
||||
if(affected != 1) throw new DbStateException();
|
||||
ps.close();
|
||||
@@ -1001,7 +1007,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<ContactId> getContacts(Connection txn)
|
||||
public Collection<ContactId> getContactIds(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
@@ -1021,6 +1027,30 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Contact> getContacts(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT contactId, name FROM contacts";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
List<Contact> contacts = new ArrayList<Contact>();
|
||||
while(rs.next()) {
|
||||
ContactId id = new ContactId(rs.getInt(1));
|
||||
String name = rs.getString(2);
|
||||
contacts.add(new Contact(id, name));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
return Collections.unmodifiableList(contacts);
|
||||
} catch(SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Endpoint> getEndpoints(Connection txn)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
|
||||
Reference in New Issue
Block a user