Remove Akka-based seed node infrastructure (for now)

In favor of using the simplified app.cli.SeedNode class while we
continue to debug basic UPnP functionality.
This commit is contained in:
Chris Beams 2014-11-09 23:53:59 +01:00
parent d062e1dbbf
commit 20db54e87b
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
13 changed files with 1 additions and 517 deletions

View File

@ -33,7 +33,6 @@ repositories {
dependencies {
compile 'org.bitcoinj:bitcoinj-core:0.12'
compile 'net.tomp2p:tomp2p-all:5.0-Alpha.32cd9f9-SNAPSHOT'
compile 'com.typesafe.akka:akka-actor_2.10:2.3.4'
compile 'org.slf4j:slf4j-api:1.7.7'
compile 'ch.qos.logback:logback-core:1.1.2'
compile 'ch.qos.logback:logback-classic:1.1.2'

View File

@ -38,17 +38,10 @@ import java.util.Properties;
import net.tomp2p.connection.Ports;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorSystem;
import scala.concurrent.duration.Duration;
/**
* Configures all non-UI modules necessary to run a Bitsquare application.
*/
public class AppModule extends BitsquareModule {
private static final Logger log = LoggerFactory.getLogger(AppModule.class);
public AppModule(Properties properties) {
super(properties);
@ -70,7 +63,6 @@ public class AppModule extends BitsquareModule {
Preconditions.checkArgument(appName != null, "App name must be non-null");
bindConstant().annotatedWith(Names.named("appName")).to(appName);
bind(ActorSystem.class).toInstance(ActorSystem.create(appName));
int randomPort = new Ports().tcpPort();
bindConstant().annotatedWith(Names.named("clientPort")).to(randomPort);
@ -98,13 +90,6 @@ public class AppModule extends BitsquareModule {
@Override
protected void doClose(Injector injector) {
ActorSystem actorSystem = injector.getInstance(ActorSystem.class);
actorSystem.shutdown();
try {
actorSystem.awaitTermination(Duration.create(5L, "seconds"));
} catch (Exception ex) {
log.error("Actor system failed to shut down properly", ex);
}
}
}

View File

@ -26,7 +26,6 @@ public class ArgumentParser {
public static final String SEED_ID_FLAG = "id";
public static final String SEED_IP_FLAG = "ip";
public static final String SEED_PORT_FLAG = "port";
public static final String INTERFACE_HINT_FLAG = "interface";
public static final String NAME_FLAG = "name";
private final net.sourceforge.argparse4j.inf.ArgumentParser parser;
@ -43,8 +42,6 @@ public class ArgumentParser {
.help("Seed node IP");
parser.addArgument("-p", "--" + SEED_PORT_FLAG)
.help("Seed node port");
parser.addArgument("-i", "--" + INTERFACE_HINT_FLAG)
.help("Network interface to listen on");
// Args for app config
parser.addArgument("-n", "--" + NAME_FLAG)

View File

@ -1,111 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.app.cli;
import io.bitsquare.app.ArgumentParser;
import io.bitsquare.msg.actor.DHTManager;
import io.bitsquare.msg.actor.command.InitializePeer;
import io.bitsquare.msg.actor.event.PeerInitialized;
import io.bitsquare.network.BootstrapNodes;
import io.bitsquare.network.Node;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Inbox;
import net.sourceforge.argparse4j.inf.Namespace;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
public class SeedNodeUsingAkka {
private static final Logger log = LoggerFactory.getLogger(SeedNodeUsingAkka.class);
private static String interfaceHint;
public static void main(String[] args) {
ArgumentParser parser = new ArgumentParser();
Namespace namespace = parser.parseArgs(args);
if (namespace.getString(ArgumentParser.INTERFACE_HINT_FLAG) != null)
interfaceHint = namespace.getString(ArgumentParser.INTERFACE_HINT_FLAG);
int serverPort = Integer.valueOf(namespace.getString(ArgumentParser.SEED_PORT_FLAG));
String seedID = BootstrapNodes.LOCALHOST.getId();
if (namespace.getString(ArgumentParser.SEED_ID_FLAG) != null) {
seedID = namespace.getString(ArgumentParser.SEED_ID_FLAG);
}
final Set<PeerAddress> peerAddresses = new HashSet<>();
for (Node node : BootstrapNodes.all()) {
if (!node.getId().equals(seedID)) {
try {
peerAddresses.add(new PeerAddress(Number160.createHash(node.getId()), node.getIp(),
node.getPort(), node.getPort()));
} catch (UnknownHostException uhe) {
log.error("Unknown Host [" + node.getIp() + "]: " + uhe.getMessage());
}
}
}
ActorSystem actorSystem = ActorSystem.create("BitsquareSeedNode");
Inbox inbox = Inbox.create(actorSystem);
ActorRef seedNode = actorSystem.actorOf(DHTManager.getProps(), DHTManager.SEED_NODE);
inbox.send(seedNode, new InitializePeer(Number160.createHash(seedID), serverPort, interfaceHint,
peerAddresses));
final String _seedID = seedID;
Thread seedNodeThread = new Thread(() -> {
Boolean quit = false;
while (!quit) {
try {
Object m = inbox.receive(FiniteDuration.create(5L, "seconds"));
if (m instanceof PeerInitialized) {
log.debug("Seed Peer with ID " + _seedID +
" initialized on port " + ((PeerInitialized) m).getPort());
}
} catch (Exception e) {
if (!(e instanceof TimeoutException)) {
quit = true;
log.error(e.getMessage());
}
}
}
actorSystem.shutdown();
try {
actorSystem.awaitTermination(Duration.create(5L, "seconds"));
} catch (Exception ex) {
if (ex instanceof TimeoutException)
log.error("ActorSystem did not shutdown properly.");
else
log.error(ex.getMessage());
}
});
seedNodeThread.start();
}
}

View File

@ -91,7 +91,6 @@ import static org.bitcoinj.script.ScriptOpCodes.OP_RETURN;
/**
* TODO: use walletextension (with protobuffer) instead of saving addressEntryList via storage
* TODO: break that class up. maybe a bitsquarewallet
* Wait until steve's akka version to see how to continue here
*/
public class WalletFacade {
private static final Logger log = LoggerFactory.getLogger(WalletFacade.class);

View File

@ -1,86 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.msg;
import io.bitsquare.util.MessageHandler;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Inbox;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import com.sun.glass.ui.Application;
import scala.concurrent.duration.FiniteDuration;
public abstract class ActorService extends Service<String> {
private final LoggingAdapter log;
private final ActorSystem system;
private final Inbox inbox;
private final ActorSelection actor;
private MessageHandler handler;
protected ActorService(ActorSystem system, String actorPath) {
this.log = Logging.getLogger(system, this);
this.system = system;
this.inbox = Inbox.create(system);
this.actor = system.actorSelection(actorPath);
log.debug(actor.pathString());
this.start();
}
public void setHandler(MessageHandler handler) {
this.handler = handler;
}
public void send(Object command) {
if (actor != null) {
actor.tell(command, inbox.getRef());
}
}
protected Task<String> createTask() {
return new Task<String>() {
protected String call() throws Exception {
while (!isCancelled()) {
if (inbox != null) {
try {
Object result = inbox.receive(FiniteDuration.create(1l, "minute"));
if (result != null) {
System.out.println(result.toString());
if (handler != null) {
Application.invokeLater(() -> handler.handle(result));
}
}
} catch (Exception e) {
//System.out.println(e.toString());
}
}
}
return null;
}
};
}
}

View File

@ -1,41 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.msg;
import io.bitsquare.msg.actor.DHTManager;
import io.bitsquare.msg.actor.command.InitializePeer;
import com.google.inject.Inject;
import net.tomp2p.peers.Number160;
import akka.actor.ActorSystem;
public class DHTSeedService extends ActorService {
@Inject
public DHTSeedService(ActorSystem system) {
super(system, "/user/" + DHTManager.SEED_NODE);
}
public void initializePeer(String id, Integer port) {
// TODO hard coded seed peer config for now, should read from config properties file
send(new InitializePeer(Number160.createHash(id), port, null, null));
}
}

View File

@ -18,8 +18,6 @@
package io.bitsquare.msg;
import io.bitsquare.BitsquareModule;
import io.bitsquare.app.ArgumentParser;
import io.bitsquare.network.BootstrapNodes;
import io.bitsquare.network.Node;
import com.google.inject.Injector;
@ -39,7 +37,6 @@ public abstract class MessageModule extends BitsquareModule {
@Override
protected final void configure() {
bind(MessageFacade.class).to(messageFacade()).asEagerSingleton();
bind(DHTSeedService.class);
// we will probably later use disk storage instead of memory storage for TomP2P
bind(Boolean.class).annotatedWith(Names.named("useDiskStorage")).toInstance(false);

View File

@ -1,135 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.msg.actor;
import io.bitsquare.msg.actor.command.InitializePeer;
import io.bitsquare.msg.actor.event.PeerInitialized;
import net.tomp2p.connection.Bindings;
import net.tomp2p.connection.StandardProtocolFamily;
import net.tomp2p.dht.PeerBuilderDHT;
import net.tomp2p.dht.PeerDHT;
import net.tomp2p.nat.PeerBuilderNAT;
import net.tomp2p.nat.PeerNAT;
import net.tomp2p.p2p.Peer;
import net.tomp2p.p2p.PeerBuilder;
import net.tomp2p.peers.PeerAddress;
import net.tomp2p.peers.PeerMapChangeListener;
import net.tomp2p.peers.PeerStatistic;
import akka.actor.AbstractActor;
import akka.actor.Props;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.japi.pf.ReceiveBuilder;
public class DHTManager extends AbstractActor {
public static final String MY_NODE = "myNodeDhtManager";
public static final String SEED_NODE = "seedNodeDhtManager";
private final LoggingAdapter log = Logging.getLogger(context().system(), this);
// TODO move into app setup
// timeout in ms
private final Long bootstrapTimeout = 10000L;
public static Props getProps() {
return Props.create(DHTManager.class);
}
private PeerDHT peerDHT;
private PeerNAT peerNAT;
public DHTManager() {
receive(ReceiveBuilder
.match(InitializePeer.class, this::doInitializePeer)
.matchAny(o -> log.info("received unknown message")).build()
);
}
private void doInitializePeer(InitializePeer initializePeer) {
log.debug("Received message: {}", initializePeer);
try {
Bindings bindings = new Bindings();
bindings.addProtocol(StandardProtocolFamily.INET);
if (initializePeer.getInterfaceHint() != null) {
bindings.addInterface(initializePeer.getInterfaceHint());
}
Peer peer = new PeerBuilder(initializePeer.getPeerId()).ports(initializePeer.getPort()).bindings(bindings)
.start();
peer.objectDataReply((sender, request) -> {
log.debug("received request: ", request.toString());
return "pong";
});
// For the moment we want not to bootstrap to other seed nodes to keep test scenarios
// simple
/* if (ip.getBootstrapPeers() != null && ip.getBootstrapPeers().size() > 0) {
peer.bootstrap().bootstrapTo(ip.getBootstrapPeers()).start();
}*/
// Needed for DHT support
peerDHT = new PeerBuilderDHT(peer).start();
// Needed for NAT support
peerNAT = new PeerBuilderNAT(peer).start();
new PeerBuilderNAT(peer).start();
peer.peerBean().peerMap().addPeerMapChangeListener(new PeerMapChangeListener() {
@Override
public void peerInserted(PeerAddress peerAddress, boolean verified) {
log.debug("Peer inserted: peerAddress=" + peerAddress + ", " +
"verified=" + verified);
}
@Override
public void peerRemoved(PeerAddress peerAddress, PeerStatistic peerStatistics) {
log.debug("Peer removed: peerAddress=" + peerAddress + ", " +
"peerStatistics=" + peerStatistics);
}
@Override
public void peerUpdated(PeerAddress peerAddress, PeerStatistic peerStatistics) {
// log.debug("Peer updated: peerAddress=" + peerAddress + ",
// peerStatistics=" + peerStatistics);
}
});
sender().tell(new PeerInitialized(peer.peerID(), initializePeer.getPort()), self());
} catch (Throwable t) {
log.error(t.getMessage());
}
}
@Override
public void postStop() throws Exception {
log.debug("postStop");
if (peerDHT != null)
peerDHT.shutdown();
if (peerNAT != null)
peerNAT.natUtils().shutdown();
super.postStop();
}
}

View File

@ -1,60 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.msg.actor.command;
import java.util.Collection;
import net.tomp2p.peers.Number160;
import net.tomp2p.peers.PeerAddress;
/**
* <p>Command to initialize TomP2P Peer.</p>
*/
public class InitializePeer {
private final Number160 peerId;
private final Integer port;
private final String interfaceHint;
private final Collection<PeerAddress> bootstrapPeers;
public InitializePeer(Number160 peerId, Integer port, String interfaceHint,
Collection<PeerAddress> bootstrapPeers) {
this.peerId = peerId;
this.port = port;
this.interfaceHint = interfaceHint;
this.bootstrapPeers = bootstrapPeers;
}
public Number160 getPeerId() {
return peerId;
}
public Integer getPort() {
return port;
}
public String getInterfaceHint() {
return interfaceHint;
}
public Collection<PeerAddress> getBootstrapPeers() {
return bootstrapPeers;
}
}

View File

@ -1,43 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.msg.actor.event;
import net.tomp2p.peers.Number160;
/**
* <p>TomP2P Peer Initialized event.</p>
*/
public class PeerInitialized {
private final Number160 peerId;
private final Integer port;
public PeerInitialized(Number160 peerId, Integer port) {
this.peerId = peerId;
this.port = port;
}
public Number160 getPeerId() {
return peerId;
}
public Integer getPort() {
return port;
}
}

View File

@ -319,7 +319,7 @@ public class SellerTakesOfferProtocol {
log.debug("state " + state);
checkState(state.ordinal() >= State.SendSignedTakerDepositTxAsHex.ordinal());
checkArgument(tradeId.equals(message.getTradeId()));
//TODO takerCommitDepositTx should be in task as well, but will be probably changed anyway when akka is used...
//TODO takerCommitDepositTx should be in task as well
Transaction tx = walletFacade.takerCommitDepositTx(message.getDepositTxAsHex());
listener.onDepositTxPublished(tx);
}

View File

@ -1,17 +0,0 @@
akka {
# Loggers to register at boot time (akka.event.Logging$DefaultLogger logs
# to STDOUT)
#loggers = ["akka.event.slf4j.Slf4jLogger"]
# Log level used by the configured loggers (see "loggers") as soon
# as they have been started; before that, see "stdout-loglevel"
# Options: OFF, ERROR, WARNING, INFO, DEBUG
loglevel = "DEBUG"
# Log level for the very basic logger activated during ActorSystem startup.
# This logger prints the log messages to stdout (System.out).
# Options: OFF, ERROR, WARNING, INFO, DEBUG
stdout-loglevel = "DEBUG"
}