From 0225ea0b9ba2baad6761a065f6b8270f73c54fcb Mon Sep 17 00:00:00 2001 From: romanman Date: Fri, 15 Aug 2014 22:46:53 +0300 Subject: [PATCH] PeerDiscovery new details added --- .../org/ethereum/gui/PeersTableModel.java | 38 ++++++++++++++++--- .../org/ethereum/gui/PeersTableWindow.java | 9 +++-- .../org/ethereum/net/client/PeerData.java | 18 +++++++-- .../EthereumPeerTasterHandler.java | 10 +++-- .../net/peerdiscovery/PeerTaster.java | 5 ++- .../net/peerdiscovery/WorkerThread.java | 5 ++- 6 files changed, 63 insertions(+), 22 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 8b309a06..474a47f1 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableModel.java @@ -11,6 +11,7 @@ import javax.swing.table.AbstractTableModel; import org.ethereum.db.IpGeoDB; import org.ethereum.manager.WorldManager; import org.ethereum.net.client.PeerData; +import org.ethereum.net.message.HelloMessage; import org.ethereum.util.Utils; import com.maxmind.geoip.Location; @@ -43,7 +44,8 @@ public class PeersTableModel extends AbstractTableModel { public String getColumnName(int column) { if (column == 0) return "Location"; if (column == 1) return "IP"; - if (column == 2) return "Live"; + if (column == 2) return "Environment"; + if (column == 3) return "Live"; else return ""; } @@ -54,7 +56,8 @@ public class PeersTableModel extends AbstractTableModel { public Class getColumnClass(int column) { if (column == 0) return ImageIcon.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; } @@ -74,10 +77,19 @@ public class PeersTableModel extends AbstractTableModel { } if (column == 1) return peerInfo.getIp().getHostAddress(); + if (column == 2) { + if (peerInfo.getLastAccessed() == 0) + return "?"; + else + return (System.currentTimeMillis() - peerInfo.getLastAccessed()) / 1000 + " seconds ago"; + } + + if (column == 3) { + ImageIcon flagIcon = null; - if (peerInfo.connected) { + if (peerInfo.isConnected()) { flagIcon = Utils.getImageIcon("connected.png"); } else { flagIcon = Utils.getImageIcon("disconnected.png"); @@ -92,7 +104,7 @@ public class PeersTableModel extends AbstractTableModel { } public int getColumnCount() { - return 3; + return 4; } public void updateModel() { @@ -105,7 +117,7 @@ public class PeersTableModel extends AbstractTableModel { PeerData peer = peers.get(i); InetAddress addr = peer.getInetAddress(); 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; InetAddress ip; 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) this.location = new Location(); @@ -125,6 +141,8 @@ public class PeersTableModel extends AbstractTableModel { this.ip = ip; this.connected = isConnected; + this.handshake = handshake; + this.lastAccessed = lastAccessed; } private Location getLocation() { @@ -138,6 +156,14 @@ public class PeersTableModel extends AbstractTableModel { private boolean isConnected() { return connected; } + + public HelloMessage getHandshake() { + return handshake; + } + + public long getLastAccessed() { + return lastAccessed; + } } } diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableWindow.java b/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableWindow.java index 73e6b617..dff1bc4a 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableWindow.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableWindow.java @@ -37,8 +37,8 @@ public class PeersTableWindow extends JFrame { // Set the frame characteristics setTitle("Ethereum Peers"); - setSize(355, 400); - setLocation(815, 30); + setSize(515, 400); + setLocation(615, 30); java.net.URL url = ClassLoader.getSystemResource("ethereum-icon.png"); Toolkit kit = Toolkit.getDefaultToolkit(); @@ -57,7 +57,7 @@ public class PeersTableWindow extends JFrame { table = new JTable(); 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.setTableHeader(null); @@ -71,7 +71,8 @@ public class PeersTableWindow extends JFrame { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.getColumnModel().getColumn(0).setPreferredWidth(60); 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.setRowHeight(50); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/client/PeerData.java b/ethereumj-core/src/main/java/org/ethereum/net/client/PeerData.java index eecbb4e7..4b656022 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/client/PeerData.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/client/PeerData.java @@ -1,5 +1,6 @@ package org.ethereum.net.client; +import org.ethereum.net.message.HelloMessage; import org.spongycastle.util.encoders.Hex; import java.net.InetAddress; @@ -16,6 +17,7 @@ public class PeerData { private int port; private byte[] peerId; private byte capabilities; + private HelloMessage handshake; private transient boolean isOnline = false; private transient long lastCheckTime = 0; @@ -50,7 +52,7 @@ public class PeerData { } public boolean isOnline() { - if (capabilities < 7) return false; + if (getCapabilities() < 7) return false; return isOnline; } @@ -67,11 +69,19 @@ public class PeerData { } public byte getCapabilities() { - return capabilities; + + if (handshake != null) + return handshake.getCapabilities(); + else + return 0; } - public void setCapabilities(byte capabilities) { - this.capabilities = capabilities; + public HelloMessage getHandshake() { + return handshake; + } + + public void setHandshake(HelloMessage handshake) { + this.handshake = handshake; } @Override 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 9b5310ac..9a199cd3 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 @@ -37,7 +37,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { private PeerListener peerListener; - private byte capabilities = 0; + private HelloMessage handshake = null; public EthereumPeerTasterHandler() { } @@ -79,7 +79,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { RLPList rlpList = RLP.decode2(payload); HelloMessage helloMessage = new HelloMessage(rlpList); - capabilities = helloMessage.getCapabilities(); + handshake = helloMessage; logger.info(helloMessage.toString()); sendGetPeers(ctx); @@ -170,7 +170,9 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { ctx.writeAndFlush(buffer); } - public byte getCapabilities() { - return capabilities; + public HelloMessage getHandshake(){ + return this.handshake; } + + } \ No newline at end of file diff --git a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerTaster.java b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerTaster.java index d5c40394..6dedb603 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerTaster.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerTaster.java @@ -6,6 +6,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.timeout.ReadTimeoutHandler; import org.ethereum.net.client.EthereumFrameDecoder; +import org.ethereum.net.message.HelloMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,8 +63,8 @@ public class PeerTaster { } } - public byte getCapabilities() { - return handler.getCapabilities(); + public HelloMessage getHandshake() { + return handler.getHandshake(); } } 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 55f95792..d1071fec 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 @@ -40,10 +40,9 @@ public class WorkerThread implements Runnable { peerTaster = new PeerTaster(); peerTaster.connect(peerData.getInetAddress().getHostAddress(), peerData.getPort()); - byte capabilities = peerTaster.getCapabilities(); peerData.setOnline(true); - peerData.setCapabilities(capabilities); + peerData.setHandshake(peerTaster.getHandshake()); logger.info("Peer: " + peerData.toString() + " isOnline: true"); } catch (Throwable e) { @@ -53,6 +52,8 @@ public class WorkerThread implements Runnable { e); logger.info("Peer: " + peerData.toString() + " isOnline: false"); peerData.setOnline(false); + } finally { + peerData.setLastCheckTime(System.currentTimeMillis()); } }