Cosmetics:

1. Clean logging for peer discovery.
2. Filter peer discovery from the output.
This commit is contained in:
romanman 2014-05-22 21:01:12 +03:00
parent 567e2e4f8a
commit 4c19299167
8 changed files with 73 additions and 77 deletions

View File

@ -31,11 +31,13 @@ public class ToolBar extends JFrame {
public ToolBar() throws HeadlessException {
logger.info("");
logger.info("EthereumJ [v0.5.1] by Roman Mandeleil");
logger.info(" design by Vitaly Buterin");
logger.info("");
logger.info("java.version: " + System.getProperty("java.version"));
logger.info("java.home: " + System.getProperty("java.home"));
logger.info("java.vendor: " + System.getProperty("java.vendor"));
if (Utils.JAVA_VERSION < 1.7) {
logger.info("EthereumJ support version 1.7 and higher of Java Runtime please update");
@ -43,6 +45,8 @@ public class ToolBar extends JFrame {
}
final JPanel cp = new JPanel(new FlowLayout());
cp.setBackground(Color.WHITE);

View File

@ -13,6 +13,8 @@ import org.ethereum.net.message.*;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import java.util.Timer;
@ -27,6 +29,8 @@ import static org.ethereum.net.Command.*;
*/
public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
Logger logger = LoggerFactory.getLogger(getClass());
Timer timer = null;
private final static byte[] MAGIC_PREFIX = {(byte)0x22, (byte)0x40, (byte)0x08, (byte)0x91};
@ -61,7 +65,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
buffer.writeBytes(MAGIC_PREFIX);
buffer.writeBytes(helloLength);
buffer.writeBytes(helloMessage.getPayload());
System.out.println("Send: " + helloMessage.toString());
logger.info("Send: " + helloMessage.toString());
ctx.writeAndFlush(buffer);
// sample for pinging in background
@ -74,11 +78,11 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
long currTime = System.currentTimeMillis();
if (currTime - lastPongTime > 30000){
System.out.println("No ping answer for [30 sec]");
logger.info("No ping answer for [30 sec]");
throw new Error("No ping return for 30 [sec]");
// TODO: shutdown the handler
}
System.out.println("[Send: PING]");
logger.info("[Send: PING]");
if (peerListener != null) peerListener.console("[Send: PING]");
sendPing(ctx);
}
@ -89,56 +93,56 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
byte[] payload = (byte[]) msg;
System.out.print("msg: ");
ByteUtil.printHexStringForByteArray(payload);
logger.info("msg: %s", Hex.toHexString(payload));
byte command = RLP.getCommandCode(payload);
// got HELLO
if (Command.fromInt(command) == HELLO) {
System.out.println("[Recv: HELLO]" );
logger.info("[Recv: HELLO]" );
RLPList rlpList = RLP.decode2(payload);
HelloMessage helloMessage = new HelloMessage(rlpList);
System.out.println(helloMessage.toString());
logger.info(helloMessage.toString());
sendGetPeers(ctx);
}
// got DISCONNECT
if (Command.fromInt(command) == DISCONNECT) {
System.out.println("[Recv: DISCONNECT]");
logger.info("[Recv: DISCONNECT]");
if (peerListener != null) peerListener.console("[Recv: DISCONNECT]");
RLPList rlpList = RLP.decode2(payload);
DisconnectMessage disconnectMessage = new DisconnectMessage(rlpList);
System.out.println(disconnectMessage);
logger.info(disconnectMessage.toString());
}
// got PING send pong
if (Command.fromInt(command) == PING) {
System.out.println("[Recv: PING]");
logger.info("[Recv: PING]");
if (peerListener != null) peerListener.console("[Recv: PING]");
sendPong(ctx);
}
// got PONG mark it
if (Command.fromInt(command) == PONG) {
System.out.println("[Recv: PONG]" );
logger.info("[Recv: PONG]" );
if (peerListener != null) peerListener.console("[Recv: PONG]");
this.lastPongTime = System.currentTimeMillis();
}
// got PEERS
if (Command.fromInt(command) == PEERS) {
System.out.println("[Recv: PEERS]");
logger.info("[Recv: PEERS]");
if (peerListener != null) peerListener.console("[Recv: PEERS]");
RLPList rlpList = RLP.decode2(payload);
PeersMessage peersMessage = new PeersMessage(rlpList);
MainData.instance.addPeers(peersMessage.getPeers());
System.out.println(peersMessage);
logger.info(peersMessage.toString());
sendDisconnectNice(ctx);
@ -161,7 +165,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
this.tearDown = true;
System.out.println("Lost connection to the server");
logger.info("Lost connection to the server");
cause.printStackTrace();
timer.cancel();
timer.purge();
@ -180,14 +184,14 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter {
}
private void sendPong(ChannelHandlerContext ctx){
System.out.println("[Send: PONG]");
logger.info("[Send: PONG]");
ByteBuf buffer = ctx.alloc().buffer(StaticMessages.PONG.length);
buffer.writeBytes(StaticMessages.PONG);
ctx.writeAndFlush(buffer);
}
private void sendDisconnectNice(ChannelHandlerContext ctx){
System.out.println("[Send: DISCONNECT]");
logger.info("[Send: DISCONNECT]");
ByteBuf buffer = ctx.alloc().buffer(StaticMessages.DISCONNECT_00.length);
buffer.writeBytes(StaticMessages.DISCONNECT_00);
ctx.writeAndFlush(buffer);

View File

@ -1,15 +1,17 @@
package org.ethereum.net.peerdiscovery;
import org.ethereum.manager.MainData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ThreadPoolExecutor;
public class PeerDiscoveryMonitorThread implements Runnable
{
Logger logger = LoggerFactory.getLogger(getClass());
private ThreadPoolExecutor executor;
private int seconds;
private boolean run=true;
public PeerDiscoveryMonitorThread(ThreadPoolExecutor executor, int delay)
@ -26,7 +28,7 @@ public class PeerDiscoveryMonitorThread implements Runnable
public void run()
{
while(run){
System.out.println(
logger.info(
String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s, peersDiscovered: %d ",
this.executor.getPoolSize(),
this.executor.getCorePoolSize(),

View File

@ -10,6 +10,9 @@ import org.ethereum.gui.PeerListener;
import org.ethereum.manager.MainData;
import org.ethereum.net.client.EthereumFrameDecoder;
import org.ethereum.net.client.PeerData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import static org.ethereum.config.SystemProperties.config;
@ -22,6 +25,8 @@ import static org.ethereum.config.SystemProperties.config;
*/
public class PeerTaster {
Logger logger = LoggerFactory.getLogger(getClass());
PeerListener peerListener;
Channel channel;
@ -63,8 +68,7 @@ public class PeerTaster {
f.channel().closeFuture().sync();
} catch (InterruptedException ie){
System.out.println("-- ClientPeer: catch (InterruptedException ie) --");
ie.printStackTrace();
logger.info("-- ClientPeer: catch (InterruptedException ie) --");
} finally {
try {
workerGroup.shutdownGracefully().sync();
@ -74,45 +78,4 @@ public class PeerTaster {
}
}
public static void main(String args[]){
PeerTaster peerTaster = new PeerTaster();
ArrayList<PeerData> peers = new ArrayList<PeerData>();
peers.add(new PeerData(new byte[]{54, (byte)211, 14, 10}, (short) 30303, null));
MainData.instance.addPeers(peers);
String ip = "54.211.14.10";
short port = 30303;
try {
peerTaster.connect(ip, port);}
catch (Throwable e) {
e.printStackTrace();
MainData.instance.updatePeerIsDead(ip, port);
}
// try {peerTaster.connect("82.217.72.169", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("54.201.28.117", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("54.2.10.41", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("0.204.10.41", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("54.204.10.41", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("54.211.14.10", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("82.217.72.169", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("54.201.28.117", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("54.2.10.41", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("0.204.10.41", 30303);} catch (Exception e) {e.printStackTrace();}
// try {peerTaster.connect("54.204.10.41", 30303);} catch (Exception e) {e.printStackTrace();}
System.out.println("End of the roaad");
for (PeerData peer : MainData.instance.getPeers()){
System.out.println(peer.getInetAddress().getHostAddress().toString());
};
}
}

View File

@ -1,5 +1,8 @@
package org.ethereum.net.peerdiscovery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
@ -10,8 +13,11 @@ import java.util.concurrent.ThreadPoolExecutor;
*/
public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {
Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(r.toString() + " is rejected");
logger.warn(r.toString() + " is rejected");
}
}

View File

@ -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.concurrent.ThreadPoolExecutor;
@ -12,6 +14,8 @@ import java.util.concurrent.ThreadPoolExecutor;
public class WorkerThread implements Runnable {
Logger logger = LoggerFactory.getLogger(getClass());
ThreadPoolExecutor poolExecutor;
private PeerData peerData;
@ -22,9 +26,9 @@ public class WorkerThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Command = "+ peerData.toString());
logger.info(Thread.currentThread().getName()+" Start. Command = "+ peerData.toString());
processCommand();
System.out.println(Thread.currentThread().getName()+" End.");
logger.info(Thread.currentThread().getName()+" End.");
poolExecutor.execute(this);
}
@ -34,10 +38,10 @@ public class WorkerThread implements Runnable {
PeerTaster peerTaster = new PeerTaster();
peerTaster.connect(peerData.getInetAddress().getHostName(), peerData.getPort());
peerData.setOnline(true);
System.out.println("Peer: " + peerData.toString() + " isOnline: true");
logger.info("Peer: " + peerData.toString() + " isOnline: true");
}
catch (Throwable e) {
System.out.println("Peer: " + peerData.toString() + " isOnline: false");
logger.info("Peer: " + peerData.toString() + " isOnline: false");
peerData.setOnline(false);
}
}

View File

@ -3,6 +3,8 @@ package org.ethereum.util;
import java.math.BigInteger;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import com.google.common.primitives.UnsignedInteger;
@ -10,6 +12,8 @@ import com.google.common.primitives.UnsignedLongs;
public class ByteUtil {
private static Logger logger = LoggerFactory.getLogger("ByteUtil");
/**
* Creates a copy of bytes and appends b to the end of it
*/
@ -71,17 +75,23 @@ public class ByteUtil {
}
public static void printHexStringForByteArray(byte[] data){
System.out.print("[");
for (int i = 0; i < data.length; ++i){
String hexNum = Integer.toHexString ((int) data[i] & 0xFF);
if (((int) data[i] & 0xFF) < 16) {
hexNum = "0" + hexNum;
if (logger.isInfoEnabled()) {
logger.info("[");
for (int i = 0; i < data.length; ++i) {
String hexNum = Integer.toHexString((int) data[i] & 0xFF);
if (((int) data[i] & 0xFF) < 16) {
hexNum = "0" + hexNum;
}
logger.info(hexNum);
logger.info(" ");
}
System.out.print( hexNum );
System.out.print(" ");
logger.info("]");
logger.info("");
}
System.out.print("]");
System.out.println();
}
// The packet size should be 4 byte long

View File

@ -5,4 +5,7 @@ log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %c{1}: %m%n
log4j.appender.stdout.layout.ConversionPattern= %d{HH:mm:ss} %c{1}: %m%n
# filter noisy classes
log4j.logger.org.ethereum.net.peerdiscovery = WARN