Handle offer removal on disconnect, cleanup

This commit is contained in:
Manfred Karrer 2016-02-17 17:33:53 +01:00
parent db363fac48
commit 17c780639f
69 changed files with 377 additions and 367 deletions

View file

@ -60,6 +60,7 @@ public abstract class AppModule extends AbstractModule {
*
* @param injector the Injector originally initialized with this module
*/
@SuppressWarnings("WeakerAccess")
protected void doClose(Injector injector) {
}
}

View file

@ -27,7 +27,7 @@ import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
import org.slf4j.LoggerFactory;
public class Log {
public static boolean PRINT_TRACE_METHOD = true;
private static boolean PRINT_TRACE_METHOD = true;
private static SizeBasedTriggeringPolicy triggeringPolicy;
private static Logger logbackLogger;

View file

@ -5,11 +5,12 @@ import io.bitsquare.app.Version;
import java.io.Serializable;
import java.util.Arrays;
// Util for comparing byte arrays
public class ByteArray implements Serializable {
// That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
public final byte[] bytes;
private final byte[] bytes;
public ByteArray(byte[] bytes) {
this.bytes = bytes;

View file

@ -55,6 +55,7 @@ public class UserThread {
return UserThread.runAfterRandomDelay(runnable, minDelayInSec, maxDelayInSec, TimeUnit.SECONDS);
}
@SuppressWarnings("WeakerAccess")
public static Timer runAfterRandomDelay(Runnable runnable, long minDelay, long maxDelay, TimeUnit timeUnit) {
return UserThread.runAfter(runnable, new Random().nextInt((int) (maxDelay - minDelay)) + minDelay, timeUnit);
}
@ -70,7 +71,7 @@ public class UserThread {
public void run() {
Thread.currentThread().setName("TimerTask-" + new Random().nextInt(10000));
try {
UserThread.execute(() -> runnable.run());
UserThread.execute(runnable::run);
} catch (Throwable t) {
t.printStackTrace();
log.error("Executing timerTask failed. " + t.getMessage());

View file

@ -40,12 +40,12 @@ public class Encryption {
private static final Logger log = LoggerFactory.getLogger(Encryption.class);
public static final String ASYM_KEY_ALGO = "RSA"; // RSA/NONE/OAEPWithSHA256AndMGF1Padding
public static final String ASYM_CIPHER = "RSA";
private static final String ASYM_CIPHER = "RSA";
public static final String SYM_KEY_ALGO = "AES"; // AES/CTR/NoPadding
public static final String SYM_CIPHER = "AES";
private static final String SYM_KEY_ALGO = "AES"; // AES/CTR/NoPadding
private static final String SYM_CIPHER = "AES";
public static final String HMAC = "HmacSHA256";
private static final String HMAC = "HmacSHA256";
public static KeyPair generateKeyPair() {
long ts = System.currentTimeMillis();
@ -66,7 +66,7 @@ public class Encryption {
// Symmetric
///////////////////////////////////////////////////////////////////////////////////////////
public static byte[] encrypt(byte[] payload, SecretKey secretKey) throws CryptoException {
private static byte[] encrypt(byte[] payload, SecretKey secretKey) throws CryptoException {
try {
Cipher cipher = Cipher.getInstance(SYM_CIPHER, "BC");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
@ -77,7 +77,7 @@ public class Encryption {
}
}
public static byte[] decrypt(byte[] encryptedPayload, SecretKey secretKey) throws CryptoException {
private static byte[] decrypt(byte[] encryptedPayload, SecretKey secretKey) throws CryptoException {
try {
Cipher cipher = Cipher.getInstance(SYM_CIPHER, "BC");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
@ -123,7 +123,7 @@ public class Encryption {
}
private static boolean verifyHmac(byte[] message, byte[] hmac, SecretKey secretKey) throws CryptoException {
private static boolean verifyHmac(byte[] message, byte[] hmac, SecretKey secretKey) {
try {
byte[] hmacTest = getHmac(message, secretKey);
return Arrays.equals(hmacTest, hmac);
@ -144,15 +144,15 @@ public class Encryption {
// Symmetric with Hmac
///////////////////////////////////////////////////////////////////////////////////////////
public static byte[] encryptPayloadWithHmac(Serializable object, SecretKey secretKey) throws CryptoException {
private static byte[] encryptPayloadWithHmac(Serializable object, SecretKey secretKey) throws CryptoException {
return encryptPayloadWithHmac(Utilities.serialize(object), secretKey);
}
public static byte[] encryptPayloadWithHmac(byte[] payload, SecretKey secretKey) throws CryptoException {
private static byte[] encryptPayloadWithHmac(byte[] payload, SecretKey secretKey) throws CryptoException {
return encrypt(getPayloadWithHmac(payload, secretKey), secretKey);
}
public static byte[] decryptPayloadWithHmac(byte[] encryptedPayloadWithHmac, SecretKey secretKey) throws CryptoException {
private static byte[] decryptPayloadWithHmac(byte[] encryptedPayloadWithHmac, SecretKey secretKey) throws CryptoException {
byte[] payloadWithHmac = decrypt(encryptedPayloadWithHmac, secretKey);
String payloadWithHmacAsHex = Hex.toHexString(payloadWithHmac);
// first part is raw message
@ -173,7 +173,7 @@ public class Encryption {
// Asymmetric
///////////////////////////////////////////////////////////////////////////////////////////
public static byte[] encrypt(byte[] payload, PublicKey publicKey) throws CryptoException {
private static byte[] encrypt(byte[] payload, PublicKey publicKey) throws CryptoException {
try {
Cipher cipher = Cipher.getInstance(ASYM_CIPHER, "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
@ -184,7 +184,7 @@ public class Encryption {
}
}
public static byte[] decrypt(byte[] encryptedPayload, PrivateKey privateKey) throws CryptoException {
private static byte[] decrypt(byte[] encryptedPayload, PrivateKey privateKey) throws CryptoException {
try {
Cipher cipher = Cipher.getInstance(ASYM_CIPHER, "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);

View file

@ -136,7 +136,7 @@ public class KeyStorage {
savePrivateKey(keyRing.getEncryptionKeyPair().getPrivate(), KeyEntry.MSG_ENCRYPTION.getFileName());
}
public void savePrivateKey(PrivateKey privateKey, String name) {
private void savePrivateKey(PrivateKey privateKey, String name) {
if (!storageDir.exists())
storageDir.mkdir();

View file

@ -36,7 +36,7 @@ public class Sig {
private static final Logger log = LoggerFactory.getLogger(Sig.class);
public static final String KEY_ALGO = "DSA";
public static final String ALGO = "SHA256withDSA";
private static final String ALGO = "SHA256withDSA";
/**
@ -99,7 +99,6 @@ public class Sig {
* @throws SignatureException
*/
public static boolean verify(PublicKey publicKey, byte[] data, byte[] signature) throws CryptoException {
byte[] sigAsBytes = new byte[0];
try {
Signature sig = Signature.getInstance(ALGO, "BC");
sig.initVerify(publicKey);

View file

@ -74,7 +74,7 @@ class DesktopUtil {
}
if (!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
logErr("BORWSE is not supported.");
logErr("BROWSE is not supported.");
return false;
}

View file

@ -20,7 +20,7 @@ package io.bitsquare.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Profiler {
class Profiler {
private static final Logger log = LoggerFactory.getLogger(Profiler.class);
public static void printSystemLoad(Logger log) {

View file

@ -75,9 +75,7 @@ public class Utilities {
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTimeInSec,
TimeUnit.SECONDS, new ArrayBlockingQueue<>(maximumPoolSize), threadFactory);
executor.allowCoreThreadTimeOut(true);
executor.setRejectedExecutionHandler((r, e) -> {
log.warn("RejectedExecutionHandler called");
});
executor.setRejectedExecutionHandler((r, e) -> log.warn("RejectedExecutionHandler called"));
return executor;
}
@ -96,9 +94,7 @@ public class Utilities {
executor.allowCoreThreadTimeOut(true);
executor.setMaximumPoolSize(maximumPoolSize);
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
executor.setRejectedExecutionHandler((r, e) -> {
log.warn("RejectedExecutionHandler called");
});
executor.setRejectedExecutionHandler((r, e) -> log.warn("RejectedExecutionHandler called"));
return executor;
}
@ -292,8 +288,10 @@ public class Utilities {
public static void deleteDirectory(File file) throws IOException {
if (file.isDirectory()) {
for (File c : file.listFiles())
deleteDirectory(c);
File[] files = file.listFiles();
if (files != null)
for (File c : files)
deleteDirectory(c);
}
if (!file.delete())
throw new FileNotFoundException("Failed to delete file: " + file);

View file

@ -15,26 +15,8 @@
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Copyright 2013 Google Inc.
* Copyright 2014 Andreas Schildbach
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.bitsquare.storage;
import com.google.common.io.Files;
import io.bitsquare.common.UserThread;
import io.bitsquare.common.util.Utilities;
@ -50,13 +32,6 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Borrowed from BitcoinJ WalletFiles
* A class that handles atomic and optionally delayed writing of a file to disk.
* It can be useful to delay writing of a file to disk on slow devices.
* By coalescing writes and doing serialization
* and disk IO on a background thread performance can be improved.
*/
public class FileManager<T> {
private static final Logger log = LoggerFactory.getLogger(FileManager.class);
@ -166,7 +141,7 @@ public class FileManager<T> {
/**
* Shut down auto-saving.
*/
public void shutDown() {
void shutDown() {
executor.shutdown();
try {
executor.awaitTermination(5, TimeUnit.SECONDS);

View file

@ -109,7 +109,7 @@ public class Storage<T extends Serializable> {
}
// Save delayed and on a background thread
public void queueUpForSave(T serializable) {
private void queueUpForSave(T serializable) {
if (serializable != null) {
log.trace("save " + fileName);
checkNotNull(storageFile, "storageFile = null. Call setupFileStorage before using read/write.");