From 9483936a2b4e14926f0246f2b20c274a4a032065 Mon Sep 17 00:00:00 2001 From: romanman Date: Thu, 22 May 2014 19:44:16 +0300 Subject: [PATCH] Properties file introduced --- .../org/ethereum/config/SystemProperties.java | 95 +++++++++++++++++++ .../org/ethereum/gui/PeersTableWindow.java | 6 +- .../EthereumPeerTasterHandler.java | 17 +++- .../net/peerdiscovery/PeerDiscovery.java | 43 +-------- .../src/main/resources/system.properties | 7 +- 5 files changed, 122 insertions(+), 46 deletions(-) create mode 100644 ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java diff --git a/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java b/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java new file mode 100644 index 00000000..6f82e111 --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java @@ -0,0 +1,95 @@ +package org.ethereum.config; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; +import java.util.Properties; + +/** + * www.ethereumJ.com + * User: Roman Mandeleil + * Created on: 22/05/2014 19:22 + */ + +public class SystemProperties { + + Properties prop = new Properties(); + InputStream input = null; + + public static SystemProperties config = new SystemProperties(); + + public SystemProperties() { + + + try { + + String filename = "system.properties"; + input = SystemProperties.class.getClassLoader().getResourceAsStream(filename); + if(input==null){ + System.out.println("Sorry, unable to find " + filename); + return; + } + + //load a properties file from class path, inside static method + prop.load(input); + + + } catch (IOException ex) { + ex.printStackTrace(); + } finally{ + if(input!=null){ + try { + input.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + + public boolean peerDiscovery(){ + + if(prop.isEmpty()) return true; + + boolean result = + Boolean.parseBoolean( prop.getProperty("peer.discovery") ); + + return result; + } + + public int peerDiscoveryWorkers(){ + if(prop.isEmpty()) return 2; + + int result = + Integer.parseInt( prop.getProperty("peer.discovery") ); + + return result; + } + + + public String toString(){ + + Enumeration e = prop.propertyNames(); + while (e.hasMoreElements()) { + String key = (String) e.nextElement(); + String value = prop.getProperty(key); + + if (!key.equals("null")) + System.out.println("Key : " + key + ", Value : " + value); + } + + return ""; + } + + + public static void main(String args[]){ + + SystemProperties systemProperties = new SystemProperties(); + System.out.println(systemProperties.toString()); + + + + } + +} 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 7762e73f..67497506 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableWindow.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/PeersTableWindow.java @@ -1,5 +1,6 @@ package org.ethereum.gui; +import org.ethereum.config.SystemProperties; import org.ethereum.manager.MainData; import java.awt.*; @@ -15,6 +16,8 @@ import javax.swing.SwingConstants; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; +import static org.ethereum.config.SystemProperties.config; + /** * www.ethereumJ.com * User: Roman Mandeleil @@ -81,7 +84,8 @@ public class PeersTableWindow extends JFrame{ } }, 1000, 1000); - MainData.instance.startPeerDiscovery(); + if (config.peerDiscovery()) + MainData.instance.startPeerDiscovery(); } public static void main(String args[]) { 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 ffea0dcd..edf2f070 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 @@ -5,6 +5,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelOption; import io.netty.channel.FixedRecvByteBufAllocator; +import org.ethereum.crypto.HashUtil; import org.ethereum.gui.PeerListener; import org.ethereum.manager.MainData; import org.ethereum.net.Command; @@ -46,13 +47,21 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(final ChannelHandlerContext ctx) { - final ByteBuf buffer = ctx.alloc().buffer(HELLO_MESSAGE.length + 8); + // Here we send the hello message with random id each time + // to not interrupt active peer + + byte[] peerIdBytes = HashUtil.randomPeerId(); + HelloMessage helloMessage = new HelloMessage((byte)0x11, (byte)0x00, "EthereumJ [v0.5.1] by RomanJ ", + (byte)0b00000111, (short)30303, peerIdBytes); + 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(HELLO_MESSAGE_LEN); - buffer.writeBytes(HELLO_MESSAGE); - System.out.println("Send: " + StaticMessages.HELLO_MESSAGE.toString()); + buffer.writeBytes(helloLength); + buffer.writeBytes(helloMessage.getPayload()); + System.out.println("Send: " + helloMessage.toString()); ctx.writeAndFlush(buffer); // sample for pinging in background diff --git a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java index 3dcf2b2e..2b7f4325 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerDiscovery.java @@ -1,11 +1,14 @@ package org.ethereum.net.peerdiscovery; +import org.ethereum.config.SystemProperties; import org.ethereum.net.client.PeerData; import java.util.List; import java.util.concurrent.*; +import static org.ethereum.config.SystemProperties.config; + /** * www.ethereumJ.com * User: Roman Mandeleil @@ -35,7 +38,7 @@ public class PeerDiscovery { threadFactory = Executors.defaultThreadFactory(); //creating the ThreadPoolExecutor - executorPool = new ThreadPoolExecutor(1, 5, 10, TimeUnit.SECONDS, + executorPool = new ThreadPoolExecutor(1, config.peerDiscoveryWorkers(), 10, TimeUnit.SECONDS, new ArrayBlockingQueue(2), threadFactory, rejectionHandler); //start the monitoring thread @@ -64,43 +67,5 @@ public class PeerDiscovery { return started; } - // todo: this main here for test erase it once upon a time - public static void main(String args[]) throws InterruptedException{ - - //RejectedExecutionHandler implementation - RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl(); - - //Get the ThreadFactory implementation to use - ThreadFactory threadFactory = Executors.defaultThreadFactory(); - - //creating the ThreadPoolExecutor - ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 10, TimeUnit.SECONDS, - new ArrayBlockingQueue(2), threadFactory, rejectionHandler); - - //start the monitoring thread - PeerDiscoveryMonitorThread monitor = new PeerDiscoveryMonitorThread(executorPool, 3); - Thread monitorThread = new Thread(monitor); - monitorThread.start(); - - //submit work to the thread pool - PeerData peer = new PeerData(new byte[]{54, (byte)211, 14, 10}, (short) 30303, new byte[]{00}); - executorPool.execute(new WorkerThread(peer, executorPool)); - - PeerData peer2 = new PeerData(new byte[]{54 , (byte)201, 28, 117}, (short) 30303, new byte[]{00}); - executorPool.execute(new WorkerThread(peer2, executorPool)); - - PeerData peer3 = new PeerData(new byte[]{54, (byte)211, 14, 10}, (short) 40404, new byte[]{00}); - executorPool.execute(new WorkerThread(peer3, executorPool)); - - Thread.sleep(30000); - //shut down the pool - executorPool.shutdown(); - //shut down the monitor thread - Thread.sleep(5000); - monitor.shutdown(); - - } - - } diff --git a/ethereumj-core/src/main/resources/system.properties b/ethereumj-core/src/main/resources/system.properties index e1e8bd4c..e8b2ead0 100644 --- a/ethereumj-core/src/main/resources/system.properties +++ b/ethereumj-core/src/main/resources/system.properties @@ -16,11 +16,14 @@ server.acceptConnections = false connection.timeout = 10 - # specify if the mechanism # to discover more and more # peers and check the already -# discovered peers is on [true/false] +# discovered peers is on +# if peer discovery is off +# the peer window will show +# only what retrieved by active +# peer [true/false] peer.discovery = true # number of workers that