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

Class Class, % Method, % Line, %
PeerConnectionData 0% (0/1) 0% (0/9) 0% (0/31)


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.client.Capability; 23 import org.ethereum.util.RLP; 24  25 import org.bouncycastle.util.encoders.Hex; 26  27 import java.net.InetAddress; 28  29 import java.util.ArrayList; 30 import java.util.List; 31  32 /** 33  * This class models a peer in the network 34  */ 35 public class PeerConnectionData { 36  37  private final InetAddress address; 38  private final int port; 39  private final String peerId; 40  private final List<Capability> capabilities; 41  42  public PeerConnectionData(InetAddress ip, int port, String peerId) { 43  this.address = ip; 44  this.port = port; 45  this.peerId = peerId; 46  this.capabilities = new ArrayList<>(); 47  } 48  49  public InetAddress getAddress() { 50  return address; 51  } 52  53  public int getPort() { 54  return port; 55  } 56  57  public String getPeerId() { 58  return peerId == null ? "" : peerId; 59  } 60  61  public List<Capability> getCapabilities() { 62  return new ArrayList<>(capabilities); 63  } 64  65  public byte[] getEncoded() { 66  byte[] ip = RLP.encodeElement(this.address.getAddress()); 67  byte[] port = RLP.encodeInt(this.port); 68  byte[] peerId = RLP.encodeElement(Hex.decode(this.peerId)); 69  byte[][] encodedCaps = new byte[this.capabilities.size()][]; 70  for (int i = 0; i < this.capabilities.size() * 2; i++) { 71  encodedCaps[i] = RLP.encodeByte(this.capabilities.get(i).getVersion()); 72  } 73  byte[] capabilities = RLP.encodeList(encodedCaps); 74  return RLP.encodeList(ip, port, peerId, capabilities); 75  } 76  77  @Override 78  public String toString() { 79  return "[ip=" + getAddress().getHostAddress() + 80  " port=" + getPort() 81  + " peerId=" + getPeerId() + "]"; 82  } 83  84  @Override 85  public boolean equals(Object obj) { 86  if (obj == null) { 87  return false; 88  } 89  90  if (this.getClass() != obj.getClass()) { 91  return false; 92  } 93  94  PeerConnectionData peerData = (PeerConnectionData) obj; 95  return peerData.peerId.equals(this.peerId) 96  || this.getAddress().equals(peerData.getAddress()); 97  } 98  99  @Override 100  public int hashCode() { 101  int result = peerId.hashCode(); 102  result = 31 * result + address.hashCode(); 103  result = 31 * result + port; 104  return result; 105  } 106 }