Code cleanup
This commit is contained in:
parent
57057298a1
commit
3e5e431d6f
@ -51,19 +51,23 @@ import java.util.Arrays;
|
|||||||
*/
|
*/
|
||||||
public class BouncyCryptography extends AbstractCryptography {
|
public class BouncyCryptography extends AbstractCryptography {
|
||||||
private static final X9ECParameters EC_CURVE_PARAMETERS = CustomNamedCurves.getByName("secp256k1");
|
private static final X9ECParameters EC_CURVE_PARAMETERS = CustomNamedCurves.getByName("secp256k1");
|
||||||
|
private static final String ALGORITHM_ECDSA = "ECDSA";
|
||||||
|
private static final String PROVIDER = "BC";
|
||||||
|
|
||||||
static {
|
static {
|
||||||
java.security.Security.addProvider(new BouncyCastleProvider());
|
java.security.Security.addProvider(new BouncyCastleProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
public BouncyCryptography() {
|
public BouncyCryptography() {
|
||||||
super("BC");
|
super(PROVIDER);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
|
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
|
||||||
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
|
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
|
||||||
|
new CBCBlockCipher(new AESEngine()),
|
||||||
|
new PKCS7Padding()
|
||||||
|
);
|
||||||
CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);
|
CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);
|
||||||
|
|
||||||
cipher.init(encrypt, params);
|
cipher.init(encrypt, params);
|
||||||
@ -105,9 +109,9 @@ public class BouncyCryptography extends AbstractCryptography {
|
|||||||
|
|
||||||
ECPoint Q = keyToPoint(pubkey.getSigningKey());
|
ECPoint Q = keyToPoint(pubkey.getSigningKey());
|
||||||
KeySpec keySpec = new ECPublicKeySpec(Q, spec);
|
KeySpec keySpec = new ECPublicKeySpec(Q, spec);
|
||||||
PublicKey publicKey = KeyFactory.getInstance("ECDSA", "BC").generatePublic(keySpec);
|
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER).generatePublic(keySpec);
|
||||||
|
|
||||||
Signature sig = Signature.getInstance("ECDSA", "BC");
|
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
|
||||||
sig.initVerify(publicKey);
|
sig.initVerify(publicKey);
|
||||||
sig.update(data);
|
sig.update(data);
|
||||||
return sig.verify(signature);
|
return sig.verify(signature);
|
||||||
@ -129,9 +133,10 @@ public class BouncyCryptography extends AbstractCryptography {
|
|||||||
|
|
||||||
BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey());
|
BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey());
|
||||||
KeySpec keySpec = new ECPrivateKeySpec(d, spec);
|
KeySpec keySpec = new ECPrivateKeySpec(d, spec);
|
||||||
java.security.PrivateKey privKey = KeyFactory.getInstance("ECDSA", "BC").generatePrivate(keySpec);
|
java.security.PrivateKey privKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER)
|
||||||
|
.generatePrivate(keySpec);
|
||||||
|
|
||||||
Signature sig = Signature.getInstance("ECDSA", "BC");
|
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
|
||||||
sig.initSign(privKey);
|
sig.initSign(privKey);
|
||||||
sig.update(data);
|
sig.update(data);
|
||||||
return sig.sign();
|
return sig.sign();
|
||||||
|
@ -51,19 +51,23 @@ import java.util.Arrays;
|
|||||||
*/
|
*/
|
||||||
public class SpongyCryptography extends AbstractCryptography {
|
public class SpongyCryptography extends AbstractCryptography {
|
||||||
private static final X9ECParameters EC_CURVE_PARAMETERS = CustomNamedCurves.getByName("secp256k1");
|
private static final X9ECParameters EC_CURVE_PARAMETERS = CustomNamedCurves.getByName("secp256k1");
|
||||||
|
private static final String ALGORITHM_ECDSA = "ECDSA";
|
||||||
|
private static final String PROVIDER = "SC";
|
||||||
|
|
||||||
static {
|
static {
|
||||||
java.security.Security.addProvider(new BouncyCastleProvider());
|
java.security.Security.addProvider(new BouncyCastleProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpongyCryptography() {
|
public SpongyCryptography() {
|
||||||
super("SC");
|
super(PROVIDER);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
|
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
|
||||||
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
|
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
|
||||||
|
new CBCBlockCipher(new AESEngine()),
|
||||||
|
new PKCS7Padding()
|
||||||
|
);
|
||||||
CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);
|
CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);
|
||||||
|
|
||||||
cipher.init(encrypt, params);
|
cipher.init(encrypt, params);
|
||||||
@ -105,9 +109,9 @@ public class SpongyCryptography extends AbstractCryptography {
|
|||||||
|
|
||||||
ECPoint Q = keyToPoint(pubkey.getSigningKey());
|
ECPoint Q = keyToPoint(pubkey.getSigningKey());
|
||||||
KeySpec keySpec = new ECPublicKeySpec(Q, spec);
|
KeySpec keySpec = new ECPublicKeySpec(Q, spec);
|
||||||
PublicKey publicKey = KeyFactory.getInstance("ECDSA", "SC").generatePublic(keySpec);
|
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER).generatePublic(keySpec);
|
||||||
|
|
||||||
Signature sig = Signature.getInstance("ECDSA", "SC");
|
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
|
||||||
sig.initVerify(publicKey);
|
sig.initVerify(publicKey);
|
||||||
sig.update(data);
|
sig.update(data);
|
||||||
return sig.verify(signature);
|
return sig.verify(signature);
|
||||||
@ -129,9 +133,10 @@ public class SpongyCryptography extends AbstractCryptography {
|
|||||||
|
|
||||||
BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey());
|
BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey());
|
||||||
KeySpec keySpec = new ECPrivateKeySpec(d, spec);
|
KeySpec keySpec = new ECPrivateKeySpec(d, spec);
|
||||||
java.security.PrivateKey privKey = KeyFactory.getInstance("ECDSA", "SC").generatePrivate(keySpec);
|
java.security.PrivateKey privKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER)
|
||||||
|
.generatePrivate(keySpec);
|
||||||
|
|
||||||
Signature sig = Signature.getInstance("ECDSA", "SC");
|
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
|
||||||
sig.initSign(privKey);
|
sig.initSign(privKey);
|
||||||
sig.update(data);
|
sig.update(data);
|
||||||
return sig.sign();
|
return sig.sign();
|
||||||
|
@ -17,31 +17,33 @@
|
|||||||
package ch.dissem.bitmessage.demo;
|
package ch.dissem.bitmessage.demo;
|
||||||
|
|
||||||
import ch.dissem.bitmessage.BitmessageContext;
|
import ch.dissem.bitmessage.BitmessageContext;
|
||||||
|
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
||||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||||
import ch.dissem.bitmessage.entity.Plaintext;
|
import ch.dissem.bitmessage.entity.Plaintext;
|
||||||
import ch.dissem.bitmessage.entity.payload.Pubkey;
|
import ch.dissem.bitmessage.entity.payload.Pubkey;
|
||||||
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
|
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
|
||||||
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
||||||
import ch.dissem.bitmessage.repository.*;
|
import ch.dissem.bitmessage.repository.*;
|
||||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
|
||||||
|
import static ch.dissem.bitmessage.demo.CommandLine.COMMAND_BACK;
|
||||||
|
import static ch.dissem.bitmessage.demo.CommandLine.ERROR_UNKNOWN_COMMAND;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple command line Bitmessage application
|
* A simple command line Bitmessage application
|
||||||
*/
|
*/
|
||||||
public class Application {
|
public class Application {
|
||||||
private final static Logger LOG = LoggerFactory.getLogger(Application.class);
|
private final static Logger LOG = LoggerFactory.getLogger(Application.class);
|
||||||
private final Scanner scanner;
|
private final CommandLine commandLine;
|
||||||
|
|
||||||
private BitmessageContext ctx;
|
private BitmessageContext ctx;
|
||||||
|
|
||||||
public Application(String syncServer, int syncPort) {
|
public Application(InetAddress syncServer, int syncPort) {
|
||||||
JdbcConfig jdbcConfig = new JdbcConfig();
|
JdbcConfig jdbcConfig = new JdbcConfig();
|
||||||
ctx = new BitmessageContext.Builder()
|
ctx = new BitmessageContext.Builder()
|
||||||
.addressRepo(new JdbcAddressRepository(jdbcConfig))
|
.addressRepo(new JdbcAddressRepository(jdbcConfig))
|
||||||
@ -68,7 +70,7 @@ public class Application {
|
|||||||
ctx.startup();
|
ctx.startup();
|
||||||
}
|
}
|
||||||
|
|
||||||
scanner = new Scanner(System.in);
|
commandLine = new CommandLine();
|
||||||
|
|
||||||
String command;
|
String command;
|
||||||
do {
|
do {
|
||||||
@ -84,7 +86,7 @@ public class Application {
|
|||||||
System.out.println("?) info");
|
System.out.println("?) info");
|
||||||
System.out.println("e) exit");
|
System.out.println("e) exit");
|
||||||
|
|
||||||
command = nextCommand();
|
command = commandLine.nextCommand();
|
||||||
try {
|
try {
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "i": {
|
case "i": {
|
||||||
@ -106,10 +108,12 @@ public class Application {
|
|||||||
case "e":
|
case "e":
|
||||||
break;
|
break;
|
||||||
case "y":
|
case "y":
|
||||||
ctx.synchronize(InetAddress.getByName(syncServer), syncPort, 120, true);
|
if (syncServer != null) {
|
||||||
|
ctx.synchronize(syncServer, syncPort, 120, true);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("Unknown command. Please try again.");
|
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.debug(e.getMessage());
|
LOG.debug(e.getMessage());
|
||||||
@ -125,32 +129,16 @@ public class Application {
|
|||||||
System.out.println(ctx.status());
|
System.out.println(ctx.status());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String nextCommand() {
|
|
||||||
return scanner.nextLine().trim().toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void identities() {
|
private void identities() {
|
||||||
String command;
|
String command;
|
||||||
List<BitmessageAddress> identities = ctx.addresses().getIdentities();
|
List<BitmessageAddress> identities = ctx.addresses().getIdentities();
|
||||||
do {
|
do {
|
||||||
System.out.println();
|
System.out.println();
|
||||||
int i = 0;
|
commandLine.listAddresses(identities, "identities");
|
||||||
for (BitmessageAddress identity : identities) {
|
|
||||||
i++;
|
|
||||||
System.out.print(i + ") ");
|
|
||||||
if (identity.getAlias() != null) {
|
|
||||||
System.out.println(identity.getAlias() + " (" + identity.getAddress() + ")");
|
|
||||||
} else {
|
|
||||||
System.out.println(identity.getAddress());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (i == 0) {
|
|
||||||
System.out.println("You have no identities yet.");
|
|
||||||
}
|
|
||||||
System.out.println("a) create identity");
|
System.out.println("a) create identity");
|
||||||
System.out.println("b) back");
|
System.out.println(COMMAND_BACK);
|
||||||
|
|
||||||
command = nextCommand();
|
command = commandLine.nextCommand();
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "a":
|
case "a":
|
||||||
addIdentity();
|
addIdentity();
|
||||||
@ -163,7 +151,7 @@ public class Application {
|
|||||||
int index = Integer.parseInt(command) - 1;
|
int index = Integer.parseInt(command) - 1;
|
||||||
address(identities.get(index));
|
address(identities.get(index));
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
System.out.println("Unknown command. Please try again.");
|
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (!"b".equals(command));
|
} while (!"b".equals(command));
|
||||||
@ -171,9 +159,9 @@ public class Application {
|
|||||||
|
|
||||||
private void addIdentity() {
|
private void addIdentity() {
|
||||||
System.out.println();
|
System.out.println();
|
||||||
BitmessageAddress identity = ctx.createIdentity(yesNo("would you like a shorter address? This will take some time to calculate."), Pubkey.Feature.DOES_ACK);
|
BitmessageAddress identity = ctx.createIdentity(commandLine.yesNo("would you like a shorter address? This will take some time to calculate."), Pubkey.Feature.DOES_ACK);
|
||||||
System.out.println("Please enter an alias for this identity, or an empty string for none");
|
System.out.println("Please enter an alias for this identity, or an empty string for none");
|
||||||
String alias = scanner.nextLine().trim();
|
String alias = commandLine.nextLineTrimmed();
|
||||||
if (alias.length() > 0) {
|
if (alias.length() > 0) {
|
||||||
identity.setAlias(alias);
|
identity.setAlias(alias);
|
||||||
}
|
}
|
||||||
@ -185,24 +173,12 @@ public class Application {
|
|||||||
List<BitmessageAddress> contacts = ctx.addresses().getContacts();
|
List<BitmessageAddress> contacts = ctx.addresses().getContacts();
|
||||||
do {
|
do {
|
||||||
System.out.println();
|
System.out.println();
|
||||||
int i = 0;
|
commandLine.listAddresses(contacts, "contacts");
|
||||||
for (BitmessageAddress contact : contacts) {
|
|
||||||
i++;
|
|
||||||
System.out.print(i + ") ");
|
|
||||||
if (contact.getAlias() != null) {
|
|
||||||
System.out.println(contact.getAlias() + " (" + contact.getAddress() + ")");
|
|
||||||
} else {
|
|
||||||
System.out.println(contact.getAddress());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (i == 0) {
|
|
||||||
System.out.println("You have no contacts yet.");
|
|
||||||
}
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println("a) add contact");
|
System.out.println("a) add contact");
|
||||||
System.out.println("b) back");
|
System.out.println(COMMAND_BACK);
|
||||||
|
|
||||||
command = nextCommand();
|
command = commandLine.nextCommand();
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "a":
|
case "a":
|
||||||
addContact(false);
|
addContact(false);
|
||||||
@ -215,7 +191,7 @@ public class Application {
|
|||||||
int index = Integer.parseInt(command) - 1;
|
int index = Integer.parseInt(command) - 1;
|
||||||
address(contacts.get(index));
|
address(contacts.get(index));
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
System.out.println("Unknown command. Please try again.");
|
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (!"b".equals(command));
|
} while (!"b".equals(command));
|
||||||
@ -225,9 +201,9 @@ public class Application {
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println("Please enter the Bitmessage address you want to add");
|
System.out.println("Please enter the Bitmessage address you want to add");
|
||||||
try {
|
try {
|
||||||
BitmessageAddress address = new BitmessageAddress(scanner.nextLine().trim());
|
BitmessageAddress address = new BitmessageAddress(commandLine.nextLineTrimmed());
|
||||||
System.out.println("Please enter an alias for this address, or an empty string for none");
|
System.out.println("Please enter an alias for this address, or an empty string for none");
|
||||||
String alias = scanner.nextLine().trim();
|
String alias = commandLine.nextLineTrimmed();
|
||||||
if (alias.length() > 0) {
|
if (alias.length() > 0) {
|
||||||
address.setAlias(alias);
|
address.setAlias(alias);
|
||||||
}
|
}
|
||||||
@ -245,24 +221,12 @@ public class Application {
|
|||||||
List<BitmessageAddress> subscriptions = ctx.addresses().getSubscriptions();
|
List<BitmessageAddress> subscriptions = ctx.addresses().getSubscriptions();
|
||||||
do {
|
do {
|
||||||
System.out.println();
|
System.out.println();
|
||||||
int i = 0;
|
commandLine.listAddresses(subscriptions, "subscriptions");
|
||||||
for (BitmessageAddress contact : subscriptions) {
|
|
||||||
i++;
|
|
||||||
System.out.print(i + ") ");
|
|
||||||
if (contact.getAlias() != null) {
|
|
||||||
System.out.println(contact.getAlias() + " (" + contact.getAddress() + ")");
|
|
||||||
} else {
|
|
||||||
System.out.println(contact.getAddress());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (i == 0) {
|
|
||||||
System.out.println("You have no subscriptions yet.");
|
|
||||||
}
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println("a) add subscription");
|
System.out.println("a) add subscription");
|
||||||
System.out.println("b) back");
|
System.out.println(COMMAND_BACK);
|
||||||
|
|
||||||
command = nextCommand();
|
command = commandLine.nextCommand();
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "a":
|
case "a":
|
||||||
addContact(true);
|
addContact(true);
|
||||||
@ -275,7 +239,7 @@ public class Application {
|
|||||||
int index = Integer.parseInt(command) - 1;
|
int index = Integer.parseInt(command) - 1;
|
||||||
address(subscriptions.get(index));
|
address(subscriptions.get(index));
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
System.out.println("Unknown command. Please try again.");
|
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (!"b".equals(command));
|
} while (!"b".equals(command));
|
||||||
@ -313,9 +277,9 @@ public class Application {
|
|||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println("c) compose message");
|
System.out.println("c) compose message");
|
||||||
System.out.println("s) compose broadcast");
|
System.out.println("s) compose broadcast");
|
||||||
System.out.println("b) back");
|
System.out.println(COMMAND_BACK);
|
||||||
|
|
||||||
command = scanner.nextLine().trim();
|
command = commandLine.nextCommand();
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "c":
|
case "c":
|
||||||
compose(false);
|
compose(false);
|
||||||
@ -330,7 +294,7 @@ public class Application {
|
|||||||
int index = Integer.parseInt(command) - 1;
|
int index = Integer.parseInt(command) - 1;
|
||||||
show(messages.get(index));
|
show(messages.get(index));
|
||||||
} catch (NumberFormatException | IndexOutOfBoundsException e) {
|
} catch (NumberFormatException | IndexOutOfBoundsException e) {
|
||||||
System.out.println("Unknown command. Please try again.");
|
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (!"b".equalsIgnoreCase(command));
|
} while (!"b".equalsIgnoreCase(command));
|
||||||
@ -350,8 +314,8 @@ public class Application {
|
|||||||
do {
|
do {
|
||||||
System.out.println("r) reply");
|
System.out.println("r) reply");
|
||||||
System.out.println("d) delete");
|
System.out.println("d) delete");
|
||||||
System.out.println("b) back");
|
System.out.println(COMMAND_BACK);
|
||||||
command = nextCommand();
|
command = commandLine.nextCommand();
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "r":
|
case "r":
|
||||||
compose(message.getTo(), message.getFrom(), "RE: " + message.getSubject());
|
compose(message.getTo(), message.getFrom(), "RE: " + message.getSubject());
|
||||||
@ -361,18 +325,18 @@ public class Application {
|
|||||||
case "b":
|
case "b":
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
System.out.println("Unknown command. Please try again.");
|
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||||
}
|
}
|
||||||
} while (!"b".equalsIgnoreCase(command));
|
} while (!"b".equalsIgnoreCase(command));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void compose(boolean broadcast) {
|
private void compose(boolean broadcast) {
|
||||||
System.out.println();
|
System.out.println();
|
||||||
BitmessageAddress from = selectAddress(true);
|
BitmessageAddress from = selectIdentity();
|
||||||
if (from == null) {
|
if (from == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
BitmessageAddress to = (broadcast ? null : selectAddress(false));
|
BitmessageAddress to = (broadcast ? null : selectContact());
|
||||||
if (!broadcast && to == null) {
|
if (!broadcast && to == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -380,58 +344,22 @@ public class Application {
|
|||||||
compose(from, to, null);
|
compose(from, to, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BitmessageAddress selectAddress(boolean id) {
|
private BitmessageAddress selectIdentity() {
|
||||||
List<BitmessageAddress> addresses = (id ? ctx.addresses().getIdentities() : ctx.addresses().getContacts());
|
List<BitmessageAddress> addresses = ctx.addresses().getIdentities();
|
||||||
while (addresses.size() == 0) {
|
while (addresses.size() == 0) {
|
||||||
if (id) {
|
addIdentity();
|
||||||
addIdentity();
|
addresses = ctx.addresses().getIdentities();
|
||||||
addresses = ctx.addresses().getIdentities();
|
|
||||||
} else {
|
|
||||||
addContact(false);
|
|
||||||
addresses = ctx.addresses().getContacts();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (addresses.size() == 1) {
|
return commandLine.selectAddress(addresses, "From:");
|
||||||
return addresses.get(0);
|
}
|
||||||
|
|
||||||
|
private BitmessageAddress selectContact() {
|
||||||
|
List<BitmessageAddress> addresses = ctx.addresses().getContacts();
|
||||||
|
while (addresses.size() == 0) {
|
||||||
|
addContact(false);
|
||||||
|
addresses = ctx.addresses().getContacts();
|
||||||
}
|
}
|
||||||
|
return commandLine.selectAddress(addresses, "To:");
|
||||||
String command;
|
|
||||||
do {
|
|
||||||
System.out.println();
|
|
||||||
if (id) {
|
|
||||||
System.out.println("From:");
|
|
||||||
} else {
|
|
||||||
System.out.println("To:");
|
|
||||||
}
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for (BitmessageAddress identity : addresses) {
|
|
||||||
i++;
|
|
||||||
System.out.print(i + ") ");
|
|
||||||
if (identity.getAlias() != null) {
|
|
||||||
System.out.println(identity.getAlias() + " (" + identity.getAddress() + ")");
|
|
||||||
} else {
|
|
||||||
System.out.println(identity.getAddress());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println("b) back");
|
|
||||||
|
|
||||||
command = nextCommand();
|
|
||||||
switch (command) {
|
|
||||||
case "b":
|
|
||||||
return null;
|
|
||||||
default:
|
|
||||||
try {
|
|
||||||
int index = Integer.parseInt(command) - 1;
|
|
||||||
if (addresses.get(index) != null) {
|
|
||||||
return addresses.get(index);
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
System.out.println("Unknown command. Please try again.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (!"b".equals(command));
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void compose(BitmessageAddress from, BitmessageAddress to, String subject) {
|
private void compose(BitmessageAddress from, BitmessageAddress to, String subject) {
|
||||||
@ -445,29 +373,19 @@ public class Application {
|
|||||||
System.out.println("Subject: " + subject);
|
System.out.println("Subject: " + subject);
|
||||||
} else {
|
} else {
|
||||||
System.out.print("Subject: ");
|
System.out.print("Subject: ");
|
||||||
subject = scanner.nextLine().trim();
|
subject = commandLine.nextLineTrimmed();
|
||||||
}
|
}
|
||||||
System.out.println("Message:");
|
System.out.println("Message:");
|
||||||
StringBuilder message = new StringBuilder();
|
StringBuilder message = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
do {
|
do {
|
||||||
line = scanner.nextLine();
|
line = commandLine.nextLine();
|
||||||
message.append(line).append('\n');
|
message.append(line).append('\n');
|
||||||
} while (line.length() > 0 || !yesNo("Send message?"));
|
} while (line.length() > 0 || !commandLine.yesNo("Send message?"));
|
||||||
if (broadcast) {
|
if (broadcast) {
|
||||||
ctx.broadcast(from, subject, message.toString());
|
ctx.broadcast(from, subject, message.toString());
|
||||||
} else {
|
} else {
|
||||||
ctx.send(from, to, subject, message.toString());
|
ctx.send(from, to, subject, message.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean yesNo(String question) {
|
|
||||||
String answer;
|
|
||||||
do {
|
|
||||||
System.out.println(question + " (y/n)");
|
|
||||||
answer = scanner.nextLine();
|
|
||||||
if ("y".equalsIgnoreCase(answer)) return true;
|
|
||||||
if ("n".equalsIgnoreCase(answer)) return false;
|
|
||||||
} while (true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
102
demo/src/main/java/ch/dissem/bitmessage/demo/CommandLine.java
Normal file
102
demo/src/main/java/ch/dissem/bitmessage/demo/CommandLine.java
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 Christian Basler
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* 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 ch.dissem.bitmessage.demo;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Christian Basler
|
||||||
|
*/
|
||||||
|
public class CommandLine {
|
||||||
|
public static final String COMMAND_BACK = "b) back";
|
||||||
|
public static final String ERROR_UNKNOWN_COMMAND = "Unknown command. Please try again.";
|
||||||
|
|
||||||
|
private Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
public String nextCommand() {
|
||||||
|
return scanner.nextLine().trim().toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String nextLine() {
|
||||||
|
return scanner.nextLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String nextLineTrimmed() {
|
||||||
|
return scanner.nextLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean yesNo(String question) {
|
||||||
|
String answer;
|
||||||
|
do {
|
||||||
|
System.out.println(question + " (y/n)");
|
||||||
|
answer = scanner.nextLine();
|
||||||
|
if ("y".equalsIgnoreCase(answer)) return true;
|
||||||
|
if ("n".equalsIgnoreCase(answer)) return false;
|
||||||
|
} while (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BitmessageAddress selectAddress(List<BitmessageAddress> addresses, String label) {
|
||||||
|
if (addresses.size() == 1) {
|
||||||
|
return addresses.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
String command;
|
||||||
|
do {
|
||||||
|
System.out.println();
|
||||||
|
System.out.println(label);
|
||||||
|
|
||||||
|
listAddresses(addresses, "contacts");
|
||||||
|
System.out.println(COMMAND_BACK);
|
||||||
|
|
||||||
|
command = nextCommand();
|
||||||
|
switch (command) {
|
||||||
|
case "b":
|
||||||
|
return null;
|
||||||
|
default:
|
||||||
|
try {
|
||||||
|
int index = Integer.parseInt(command) - 1;
|
||||||
|
if (addresses.get(index) != null) {
|
||||||
|
return addresses.get(index);
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (!"b".equals(command));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void listAddresses(List<BitmessageAddress> addresses, String kind) {
|
||||||
|
int i = 0;
|
||||||
|
for (BitmessageAddress address : addresses) {
|
||||||
|
i++;
|
||||||
|
System.out.print(i + ") ");
|
||||||
|
if (address.getAlias() == null) {
|
||||||
|
System.out.println(address.getAddress());
|
||||||
|
} else {
|
||||||
|
System.out.println(address.getAlias() + " (" + address.getAddress() + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == 0) {
|
||||||
|
System.out.println("You have no " + kind + " yet.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,10 +17,10 @@
|
|||||||
package ch.dissem.bitmessage.demo;
|
package ch.dissem.bitmessage.demo;
|
||||||
|
|
||||||
import ch.dissem.bitmessage.BitmessageContext;
|
import ch.dissem.bitmessage.BitmessageContext;
|
||||||
|
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
||||||
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
|
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
|
||||||
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
||||||
import ch.dissem.bitmessage.repository.*;
|
import ch.dissem.bitmessage.repository.*;
|
||||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
|
||||||
import ch.dissem.bitmessage.wif.WifExporter;
|
import ch.dissem.bitmessage.wif.WifExporter;
|
||||||
import ch.dissem.bitmessage.wif.WifImporter;
|
import ch.dissem.bitmessage.wif.WifImporter;
|
||||||
import org.kohsuke.args4j.CmdLineException;
|
import org.kohsuke.args4j.CmdLineException;
|
||||||
@ -29,6 +29,7 @@ import org.kohsuke.args4j.Option;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
@ -64,7 +65,8 @@ public class Main {
|
|||||||
new WifImporter(ctx, options.importWIF).importAll();
|
new WifImporter(ctx, options.importWIF).importAll();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
new Application(options.syncServer, options.syncPort);
|
InetAddress syncServer = options.syncServer == null ? null : InetAddress.getByName(options.syncServer);
|
||||||
|
new Application(syncServer, options.syncPort);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,51 +203,16 @@ class Connection {
|
|||||||
private void receiveMessage(MessagePayload messagePayload) {
|
private void receiveMessage(MessagePayload messagePayload) {
|
||||||
switch (messagePayload.getCommand()) {
|
switch (messagePayload.getCommand()) {
|
||||||
case INV:
|
case INV:
|
||||||
Inv inv = (Inv) messagePayload;
|
receiveMessage((Inv) messagePayload);
|
||||||
int originalSize = inv.getInventory().size();
|
|
||||||
updateIvCache(inv.getInventory());
|
|
||||||
List<InventoryVector> missing = ctx.getInventory().getMissing(inv.getInventory(), streams);
|
|
||||||
missing.removeAll(commonRequestedObjects);
|
|
||||||
LOG.debug("Received inventory with " + originalSize + " elements, of which are "
|
|
||||||
+ missing.size() + " missing.");
|
|
||||||
send(new GetData.Builder().inventory(missing).build());
|
|
||||||
break;
|
break;
|
||||||
case GETDATA:
|
case GETDATA:
|
||||||
GetData getData = (GetData) messagePayload;
|
receiveMessage((GetData) messagePayload);
|
||||||
for (InventoryVector iv : getData.getInventory()) {
|
|
||||||
ObjectMessage om = ctx.getInventory().getObject(iv);
|
|
||||||
if (om != null) sendingQueue.offer(om);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case OBJECT:
|
case OBJECT:
|
||||||
ObjectMessage objectMessage = (ObjectMessage) messagePayload;
|
receiveMessage((ObjectMessage) messagePayload);
|
||||||
try {
|
|
||||||
requestedObjects.remove(objectMessage.getInventoryVector());
|
|
||||||
if (ctx.getInventory().contains(objectMessage)) {
|
|
||||||
LOG.trace("Received object " + objectMessage.getInventoryVector() + " - already in inventory");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
listener.receive(objectMessage);
|
|
||||||
security().checkProofOfWork(objectMessage, ctx.getNetworkNonceTrialsPerByte(), ctx.getNetworkExtraBytes());
|
|
||||||
ctx.getInventory().storeObject(objectMessage);
|
|
||||||
// offer object to some random nodes so it gets distributed throughout the network:
|
|
||||||
networkHandler.offer(objectMessage.getInventoryVector());
|
|
||||||
lastObjectTime = UnixTime.now();
|
|
||||||
} catch (InsufficientProofOfWorkException e) {
|
|
||||||
LOG.warn(e.getMessage());
|
|
||||||
// DebugUtils.saveToFile(objectMessage); // this line must not be committed active
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOG.error("Stream " + objectMessage.getStream() + ", object type " + objectMessage.getType() + ": " + e.getMessage(), e);
|
|
||||||
} finally {
|
|
||||||
if (commonRequestedObjects.remove(objectMessage.getInventoryVector())) {
|
|
||||||
LOG.debug("Received object that wasn't requested.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case ADDR:
|
case ADDR:
|
||||||
Addr addr = (Addr) messagePayload;
|
receiveMessage((Addr) messagePayload);
|
||||||
LOG.debug("Received " + addr.getAddresses().size() + " addresses.");
|
|
||||||
ctx.getNodeRegistry().offerAddresses(addr.getAddresses());
|
|
||||||
break;
|
break;
|
||||||
case CUSTOM:
|
case CUSTOM:
|
||||||
case VERACK:
|
case VERACK:
|
||||||
@ -257,6 +222,53 @@ class Connection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void receiveMessage(Inv inv) {
|
||||||
|
int originalSize = inv.getInventory().size();
|
||||||
|
updateIvCache(inv.getInventory());
|
||||||
|
List<InventoryVector> missing = ctx.getInventory().getMissing(inv.getInventory(), streams);
|
||||||
|
missing.removeAll(commonRequestedObjects);
|
||||||
|
LOG.debug("Received inventory with " + originalSize + " elements, of which are "
|
||||||
|
+ missing.size() + " missing.");
|
||||||
|
send(new GetData.Builder().inventory(missing).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void receiveMessage(GetData getData) {
|
||||||
|
for (InventoryVector iv : getData.getInventory()) {
|
||||||
|
ObjectMessage om = ctx.getInventory().getObject(iv);
|
||||||
|
if (om != null) sendingQueue.offer(om);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void receiveMessage(ObjectMessage objectMessage) {
|
||||||
|
requestedObjects.remove(objectMessage.getInventoryVector());
|
||||||
|
if (ctx.getInventory().contains(objectMessage)) {
|
||||||
|
LOG.trace("Received object " + objectMessage.getInventoryVector() + " - already in inventory");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
listener.receive(objectMessage);
|
||||||
|
security().checkProofOfWork(objectMessage, ctx.getNetworkNonceTrialsPerByte(), ctx.getNetworkExtraBytes());
|
||||||
|
ctx.getInventory().storeObject(objectMessage);
|
||||||
|
// offer object to some random nodes so it gets distributed throughout the network:
|
||||||
|
networkHandler.offer(objectMessage.getInventoryVector());
|
||||||
|
lastObjectTime = UnixTime.now();
|
||||||
|
} catch (InsufficientProofOfWorkException e) {
|
||||||
|
LOG.warn(e.getMessage());
|
||||||
|
// DebugUtils.saveToFile(objectMessage); // this line must not be committed active
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.error("Stream " + objectMessage.getStream() + ", object type " + objectMessage.getType() + ": " + e.getMessage(), e);
|
||||||
|
} finally {
|
||||||
|
if (commonRequestedObjects.remove(objectMessage.getInventoryVector())) {
|
||||||
|
LOG.debug("Received object that wasn't requested.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void receiveMessage(Addr addr) {
|
||||||
|
LOG.debug("Received " + addr.getAddresses().size() + " addresses.");
|
||||||
|
ctx.getNodeRegistry().offerAddresses(addr.getAddresses());
|
||||||
|
}
|
||||||
|
|
||||||
private void sendAddresses() {
|
private void sendAddresses() {
|
||||||
List<NetworkAddress> addresses = ctx.getNodeRegistry().getKnownAddresses(1000, streams);
|
List<NetworkAddress> addresses = ctx.getNodeRegistry().getKnownAddresses(1000, streams);
|
||||||
sendingQueue.offer(new Addr.Builder().addresses(addresses).build());
|
sendingQueue.offer(new Addr.Builder().addresses(addresses).build());
|
||||||
@ -358,77 +370,10 @@ class Connection {
|
|||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
receive();
|
||||||
NetworkMessage msg = Factory.getNetworkMessage(version, in);
|
|
||||||
if (msg == null)
|
|
||||||
continue;
|
|
||||||
switch (state) {
|
|
||||||
case ACTIVE:
|
|
||||||
receiveMessage(msg.getPayload());
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
switch (msg.getPayload().getCommand()) {
|
|
||||||
case VERSION:
|
|
||||||
Version payload = (Version) msg.getPayload();
|
|
||||||
if (payload.getNonce() == ctx.getClientNonce()) {
|
|
||||||
LOG.info("Tried to connect to self, disconnecting.");
|
|
||||||
disconnect();
|
|
||||||
} else if (payload.getVersion() >= BitmessageContext.CURRENT_VERSION) {
|
|
||||||
version = payload.getVersion();
|
|
||||||
streams = payload.getStreams();
|
|
||||||
send(new VerAck());
|
|
||||||
switch (mode) {
|
|
||||||
case SERVER:
|
|
||||||
send(new Version.Builder().defaults().addrFrom(host).addrRecv(node).build());
|
|
||||||
break;
|
|
||||||
case CLIENT:
|
|
||||||
case SYNC:
|
|
||||||
activateConnection();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// NO OP
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LOG.info("Received unsupported version " + payload.getVersion() + ", disconnecting.");
|
|
||||||
disconnect();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case VERACK:
|
|
||||||
switch (mode) {
|
|
||||||
case SERVER:
|
|
||||||
activateConnection();
|
|
||||||
break;
|
|
||||||
case CLIENT:
|
|
||||||
case SYNC:
|
|
||||||
default:
|
|
||||||
// NO OP
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case CUSTOM:
|
|
||||||
MessagePayload response = ctx.getCustomCommandHandler().handle((CustomMessage) msg.getPayload());
|
|
||||||
if (response != null) {
|
|
||||||
send(response);
|
|
||||||
}
|
|
||||||
disconnect();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new NodeException("Command 'version' or 'verack' expected, but was '"
|
|
||||||
+ msg.getPayload().getCommand() + "'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (socket.isClosed() || syncFinished(msg) || checkOpenRequests()) disconnect();
|
|
||||||
} catch (SocketTimeoutException ignore) {
|
|
||||||
if (state == ACTIVE) {
|
|
||||||
if (syncFinished(null)) disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (InterruptedException | IOException | NodeException e) {
|
} catch (Exception e) {
|
||||||
LOG.trace("Reader disconnected from node " + node + ": " + e.getMessage());
|
LOG.trace("Reader disconnected from node " + node + ": " + e.getMessage());
|
||||||
} catch (RuntimeException e) {
|
|
||||||
LOG.trace("Reader disconnecting from node " + node + " due to error: " + e.getMessage(), e);
|
|
||||||
} finally {
|
} finally {
|
||||||
disconnect();
|
disconnect();
|
||||||
try {
|
try {
|
||||||
@ -438,6 +383,81 @@ class Connection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void receive() throws InterruptedException {
|
||||||
|
try {
|
||||||
|
NetworkMessage msg = Factory.getNetworkMessage(version, in);
|
||||||
|
if (msg == null)
|
||||||
|
return;
|
||||||
|
switch (state) {
|
||||||
|
case ACTIVE:
|
||||||
|
receiveMessage(msg.getPayload());
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
handleCommand(msg.getPayload());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (socket.isClosed() || syncFinished(msg) || checkOpenRequests()) disconnect();
|
||||||
|
} catch (SocketTimeoutException ignore) {
|
||||||
|
if (state == ACTIVE && syncFinished(null)) disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleCommand(MessagePayload payload) {
|
||||||
|
switch (payload.getCommand()) {
|
||||||
|
case VERSION:
|
||||||
|
handleVersion((Version) payload);
|
||||||
|
break;
|
||||||
|
case VERACK:
|
||||||
|
switch (mode) {
|
||||||
|
case SERVER:
|
||||||
|
activateConnection();
|
||||||
|
break;
|
||||||
|
case CLIENT:
|
||||||
|
case SYNC:
|
||||||
|
default:
|
||||||
|
// NO OP
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CUSTOM:
|
||||||
|
MessagePayload response = ctx.getCustomCommandHandler().handle((CustomMessage) payload);
|
||||||
|
if (response != null) {
|
||||||
|
send(response);
|
||||||
|
}
|
||||||
|
disconnect();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new NodeException("Command 'version' or 'verack' expected, but was '"
|
||||||
|
+ payload.getCommand() + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleVersion(Version version) {
|
||||||
|
if (version.getNonce() == ctx.getClientNonce()) {
|
||||||
|
LOG.info("Tried to connect to self, disconnecting.");
|
||||||
|
disconnect();
|
||||||
|
} else if (version.getVersion() >= BitmessageContext.CURRENT_VERSION) {
|
||||||
|
Connection.this.version = version.getVersion();
|
||||||
|
streams = version.getStreams();
|
||||||
|
send(new VerAck());
|
||||||
|
switch (mode) {
|
||||||
|
case SERVER:
|
||||||
|
send(new Version.Builder().defaults().addrFrom(host).addrRecv(node).build());
|
||||||
|
break;
|
||||||
|
case CLIENT:
|
||||||
|
case SYNC:
|
||||||
|
activateConnection();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// NO OP
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOG.info("Received unsupported version " + version.getVersion() + ", disconnecting.");
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkOpenRequests() {
|
private boolean checkOpenRequests() {
|
||||||
@ -450,10 +470,10 @@ class Connection {
|
|||||||
try (Socket socket = Connection.this.socket) {
|
try (Socket socket = Connection.this.socket) {
|
||||||
initSocket(socket);
|
initSocket(socket);
|
||||||
while (state != DISCONNECTED) {
|
while (state != DISCONNECTED) {
|
||||||
if (!sendingQueue.isEmpty()) {
|
if (sendingQueue.isEmpty()) {
|
||||||
send(sendingQueue.poll());
|
|
||||||
} else {
|
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
} else {
|
||||||
|
send(sendingQueue.poll());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user