Coverage Summary for Class: DisconnectMessage (org.ethereum.net.p2p)

Class Class, % Method, % Line, %
DisconnectMessage 0% (0/1) 0% (0/9) 0% (0/23)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>) 5  * 6  * This program is free software: you can redistribute it and/or modify 7  * it under the terms of the GNU Lesser General Public License as published by 8  * the Free Software Foundation, either version 3 of the License, or 9  * (at your option) any later version. 10  * 11  * This program is distributed in the hope that it will be useful, 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14  * GNU Lesser General Public License for more details. 15  * 16  * You should have received a copy of the GNU Lesser General Public License 17  * along with this program. If not, see <http://www.gnu.org/licenses/>. 18  */ 19  20 package org.ethereum.net.p2p; 21  22 import org.ethereum.net.message.ReasonCode; 23 import org.ethereum.util.RLP; 24 import org.ethereum.util.RLPList; 25  26 import static org.ethereum.net.message.ReasonCode.REQUESTED; 27  28 /** 29  * Wrapper around an Ethereum Disconnect message on the network 30  * 31  * @see org.ethereum.net.p2p.P2pMessageCodes#DISCONNECT 32  */ 33 public class DisconnectMessage extends P2pMessage { 34  35  private ReasonCode reason; 36  37  public DisconnectMessage(byte[] encoded) { 38  super(encoded); 39  } 40  41  public DisconnectMessage(ReasonCode reason) { 42  this.reason = reason; 43  parsed = true; 44  } 45  46  private void parse() { 47  RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); 48  49  byte[] reasonBytes = paramsList.get(0).getRLPData(); 50  if (reasonBytes == null) { 51  this.reason = REQUESTED; 52  } else { 53  this.reason = ReasonCode.fromInt(reasonBytes[0]); 54  } 55  56  parsed = true; 57  } 58  59  private void encode() { 60  byte[] encodedReason = RLP.encodeByte(this.reason.asByte()); 61  this.encoded = RLP.encodeList(encodedReason); 62  } 63  64  @Override 65  public byte[] getEncoded() { 66  if (encoded == null) { 67  encode(); 68  } 69  return encoded; 70  } 71  72  @Override 73  public P2pMessageCodes getCommand() { 74  return P2pMessageCodes.DISCONNECT; 75  } 76  77  @Override 78  public Class<?> getAnswerMessage() { 79  return null; 80  } 81  82  public ReasonCode getReason() { 83  if (!parsed) { 84  parse(); 85  } 86  return reason; 87  } 88  89  public String toString() { 90  if (!parsed) { 91  parse(); 92  } 93  return "[" + this.getCommand().name() + " reason=" + reason + "]"; 94  } 95 }