Move ReasonCode to enum

This commit is contained in:
nicksavers 2014-05-05 15:01:37 +02:00
parent b7d827a87a
commit 4e08b4ee65
3 changed files with 50 additions and 18 deletions

View File

@ -4,6 +4,7 @@ import org.ethereum.net.rlp.RLPItem;
import org.ethereum.net.rlp.RLPList;
import org.ethereum.net.Command;
import static org.ethereum.net.Command.DISCONNECT;
import static org.ethereum.net.message.ReasonCode.DISCONNECT_REQUESTED;
/**
* www.ethereumJ.com
@ -12,17 +13,7 @@ import static org.ethereum.net.Command.DISCONNECT;
*/
public class DisconnectMessage extends Message {
private byte reason;
public static byte REASON_DISCONNECT_REQUESTED = 0x00;
public static byte REASON_TCP_ERROR = 0x01;
public static byte REASON_BAD_PROTOCOL = 0x02;
public static byte REASON_USELESS_PEER = 0x03;
public static byte REASON_TOO_MANY_PEERS = 0x04;
public static byte REASON_ALREADY_CONNECTED = 0x05;
public static byte REASON_WRONG_GENESIS = 0x06;
public static byte REASON_INCOMPATIBLE_PROTOCOL = 0x07;
public static byte REASON_PEER_QUITING = 0x08;
private ReasonCode reason;
public DisconnectMessage(RLPList rawData) {
super(rawData);
@ -39,9 +30,9 @@ public class DisconnectMessage extends Message {
byte[] reasonB = ((RLPItem)paramsList.getElement(1)).getData();
if (reasonB == null){
this.reason = 0;
this.reason = DISCONNECT_REQUESTED;
} else {
this.reason = reasonB[0];
this.reason = ReasonCode.fromInt(reasonB[0]);
}
this.parsed = true;
// todo: what to do when mal data ?
@ -52,7 +43,7 @@ public class DisconnectMessage extends Message {
return null;
}
public byte getReason() {
public ReasonCode getReason() {
if (!parsed) parseRLP();
return reason;
}

View File

@ -0,0 +1,42 @@
package org.ethereum.net.message;
import java.util.HashMap;
import java.util.Map;
public enum ReasonCode {
DISCONNECT_REQUESTED(0x00),
TCP_ERROR(0x01),
BAD_PROTOCOL(0x02),
USELESS_PEER(0x03),
TOO_MANY_PEERS(0x04),
ALREADY_CONNECTED(0x05),
WRONG_GENESIS(0x06),
INCOMPATIBLE_PROTOCOL(0x07),
PEER_QUITING(0x08),
UNKNOWN(0xFF);
private int reason;
private static final Map<Integer, ReasonCode> intToTypeMap = new HashMap<Integer, ReasonCode>();
static {
for (ReasonCode type : ReasonCode.values()) {
intToTypeMap.put(type.reason, type);
}
}
private ReasonCode(int cmd) {
this.reason = cmd;
}
public static ReasonCode fromInt(int i) {
ReasonCode type = intToTypeMap.get(Integer.valueOf(i));
if (type == null)
return ReasonCode.UNKNOWN;
return type;
}
public byte asByte() {
return (byte) reason;
}
}

View File

@ -11,6 +11,7 @@ import org.ethereum.net.message.GetChainMessage;
import org.ethereum.net.message.HelloMessage;
import org.ethereum.net.message.NotInChainMessage;
import org.ethereum.net.message.PeersMessage;
import org.ethereum.net.message.ReasonCode;
import org.ethereum.net.message.TransactionsMessage;
import org.ethereum.net.rlp.RLP;
import org.ethereum.net.rlp.RLPList;
@ -90,8 +91,7 @@ public class MessagesTest {
DisconnectMessage disconnectMessage = new DisconnectMessage(rlpList);
System.out.println(disconnectMessage);
assertEquals(disconnectMessage.getReason(),
DisconnectMessage.REASON_DISCONNECT_REQUESTED);
assertEquals(disconnectMessage.getReason(), ReasonCode.DISCONNECT_REQUESTED);
}
@Test /* DisconnectMessage 2 */
@ -105,8 +105,7 @@ public class MessagesTest {
DisconnectMessage disconnectMessage = new DisconnectMessage(rlpList);
System.out.println(disconnectMessage);
assertEquals(disconnectMessage.getReason(),
DisconnectMessage.REASON_TCP_ERROR);
assertEquals(disconnectMessage.getReason(), ReasonCode.TCP_ERROR);
}
/* PEERS */