Move clientName to SystemProperties

This commit is contained in:
nicksavers 2014-05-24 19:54:20 +02:00
parent c61569bf69
commit 8b57d3facb
7 changed files with 65 additions and 82 deletions

View File

@ -5,100 +5,81 @@ import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 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();
private static Logger logger = LoggerFactory.getLogger(SystemProperties.class);
public static SystemProperties CONFIG = new SystemProperties();
private Properties prop = new Properties();
private InputStream input = null;
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;
}
String filename = "system.properties";
input = SystemProperties.class.getClassLoader().getResourceAsStream(filename);
if (input == null) {
logger.warn("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();
}
}
}
}
logger.error(ex.getMessage(), ex);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
}
public boolean peerDiscovery(){
if(prop.isEmpty()) return true;
boolean result =
Boolean.parseBoolean( prop.getProperty("peer.discovery") );
return result;
}
public boolean peerDiscovery() {
if (prop.isEmpty())
return true;
return Boolean.parseBoolean(prop.getProperty("peer.discovery"));
}
public int peerDiscoveryWorkers(){
if(prop.isEmpty()) return 2;
int result =
Integer.parseInt( prop.getProperty("peer.discovery.workers") );
return result;
return Integer.parseInt( prop.getProperty("peer.discovery.workers") );
}
public int peerDiscoveryTimeout(){
if(prop.isEmpty()) return 10000;
int result =
Integer.parseInt( prop.getProperty("peer.discovery.timeout") );
return result * 1000;
}
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 "";
if (prop.isEmpty())
return 10000;
return Integer.parseInt(prop.getProperty("peer.discovery.timeout")) * 1000;
}
public String clientName() {
if(prop.isEmpty()) return "";
return prop.getProperty("client.name");
}
public String toString() {
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
if (!key.equals("null"))
logger.info("Key: " + key + ", Value: " + value);
}
return "";
}
public static void main(String args[]){
SystemProperties systemProperties = new SystemProperties();
System.out.println(systemProperties.toString());
logger.info(systemProperties.toString());
}
}

View File

@ -15,7 +15,7 @@ import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import static org.ethereum.config.SystemProperties.config;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
* www.ethereumJ.com
@ -92,7 +92,7 @@ public class PeersTableWindow extends JFrame{
}
}, 1000, 1000);
if (config.peerDiscovery())
if (CONFIG.peerDiscovery())
MainData.instance.startPeerDiscovery();
}

View File

@ -120,10 +120,10 @@ public class HelloMessage extends Message {
return "Hello Message [ command=" + HELLO.asByte() + " " +
" protocolVersion=" + this.protocolVersion + " " +
" networkId=" + this.networkId + " " +
" clientId= " + this.clientId + " " +
" capabilities= " + this.capabilities + " " +
" peerPort= " + this.peerPort + " " +
" peerId= " + Hex.toHexString(this.peerId) + " " +
" clientId=" + this.clientId + " " +
" capabilities=" + this.capabilities + " " +
" peerPort=" + this.peerPort + " " +
" peerId=" + Hex.toHexString(this.peerId) + " " +
"]";
}
}

View File

@ -4,6 +4,8 @@ import org.ethereum.core.Genesis;
import org.ethereum.crypto.HashUtil;
import org.spongycastle.util.encoders.Hex;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
* www.ethereumJ.com
* User: Roman Mandeleil
@ -24,7 +26,7 @@ public class StaticMessages {
byte[] peerIdBytes = HashUtil.randomPeerId();
HELLO_MESSAGE = new HelloMessage((byte) 0x11, (byte) 0x00,
"EthereumJ [v0.5.1] by RomanJ", (byte) 0b00000111,
CONFIG.clientName(), (byte) 0b00000111,
(short) 30303, peerIdBytes);
}
public static final HelloMessage HELLO_MESSAGE;

View File

@ -5,7 +5,7 @@ import org.ethereum.net.client.PeerData;
import java.util.List;
import java.util.concurrent.*;
import static org.ethereum.config.SystemProperties.config;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
* www.ethereumJ.com
@ -37,7 +37,7 @@ public class PeerDiscovery {
//creating the ThreadPoolExecutor
executorPool = new ThreadPoolExecutor(1, 1000, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(config.peerDiscoveryWorkers()), threadFactory, rejectionHandler);
new ArrayBlockingQueue<Runnable>(CONFIG.peerDiscoveryWorkers()), threadFactory, rejectionHandler);
//start the monitoring thread
monitor = new PeerDiscoveryMonitorThread(executorPool, 3);

View File

@ -10,7 +10,7 @@ import org.ethereum.net.client.EthereumFrameDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.ethereum.config.SystemProperties.config;
import static org.ethereum.config.SystemProperties.CONFIG;
/**
* www.ethereumJ.com
@ -54,7 +54,7 @@ public class PeerTaster {
});
// Start the client.
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerDiscoveryTimeout());
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONFIG.peerDiscoveryTimeout());
ChannelFuture f = b.connect(host, port).sync(); // (5)
// Wait until the connection is closed.

View File

@ -2,7 +2,7 @@
#***
# Starting to gather some properties the system going to support
#***
client.name=EthereumJ [v0.5.1] by RomanJ
# if the system will work as a server also
# accept for incoming connections [true/false]