use deamon for seednode

This commit is contained in:
Manfred Karrer 2014-08-08 12:48:49 +02:00
parent af30d89921
commit 7d72122a91
6 changed files with 222 additions and 97 deletions

View file

@ -1,6 +1,7 @@
package io.bitsquare;
import io.bitsquare.msg.SeedNodeAddress;
import java.io.IOException;
import java.util.List;
import net.tomp2p.dht.PeerBuilderDHT;
import net.tomp2p.futures.BaseFuture;
@ -25,20 +26,17 @@ import org.slf4j.LoggerFactory;
* <p>
* TODO: Alternative bootstrap methods will follow later (save locally list of known nodes reported form other peers,...)
*/
public class SeedNode
public class SeedNode extends Thread
{
private static final Logger log = LoggerFactory.getLogger(SeedNode.class);
private static final List<SeedNodeAddress.StaticSeedNodeAddresses> staticSedNodeAddresses = SeedNodeAddress.StaticSeedNodeAddresses.getAllSeedNodeAddresses();
/**
* @param args If no args passed we use localhost, otherwise the param is used as index for selecting an address from seedNodeAddresses
* @throws Exception
*/
public static void main(String[] args)
{
int index = 0;
SeedNode seedNode = new SeedNode();
if (args.length > 0)
{
// use host index passes as param
@ -46,24 +44,32 @@ public class SeedNode
if (param < staticSedNodeAddresses.size())
index = param;
}
SeedNode seedNode = new SeedNode(new SeedNodeAddress(staticSedNodeAddresses.get(index)));
seedNode.setDaemon(true);
seedNode.start();
try
{
seedNode.startupUsingAddress(new SeedNodeAddress(staticSedNodeAddresses.get(index)));
} catch (Exception e)
// keep main thread up
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e)
{
e.printStackTrace();
log.error(e.toString());
}
}
private final SeedNodeAddress seedNodeAddress;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
public SeedNode()
public SeedNode(SeedNodeAddress seedNodeAddress)
{
this.seedNodeAddress = seedNodeAddress;
}
@ -71,11 +77,30 @@ public class SeedNode
// Public Methods
///////////////////////////////////////////////////////////////////////////////////////////
public void startupUsingAddress(SeedNodeAddress seedNodeAddress)
public void run()
{
Peer peer = startupPeer();
for (; ; )
{
try
{
// ping(peer);
Thread.sleep(300);
} catch (InterruptedException e)
{
log.error(e.toString());
}
}
}
public Peer startupPeer()
{
Peer peer = null;
try
{
Peer peer = new PeerBuilder(Number160.createHash(seedNodeAddress.getId())).ports(seedNodeAddress.getPort()).start();
peer = new PeerBuilder(Number160.createHash(seedNodeAddress.getId())).ports(seedNodeAddress.getPort()).start();
// Need to add all features the clients will use (otherwise msg type is UNKNOWN_ID)
new PeerBuilderDHT(peer).start();
@ -106,64 +131,69 @@ public class SeedNode
log.debug("Peer updated: peerAddress=" + peerAddress + ", peerStatistics=" + peerStatistics);
}
});
} catch (IOException e)
{
e.printStackTrace();
}
return peer;
}
// We keep server in endless loop
for (; ; )
private void ping(Peer peer)
{
if (peer != null)
return;
try
{
// Optional pinging
for (PeerAddress peerAddress : peer.peerBean().peerMap().all())
{
// Optional pinging
boolean pingPeers = false;
if (pingPeers)
BaseFuture future = peer.ping().peerAddress(peerAddress).tcpPing().start();
future.addListener(new BaseFutureListener<BaseFuture>()
{
for (PeerAddress peerAddress : peer.peerBean().peerMap().all())
@Override
public void operationComplete(BaseFuture future) throws Exception
{
BaseFuture future = peer.ping().peerAddress(peerAddress).tcpPing().start();
future.addListener(new BaseFutureListener<BaseFuture>()
if (future.isSuccess())
{
@Override
public void operationComplete(BaseFuture future) throws Exception
{
if (future.isSuccess())
{
log.debug("peer online (TCP):" + peerAddress);
}
else
{
log.debug("offline " + peerAddress);
}
}
@Override
public void exceptionCaught(Throwable t) throws Exception
{
log.error("exceptionCaught " + t);
}
});
future = peer.ping().peerAddress(peerAddress).start();
future.addListener(new BaseFutureListener<BaseFuture>()
log.debug("peer online (TCP):" + peerAddress);
}
else
{
@Override
public void operationComplete(BaseFuture future) throws Exception
{
if (future.isSuccess())
{
log.debug("peer online (UDP):" + peerAddress);
}
else
{
log.debug("offline " + peerAddress);
}
}
@Override
public void exceptionCaught(Throwable t) throws Exception
{
log.error("exceptionCaught " + t);
}
});
log.debug("offline " + peerAddress);
}
}
Thread.sleep(1500);
}
@Override
public void exceptionCaught(Throwable t) throws Exception
{
log.error("exceptionCaught " + t);
}
});
future = peer.ping().peerAddress(peerAddress).start();
future.addListener(new BaseFutureListener<BaseFuture>()
{
@Override
public void operationComplete(BaseFuture future) throws Exception
{
if (future.isSuccess())
{
log.debug("peer online (UDP):" + peerAddress);
}
else
{
log.debug("offline " + peerAddress);
}
}
@Override
public void exceptionCaught(Throwable t) throws Exception
{
log.error("exceptionCaught " + t);
}
});
Thread.sleep(1500);
}
} catch (Exception e)
{

View file

@ -1,29 +0,0 @@
package io.bitsquare.di;
import com.google.inject.Injector;
import javafx.util.Callback;
/**
* A JavaFX controller factory for constructing controllers via Guice DI. To
* install this in the {@link javafx.fxml.FXMLLoader}, pass it as a parameter to
* {@link javafx.fxml.FXMLLoader#setControllerFactory(Callback)}.
* <p>
* Once set, make sure you do <b>not</b> use the static methods on
* {@link javafx.fxml.FXMLLoader} when creating your JavaFX node.
*/
class GuiceControllerFactory implements Callback<Class<?>, Object>
{
private final Injector injector;
public GuiceControllerFactory(Injector injector)
{
this.injector = injector;
}
@Override
public Object call(Class<?> aClass)
{
return injector.getInstance(aClass);
}
}

View file

@ -4,6 +4,7 @@ import com.google.inject.Injector;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXMLLoader;
import javafx.util.Callback;
/**
* Guice support for fxml controllers
@ -16,10 +17,12 @@ public class GuiceFXMLLoader extends FXMLLoader
{
GuiceFXMLLoader.injector = injector;
}
// private static ClassLoader cachingClassLoader = new CachingClassLoader(FXMLLoader.getDefaultClassLoader());
public GuiceFXMLLoader(URL url, ResourceBundle resourceBundle)
{
super(url, resourceBundle);
// might be helpful for performance, but need further profiling. has memory drawbacks
//setClassLoader(cachingClassLoader);
setupControllerFactory();
}
@ -30,5 +33,30 @@ public class GuiceFXMLLoader extends FXMLLoader
setControllerFactory(new GuiceControllerFactory(GuiceFXMLLoader.injector));
}
}
}
/**
* A JavaFX controller factory for constructing controllers via Guice DI. To
* install this in the {@link javafx.fxml.FXMLLoader}, pass it as a parameter to
* {@link javafx.fxml.FXMLLoader#setControllerFactory(javafx.util.Callback)}.
* <p>
* Once set, make sure you do <b>not</b> use the static methods on
* {@link javafx.fxml.FXMLLoader} when creating your JavaFX node.
*/
class GuiceControllerFactory implements Callback<Class<?>, Object>
{
private final Injector injector;
public GuiceControllerFactory(Injector injector)
{
this.injector = injector;
}
@Override
public Object call(Class<?> aClass)
{
return injector.getInstance(aClass);
}
}

View file

@ -45,7 +45,6 @@ public class LazyLoadingTabPane extends TabPane
throw new IllegalArgumentException("No tabContentFXMLUrls defined");
}
this.tabContentFXMLUrls = tabContentFXMLUrls;
this.navigationController = navigationController;
this.tabContentFXMLUrls = tabContentFXMLUrls;
this.persistence = persistence;
@ -117,6 +116,7 @@ public class LazyLoadingTabPane extends TabPane
try
{
view = loader.load();
log.debug("######## " + view.toString());
views.put(index, view);
childController = loader.getController();

View file

@ -57,14 +57,12 @@ class ViewModel
final StringProperty directionLabel = new SimpleStringProperty();
final StringProperty collateralLabel = new SimpleStringProperty();
final StringProperty feeLabel = new SimpleStringProperty();
final StringProperty bankAccountType = new SimpleStringProperty();
final StringProperty bankAccountCurrency = new SimpleStringProperty();
final StringProperty bankAccountCounty = new SimpleStringProperty();
final StringProperty acceptedCountries = new SimpleStringProperty();
final StringProperty acceptedLanguages = new SimpleStringProperty();
final StringProperty transactionId = new SimpleStringProperty();
final BooleanProperty isOfferPlacedScreen = new SimpleBooleanProperty();
final BooleanProperty isPlaceOfferButtonDisabled = new SimpleBooleanProperty();
}
@ -149,7 +147,6 @@ public class CreateOfferController implements Initializable, ChildController, Hi
// Interface implementation: Initializable
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void initialize(URL url, ResourceBundle rb)
{
@ -232,6 +229,7 @@ public class CreateOfferController implements Initializable, ChildController, Hi
///////////////////////////////////////////////////////////////////////////////////////////
// UI Handlers
///////////////////////////////////////////////////////////////////////////////////////////
@FXML
public void onPlaceOffer()
{

View file

@ -0,0 +1,98 @@
package io.bitsquare.util;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class CachingClassLoader extends ClassLoader
{
private final Map<String, Class> classes = new HashMap<String, Class>();
private final ClassLoader parent;
public CachingClassLoader(ClassLoader parent)
{
this.parent = parent;
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException
{
Class<?> c = findClass(name);
if (c == null)
{
throw new ClassNotFoundException(name);
}
return c;
}
@Override
protected Class<?> findClass(String className) throws ClassNotFoundException
{
if (classes.containsKey(className))
{
// System.out.print("############## cached " + className);
Class<?> result = classes.get(className);
return result;
}
else
{
try
{
Class<?> result = parent.loadClass(className);
System.out.print("############## not cached " + className);
classes.put(className, result);
return result;
} catch (ClassNotFoundException ignore)
{
// System.out.println();
classes.put(className, null);
return null;
}
}
}
// ========= delegating methods =============
@Override
public URL getResource(String name)
{
return parent.getResource(name);
}
@Override
public Enumeration<URL> getResources(String name) throws IOException
{
return parent.getResources(name);
}
@Override
public String toString()
{
return parent.toString();
}
@Override
public void setDefaultAssertionStatus(boolean enabled)
{
parent.setDefaultAssertionStatus(enabled);
}
@Override
public void setPackageAssertionStatus(String packageName, boolean enabled)
{
parent.setPackageAssertionStatus(packageName, enabled);
}
@Override
public void setClassAssertionStatus(String className, boolean enabled)
{
parent.setClassAssertionStatus(className, enabled);
}
@Override
public void clearAssertionStatus()
{
parent.clearAssertionStatus();
}
}