/** Created with IntelliJ IDEA. User: uriunger Date: 2/7/14 Time: 10:26 PM */
public class ProvidedInstanceBitcoindCallingPolicy extends AbstractCallingPolicy
    implements BitcoindCallingPolicy {

  private final Logger logger = LoggerFactory.getLogger(getClass());

  private final Configuration configuration;
  private final Herder<Configuration> herder;

  public ProvidedInstanceBitcoindCallingPolicy(
      Herder<Configuration> herder,
      Configuration configuration,
      IGlobalResponseCache responseCache) {
    super(responseCache);
    this.configuration = configuration;
    this.herder = herder;
  }

  @Override
  public Map callServer(RpcRequest rpcRequest) {
    try {
      herder.take(configuration);
      logger.tracef("id=%d, took: %s", getId(), configuration);
      return callServer(rpcRequest, configuration);
    } catch (Exception e) {
      throw BitcoindCallException.wrap(e);
    } finally {
      herder.offer(configuration);
      logger.tracef("id=%d, offer: %s", getId(), configuration);
    }
  }
}
Ejemplo n.º 2
0
/** Created with IntelliJ IDEA. User: uriunger Date: 3/27/14 Time: 5:08 PM */
@Singleton
public class WatermarkCache {

  private Logger logger = LoggerFactory.getLogger(getClass());

  private final LoadingCache<Boolean, RDSPersistenceManager.Watermark> cache;

  @Inject
  public WatermarkCache(final RDSPersistenceManager rdsPersistenceManager) {
    cache =
        CacheBuilder.newBuilder()
            .expireAfterWrite(1, TimeUnit.SECONDS)
            .build(
                new CacheLoader<Boolean, RDSPersistenceManager.Watermark>() {
                  @Override
                  public RDSPersistenceManager.Watermark load(Boolean key) throws Exception {
                    return rdsPersistenceManager.getWatermark();
                  }
                });
  }

  public RDSPersistenceManager.Watermark get() throws ExecutionException {
    return cache.get(true); // true is dummy
  }
}
Ejemplo n.º 3
0
/** Created with IntelliJ IDEA. User: uriunger Date: 4/22/13 Time: 5:45 PM */
public class HiddenServerInitializer implements ServletContextListener {

  public static final String INJECTOR = "Injector";

  protected Logger logger = LoggerFactory.getLogger(getClass());

  private HiddenServerLogic hiddenServerLogic;
  private RollingProfitParticipantLogic rollingProfitParticipantLogic;
  private AddressManager addressManager;
  private BitcoindFacadeFactory bitcoindFacadeFactory;
  private BitcoindProcessManager bitcoindProcessManager;

  public void contextInitialized(ServletContextEvent servletContextEvent) {
    servletContextEvent.getServletContext().setAttribute(INJECTOR, startup());
  }

  public void contextDestroyed(ServletContextEvent servletContextEvent) {
    shutdown();
  }

  private Injector startup() {
    Injector injector;
    while (true) {
      try {
        injector = Guice.createInjector(new CommonModule(), new HiddenServerModule());

        addressManager = injector.getInstance(AddressManager.class);
        addressManager.start();

        // restart will use -rescan parameter so new imported addresses are available
        bitcoindProcessManager = injector.getInstance(BitcoindProcessManager.class);
        if (!bitcoindProcessManager.startAllBitcoindInstances()) {
          throw new RuntimeException("bitcoind instances not up");
        }
        bitcoindProcessManager.start();

        hiddenServerLogic = injector.getInstance(HiddenServerLogic.class);
        hiddenServerLogic.start();

        rollingProfitParticipantLogic = injector.getInstance(RollingProfitParticipantLogic.class);
        rollingProfitParticipantLogic.start();

        bitcoindFacadeFactory = injector.getInstance(BitcoindFacadeFactory.class);

        // consider testing profit & change addresses in P50PotConfig

        return injector;
      } catch (Throwable e) {
        logger.error("oh noes", e);
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e1) {
          Thread.currentThread().interrupt();
          throw new RuntimeException(e1);
        }
      }
    }
  }

  private void shutdown() {
    bitcoindProcessManager.shutdown();
    addressManager.shutdown();
    bitcoindFacadeFactory.shutdown();
    hiddenServerLogic.shutdown();
    rollingProfitParticipantLogic.shutdown();
    logger.info("shutdown");
  }
}