PeerDiscovery new details added

This commit is contained in:
romanman 2014-08-15 22:46:53 +03:00
parent 87d07108e3
commit 0225ea0b9b
6 changed files with 63 additions and 22 deletions

View File

@ -11,6 +11,7 @@ import javax.swing.table.AbstractTableModel;
import org.ethereum.db.IpGeoDB; import org.ethereum.db.IpGeoDB;
import org.ethereum.manager.WorldManager; import org.ethereum.manager.WorldManager;
import org.ethereum.net.client.PeerData; import org.ethereum.net.client.PeerData;
import org.ethereum.net.message.HelloMessage;
import org.ethereum.util.Utils; import org.ethereum.util.Utils;
import com.maxmind.geoip.Location; import com.maxmind.geoip.Location;
@ -43,7 +44,8 @@ public class PeersTableModel extends AbstractTableModel {
public String getColumnName(int column) { public String getColumnName(int column) {
if (column == 0) return "Location"; if (column == 0) return "Location";
if (column == 1) return "IP"; if (column == 1) return "IP";
if (column == 2) return "Live"; if (column == 2) return "Environment";
if (column == 3) return "Live";
else return ""; else return "";
} }
@ -54,7 +56,8 @@ public class PeersTableModel extends AbstractTableModel {
public Class<?> getColumnClass(int column) { public Class<?> getColumnClass(int column) {
if (column == 0) return ImageIcon.class; if (column == 0) return ImageIcon.class;
if (column == 1) return String.class; if (column == 1) return String.class;
if (column == 2) return ImageIcon.class; if (column == 2) return String.class;
if (column == 3) return ImageIcon.class;
else return String.class; else return String.class;
} }
@ -74,10 +77,19 @@ public class PeersTableModel extends AbstractTableModel {
} }
if (column == 1) if (column == 1)
return peerInfo.getIp().getHostAddress(); return peerInfo.getIp().getHostAddress();
if (column == 2) { if (column == 2) {
if (peerInfo.getLastAccessed() == 0)
return "?";
else
return (System.currentTimeMillis() - peerInfo.getLastAccessed()) / 1000 + " seconds ago";
}
if (column == 3) {
ImageIcon flagIcon = null; ImageIcon flagIcon = null;
if (peerInfo.connected) { if (peerInfo.isConnected()) {
flagIcon = Utils.getImageIcon("connected.png"); flagIcon = Utils.getImageIcon("connected.png");
} else { } else {
flagIcon = Utils.getImageIcon("disconnected.png"); flagIcon = Utils.getImageIcon("disconnected.png");
@ -92,7 +104,7 @@ public class PeersTableModel extends AbstractTableModel {
} }
public int getColumnCount() { public int getColumnCount() {
return 3; return 4;
} }
public void updateModel() { public void updateModel() {
@ -105,7 +117,7 @@ public class PeersTableModel extends AbstractTableModel {
PeerData peer = peers.get(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(), peer.getHandshake(), peer.getLastCheckTime()));
} }
} }
} }
@ -115,8 +127,12 @@ public class PeersTableModel extends AbstractTableModel {
Location location; Location location;
InetAddress ip; InetAddress ip;
boolean connected; boolean connected;
HelloMessage handshake;
long lastAccessed = 0;
private PeerInfo(Location location, InetAddress ip, boolean isConnected) {
private PeerInfo(Location location, InetAddress ip, boolean isConnected,
HelloMessage handshake, long lastAccessed) {
if (location == null) if (location == null)
this.location = new Location(); this.location = new Location();
@ -125,6 +141,8 @@ public class PeersTableModel extends AbstractTableModel {
this.ip = ip; this.ip = ip;
this.connected = isConnected; this.connected = isConnected;
this.handshake = handshake;
this.lastAccessed = lastAccessed;
} }
private Location getLocation() { private Location getLocation() {
@ -138,6 +156,14 @@ public class PeersTableModel extends AbstractTableModel {
private boolean isConnected() { private boolean isConnected() {
return connected; return connected;
} }
public HelloMessage getHandshake() {
return handshake;
}
public long getLastAccessed() {
return lastAccessed;
}
} }
} }

View File

@ -37,8 +37,8 @@ public class PeersTableWindow extends JFrame {
// Set the frame characteristics // Set the frame characteristics
setTitle("Ethereum Peers"); setTitle("Ethereum Peers");
setSize(355, 400); setSize(515, 400);
setLocation(815, 30); setLocation(615, 30);
java.net.URL url = ClassLoader.getSystemResource("ethereum-icon.png"); java.net.URL url = ClassLoader.getSystemResource("ethereum-icon.png");
Toolkit kit = Toolkit.getDefaultToolkit(); Toolkit kit = Toolkit.getDefaultToolkit();
@ -57,7 +57,7 @@ public class PeersTableWindow extends JFrame {
table = new JTable(); table = new JTable();
table.setModel(new PeersTableModel()); table.setModel(new PeersTableModel());
table.setFont(new Font("Courier New", Font.PLAIN, 18)); table.setFont(new Font("Courier New", Font.PLAIN, 15));
table.setForeground(Color.GRAY); table.setForeground(Color.GRAY);
table.setTableHeader(null); table.setTableHeader(null);
@ -71,7 +71,8 @@ public class PeersTableWindow extends JFrame {
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(60); table.getColumnModel().getColumn(0).setPreferredWidth(60);
table.getColumnModel().getColumn(1).setPreferredWidth(200); table.getColumnModel().getColumn(1).setPreferredWidth(200);
table.getColumnModel().getColumn(2).setPreferredWidth(60); table.getColumnModel().getColumn(2).setPreferredWidth(160);
table.getColumnModel().getColumn(3).setPreferredWidth(60);
table.setRowMargin(3); table.setRowMargin(3);
table.setRowHeight(50); table.setRowHeight(50);

View File

@ -1,5 +1,6 @@
package org.ethereum.net.client; package org.ethereum.net.client;
import org.ethereum.net.message.HelloMessage;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import java.net.InetAddress; import java.net.InetAddress;
@ -16,6 +17,7 @@ public class PeerData {
private int port; private int port;
private byte[] peerId; private byte[] peerId;
private byte capabilities; private byte capabilities;
private HelloMessage handshake;
private transient boolean isOnline = false; private transient boolean isOnline = false;
private transient long lastCheckTime = 0; private transient long lastCheckTime = 0;
@ -50,7 +52,7 @@ public class PeerData {
} }
public boolean isOnline() { public boolean isOnline() {
if (capabilities < 7) return false; if (getCapabilities() < 7) return false;
return isOnline; return isOnline;
} }
@ -67,11 +69,19 @@ public class PeerData {
} }
public byte getCapabilities() { public byte getCapabilities() {
return capabilities;
if (handshake != null)
return handshake.getCapabilities();
else
return 0;
} }
public void setCapabilities(byte capabilities) { public HelloMessage getHandshake() {
this.capabilities = capabilities; return handshake;
}
public void setHandshake(HelloMessage handshake) {
this.handshake = handshake;
} }
@Override @Override

View File

@ -37,7 +37,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
private PeerListener peerListener; private PeerListener peerListener;
private byte capabilities = 0; private HelloMessage handshake = null;
public EthereumPeerTasterHandler() { } public EthereumPeerTasterHandler() { }
@ -79,7 +79,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
RLPList rlpList = RLP.decode2(payload); RLPList rlpList = RLP.decode2(payload);
HelloMessage helloMessage = new HelloMessage(rlpList); HelloMessage helloMessage = new HelloMessage(rlpList);
capabilities = helloMessage.getCapabilities(); handshake = helloMessage;
logger.info(helloMessage.toString()); logger.info(helloMessage.toString());
sendGetPeers(ctx); sendGetPeers(ctx);
@ -170,7 +170,9 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
ctx.writeAndFlush(buffer); ctx.writeAndFlush(buffer);
} }
public byte getCapabilities() { public HelloMessage getHandshake(){
return capabilities; return this.handshake;
} }
} }

View File

@ -6,6 +6,7 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.ReadTimeoutHandler;
import org.ethereum.net.client.EthereumFrameDecoder; import org.ethereum.net.client.EthereumFrameDecoder;
import org.ethereum.net.message.HelloMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -62,8 +63,8 @@ public class PeerTaster {
} }
} }
public byte getCapabilities() { public HelloMessage getHandshake() {
return handler.getCapabilities(); return handler.getHandshake();
} }
} }

View File

@ -40,10 +40,9 @@ public class WorkerThread implements Runnable {
peerTaster = new PeerTaster(); peerTaster = new PeerTaster();
peerTaster.connect(peerData.getInetAddress().getHostAddress(), peerData.getPort()); peerTaster.connect(peerData.getInetAddress().getHostAddress(), peerData.getPort());
byte capabilities = peerTaster.getCapabilities();
peerData.setOnline(true); peerData.setOnline(true);
peerData.setCapabilities(capabilities); peerData.setHandshake(peerTaster.getHandshake());
logger.info("Peer: " + peerData.toString() + " isOnline: true"); logger.info("Peer: " + peerData.toString() + " isOnline: true");
} }
catch (Throwable e) { catch (Throwable e) {
@ -53,6 +52,8 @@ public class WorkerThread implements Runnable {
e); e);
logger.info("Peer: " + peerData.toString() + " isOnline: false"); logger.info("Peer: " + peerData.toString() + " isOnline: false");
peerData.setOnline(false); peerData.setOnline(false);
} finally {
peerData.setLastCheckTime(System.currentTimeMillis());
} }
} }