PeerDiscovery - improve + bug fix
This commit is contained in:
parent
d479f5cb72
commit
aeec38085b
|
@ -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<PeerData> 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()));
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<PeerData> peers;
|
||||
|
||||
Logger logger = LoggerFactory.getLogger("peerdiscovery");
|
||||
|
||||
private boolean started = false;
|
||||
|
||||
public PeerDiscovery(List<PeerData> 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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue