PeerDiscovery - improve + bug fix

This commit is contained in:
romanman 2014-08-11 10:03:25 +03:00
parent d479f5cb72
commit aeec38085b
4 changed files with 28 additions and 34 deletions

View File

@ -3,8 +3,9 @@ package org.ethereum.gui;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
import java.util.Timer;
import javax.swing.ImageIcon; import javax.swing.*;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import org.ethereum.db.IpGeoDB; import org.ethereum.db.IpGeoDB;
@ -27,8 +28,15 @@ public class PeersTableModel extends AbstractTableModel {
public PeersTableModel() { public PeersTableModel() {
updater.scheduleAtFixedRate(new TimerTask() { updater.scheduleAtFixedRate(new TimerTask() {
public void run() { public void run() {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
updateModel(); updateModel();
} }
}
);
}
}, 0, 100); }, 0, 100);
} }
@ -90,7 +98,11 @@ public class PeersTableModel extends AbstractTableModel {
public void updateModel() { public void updateModel() {
synchronized (peerInfoList) { synchronized (peerInfoList) {
peerInfoList.clear(); peerInfoList.clear();
for (PeerData peer : WorldManager.getInstance().getPeers()) {
List<PeerData> peers = WorldManager.getInstance().getPeers();
for (int i = 0; i < peers.size(); ++i) {
PeerData peer = peers.get(i);
InetAddress addr = peer.getInetAddress(); InetAddress addr = peer.getInetAddress();
Location cr = IpGeoDB.getLocationForIp(addr); Location cr = IpGeoDB.getLocationForIp(addr);
peerInfoList.add(new PeerInfo(cr, addr, peer.isOnline())); peerInfoList.add(new PeerInfo(cr, addr, peer.isOnline()));

View File

@ -31,7 +31,6 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
private Logger logger = LoggerFactory.getLogger("peerdiscovery"); 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 final static byte[] MAGIC_PREFIX = {(byte)0x22, (byte)0x40, (byte)0x08, (byte)0x91};
private long lastPongTime = 0; private long lastPongTime = 0;
@ -58,7 +57,6 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
byte[] helloLength =ByteUtil.calcPacketLength(helloMessage.getPayload()); byte[] helloLength =ByteUtil.calcPacketLength(helloMessage.getPayload());
final ByteBuf buffer = ctx.alloc().buffer(helloMessage.getPayload().length + 8); final ByteBuf buffer = ctx.alloc().buffer(helloMessage.getPayload().length + 8);
timer = new Timer();
buffer.writeBytes(MAGIC_PREFIX); buffer.writeBytes(MAGIC_PREFIX);
buffer.writeBytes(helloLength); buffer.writeBytes(helloLength);
@ -66,25 +64,6 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
logger.info("Send: " + helloMessage.toString()); logger.info("Send: " + helloMessage.toString());
ctx.writeAndFlush(buffer); 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 @Override
@ -144,10 +123,6 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
sendDisconnectNice(ctx); sendDisconnectNice(ctx);
timer.cancel();
timer.purge();
timer = null;
ctx.close().sync(); ctx.close().sync();
ctx.disconnect().sync(); ctx.disconnect().sync();
} }
@ -165,14 +140,9 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
this.tearDown = true; this.tearDown = true;
logger.info("Lost connection to the server"); logger.info("Lost connection to the server");
logger.error(cause.getMessage(), cause); logger.error(cause.getMessage(), cause);
timer.cancel();
timer.purge();
timer = null;
ctx.close().sync(); ctx.close().sync();
ctx.disconnect().sync(); ctx.disconnect().sync();
throw new Error("Peer is dead");
} }
private void sendPing(ChannelHandlerContext ctx) { private void sendPing(ChannelHandlerContext ctx) {

View File

@ -1,6 +1,8 @@
package org.ethereum.net.peerdiscovery; package org.ethereum.net.peerdiscovery;
import org.ethereum.net.client.PeerData; import org.ethereum.net.client.PeerData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List; import java.util.List;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -20,6 +22,8 @@ public class PeerDiscovery {
private PeerDiscoveryMonitorThread monitor; private PeerDiscoveryMonitorThread monitor;
private List<PeerData> peers; private List<PeerData> peers;
Logger logger = LoggerFactory.getLogger("peerdiscovery");
private boolean started = false; private boolean started = false;
public PeerDiscovery(List<PeerData> peers) { public PeerDiscovery(List<PeerData> peers) {
@ -50,6 +54,8 @@ public class PeerDiscovery {
} }
public void addNewPeerData(PeerData peerData) { public void addNewPeerData(PeerData peerData) {
logger.debug("add new peer for discovery: {}", peerData);
executorPool.execute(new WorkerThread(peerData, executorPool)); executorPool.execute(new WorkerThread(peerData, executorPool));
} }

View File

@ -17,7 +17,7 @@ public class WorkerThread implements Runnable {
ThreadPoolExecutor poolExecutor; ThreadPoolExecutor poolExecutor;
private PeerData peerData; private PeerData peerData;
private PeerTaster peerTaster = new PeerTaster(); private PeerTaster peerTaster;
public WorkerThread(PeerData peerData, ThreadPoolExecutor poolExecutor) { public WorkerThread(PeerData peerData, ThreadPoolExecutor poolExecutor) {
this.poolExecutor = poolExecutor; this.poolExecutor = poolExecutor;
@ -37,6 +37,8 @@ public class WorkerThread implements Runnable {
private void processCommand() { private void processCommand() {
try { try {
peerTaster = new PeerTaster();
peerTaster.connect(peerData.getInetAddress().getHostAddress(), peerData.getPort()); peerTaster.connect(peerData.getInetAddress().getHostAddress(), peerData.getPort());
byte capabilities = peerTaster.getCapabilities(); byte capabilities = peerTaster.getCapabilities();
@ -45,6 +47,10 @@ public class WorkerThread implements Runnable {
logger.info("Peer: " + peerData.toString() + " isOnline: true"); logger.info("Peer: " + peerData.toString() + " isOnline: true");
} }
catch (Throwable e) { catch (Throwable e) {
if (peerData.isOnline() == true)
logger.info("Peer: [ {} ] got offline, due: [ {} ]",
peerData.getInetAddress().getHostAddress(),
e);
logger.info("Peer: " + peerData.toString() + " isOnline: false"); logger.info("Peer: " + peerData.toString() + " isOnline: false");
peerData.setOnline(false); peerData.setOnline(false);
} }