Use consistency in constructors

This commit is contained in:
nicksavers 2014-10-25 17:13:15 +02:00
parent ee8065ed36
commit ebb5b2de73
5 changed files with 15 additions and 160 deletions

View File

@ -6,7 +6,6 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.ReadTimeoutHandler;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.MessageQueue;
import org.ethereum.net.PeerListener;
import org.ethereum.net.eth.EthHandler;
@ -62,10 +61,7 @@ public class PeerClient {
if (peerListener != null)
peerListener.console("Connecting to: " + host + ":" + port);
if (peerDiscoveryMode)
p2pHandler = new P2pHandler(peerDiscoveryMode, msgQueue);
else
p2pHandler = new P2pHandler(peerListener, msgQueue);
p2pHandler = new P2pHandler(msgQueue, peerListener, peerDiscoveryMode);
p2pHandler.activate();
ethHandler = new EthHandler(msgQueue, peerListener, peerDiscoveryMode);
@ -136,12 +132,8 @@ public class PeerClient {
}
public boolean isSyncDone(){
if (ethHandler.isActive() && ethHandler.getSyncStatus() == EthHandler.SyncSatus.SYNC_DONE)
return true;
else
return false;
return ethHandler.isActive()
&& ethHandler.getSyncStatus() == EthHandler.SyncSatus.SYNC_DONE;
}
public HelloMessage getHelloHandshake(){

View File

@ -29,8 +29,8 @@ public class MessageFactory {
return StaticMessages.GET_PEERS_MESSAGE;
case PEERS:
return new PeersMessage(encoded);
case USER:
return new UserMessage(encoded);
default:
break;
}
}
@ -54,8 +54,8 @@ public class MessageFactory {
return new BlocksMessage(encoded);
case NEW_BLOCK:
return new NewBlockMessage(encoded);
case PACKET_COUNT:
return new PacketCountMessage(encoded);
default:
break;
}
}

View File

@ -57,18 +57,13 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
private HelloMessage handshakeHelloMessage = null;
public P2pHandler(MessageQueue msgQueue) {
this.msgQueue = msgQueue;
}
public P2pHandler(boolean peerDiscoveryMode, MessageQueue msgQueue) {
this(msgQueue);
this.peerDiscoveryMode = peerDiscoveryMode;
public P2pHandler() {
}
public P2pHandler(PeerListener peerListener, MessageQueue msgQueue) {
this(msgQueue);
public P2pHandler(MessageQueue msgQueue, PeerListener peerListener, boolean peerDiscoveryMode) {
this.msgQueue = msgQueue;
this.peerListener = peerListener;
this.peerDiscoveryMode = peerDiscoveryMode;
}
@Override
@ -114,7 +109,7 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
break;
case GET_PEERS:
msgQueue.receivedMessage(msg);
//sendPeers(); // todo: implement session management for peer request
sendPeers(); // todo: implement session management for peer request
break;
case PEERS:
msgQueue.receivedMessage(msg);
@ -127,10 +122,6 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
ctx.close().sync();
ctx.disconnect().sync();
}
break;
case USER:
processUser((UserMessage) msg);
break;
default:
ctx.fireChannelRead(msg);
@ -138,10 +129,6 @@ public class P2pHandler extends SimpleChannelInboundHandler<P2pMessage> {
}
}
public void processUser(UserMessage msg){
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
this.killTimers();

View File

@ -1,118 +0,0 @@
package org.ethereum.net.p2p;
import org.ethereum.util.ByteUtil;
import org.ethereum.util.RLP;
import org.ethereum.util.RLPItem;
import org.ethereum.util.RLPList;
import org.spongycastle.util.encoders.Hex;
import static org.ethereum.net.p2p.P2pMessageCodes.USER;
/**
* Wrapper around an Ethereum Status message on the network
*
*/
public class UserMessage extends P2pMessage {
private byte protocolVersion;
private byte networkId;
/** Total difficulty of the best chain as found in block header. */
private byte[] totalDifficulty;
/** The hash of the best (i.e. highest TD) known block. */
private byte[] bestHash;
/** The hash of the Genesis block */
private byte[] genesisHash;
public UserMessage(byte[] encoded) {
super(encoded);
}
public UserMessage(byte protocolVersion, byte networkId,
byte[] totalDifficulty, byte[] bestHash, byte[] genesisHash) {
this.protocolVersion = protocolVersion;
this.networkId = networkId;
this.totalDifficulty = totalDifficulty;
this.bestHash = bestHash;
this.genesisHash = genesisHash;
this.parsed = true;
}
private void parse() {
RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
this.protocolVersion = ((RLPItem) paramsList.get(1)).getRLPData()[0];
byte[] networkIdBytes = ((RLPItem) paramsList.get(2)).getRLPData();
this.networkId = networkIdBytes == null ? 0 : networkIdBytes[0];
this.totalDifficulty = ((RLPItem) paramsList.get(3)).getRLPData();
this.bestHash = ((RLPItem) paramsList.get(4)).getRLPData();
this.genesisHash = ((RLPItem) paramsList.get(5)).getRLPData();
parsed = true;
}
private void encode() {
byte[] command = RLP.encodeByte(USER.asByte());
byte[] protocolVersion = RLP.encodeByte(this.protocolVersion);
byte[] networkId = RLP.encodeByte(this.networkId);
byte[] totalDifficulty = RLP.encodeElement(this.totalDifficulty);
byte[] bestHash = RLP.encodeElement(this.bestHash);
byte[] genesisHash = RLP.encodeElement(this.genesisHash);
this.encoded = RLP.encodeList(command, protocolVersion, networkId,
totalDifficulty, bestHash, genesisHash);
}
@Override
public byte[] getEncoded() {
if (encoded == null) encode();
return encoded;
}
@Override
public Class<?> getAnswerMessage() {
return null;
}
public byte getProtocolVersion() {
if (!parsed) parse();
return protocolVersion;
}
public byte getNetworkId() {
if (!parsed) parse();
return networkId;
}
public byte[] getTotalDifficulty() {
if (!parsed) parse();
return totalDifficulty;
}
public byte[] getBestHash() {
if (!parsed) parse();
return bestHash;
}
public byte[] getGenesisHash() {
if (!parsed) parse();
return genesisHash;
}
@Override
public P2pMessageCodes getCommand(){
return P2pMessageCodes.USER;
}
@Override
public String toString() {
if (!parsed) parse();
return "[" + this.getCommand().name() +
" protocolVersion=" + this.protocolVersion +
" networkId=" + this.networkId +
" totalDifficulty=" + ByteUtil.toHexString(this.totalDifficulty) +
" bestHash=" + Hex.toHexString(this.bestHash) +
" genesisHash=" + Hex.toHexString(this.genesisHash) +
"]";
}
}

View File

@ -20,7 +20,6 @@ import java.util.List;
* @author: Roman Mandeleil
* Created on: 15/10/2014 12:34
*/
public class AdaptiveMessageIdsTest {
@Before
@ -43,7 +42,6 @@ public class AdaptiveMessageIdsTest {
Assert.assertEquals(15, P2pMessageCodes.USER.asByte());
}
@Test
public void test2() {
@ -71,7 +69,6 @@ public class AdaptiveMessageIdsTest {
Assert.assertEquals(0x10 + 8, EthMessageCodes.PACKET_COUNT.asByte());
}
@Test
public void test3(){
@ -91,11 +88,10 @@ public class AdaptiveMessageIdsTest {
Assert.assertEquals(0x20 + 4, ShhMessageCodes.PACKET_COUNT.asByte());
}
@Test
public void test4(){
P2pHandler p2pHandler = new P2pHandler(null);
P2pHandler p2pHandler = new P2pHandler();
List<Capability> capabilities = Arrays.asList(
new Capability(Capability.ETH, EthHandler.VERSION),
@ -122,7 +118,7 @@ public class AdaptiveMessageIdsTest {
@Test // Capabilities should be read in alphabetical order
public void test5(){
P2pHandler p2pHandler = new P2pHandler(null);
P2pHandler p2pHandler = new P2pHandler();
List<Capability> capabilities = Arrays.asList(
new Capability(Capability.SHH, ShhHandler.VERSION),
@ -145,6 +141,4 @@ public class AdaptiveMessageIdsTest {
Assert.assertEquals(0x19 + 3, ShhMessageCodes.REMOVE_FILTER.asByte());
Assert.assertEquals(0x19 + 4, ShhMessageCodes.PACKET_COUNT.asByte());
}
}