use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.
the class PeerManagerTest method test2SeedNodes.
@Test
public void test2SeedNodes() throws InterruptedException {
LocalhostNetworkNode.setSimulateTorDelayTorNode(0);
LocalhostNetworkNode.setSimulateTorDelayHiddenService(0);
seedNodes = new HashSet<>();
NodeAddress nodeAddress1 = new NodeAddress("localhost:8001");
seedNodes.add(nodeAddress1);
NodeAddress nodeAddress2 = new NodeAddress("localhost:8002");
seedNodes.add(nodeAddress2);
latch = new CountDownLatch(6);
seedNode1 = new DummySeedNode("test_dummy_dir");
seedNode1.createAndStartP2PService(nodeAddress1, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodes, new P2PServiceListener() {
@Override
public void onRequestingDataCompleted() {
latch.countDown();
}
@Override
public void onNoSeedNodeAvailable() {
}
@Override
public void onTorNodeReady() {
}
@Override
public void onNoPeersAvailable() {
}
@Override
public void onBootstrapComplete() {
latch.countDown();
}
@Override
public void onHiddenServicePublished() {
latch.countDown();
}
@Override
public void onSetupFailed(Throwable throwable) {
}
});
P2PService p2PService1 = seedNode1.getSeedNodeP2PService();
Thread.sleep(500);
seedNode2 = new DummySeedNode("test_dummy_dir");
seedNode2.createAndStartP2PService(nodeAddress2, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodes, new P2PServiceListener() {
@Override
public void onRequestingDataCompleted() {
latch.countDown();
}
@Override
public void onNoSeedNodeAvailable() {
}
@Override
public void onTorNodeReady() {
}
@Override
public void onNoPeersAvailable() {
}
@Override
public void onBootstrapComplete() {
latch.countDown();
}
@Override
public void onHiddenServicePublished() {
latch.countDown();
}
@Override
public void onSetupFailed(Throwable throwable) {
}
});
P2PService p2PService2 = seedNode2.getSeedNodeP2PService();
latch.await();
// Assert.assertEquals(1, p2PService1.getPeerManager().getAuthenticatedAndReportedPeers().size());
// Assert.assertEquals(1, p2PService2.getPeerManager().getAuthenticatedAndReportedPeers().size());
}
use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.
the class PeerManagerTest method testSingleSeedNode.
// @Test
public void testSingleSeedNode() throws InterruptedException {
LocalhostNetworkNode.setSimulateTorDelayTorNode(0);
LocalhostNetworkNode.setSimulateTorDelayHiddenService(0);
seedNodes = new HashSet<>();
NodeAddress nodeAddress = new NodeAddress("localhost:8001");
seedNodes.add(nodeAddress);
seedNode1 = new DummySeedNode("test_dummy_dir");
latch = new CountDownLatch(2);
seedNode1.createAndStartP2PService(nodeAddress, MAX_CONNECTIONS, useLocalhost, 2, true, seedNodes, new P2PServiceListener() {
@Override
public void onRequestingDataCompleted() {
latch.countDown();
}
@Override
public void onTorNodeReady() {
}
@Override
public void onNoSeedNodeAvailable() {
}
@Override
public void onNoPeersAvailable() {
}
@Override
public void onBootstrapComplete() {
}
@Override
public void onHiddenServicePublished() {
latch.countDown();
}
@Override
public void onSetupFailed(Throwable throwable) {
}
});
P2PService p2PService1 = seedNode1.getSeedNodeP2PService();
latch.await();
Thread.sleep(500);
//Assert.assertEquals(0, p2PService1.getPeerManager().getAuthenticatedAndReportedPeers().size());
}
use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.
the class BroadcastHandler method sendToPeer.
private void sendToPeer(Connection connection, BroadcastMessage message) {
String errorMessage = "Message not broadcasted because we have stopped the handler already.\n\t" + "message = " + Utilities.toTruncatedString(message);
if (!stopped) {
if (!connection.isStopped()) {
if (!connection.isCapabilityRequired(message) || connection.isCapabilitySupported(message)) {
NodeAddress nodeAddress = connection.getPeersNodeAddressOptional().get();
log.trace("Broadcast message to " + nodeAddress + ".");
SettableFuture<Connection> future = networkNode.sendMessage(connection, message);
Futures.addCallback(future, new FutureCallback<Connection>() {
@Override
public void onSuccess(Connection connection) {
numOfCompletedBroadcasts++;
if (!stopped) {
log.trace("Broadcast to " + nodeAddress + " succeeded.");
if (listener != null)
listener.onBroadcasted(message, numOfCompletedBroadcasts);
if (listener != null && numOfCompletedBroadcasts == 1)
listener.onBroadcastedToFirstPeer(message);
if (numOfCompletedBroadcasts + numOfFailedBroadcasts == numOfPeers) {
if (listener != null)
listener.onBroadcastCompleted(message, numOfCompletedBroadcasts, numOfFailedBroadcasts);
cleanup();
resultHandler.onCompleted(BroadcastHandler.this);
}
} else {
// TODO investigate why that is called very often at seed nodes
onFault("stopped at onSuccess: " + errorMessage, false);
}
}
@Override
public void onFailure(@NotNull Throwable throwable) {
numOfFailedBroadcasts++;
if (!stopped) {
log.debug("Broadcast to " + nodeAddress + " failed.\n\t" + "ErrorMessage=" + throwable.getMessage());
if (numOfCompletedBroadcasts + numOfFailedBroadcasts == numOfPeers)
onFault("stopped at onFailure: " + errorMessage);
} else {
onFault("stopped at onFailure: " + errorMessage);
}
}
});
}
} else {
onFault("Connection stopped already", false);
}
} else {
onFault("stopped at sendToPeer: " + errorMessage, false);
}
}
use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.
the class ClosedTradesView method initialize.
@Override
public void initialize() {
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
tableView.setPlaceholder(new Label("No closed trades available"));
setTradeIdColumnCellFactory();
setDirectionColumnCellFactory();
setAmountColumnCellFactory();
setPriceColumnCellFactory();
setVolumeColumnCellFactory();
setDateColumnCellFactory();
setMarketColumnCellFactory();
setStateColumnCellFactory();
setAvatarColumnCellFactory();
tradeIdColumn.setComparator((o1, o2) -> o1.getTradable().getId().compareTo(o2.getTradable().getId()));
dateColumn.setComparator((o1, o2) -> o1.getTradable().getDate().compareTo(o2.getTradable().getDate()));
directionColumn.setComparator((o1, o2) -> o1.getTradable().getOffer().getDirection().compareTo(o2.getTradable().getOffer().getDirection()));
marketColumn.setComparator((o1, o2) -> model.getMarketLabel(o1).compareTo(model.getMarketLabel(o2)));
priceColumn.setComparator((o1, o2) -> {
final Tradable tradable1 = o1.getTradable();
final Tradable tradable2 = o2.getTradable();
Fiat price1 = null;
Fiat price2 = null;
if (tradable1 != null)
price1 = tradable1 instanceof Trade ? ((Trade) tradable1).getTradePrice() : tradable1.getOffer().getPrice();
if (tradable2 != null)
price2 = tradable2 instanceof Trade ? ((Trade) tradable2).getTradePrice() : tradable2.getOffer().getPrice();
return price1 != null && price2 != null ? price1.compareTo(price2) : 0;
});
volumeColumn.setComparator((o1, o2) -> {
if (o1.getTradable() instanceof Trade && o2.getTradable() instanceof Trade) {
Fiat tradeVolume1 = ((Trade) o1.getTradable()).getTradeVolume();
Fiat tradeVolume2 = ((Trade) o2.getTradable()).getTradeVolume();
return tradeVolume1 != null && tradeVolume2 != null ? tradeVolume1.compareTo(tradeVolume2) : 0;
} else
return 0;
});
amountColumn.setComparator((o1, o2) -> {
if (o1.getTradable() instanceof Trade && o2.getTradable() instanceof Trade) {
Coin amount1 = ((Trade) o1.getTradable()).getTradeAmount();
Coin amount2 = ((Trade) o2.getTradable()).getTradeAmount();
return amount1 != null && amount2 != null ? amount1.compareTo(amount2) : 0;
} else
return 0;
});
avatarColumn.setComparator((o1, o2) -> {
if (o1.getTradable() instanceof Trade && o2.getTradable() instanceof Trade) {
NodeAddress tradingPeerNodeAddress1 = ((Trade) o1.getTradable()).getTradingPeerNodeAddress();
NodeAddress tradingPeerNodeAddress2 = ((Trade) o2.getTradable()).getTradingPeerNodeAddress();
String address1 = tradingPeerNodeAddress1 != null ? tradingPeerNodeAddress1.hostName : "";
String address2 = tradingPeerNodeAddress2 != null ? tradingPeerNodeAddress2.hostName : "";
return address1 != null && address2 != null ? address1.compareTo(address2) : 0;
} else
return 0;
});
stateColumn.setComparator((o1, o2) -> model.getState(o1).compareTo(model.getState(o2)));
dateColumn.setSortType(TableColumn.SortType.DESCENDING);
tableView.getSortOrder().add(dateColumn);
exportButton.setText("Export to csv");
}
use of io.bitsquare.p2p.NodeAddress in project bitsquare by bitsquare.
the class OpenOfferManager method handleOfferAvailabilityRequest.
///////////////////////////////////////////////////////////////////////////////////////////
// Offer Availability
///////////////////////////////////////////////////////////////////////////////////////////
private void handleOfferAvailabilityRequest(OfferAvailabilityRequest message, NodeAddress sender) {
log.trace("handleNewMessage: message = " + message.getClass().getSimpleName() + " from " + sender);
if (!stopped) {
try {
nonEmptyStringOf(message.offerId);
checkNotNull(message.getPubKeyRing());
} catch (Throwable t) {
log.warn("Invalid message " + message.toString());
return;
}
Optional<OpenOffer> openOfferOptional = findOpenOffer(message.offerId);
AvailabilityResult availabilityResult;
if (openOfferOptional.isPresent()) {
if (openOfferOptional.get().getState() == OpenOffer.State.AVAILABLE) {
final Offer offer = openOfferOptional.get().getOffer();
if (!preferences.getIgnoreTradersList().stream().filter(i -> i.equals(offer.getOffererNodeAddress().getHostNameWithoutPostFix())).findAny().isPresent()) {
availabilityResult = AvailabilityResult.AVAILABLE;
List<NodeAddress> acceptedArbitrators = user.getAcceptedArbitratorAddresses();
if (acceptedArbitrators != null && !acceptedArbitrators.isEmpty()) {
// We need to be backward compatible. takersTradePrice was not used before 0.4.9.
if (message.takersTradePrice > 0) {
// losses and therefore an outdated market price.
try {
offer.checkTradePriceTolerance(message.takersTradePrice);
} catch (TradePriceOutOfToleranceException e) {
log.warn("Trade price check failed because takers price is outside out tolerance.");
availabilityResult = AvailabilityResult.PRICE_OUT_OF_TOLERANCE;
} catch (MarketPriceNotAvailableException e) {
log.warn(e.getMessage());
availabilityResult = AvailabilityResult.MARKET_PRICE_NOT_AVAILABLE;
} catch (Throwable e) {
log.warn("Trade price check failed. " + e.getMessage());
availabilityResult = AvailabilityResult.UNKNOWN_FAILURE;
}
}
} else {
log.warn("acceptedArbitrators is null or empty: acceptedArbitrators=" + acceptedArbitrators);
availabilityResult = AvailabilityResult.NO_ARBITRATORS;
}
} else {
availabilityResult = AvailabilityResult.USER_IGNORED;
}
} else {
availabilityResult = AvailabilityResult.OFFER_TAKEN;
}
} else {
log.warn("handleOfferAvailabilityRequest: openOffer not found. That should never happen.");
availabilityResult = AvailabilityResult.OFFER_TAKEN;
}
try {
p2PService.sendEncryptedDirectMessage(sender, message.getPubKeyRing(), new OfferAvailabilityResponse(message.offerId, availabilityResult), new SendDirectMessageListener() {
@Override
public void onArrived() {
log.trace("OfferAvailabilityResponse successfully arrived at peer");
}
@Override
public void onFault() {
log.debug("Sending OfferAvailabilityResponse failed.");
}
});
} catch (Throwable t) {
t.printStackTrace();
log.debug("Exception at handleRequestIsOfferAvailableMessage " + t.getMessage());
}
} else {
log.debug("We have stopped already. We ignore that handleOfferAvailabilityRequest call.");
}
}
Aggregations