Coverage Summary for Class: PongPeerMessage (co.rsk.net.discovery.message)

Class Class, % Method, % Line, %
PongPeerMessage 0% (0/1) 0% (0/9) 0% (0/43)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2017 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.net.discovery.message; 20  21 import co.rsk.net.discovery.PeerDiscoveryException; 22 import org.apache.commons.lang3.builder.ToStringBuilder; 23 import org.ethereum.crypto.ECKey; 24 import org.ethereum.util.ByteUtil; 25 import org.ethereum.util.RLP; 26 import org.ethereum.util.RLPItem; 27 import org.ethereum.util.RLPList; 28  29 import java.nio.charset.Charset; 30 import java.nio.charset.StandardCharsets; 31 import java.util.OptionalInt; 32  33 import static org.ethereum.util.ByteUtil.intToBytes; 34 import static org.ethereum.util.ByteUtil.longToBytes; 35 import static org.ethereum.util.ByteUtil.stripLeadingZeroes; 36  37 /** 38  * Created by mario on 16/02/17. 39  */ 40 public class PongPeerMessage extends PeerDiscoveryMessage { 41  public static final String MORE_DATA = "PongPeerMessage needs more data"; 42  public static final String MORE_FROM_DATA = "PongPeerMessage needs more data in the from"; 43  private String host; 44  private int port; 45  private String messageId; 46  47  public PongPeerMessage(byte[] wire, byte[] mdc, byte[] signature, byte[] type, byte[] data) { 48  super(wire, mdc, signature, type, data); 49  this.parse(data); 50  } 51  52  private PongPeerMessage() { 53  } 54  55  public static PongPeerMessage create(String host, int port, String check, ECKey privKey, Integer networkId) { 56  /* RLP Encode data */ 57  byte[] rlpIp = RLP.encodeElement(host.getBytes(StandardCharsets.UTF_8)); 58  59  byte[] tmpPort = longToBytes(port); 60  byte[] rlpPort = RLP.encodeElement(stripLeadingZeroes(tmpPort)); 61  62  byte[] rlpIpTo = RLP.encodeElement(host.getBytes(StandardCharsets.UTF_8)); 63  byte[] tmpPortTo = longToBytes(port); 64  byte[] rlpPortTo = RLP.encodeElement(stripLeadingZeroes(tmpPortTo)); 65  66  byte[] rlpCheck = RLP.encodeElement(check.getBytes(StandardCharsets.UTF_8)); 67  68  byte[] type = new byte[]{(byte) DiscoveryMessageType.PONG.getTypeValue()}; 69  byte[] rlpFromList = RLP.encodeList(rlpIp, rlpPort, rlpPort); 70  byte[] rlpToList = RLP.encodeList(rlpIpTo, rlpPortTo, rlpPortTo); 71  byte[] data; 72  73  byte[] tmpNetworkId = intToBytes(networkId); 74  byte[] rlpNetworkID = RLP.encodeElement(stripLeadingZeroes(tmpNetworkId)); 75  data = RLP.encodeList(rlpFromList, rlpToList, rlpCheck, rlpNetworkID); 76  77  PongPeerMessage message = new PongPeerMessage(); 78  message.encode(type, data, privKey); 79  80  message.setNetworkId(OptionalInt.of(networkId)); 81  message.messageId = check; 82  message.host = host; 83  message.port = port; 84  85  return message; 86  } 87  88  public String getHost() { 89  return this.host; 90  } 91  92  public int getPort() { 93  return this.port; 94  } 95  96  @Override 97  public final void parse(byte[] data) { 98  RLPList dataList = (RLPList) RLP.decode2OneItem(data, 0); 99  if (dataList.size() < 3) { 100  throw new PeerDiscoveryException(MORE_DATA); 101  } 102  RLPList fromList = (RLPList) dataList.get(1); 103  104  if (fromList.size() != 3) { 105  throw new PeerDiscoveryException(MORE_FROM_DATA); 106  } 107  108  byte[] ipB = fromList.get(0).getRLPData(); 109  this.host = new String(ipB, Charset.forName("UTF-8")); 110  this.port = ByteUtil.byteArrayToInt(fromList.get(1).getRLPData()); 111  112  RLPItem chk = (RLPItem) dataList.get(2); 113  114  this.messageId = new String(chk.getRLPData(), Charset.forName("UTF-8")); 115  116  //Message from nodes that do not have this 117  this.setNetworkIdWithRLP(dataList.size()>3?dataList.get(3):null); 118  } 119  120  public String getMessageId() { 121  return this.messageId; 122  } 123  124  @Override 125  public DiscoveryMessageType getMessageType() { 126  return DiscoveryMessageType.PONG; 127  } 128  129  @Override 130  public String toString() { 131  return new ToStringBuilder(this) 132  .append(this.host) 133  .append(this.port) 134  .append(this.messageId).toString(); 135  } 136 }