mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 21:29:54 +01:00
Code formatting and small cleanups.
This commit is contained in:
@@ -25,8 +25,8 @@ class FortunaGenerator {
|
||||
private final byte[] counter = new byte[BLOCK_BYTES];
|
||||
private final byte[] buffer = new byte[BLOCK_BYTES];
|
||||
private final byte[] newKey = new byte[KEY_BYTES];
|
||||
|
||||
private final Lock synchLock = new ReentrantLock();
|
||||
|
||||
private final Lock synchLock = new ReentrantLock();
|
||||
|
||||
FortunaGenerator(byte[] seed) {
|
||||
reseed(seed);
|
||||
@@ -34,13 +34,12 @@ class FortunaGenerator {
|
||||
|
||||
void reseed(byte[] seed) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
digest.update(key);
|
||||
digest.update(seed);
|
||||
digest.digest(key, 0, KEY_BYTES);
|
||||
incrementCounter();
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
@@ -49,26 +48,24 @@ class FortunaGenerator {
|
||||
// Package access for testing
|
||||
void incrementCounter() {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
counter[0]++;
|
||||
for(int i = 0; counter[i] == 0; i++) {
|
||||
if(i + 1 == BLOCK_BYTES)
|
||||
throw new RuntimeException("Counter exhausted");
|
||||
counter[i + 1]++;
|
||||
}
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Package access for testing
|
||||
byte[] getCounter() {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
return counter;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
@@ -76,7 +73,7 @@ class FortunaGenerator {
|
||||
|
||||
int nextBytes(byte[] dest, int off, int len) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
// Don't write more than the maximum number of bytes in one request
|
||||
if(len > MAX_BYTES_PER_REQUEST) len = MAX_BYTES_PER_REQUEST;
|
||||
cipher.init(true, new KeyParameter(key));
|
||||
@@ -105,8 +102,7 @@ class FortunaGenerator {
|
||||
for(int i = 0; i < KEY_BYTES; i++) newKey[i] = 0;
|
||||
// Return the number of bytes written
|
||||
return len;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class PseudoRandomImpl implements PseudoRandom {
|
||||
|
||||
private byte[] state;
|
||||
private int offset;
|
||||
|
||||
|
||||
private final Lock synchLock = new ReentrantLock();
|
||||
|
||||
PseudoRandomImpl(MessageDigest messageDigest, int seed1, int seed2) {
|
||||
@@ -28,7 +28,7 @@ class PseudoRandomImpl implements PseudoRandom {
|
||||
|
||||
public byte[] nextBytes(int bytes) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
byte[] b = new byte[bytes];
|
||||
int half = state.length / 2;
|
||||
int off = 0, len = b.length, available = half - offset;
|
||||
@@ -44,8 +44,7 @@ class PseudoRandomImpl implements PseudoRandom {
|
||||
System.arraycopy(state, offset, b, off, len);
|
||||
offset += len;
|
||||
return b;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,7 +325,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
|
||||
protected abstract Connection createConnection() throws SQLException;
|
||||
protected abstract void flushBuffersToDisk(Statement s) throws SQLException;
|
||||
|
||||
|
||||
private final Lock connectionsLock = new ReentrantLock();
|
||||
private final Condition connectionsChanged = connectionsLock.newCondition();
|
||||
|
||||
@@ -441,11 +441,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
try {
|
||||
if(closed) throw new DbClosedException();
|
||||
txn = connections.poll();
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
connectionsLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
if(txn == null) {
|
||||
// Open a new connection
|
||||
@@ -455,8 +454,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
connectionsLock.lock();
|
||||
try {
|
||||
openConnections++;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
connectionsLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -474,8 +472,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
try {
|
||||
connections.add(txn);
|
||||
connectionsChanged.signalAll();
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
connectionsLock.unlock();
|
||||
}
|
||||
} catch(SQLException e) {
|
||||
@@ -491,10 +488,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
try {
|
||||
openConnections--;
|
||||
connectionsChanged.signalAll();
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
connectionsLock.unlock();
|
||||
} }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void commitTransaction(Connection txn) throws DbException {
|
||||
@@ -509,11 +506,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
throw new DbException(e);
|
||||
}
|
||||
connectionsLock.lock();
|
||||
try{
|
||||
try {
|
||||
connections.add(txn);
|
||||
connectionsChanged.signalAll();
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
connectionsLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -529,7 +525,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
protected void closeAllConnections() throws SQLException {
|
||||
boolean interrupted = false;
|
||||
connectionsLock.lock();
|
||||
try{
|
||||
try {
|
||||
closed = true;
|
||||
for(Connection c : connections) c.close();
|
||||
openConnections -= connections.size();
|
||||
@@ -545,11 +541,10 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
openConnections -= connections.size();
|
||||
connections.clear();
|
||||
}
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
connectionsLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
if(interrupted) Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
private final Collection<InvitationListener> listeners;
|
||||
private final AtomicBoolean connected;
|
||||
private final CountDownLatch localConfirmationLatch;
|
||||
|
||||
|
||||
private final Lock synchLock = new ReentrantLock();
|
||||
|
||||
/*The state that's accessed in addListener() after
|
||||
@@ -107,14 +107,14 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
|
||||
public InvitationState addListener(InvitationListener l) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
listeners.add(l);
|
||||
return new InvitationState(localInvitationCode, remoteInvitationCode,
|
||||
localConfirmationCode, remoteConfirmationCode, connected.get(),
|
||||
connectionFailed, localCompared, remoteCompared, localMatched,
|
||||
remoteMatched, remoteName);
|
||||
}
|
||||
finally{
|
||||
return new InvitationState(localInvitationCode,
|
||||
remoteInvitationCode, localConfirmationCode,
|
||||
remoteConfirmationCode, connected.get(), connectionFailed,
|
||||
localCompared, remoteCompared, localMatched, remoteMatched,
|
||||
remoteName);
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -140,8 +140,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
synchLock.lock();
|
||||
try {
|
||||
connectionFailed = true;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
for(InvitationListener l : listeners) l.connectionFailed();
|
||||
@@ -177,8 +176,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
synchLock.lock();
|
||||
try {
|
||||
connectionFailed = true;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
for(InvitationListener l : listeners) l.connectionFailed();
|
||||
@@ -212,8 +210,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
try {
|
||||
localCompared = true;
|
||||
localMatched = true;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
localConfirmationLatch.countDown();
|
||||
@@ -224,8 +221,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
try {
|
||||
localCompared = true;
|
||||
localMatched = false;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
localConfirmationLatch.countDown();
|
||||
@@ -243,8 +239,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
try {
|
||||
localConfirmationCode = localCode;
|
||||
remoteConfirmationCode = remoteCode;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
for(InvitationListener l : listeners)
|
||||
@@ -260,8 +255,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
synchLock.lock();
|
||||
try {
|
||||
return localMatched;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -271,8 +265,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
try {
|
||||
remoteCompared = true;
|
||||
remoteMatched = true;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
for(InvitationListener l : listeners) l.remoteConfirmationSucceeded();
|
||||
@@ -283,8 +276,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
try {
|
||||
remoteCompared = true;
|
||||
remoteMatched = false;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
for(InvitationListener l : listeners) l.remoteConfirmationFailed();
|
||||
@@ -295,8 +287,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
synchLock.lock();
|
||||
try {
|
||||
remoteName = name;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
for(InvitationListener l : listeners)
|
||||
|
||||
@@ -12,7 +12,7 @@ class ShutdownManagerImpl implements ShutdownManager {
|
||||
protected final Map<Integer, Thread> hooks;
|
||||
|
||||
private int nextHandle = 0;
|
||||
|
||||
|
||||
private final Lock synchLock = new ReentrantLock();
|
||||
|
||||
ShutdownManagerImpl() {
|
||||
@@ -21,14 +21,13 @@ class ShutdownManagerImpl implements ShutdownManager {
|
||||
|
||||
public int addShutdownHook(Runnable r) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
int handle = nextHandle++;
|
||||
Thread hook = createThread(r);
|
||||
hooks.put(handle, hook);
|
||||
Runtime.getRuntime().addShutdownHook(hook);
|
||||
return handle;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
@@ -40,12 +39,11 @@ class ShutdownManagerImpl implements ShutdownManager {
|
||||
|
||||
public boolean removeShutdownHook(int handle) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
Thread hook = hooks.remove(handle);
|
||||
if(hook == null) return false;
|
||||
else return Runtime.getRuntime().removeShutdownHook(hook);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ import org.briarproject.api.messaging.Group;
|
||||
import org.briarproject.api.messaging.GroupFactory;
|
||||
import org.briarproject.api.messaging.MessageFactory;
|
||||
import org.briarproject.api.messaging.MessageVerifier;
|
||||
import org.briarproject.api.messaging.MessagingSessionFactory;
|
||||
import org.briarproject.api.messaging.PacketReaderFactory;
|
||||
import org.briarproject.api.messaging.PacketWriterFactory;
|
||||
import org.briarproject.api.messaging.MessagingSessionFactory;
|
||||
import org.briarproject.api.messaging.SubscriptionUpdate;
|
||||
import org.briarproject.api.messaging.UnverifiedMessage;
|
||||
import org.briarproject.api.serial.StructReader;
|
||||
|
||||
@@ -31,7 +31,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
private final Map<TransportId, Map<ContactId, Integer>> connections;
|
||||
// Locking: this
|
||||
private final Map<ContactId, Integer> contactCounts;
|
||||
|
||||
|
||||
private final Lock synchLock = new ReentrantLock();
|
||||
|
||||
@Inject
|
||||
@@ -61,8 +61,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
} else {
|
||||
contactCounts.put(c, count + 1);
|
||||
}
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
@@ -94,8 +93,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
} else {
|
||||
contactCounts.put(c, count - 1);
|
||||
}
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
@@ -108,14 +106,13 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
public Collection<ContactId> getConnectedContacts(
|
||||
TransportId t) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
Map<ContactId, Integer> m = connections.get(t);
|
||||
if(m == null) return Collections.emptyList();
|
||||
List<ContactId> ids = new ArrayList<ContactId>(m.keySet());
|
||||
if(LOG.isLoggable(INFO)) LOG.info(ids.size() + " contacts connected");
|
||||
return Collections.unmodifiableList(ids);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
@@ -123,10 +120,9 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
|
||||
public boolean isConnected(ContactId c) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
return contactCounts.containsKey(c);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package org.briarproject.reliability;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
@@ -38,12 +39,12 @@ class Receiver implements ReadHandler {
|
||||
|
||||
Data read() throws IOException, InterruptedException {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
long now = clock.currentTimeMillis(), end = now + READ_TIMEOUT;
|
||||
while(now < end && valid) {
|
||||
if(dataFrames.isEmpty()) {
|
||||
// Wait for a data frame
|
||||
dataFrameAvailable.await(end - now, TimeUnit.MILLISECONDS);
|
||||
dataFrameAvailable.await(end - now, MILLISECONDS);
|
||||
} else {
|
||||
Data d = dataFrames.first();
|
||||
if(d.getSequenceNumber() == nextSequenceNumber) {
|
||||
@@ -55,15 +56,14 @@ class Receiver implements ReadHandler {
|
||||
return d;
|
||||
} else {
|
||||
// Wait for the next in-order data frame
|
||||
dataFrameAvailable.await(end - now, TimeUnit.MILLISECONDS);
|
||||
dataFrameAvailable.await(end - now, MILLISECONDS);
|
||||
}
|
||||
}
|
||||
now = clock.currentTimeMillis();
|
||||
}
|
||||
if(valid) throw new IOException("Read timed out");
|
||||
throw new IOException("Connection closed");
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -73,8 +73,7 @@ class Receiver implements ReadHandler {
|
||||
synchLock.lock();
|
||||
try {
|
||||
dataFrameAvailable.signalAll();
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -97,7 +96,7 @@ class Receiver implements ReadHandler {
|
||||
|
||||
private void handleData(byte[] b) throws IOException {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
if(b.length < Data.MIN_LENGTH || b.length > Data.MAX_LENGTH) {
|
||||
// Ignore data frame with invalid length
|
||||
return;
|
||||
@@ -134,8 +133,7 @@ class Receiver implements ReadHandler {
|
||||
}
|
||||
// Acknowledge the data frame even if it's a duplicate
|
||||
sender.sendAck(sequenceNumber, windowSize);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package org.briarproject.reliability;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
@@ -101,9 +102,9 @@ class Sender {
|
||||
// Don't accept an unreasonably large window size
|
||||
windowSize = Math.min(a.getWindowSize(), MAX_WINDOW_SIZE);
|
||||
// If space has become available, notify any waiting writers
|
||||
if(windowSize > oldWindowSize || foundIndex != -1) sendWindowAvailable.signalAll();
|
||||
}
|
||||
finally{
|
||||
if(windowSize > oldWindowSize || foundIndex != -1)
|
||||
sendWindowAvailable.signalAll();
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
// Fast retransmission
|
||||
@@ -145,8 +146,7 @@ class Sender {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
// Send a window probe if necessary
|
||||
@@ -171,7 +171,7 @@ class Sender {
|
||||
long now = clock.currentTimeMillis(), end = now + WRITE_TIMEOUT;
|
||||
while(now < end && outstandingBytes + payloadLength >= windowSize) {
|
||||
dataWaiting = true;
|
||||
sendWindowAvailable.await(end - now, TimeUnit.MILLISECONDS);
|
||||
sendWindowAvailable.await(end - now, MILLISECONDS);
|
||||
now = clock.currentTimeMillis();
|
||||
}
|
||||
if(outstandingBytes + payloadLength >= windowSize)
|
||||
@@ -179,8 +179,7 @@ class Sender {
|
||||
outstanding.add(new Outstanding(d, now));
|
||||
outstandingBytes += payloadLength;
|
||||
dataWaiting = false;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
writeHandler.handleWrite(d.getBuffer());
|
||||
@@ -188,10 +187,10 @@ class Sender {
|
||||
|
||||
void flush() throws IOException, InterruptedException {
|
||||
synchLock.lock();
|
||||
try{
|
||||
while(dataWaiting || !outstanding.isEmpty()) sendWindowAvailable.await();
|
||||
}
|
||||
finally{
|
||||
try {
|
||||
while(dataWaiting || !outstanding.isEmpty())
|
||||
sendWindowAvailable.await();
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
synchLock.lock();
|
||||
try {
|
||||
eventBus.addListener(this);
|
||||
// Load the temporary secrets and transport latencies from the database
|
||||
// Load the temporary secrets and transport latencies from the DB
|
||||
Collection<TemporarySecret> secrets;
|
||||
try {
|
||||
secrets = db.getSecrets();
|
||||
@@ -89,15 +89,18 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
}
|
||||
// Work out what phase of its lifecycle each secret is in
|
||||
long now = clock.currentTimeMillis();
|
||||
Collection<TemporarySecret> dead = assignSecretsToMaps(now, secrets);
|
||||
Collection<TemporarySecret> dead =
|
||||
assignSecretsToMaps(now, secrets);
|
||||
// Replace any dead secrets
|
||||
Collection<TemporarySecret> created = replaceDeadSecrets(now, dead);
|
||||
if(!created.isEmpty()) {
|
||||
// Store any secrets that have been created, removing any dead ones
|
||||
// Store any secrets that have been created,
|
||||
// removing any dead ones
|
||||
try {
|
||||
db.addSecrets(created);
|
||||
} catch(DbException e) {
|
||||
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
if(LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -109,10 +112,10 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
for(TemporarySecret s : newSecrets.values())
|
||||
tagRecogniser.addSecret(s);
|
||||
// Schedule periodic key rotation
|
||||
timer.scheduleAtFixedRate(this, MS_BETWEEN_CHECKS, MS_BETWEEN_CHECKS);
|
||||
timer.scheduleAtFixedRate(this, MS_BETWEEN_CHECKS,
|
||||
MS_BETWEEN_CHECKS);
|
||||
return true;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -209,7 +212,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
|
||||
public boolean stop() {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
eventBus.removeListener(this);
|
||||
timer.cancel();
|
||||
tagRecogniser.removeSecrets();
|
||||
@@ -218,8 +221,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
currentSecrets.clear();
|
||||
newSecrets.clear();
|
||||
return true;
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -227,7 +229,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
public StreamContext getStreamContext(ContactId c,
|
||||
TransportId t) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
TemporarySecret s = currentSecrets.get(new EndpointKey(c, t));
|
||||
if(s == null) {
|
||||
LOG.info("No secret for endpoint");
|
||||
@@ -244,10 +246,9 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
if(LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
return null;
|
||||
}
|
||||
byte[] secret = s.getSecret();
|
||||
byte[] secret = s.getSecret();
|
||||
return new StreamContext(c, t, secret, streamNumber, s.getAlice());
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -255,7 +256,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
public synchronized void endpointAdded(Endpoint ep, int maxLatency,
|
||||
byte[] initialSecret) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
maxLatencies.put(ep.getTransportId(), maxLatency);
|
||||
// Work out which rotation period we're in
|
||||
long elapsed = clock.currentTimeMillis() - ep.getEpoch();
|
||||
@@ -287,8 +288,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
tagRecogniser.addSecret(s1);
|
||||
tagRecogniser.addSecret(s2);
|
||||
tagRecogniser.addSecret(s3);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -296,8 +296,8 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
@Override
|
||||
public void run() {
|
||||
synchLock.lock();
|
||||
try{
|
||||
// Rebuild the maps because we may be running a whole period late
|
||||
try {
|
||||
// Rebuild the maps because we may be running a whole period late
|
||||
Collection<TemporarySecret> secrets = new ArrayList<TemporarySecret>();
|
||||
secrets.addAll(oldSecrets.values());
|
||||
secrets.addAll(currentSecrets.values());
|
||||
@@ -327,8 +327,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
// Pass any secrets that have been created to the recogniser
|
||||
for(TemporarySecret s : created) tagRecogniser.addSecret(s);
|
||||
}
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -401,12 +400,11 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
ContactId c = event.getContactId();
|
||||
tagRecogniser.removeSecrets(c);
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
removeSecrets(c, oldSecrets);
|
||||
removeSecrets(c, currentSecrets);
|
||||
removeSecrets(c, newSecrets);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -425,8 +423,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
synchLock.lock();
|
||||
try {
|
||||
maxLatencies.put(event.getTransportId(), event.getMaxLatency());
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -450,8 +447,7 @@ class KeyManagerImpl extends TimerTask implements KeyManager, EventListener {
|
||||
removeSecrets(t, oldSecrets);
|
||||
removeSecrets(t, currentSecrets);
|
||||
removeSecrets(t, newSecrets);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ class TagRecogniserImpl implements TagRecogniser {
|
||||
private final DatabaseComponent db;
|
||||
|
||||
private final Map<TransportId, TransportTagRecogniser> recognisers;
|
||||
|
||||
|
||||
private final Lock synchLock = new ReentrantLock();
|
||||
|
||||
|
||||
@@ -39,8 +39,7 @@ class TagRecogniserImpl implements TagRecogniser {
|
||||
synchLock.lock();
|
||||
try {
|
||||
r = recognisers.get(t);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
if(r == null) return null;
|
||||
@@ -57,8 +56,7 @@ class TagRecogniserImpl implements TagRecogniser {
|
||||
r = new TransportTagRecogniser(crypto, db, t);
|
||||
recognisers.put(t, r);
|
||||
}
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
r.addSecret(s);
|
||||
@@ -69,8 +67,7 @@ class TagRecogniserImpl implements TagRecogniser {
|
||||
synchLock.lock();
|
||||
try {
|
||||
r = recognisers.get(t);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
if(r != null) r.removeSecret(c, period);
|
||||
@@ -78,21 +75,19 @@ class TagRecogniserImpl implements TagRecogniser {
|
||||
|
||||
public void removeSecrets(ContactId c) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
for(TransportTagRecogniser r : recognisers.values())
|
||||
r.removeSecrets(c);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSecrets(TransportId t) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
recognisers.remove(t);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
@@ -100,11 +95,10 @@ class TagRecogniserImpl implements TagRecogniser {
|
||||
|
||||
public void removeSecrets() {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
for(TransportTagRecogniser r : recognisers.values())
|
||||
r.removeSecrets();
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ class TransportTagRecogniser {
|
||||
|
||||
StreamContext recogniseTag(byte[] tag) throws DbException {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
TagContext t = tagMap.remove(new Bytes(tag));
|
||||
if(t == null) return null; // The tag was not expected
|
||||
// Update the reordering window and the expected tags
|
||||
@@ -65,17 +65,16 @@ class TransportTagRecogniser {
|
||||
// Store the updated reordering window in the DB
|
||||
db.setReorderingWindow(t.contactId, transportId, t.period,
|
||||
t.window.getCentre(), t.window.getBitmap());
|
||||
return new StreamContext(t.contactId, transportId, t.secret,
|
||||
return new StreamContext(t.contactId, transportId, t.secret,
|
||||
t.streamNumber, t.alice);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void addSecret(TemporarySecret s) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
ContactId contactId = s.getContactId();
|
||||
boolean alice = s.getAlice();
|
||||
long period = s.getPeriod();
|
||||
@@ -96,21 +95,19 @@ class TransportTagRecogniser {
|
||||
// Create a removal context to remove the window and the tags later
|
||||
RemovalContext r = new RemovalContext(window, secret, alice);
|
||||
removalMap.put(new RemovalKey(contactId, period), r);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void removeSecret(ContactId contactId, long period) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
RemovalKey k = new RemovalKey(contactId, period);
|
||||
RemovalContext removed = removalMap.remove(k);
|
||||
if(removed == null) throw new IllegalArgumentException();
|
||||
removeSecret(removed);
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -128,25 +125,24 @@ class TransportTagRecogniser {
|
||||
|
||||
void removeSecrets(ContactId c) {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
Collection<RemovalKey> keysToRemove = new ArrayList<RemovalKey>();
|
||||
for(RemovalKey k : removalMap.keySet())
|
||||
if(k.contactId.equals(c)) keysToRemove.add(k);
|
||||
for(RemovalKey k : keysToRemove) removeSecret(k.contactId, k.period);
|
||||
}
|
||||
finally{
|
||||
for(RemovalKey k : keysToRemove)
|
||||
removeSecret(k.contactId, k.period);
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void removeSecrets() {
|
||||
synchLock.lock();
|
||||
try{
|
||||
try {
|
||||
for(RemovalContext r : removalMap.values()) removeSecret(r);
|
||||
assert tagMap.isEmpty();
|
||||
removalMap.clear();
|
||||
}
|
||||
finally{
|
||||
} finally {
|
||||
synchLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package org.briarproject.util;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class LatchedReference<T> {
|
||||
@@ -23,7 +24,7 @@ public class LatchedReference<T> {
|
||||
}
|
||||
|
||||
public T waitForReference(long timeout) throws InterruptedException {
|
||||
latch.await(timeout, TimeUnit.MILLISECONDS);
|
||||
latch.await(timeout, MILLISECONDS);
|
||||
return reference.get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user