From aeec38085bdd8625a5a08be22f8291060dea5959 Mon Sep 17 00:00:00 2001 From: romanman Date: Mon, 11 Aug 2014 10:03:25 +0300 Subject: [PATCH] PeerDiscovery - improve + bug fix --- .../org/ethereum/gui/PeersTableModel.java | 18 +++++++++-- .../EthereumPeerTasterHandler.java | 30 ------------------- .../net/peerdiscovery/PeerDiscovery.java | 6 ++++ .../net/peerdiscovery/WorkerThread.java | 8 ++++- 4 files changed, 28 insertions(+), 34 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java b/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java index 6211aad0..8b309a06 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java @@ -3,8 +3,9 @@ package org.ethereum.gui; import java.net.InetAddress; import java.net.URL; import java.util.*; +import java.util.Timer; -import javax.swing.ImageIcon; +import javax.swing.*; import javax.swing.table.AbstractTableModel; import org.ethereum.db.IpGeoDB; @@ -27,7 +28,14 @@ public class PeersTableModel extends AbstractTableModel { public PeersTableModel() { updater.scheduleAtFixedRate(new TimerTask() { public void run() { - updateModel(); + SwingUtilities.invokeLater( + new Runnable() { + @Override + public void run() { + updateModel(); + } + } + ); } }, 0, 100); } @@ -90,7 +98,11 @@ public class PeersTableModel extends AbstractTableModel { public void updateModel() { synchronized (peerInfoList) { peerInfoList.clear(); - for (PeerData peer : WorldManager.getInstance().getPeers()) { + + List peers = WorldManager.getInstance().getPeers(); + for (int i = 0; i < peers.size(); ++i) { + + PeerData peer = peers.get(i); InetAddress addr = peer.getInetAddress(); Location cr = IpGeoDB.getLocationForIp(addr); peerInfoList.add(new PeerInfo(cr, addr, peer.isOnline())); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/EthereumPeerTasterHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/EthereumPeerTasterHandler.java index bbbb4c73..9466e06c 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/EthereumPeerTasterHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/EthereumPeerTasterHandler.java @@ -31,7 +31,6 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { private Logger logger = LoggerFactory.getLogger("peerdiscovery"); - private Timer timer = null; private final static byte[] MAGIC_PREFIX = {(byte)0x22, (byte)0x40, (byte)0x08, (byte)0x91}; private long lastPongTime = 0; @@ -58,7 +57,6 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { byte[] helloLength =ByteUtil.calcPacketLength(helloMessage.getPayload()); final ByteBuf buffer = ctx.alloc().buffer(helloMessage.getPayload().length + 8); - timer = new Timer(); buffer.writeBytes(MAGIC_PREFIX); buffer.writeBytes(helloLength); @@ -66,25 +64,6 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { logger.info("Send: " + helloMessage.toString()); ctx.writeAndFlush(buffer); - // sample for pinging in background - timer.scheduleAtFixedRate(new TimerTask() { - - public void run() { - - if (lastPongTime == 0) lastPongTime = System.currentTimeMillis(); - if (tearDown) this.cancel(); - - long currTime = System.currentTimeMillis(); - if (currTime - lastPongTime > 30000) { - logger.info("No ping answer for [30 sec]"); - throw new Error("No ping return for 30 [sec]"); - // TODO: shutdown the handler - } - logger.info("[Send: PING]"); - if (peerListener != null) peerListener.console("[Send: PING]"); - sendPing(ctx); - } - }, 2000, 5000); } @Override @@ -144,10 +123,6 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { sendDisconnectNice(ctx); - timer.cancel(); - timer.purge(); - timer = null; - ctx.close().sync(); ctx.disconnect().sync(); } @@ -165,14 +140,9 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { this.tearDown = true; logger.info("Lost connection to the server"); logger.error(cause.getMessage(), cause); - timer.cancel(); - timer.purge(); - timer = null; ctx.close().sync(); ctx.disconnect().sync(); - - throw new Error("Peer is dead"); } private void sendPing(ChannelHandlerContext ctx) { diff --git a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java index b9a9af3b..f15be4e8 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java @@ -1,6 +1,8 @@ package org.ethereum.net.peerdiscovery; import org.ethereum.net.client.PeerData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.concurrent.*; @@ -20,6 +22,8 @@ public class PeerDiscovery { private PeerDiscoveryMonitorThread monitor; private List peers; + Logger logger = LoggerFactory.getLogger("peerdiscovery"); + private boolean started = false; public PeerDiscovery(List peers) { @@ -50,6 +54,8 @@ public class PeerDiscovery { } public void addNewPeerData(PeerData peerData) { + + logger.debug("add new peer for discovery: {}", peerData); executorPool.execute(new WorkerThread(peerData, executorPool)); } diff --git a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/WorkerThread.java b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/WorkerThread.java index 7451536c..55f95792 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/WorkerThread.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/WorkerThread.java @@ -17,7 +17,7 @@ public class WorkerThread implements Runnable { ThreadPoolExecutor poolExecutor; private PeerData peerData; - private PeerTaster peerTaster = new PeerTaster(); + private PeerTaster peerTaster; public WorkerThread(PeerData peerData, ThreadPoolExecutor poolExecutor) { this.poolExecutor = poolExecutor; @@ -37,6 +37,8 @@ public class WorkerThread implements Runnable { private void processCommand() { try { + + peerTaster = new PeerTaster(); peerTaster.connect(peerData.getInetAddress().getHostAddress(), peerData.getPort()); byte capabilities = peerTaster.getCapabilities(); @@ -45,6 +47,10 @@ public class WorkerThread implements Runnable { logger.info("Peer: " + peerData.toString() + " isOnline: true"); } catch (Throwable e) { + if (peerData.isOnline() == true) + logger.info("Peer: [ {} ] got offline, due: [ {} ]", + peerData.getInetAddress().getHostAddress(), + e); logger.info("Peer: " + peerData.toString() + " isOnline: false"); peerData.setOnline(false); }