mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Protocol Buffers will be used for the wire protocol. As a quick test it's now used to serialize transport details when creating an invitation.
This commit is contained in:
@@ -15,5 +15,6 @@
|
||||
<classpathentry kind="lib" path="lib/test/hamcrest-core-1.1.jar"/>
|
||||
<classpathentry kind="lib" path="lib/test/hamcrest-library-1.1.jar"/>
|
||||
<classpathentry kind="lib" path="lib/test/jmock-2.5.1.jar"/>
|
||||
<classpathentry kind="lib" path="lib/protobuf.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
||||
@@ -59,6 +59,9 @@ public interface DatabaseComponent {
|
||||
/** Returns the set of groups to which the user subscribes. */
|
||||
Set<GroupId> getSubscriptions() throws DbException;
|
||||
|
||||
/** Returns the local transport details. */
|
||||
Map<String, String> getTransports() throws DbException;
|
||||
|
||||
/** Returns the transport details for the given contact. */
|
||||
Map<String, String> getTransports(ContactId c) throws DbException;
|
||||
|
||||
|
||||
1108
api/net/sf/briar/api/protocol/Transport.java
Normal file
1108
api/net/sf/briar/api/protocol/Transport.java
Normal file
File diff suppressed because it is too large
Load Diff
13
api/net/sf/briar/api/protocol/Transport.proto
Normal file
13
api/net/sf/briar/api/protocol/Transport.proto
Normal file
@@ -0,0 +1,13 @@
|
||||
package protocol;
|
||||
|
||||
option java_package = "net.sf.briar.api.protocol";
|
||||
|
||||
message TransportDetails {
|
||||
|
||||
message TransportDetail {
|
||||
required string key = 1;
|
||||
optional string value = 2;
|
||||
}
|
||||
|
||||
repeated TransportDetail details = 1;
|
||||
}
|
||||
@@ -1062,6 +1062,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " WHERE contactSubscriptions.contactId = ?"
|
||||
+ " AND statuses.contactId = ? AND status = ?"
|
||||
+ " AND sendability > ZERO()";
|
||||
// FIXME: Investigate the performance impact of "ORDER BY timestamp"
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setInt(1, c.getInt());
|
||||
ps.setInt(2, c.getInt());
|
||||
|
||||
@@ -393,6 +393,23 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getTransports() throws DbException {
|
||||
transportLock.readLock().lock();
|
||||
try {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
Map<String, String> transports = db.getTransports(txn);
|
||||
db.commitTransaction(txn);
|
||||
return transports;
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
} finally {
|
||||
transportLock.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getTransports(ContactId c) throws DbException {
|
||||
contactLock.readLock().lock();
|
||||
try {
|
||||
|
||||
@@ -296,6 +296,20 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getTransports() throws DbException {
|
||||
synchronized(transportLock) {
|
||||
Txn txn = db.startTransaction();
|
||||
try {
|
||||
Map<String, String> transports = db.getTransports(txn);
|
||||
db.commitTransaction(txn);
|
||||
return transports;
|
||||
} catch(DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> getTransports(ContactId c) throws DbException {
|
||||
synchronized(contactLock) {
|
||||
if(!containsContact(c)) throw new NoSuchContactException();
|
||||
|
||||
@@ -6,21 +6,29 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.invitation.InvitationCallback;
|
||||
import net.sf.briar.api.invitation.InvitationParameters;
|
||||
import net.sf.briar.api.protocol.Transport.TransportDetails;
|
||||
import net.sf.briar.api.protocol.Transport.TransportDetails.TransportDetail;
|
||||
import net.sf.briar.util.FileUtils;
|
||||
|
||||
class InvitationWorker implements Runnable {
|
||||
|
||||
private final InvitationCallback callback;
|
||||
private final InvitationParameters parameters;
|
||||
private final DatabaseComponent databaseComponent;
|
||||
|
||||
InvitationWorker(final InvitationCallback callback,
|
||||
InvitationParameters parameters) {
|
||||
InvitationParameters parameters,
|
||||
DatabaseComponent databaseComponent) {
|
||||
this.callback = callback;
|
||||
this.parameters = parameters;
|
||||
this.databaseComponent = databaseComponent;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@@ -60,16 +68,25 @@ class InvitationWorker implements Runnable {
|
||||
File invitationDat = new File(dir, "invitation.dat");
|
||||
callback.encryptingFile(invitationDat);
|
||||
// FIXME: Create a real invitation
|
||||
Map<String, String> transports;
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch(InterruptedException ignored) {}
|
||||
Arrays.fill(password, (char) 0);
|
||||
transports = databaseComponent.getTransports();
|
||||
} catch(DbException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
TransportDetails.Builder b = TransportDetails.newBuilder();
|
||||
for(Entry<String, String> e : transports.entrySet()) {
|
||||
TransportDetail.Builder b1 = TransportDetail.newBuilder();
|
||||
b1.setKey(e.getKey());
|
||||
b1.setValue(e.getValue());
|
||||
b.addDetails(b1.build());
|
||||
}
|
||||
TransportDetails t = b.build();
|
||||
FileOutputStream out = new FileOutputStream(invitationDat);
|
||||
byte[] buf = new byte[1024];
|
||||
new Random().nextBytes(buf);
|
||||
out.write(buf, 0, buf.length);
|
||||
t.writeTo(out);
|
||||
out.flush();
|
||||
out.close();
|
||||
Arrays.fill(password, (char) 0);
|
||||
return invitationDat;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
package net.sf.briar.invitation;
|
||||
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.invitation.InvitationCallback;
|
||||
import net.sf.briar.api.invitation.InvitationParameters;
|
||||
import net.sf.briar.api.invitation.InvitationWorkerFactory;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
class InvitationWorkerFactoryImpl implements InvitationWorkerFactory {
|
||||
|
||||
private final DatabaseComponent databaseComponent;
|
||||
|
||||
@Inject
|
||||
InvitationWorkerFactoryImpl(DatabaseComponent databaseComponent) {
|
||||
this.databaseComponent = databaseComponent;
|
||||
}
|
||||
|
||||
public Runnable createWorker(InvitationCallback callback,
|
||||
InvitationParameters parameters) {
|
||||
return new InvitationWorker(callback, parameters);
|
||||
return new InvitationWorker(callback, parameters, databaseComponent);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
lib/protobuf.jar
Normal file
BIN
lib/protobuf.jar
Normal file
Binary file not shown.
@@ -3,10 +3,13 @@ package net.sf.briar.invitation;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.briar.TestUtils;
|
||||
import net.sf.briar.api.db.DatabaseComponent;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.api.invitation.InvitationCallback;
|
||||
import net.sf.briar.api.invitation.InvitationParameters;
|
||||
|
||||
@@ -33,13 +36,15 @@ public class InvitationWorkerTest extends TestCase {
|
||||
context.mock(InvitationCallback.class);
|
||||
final InvitationParameters params =
|
||||
context.mock(InvitationParameters.class);
|
||||
final DatabaseComponent database =
|
||||
context.mock(DatabaseComponent.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(params).getChosenLocation();
|
||||
will(returnValue(nonExistent));
|
||||
oneOf(callback).notFound(nonExistent);
|
||||
}});
|
||||
|
||||
new InvitationWorker(callback, params).run();
|
||||
new InvitationWorker(callback, params, database).run();
|
||||
|
||||
context.assertIsSatisfied();
|
||||
File[] children = testDir.listFiles();
|
||||
@@ -57,13 +62,15 @@ public class InvitationWorkerTest extends TestCase {
|
||||
context.mock(InvitationCallback.class);
|
||||
final InvitationParameters params =
|
||||
context.mock(InvitationParameters.class);
|
||||
final DatabaseComponent database =
|
||||
context.mock(DatabaseComponent.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(params).getChosenLocation();
|
||||
will(returnValue(exists));
|
||||
oneOf(callback).notDirectory(exists);
|
||||
}});
|
||||
|
||||
new InvitationWorker(callback, params).run();
|
||||
new InvitationWorker(callback, params, database).run();
|
||||
|
||||
context.assertIsSatisfied();
|
||||
File[] children = testDir.listFiles();
|
||||
@@ -73,27 +80,27 @@ public class InvitationWorkerTest extends TestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatesExe() throws IOException {
|
||||
public void testCreatesExe() throws IOException, DbException {
|
||||
testInstallerCreation(true, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatesJar() throws IOException {
|
||||
public void testCreatesJar() throws IOException, DbException {
|
||||
testInstallerCreation(false, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatesBoth() throws IOException {
|
||||
public void testCreatesBoth() throws IOException, DbException {
|
||||
testInstallerCreation(true, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatesNeither() throws IOException {
|
||||
public void testCreatesNeither() throws IOException, DbException {
|
||||
testInstallerCreation(false, false);
|
||||
}
|
||||
|
||||
private void testInstallerCreation(final boolean createExe,
|
||||
final boolean createJar) throws IOException {
|
||||
final boolean createJar) throws IOException, DbException {
|
||||
final File setup = new File(testDir, "setup.dat");
|
||||
TestUtils.createFile(setup, "foo bar baz");
|
||||
final File invitation = new File(testDir, "invitation.dat");
|
||||
@@ -112,6 +119,8 @@ public class InvitationWorkerTest extends TestCase {
|
||||
context.mock(InvitationCallback.class);
|
||||
final InvitationParameters params =
|
||||
context.mock(InvitationParameters.class);
|
||||
final DatabaseComponent database =
|
||||
context.mock(DatabaseComponent.class);
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(params).getChosenLocation();
|
||||
will(returnValue(testDir));
|
||||
@@ -120,6 +129,8 @@ public class InvitationWorkerTest extends TestCase {
|
||||
oneOf(params).getPassword();
|
||||
will(returnValue(new char[] {'x', 'y', 'z', 'z', 'y'}));
|
||||
oneOf(callback).encryptingFile(invitation);
|
||||
oneOf(database).getTransports();
|
||||
will(returnValue(Collections.singletonMap("foo", "bar")));
|
||||
oneOf(params).shouldCreateExe();
|
||||
will(returnValue(createExe));
|
||||
oneOf(params).shouldCreateJar();
|
||||
@@ -137,7 +148,7 @@ public class InvitationWorkerTest extends TestCase {
|
||||
oneOf(callback).created(expectedFiles);
|
||||
}});
|
||||
|
||||
new InvitationWorker(callback, params).run();
|
||||
new InvitationWorker(callback, params, database).run();
|
||||
|
||||
assertTrue(invitation.exists());
|
||||
assertEquals(createExe, exe.exists());
|
||||
|
||||
Reference in New Issue
Block a user