/** * Adds an event listener object. Methods on this object are called when something interesting happens, * like new connection to a server. The listener is executed by {@link org.bitcoinj.utils.Threading#USER_THREAD}. */ @Override public void addEventListener(ConnectionEventListener listener) { addEventListener(listener, Threading.USER_THREAD); }
/** * Adds an event listener object. Methods on this object are called when something interesting happens, * like new connection to a server. The listener is executed by the given executor. */ private void addEventListener(ConnectionEventListener listener, Executor executor) { boolean isNew = !ListenerRegistration.removeFromList(listener, eventListeners); eventListeners.add(new ListenerRegistration<ConnectionEventListener>(listener, executor)); if (isNew && isActivelyConnected()) { broadcastOnConnection(); } }
public void stopAsync() { if (stopped) return; stopped = true; if (isActivelyConnected()) broadcastOnDisconnect(); eventListeners.clear(); connectionExec.remove(reconnectTask); if (stratumClient != null) { stratumClient.stopAsync(); stratumClient = null; } }
private BlockchainConnection getConnection(CoinType type) { if (connections.containsKey(type)) return connections.get(type); // Try to create a connection if (addresses.containsKey(type)) { if (type instanceof BitFamily) { ServerClient client = new ServerClient(addresses.get(type), connectivityHelper); client.setCacheDir(cacheDir, cacheSize); connections.put(type, client); return client; } else if (type instanceof NxtFamily) { NxtServerClient client = new NxtServerClient(addresses.get(type), connectivityHelper); client.setCacheDir(cacheDir, cacheSize); connections.put(type, client); return client; } else { throw new UnsupportedCoinTypeException(type); } } else { // Should not happen throw new RuntimeException("Tried to create connection for an unknown server."); } }
@Override public void run() { if (isActivelyConnected()) { reconnectAt = 0; retrySeconds = 0; } } };
public ServerClient(CoinAddress coinAddress, ConnectivityHelper connectivityHelper) { this.connectivityHelper = connectivityHelper; eventListeners = new CopyOnWriteArrayList<ListenerRegistration<ConnectionEventListener>>(); failedAddresses = new HashSet<ServerAddress>(); type = coinAddress.getType(); addresses = ImmutableList.copyOf(coinAddress.getAddresses()); createStratumClient(); }
} else { getTransactionFromNetwork(txHash, listener);
private StratumClient createStratumClient() { checkState(stratumClient == null); lastServerAddress = getServerAddress(); stratumClient = new StratumClient(lastServerAddress); stratumClient.addListener(serviceListener, Threading.USER_THREAD); return stratumClient; }
@Override public void running() { // Check if connection is up as this event is fired even if there is no connection if (isActivelyConnected()) { log.info("{} client connected to {}", type.getName(), lastServerAddress); broadcastOnConnection(); // Test that the connection is stable reschedule(connectionCheckTask, CONNECTION_STABILIZATION, TimeUnit.SECONDS); } }
public void startAsync() { if (stratumClient == null){ log.info("Forcing service start"); connectionExec.remove(reconnectTask); createStratumClient(); } Service.State state = stratumClient.state(); if (state != NEW || stopped) { log.debug("Not starting service as it is already started or explicitly stopped"); return; } try { stratumClient.startAsync(); } catch (IllegalStateException e) { // This can happen if the service has already been started or stopped (e.g. by another // service or listener). Our contract says it is safe to call this method if // all services were NEW when it was called, and this has already been verified above, so we // don't propagate the exception. log.warn("Unable to start Service " + type.getName(), e); } }
@Override public void ping(@Nullable String versionString) { if (!isActivelyConnected()) { log.warn("There is no connection with {} server, skipping ping.", type.getName()); return;