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

Class Class, % Method, % Line, %
PeersMessage 0% (0/1) 0% (0/9) 0% (0/42)


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.util.ByteUtil; 23 import org.ethereum.util.RLP; 24 import org.ethereum.util.RLPList; 25  26 import java.net.InetAddress; 27 import java.net.UnknownHostException; 28 import java.util.ArrayList; 29 import java.util.LinkedHashSet; 30 import java.util.List; 31 import java.util.Set; 32  33 /** 34  * Wrapper around an Ethereum Peers message on the network 35  * 36  * @see org.ethereum.net.p2p.P2pMessageCodes#PEERS 37  */ 38 public class PeersMessage extends P2pMessage { 39  40  private boolean parsed = false; 41  42  private Set<PeerConnectionData> peers; 43  44  public PeersMessage(byte[] payload) { 45  super(payload); 46  } 47  48  public PeersMessage(Set<PeerConnectionData> peers) { 49  this.peers = peers; 50  parsed = true; 51  } 52  53  private void parse() { 54  RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0); 55  56  peers = new LinkedHashSet<>(); 57  for (int i = 1; i < paramsList.size(); ++i) { 58  RLPList peerParams = (RLPList) paramsList.get(i); 59  byte[] ipBytes = peerParams.get(0).getRLPData(); 60  byte[] portBytes = peerParams.get(1).getRLPData(); 61  byte[] peerIdRaw = peerParams.get(2).getRLPData(); 62  63  try { 64  int peerPort = ByteUtil.byteArrayToInt(portBytes); 65  InetAddress address = InetAddress.getByAddress(ipBytes); 66  67  String peerId = peerIdRaw == null ? "" : ByteUtil.toHexString(peerIdRaw); 68  PeerConnectionData peer = new PeerConnectionData(address, peerPort, peerId); 69  peers.add(peer); 70  } catch (UnknownHostException e) { 71  throw new RuntimeException("Malformed ip", e); 72  } 73  } 74  this.parsed = true; 75  } 76  77  private void encode() { 78  byte[][] encodedByteArrays = new byte[this.peers.size() + 1][]; 79  encodedByteArrays[0] = RLP.encodeByte(this.getCommand().asByte()); 80  List<PeerConnectionData> peerList = new ArrayList<>(this.peers); 81  for (int i = 0; i < peerList.size(); i++) { 82  encodedByteArrays[i + 1] = peerList.get(i).getEncoded(); 83  } 84  this.encoded = RLP.encodeList(encodedByteArrays); 85  } 86  87  @Override 88  public byte[] getEncoded() { 89  if (encoded == null) { 90  encode(); 91  } 92  return encoded; 93  } 94  95  public Set<PeerConnectionData> getPeers() { 96  if (!parsed) { 97  this.parse(); 98  } 99  return peers; 100  } 101  102  @Override 103  public P2pMessageCodes getCommand() { 104  return P2pMessageCodes.PEERS; 105  } 106  107  @Override 108  public Class<?> getAnswerMessage() { 109  return null; 110  } 111  112  public String toString() { 113  if (!parsed) { 114  this.parse(); 115  } 116  117  StringBuilder sb = new StringBuilder(); 118  for (PeerConnectionData peerData : peers) { 119  sb.append("\n ").append(peerData); 120  } 121  return "[" + this.getCommand().name() + sb.toString() + "]"; 122  } 123 }