Added option to connect to local Bitmessage client
(This makes it easier to debug some problem or make some tests)
This commit is contained in:
parent
92229151a5
commit
505818a712
@ -17,14 +17,10 @@
|
||||
package ch.dissem.bitmessage.demo;
|
||||
|
||||
import ch.dissem.bitmessage.BitmessageContext;
|
||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||
import ch.dissem.bitmessage.entity.Plaintext;
|
||||
import ch.dissem.bitmessage.entity.payload.Pubkey;
|
||||
import ch.dissem.bitmessage.entity.valueobject.Label;
|
||||
import ch.dissem.bitmessage.networking.nio.NioNetworkHandler;
|
||||
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
||||
import ch.dissem.bitmessage.repository.*;
|
||||
import org.apache.commons.lang3.text.WordUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -45,19 +41,10 @@ public class Application {
|
||||
|
||||
private BitmessageContext ctx;
|
||||
|
||||
public Application(InetAddress syncServer, int syncPort) {
|
||||
JdbcConfig jdbcConfig = new JdbcConfig();
|
||||
ctx = new BitmessageContext.Builder()
|
||||
.addressRepo(new JdbcAddressRepository(jdbcConfig))
|
||||
.inventory(new JdbcInventory(jdbcConfig))
|
||||
.nodeRegistry(new MemoryNodeRegistry())
|
||||
.messageRepo(new JdbcMessageRepository(jdbcConfig))
|
||||
.powRepo(new JdbcProofOfWorkRepository(jdbcConfig))
|
||||
.networkHandler(new NioNetworkHandler())
|
||||
.cryptography(new BouncyCryptography())
|
||||
.port(48444)
|
||||
.listener(plaintext -> System.out.println("New Message from " + plaintext.getFrom() + ": " + plaintext.getSubject()))
|
||||
.build();
|
||||
public Application(BitmessageContext.Builder ctxBuilder, InetAddress syncServer, int syncPort) {
|
||||
ctx = ctxBuilder
|
||||
.listener(plaintext -> System.out.println("New Message from " + plaintext.getFrom() + ": " + plaintext.getSubject()))
|
||||
.build();
|
||||
|
||||
if (syncServer == null) {
|
||||
ctx.startup();
|
||||
@ -392,7 +379,7 @@ public class Application {
|
||||
System.out.println(WordUtils.wrap(message.getText(), 120));
|
||||
System.out.println();
|
||||
System.out.println(message.getLabels().stream().map(Label::toString).collect(
|
||||
Collectors.joining(", ", "Labels: ", "")));
|
||||
Collectors.joining(", ", "Labels: ", "")));
|
||||
System.out.println();
|
||||
ctx.labeler().markAsRead(message);
|
||||
ctx.messages().save(message);
|
||||
|
@ -18,20 +18,29 @@ package ch.dissem.bitmessage.demo;
|
||||
|
||||
import ch.dissem.bitmessage.BitmessageContext;
|
||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
||||
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
||||
import ch.dissem.bitmessage.networking.nio.NioNetworkHandler;
|
||||
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
||||
import ch.dissem.bitmessage.ports.NodeRegistry;
|
||||
import ch.dissem.bitmessage.repository.*;
|
||||
import ch.dissem.bitmessage.wif.WifExporter;
|
||||
import ch.dissem.bitmessage.wif.WifImporter;
|
||||
import org.kohsuke.args4j.CmdLineException;
|
||||
import org.kohsuke.args4j.CmdLineParser;
|
||||
import org.kohsuke.args4j.Option;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (System.getProperty("org.slf4j.simpleLogger.defaultLogLevel") == null)
|
||||
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "ERROR");
|
||||
@ -45,18 +54,39 @@ public class Main {
|
||||
} catch (CmdLineException e) {
|
||||
parser.printUsage(System.err);
|
||||
}
|
||||
|
||||
JdbcConfig jdbcConfig = new JdbcConfig();
|
||||
BitmessageContext.Builder ctxBuilder = new BitmessageContext.Builder()
|
||||
.addressRepo(new JdbcAddressRepository(jdbcConfig))
|
||||
.inventory(new JdbcInventory(jdbcConfig))
|
||||
.messageRepo(new JdbcMessageRepository(jdbcConfig))
|
||||
.powRepo(new JdbcProofOfWorkRepository(jdbcConfig))
|
||||
.networkHandler(new NioNetworkHandler())
|
||||
.cryptography(new BouncyCryptography())
|
||||
.port(48444);
|
||||
if (options.localPort != null) {
|
||||
ctxBuilder.nodeRegistry(new NodeRegistry() {
|
||||
@Override
|
||||
public List<NetworkAddress> getKnownAddresses(int limit, long... streams) {
|
||||
return Arrays.stream(streams)
|
||||
.mapToObj(s -> new NetworkAddress.Builder()
|
||||
.ipv4(127, 0, 0, 1)
|
||||
.port(options.localPort)
|
||||
.stream(s).build())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void offerAddresses(List<NetworkAddress> addresses) {
|
||||
LOG.info("Local node registry ignored offered addresses: " + addresses);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
ctxBuilder.nodeRegistry(new MemoryNodeRegistry());
|
||||
}
|
||||
|
||||
if (options.exportWIF != null || options.importWIF != null) {
|
||||
JdbcConfig jdbcConfig = new JdbcConfig();
|
||||
BitmessageContext ctx = new BitmessageContext.Builder()
|
||||
.addressRepo(new JdbcAddressRepository(jdbcConfig))
|
||||
.inventory(new JdbcInventory(jdbcConfig))
|
||||
.nodeRegistry(new MemoryNodeRegistry())
|
||||
.messageRepo(new JdbcMessageRepository(jdbcConfig))
|
||||
.powRepo(new JdbcProofOfWorkRepository(jdbcConfig))
|
||||
.networkHandler(new NioNetworkHandler())
|
||||
.cryptography(new BouncyCryptography())
|
||||
.port(48444)
|
||||
.build();
|
||||
BitmessageContext ctx = ctxBuilder.build();
|
||||
|
||||
if (options.exportWIF != null) {
|
||||
new WifExporter(ctx).addAll().write(options.exportWIF);
|
||||
@ -66,11 +96,14 @@ public class Main {
|
||||
}
|
||||
} else {
|
||||
InetAddress syncServer = options.syncServer == null ? null : InetAddress.getByName(options.syncServer);
|
||||
new Application(syncServer, options.syncPort);
|
||||
new Application(ctxBuilder, syncServer, options.syncPort);
|
||||
}
|
||||
}
|
||||
|
||||
private static class CmdLineOptions {
|
||||
@Option(name = "-local", usage = "Connect to local Bitmessage client on given port, instead of the usual connections from node.txt")
|
||||
private Integer localPort;
|
||||
|
||||
@Option(name = "-import", usage = "Import from keys.dat or other WIF file.")
|
||||
private File importWIF;
|
||||
|
||||
|
@ -49,7 +49,9 @@ import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* FIXME: there really should be sensible tests for the network handler
|
||||
* Tests network handlers. This test is parametrized, so it can test both the nio and classic implementation
|
||||
* as well as their combinations. It might be slightly over the top and will most probably be cleaned up once
|
||||
* the nio implementation is deemed stable.
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class NetworkHandlerTest {
|
||||
@ -76,10 +78,10 @@ public class NetworkHandlerTest {
|
||||
@Parameterized.Parameters
|
||||
public static List<Object[]> parameters() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{new DefaultNetworkHandler(), new DefaultNetworkHandler()},
|
||||
{new DefaultNetworkHandler(), new NioNetworkHandler()},
|
||||
{new NioNetworkHandler(), new DefaultNetworkHandler()},
|
||||
{new NioNetworkHandler(), new NioNetworkHandler()}
|
||||
{new DefaultNetworkHandler(), new DefaultNetworkHandler()},
|
||||
{new DefaultNetworkHandler(), new NioNetworkHandler()},
|
||||
{new NioNetworkHandler(), new DefaultNetworkHandler()},
|
||||
{new NioNetworkHandler(), new NioNetworkHandler()}
|
||||
});
|
||||
}
|
||||
|
||||
@ -87,50 +89,50 @@ public class NetworkHandlerTest {
|
||||
public void setUp() {
|
||||
peerInventory = new TestInventory();
|
||||
peer = new BitmessageContext.Builder()
|
||||
.addressRepo(mock(AddressRepository.class))
|
||||
.inventory(peerInventory)
|
||||
.messageRepo(mock(MessageRepository.class))
|
||||
.powRepo(mock(ProofOfWorkRepository.class))
|
||||
.port(peerAddress.getPort())
|
||||
.nodeRegistry(new TestNodeRegistry())
|
||||
.networkHandler(peerNetworkHandler)
|
||||
.cryptography(new BouncyCryptography())
|
||||
.listener(mock(BitmessageContext.Listener.class))
|
||||
.customCommandHandler(new CustomCommandHandler() {
|
||||
@Override
|
||||
public MessagePayload handle(CustomMessage request) {
|
||||
byte[] data = request.getData();
|
||||
if (data.length > 0) {
|
||||
switch (data[0]) {
|
||||
case 0:
|
||||
return null;
|
||||
case 1:
|
||||
break;
|
||||
case 3:
|
||||
data[0] = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
.addressRepo(mock(AddressRepository.class))
|
||||
.inventory(peerInventory)
|
||||
.messageRepo(mock(MessageRepository.class))
|
||||
.powRepo(mock(ProofOfWorkRepository.class))
|
||||
.port(peerAddress.getPort())
|
||||
.nodeRegistry(new TestNodeRegistry())
|
||||
.networkHandler(peerNetworkHandler)
|
||||
.cryptography(new BouncyCryptography())
|
||||
.listener(mock(BitmessageContext.Listener.class))
|
||||
.customCommandHandler(new CustomCommandHandler() {
|
||||
@Override
|
||||
public MessagePayload handle(CustomMessage request) {
|
||||
byte[] data = request.getData();
|
||||
if (data.length > 0) {
|
||||
switch (data[0]) {
|
||||
case 0:
|
||||
return null;
|
||||
case 1:
|
||||
break;
|
||||
case 3:
|
||||
data[0] = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return new CustomMessage("test response", request.getData());
|
||||
}
|
||||
})
|
||||
.build();
|
||||
return new CustomMessage("test response", request.getData());
|
||||
}
|
||||
})
|
||||
.build();
|
||||
peer.startup();
|
||||
|
||||
nodeInventory = new TestInventory();
|
||||
node = new BitmessageContext.Builder()
|
||||
.addressRepo(mock(AddressRepository.class))
|
||||
.inventory(nodeInventory)
|
||||
.messageRepo(mock(MessageRepository.class))
|
||||
.powRepo(mock(ProofOfWorkRepository.class))
|
||||
.port(6002)
|
||||
.nodeRegistry(new TestNodeRegistry(peerAddress))
|
||||
.networkHandler(nodeNetworkHandler)
|
||||
.cryptography(new BouncyCryptography())
|
||||
.listener(mock(BitmessageContext.Listener.class))
|
||||
.build();
|
||||
.addressRepo(mock(AddressRepository.class))
|
||||
.inventory(nodeInventory)
|
||||
.messageRepo(mock(MessageRepository.class))
|
||||
.powRepo(mock(ProofOfWorkRepository.class))
|
||||
.port(6002)
|
||||
.nodeRegistry(new TestNodeRegistry(peerAddress))
|
||||
.networkHandler(nodeNetworkHandler)
|
||||
.cryptography(new BouncyCryptography())
|
||||
.listener(mock(BitmessageContext.Listener.class))
|
||||
.build();
|
||||
}
|
||||
|
||||
@After
|
||||
@ -162,7 +164,7 @@ public class NetworkHandlerTest {
|
||||
} catch (InterruptedException ignore) {
|
||||
if (networkHandler.isRunning()) {
|
||||
LOG.warn("Thread interrupted while waiting for network shutdown - " +
|
||||
"this could cause problems in subsequent tests.");
|
||||
"this could cause problems in subsequent tests.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -219,18 +221,18 @@ public class NetworkHandlerTest {
|
||||
@Test
|
||||
public void ensureObjectsAreSynchronizedIfBothHaveObjects() throws Exception {
|
||||
peerInventory.init(
|
||||
"V4Pubkey.payload",
|
||||
"V5Broadcast.payload"
|
||||
"V4Pubkey.payload",
|
||||
"V5Broadcast.payload"
|
||||
);
|
||||
|
||||
nodeInventory.init(
|
||||
"V1Msg.payload",
|
||||
"V4Pubkey.payload"
|
||||
"V1Msg.payload",
|
||||
"V4Pubkey.payload"
|
||||
);
|
||||
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(),
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
future.get();
|
||||
assertInventorySize(3, nodeInventory);
|
||||
assertInventorySize(3, peerInventory);
|
||||
@ -239,15 +241,15 @@ public class NetworkHandlerTest {
|
||||
@Test
|
||||
public void ensureObjectsAreSynchronizedIfOnlyPeerHasObjects() throws Exception {
|
||||
peerInventory.init(
|
||||
"V4Pubkey.payload",
|
||||
"V5Broadcast.payload"
|
||||
"V4Pubkey.payload",
|
||||
"V5Broadcast.payload"
|
||||
);
|
||||
|
||||
nodeInventory.init();
|
||||
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(),
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
future.get();
|
||||
assertInventorySize(2, nodeInventory);
|
||||
assertInventorySize(2, peerInventory);
|
||||
@ -258,12 +260,12 @@ public class NetworkHandlerTest {
|
||||
peerInventory.init();
|
||||
|
||||
nodeInventory.init(
|
||||
"V1Msg.payload"
|
||||
"V1Msg.payload"
|
||||
);
|
||||
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(),
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
future.get();
|
||||
assertInventorySize(1, nodeInventory);
|
||||
assertInventorySize(1, peerInventory);
|
||||
|
Loading…
Reference in New Issue
Block a user