Don't block when adding a new contact / searching for its public key
This commit is contained in:
parent
31eca20cca
commit
e1dcbbf19c
@ -30,6 +30,8 @@ import java.io.IOException;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The internal context should normally only be used for port implementations. If you need it in your client
|
* The internal context should normally only be used for port implementations. If you need it in your client
|
||||||
@ -45,6 +47,8 @@ public class InternalContext {
|
|||||||
public final static long NETWORK_NONCE_TRIALS_PER_BYTE = 1000;
|
public final static long NETWORK_NONCE_TRIALS_PER_BYTE = 1000;
|
||||||
public final static long NETWORK_EXTRA_BYTES = 1000;
|
public final static long NETWORK_EXTRA_BYTES = 1000;
|
||||||
|
|
||||||
|
private final Executor threadPool = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
private final Cryptography cryptography;
|
private final Cryptography cryptography;
|
||||||
private final Inventory inventory;
|
private final Inventory inventory;
|
||||||
private final NodeRegistry nodeRegistry;
|
private final NodeRegistry nodeRegistry;
|
||||||
@ -226,31 +230,36 @@ public class InternalContext {
|
|||||||
* for freshly received pubkeys will not be called. Instead the pubkey is added to the contact and stored on DB.
|
* for freshly received pubkeys will not be called. Instead the pubkey is added to the contact and stored on DB.
|
||||||
*/
|
*/
|
||||||
public void requestPubkey(final BitmessageAddress contact) {
|
public void requestPubkey(final BitmessageAddress contact) {
|
||||||
BitmessageAddress stored = addressRepository.getAddress(contact.getAddress());
|
threadPool.execute(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
BitmessageAddress stored = addressRepository.getAddress(contact.getAddress());
|
||||||
|
|
||||||
tryToFindMatchingPubkey(contact);
|
tryToFindMatchingPubkey(contact);
|
||||||
if (contact.getPubkey() != null) {
|
if (contact.getPubkey() != null) {
|
||||||
if (stored != null) {
|
if (stored != null) {
|
||||||
stored.setPubkey(contact.getPubkey());
|
stored.setPubkey(contact.getPubkey());
|
||||||
addressRepository.save(stored);
|
addressRepository.save(stored);
|
||||||
} else {
|
} else {
|
||||||
addressRepository.save(contact);
|
addressRepository.save(contact);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stored == null) {
|
||||||
|
addressRepository.save(contact);
|
||||||
|
}
|
||||||
|
|
||||||
|
long expires = UnixTime.now(TTL.getpubkey());
|
||||||
|
LOG.info("Expires at " + expires);
|
||||||
|
final ObjectMessage request = new ObjectMessage.Builder()
|
||||||
|
.stream(contact.getStream())
|
||||||
|
.expiresTime(expires)
|
||||||
|
.payload(new GetPubkey(contact))
|
||||||
|
.build();
|
||||||
|
proofOfWorkService.doProofOfWork(request);
|
||||||
}
|
}
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (stored == null) {
|
|
||||||
addressRepository.save(contact);
|
|
||||||
}
|
|
||||||
|
|
||||||
long expires = UnixTime.now(TTL.getpubkey());
|
|
||||||
LOG.info("Expires at " + expires);
|
|
||||||
final ObjectMessage request = new ObjectMessage.Builder()
|
|
||||||
.stream(contact.getStream())
|
|
||||||
.expiresTime(expires)
|
|
||||||
.payload(new GetPubkey(contact))
|
|
||||||
.build();
|
|
||||||
proofOfWorkService.doProofOfWork(request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryToFindMatchingPubkey(BitmessageAddress address) {
|
private void tryToFindMatchingPubkey(BitmessageAddress address) {
|
||||||
|
Loading…
Reference in New Issue
Block a user