Coverage Summary for Class: InetAddressTable (co.rsk.scoring)

Class Class, % Method, % Line, %
InetAddressTable 0% (0/1) 0% (0/8) 0% (0/30)


1 package co.rsk.scoring; 2  3 import java.net.InetAddress; 4 import java.util.ArrayList; 5 import java.util.List; 6 import java.util.Set; 7 import java.util.concurrent.ConcurrentHashMap; 8  9 /** 10  * InetAddressTable has a set of net addresses and blocks 11  * It is used by scoring manager to keep the banned addresses 12  * @see PeerScoringManager 13  * <p> 14  * Created by ajlopez on 10/07/2017. 15  */ 16 public class InetAddressTable { 17  private final Set<InetAddress> addresses = ConcurrentHashMap.newKeySet(); 18  private final Set<InetAddressBlock> blocks = ConcurrentHashMap.newKeySet(); 19  20  /** 21  * Adds an address into the address set 22  * 23  * @param address the address to add 24  */ 25  public void addAddress(InetAddress address) { 26  this.addresses.add(address); 27  } 28  29  /** 30  * Removes an address from the address set 31  * 32  * @param address the address to remove 33  */ 34  public void removeAddress(InetAddress address) { this.addresses.remove(address); } 35  36  /** 37  * Adds an address block into the address block set 38  * 39  * @param addressBlock the address block to add 40  */ 41  public void addAddressBlock(InetAddressBlock addressBlock) { 42  this.blocks.add(addressBlock); 43  } 44  45  /** 46  * Removes an address block from the address block set 47  * 48  * @param addressBlock the address block to remove 49  */ 50  public void removeAddressBlock(InetAddressBlock addressBlock) { 51  this.blocks.remove(addressBlock); 52  } 53  54  /** 55  * Checks if the given address is contained in the address set 56  * or it is contained into an address block 57  * 58  * @param address the address to check 59  * @return <tt>true</tt> if the address is in the address set 60  * or is contained in some address block 61  */ 62  public boolean contains(InetAddress address) { 63  if (this.addresses.contains(address)) { 64  return true; 65  } 66  67  if (this.blocks.isEmpty()) { 68  return false; 69  } 70  71  //TODO(mmarquez): we need to check if this is thread safe 72  InetAddressBlock[] bs = this.blocks.toArray(new InetAddressBlock[0]); 73  for (InetAddressBlock mask : bs) { 74  if (mask.contains(address)) { 75  return true; 76  } 77  } 78  79  return false; 80  } 81  82  /** 83  * Returns the list of InetAddress 84  * 85  * @return the list of known addresses 86  */ 87  public List<InetAddress> getAddressList() { 88  if (this.addresses.isEmpty()) { 89  return new ArrayList<>(); 90  } 91  92  //TODO(mmarquez): we need to check if this is thread safe 93  InetAddress[] as = this.addresses.toArray(new InetAddress[0]); 94  List<InetAddress> list = new ArrayList<>(as.length); 95  96  for (InetAddress inetAddress : as) { 97  list.add(inetAddress); 98  } 99  100  return list; 101  } 102  103  /** 104  * Returns the list of known address blocks 105  * 106  * @return the list of known address blocks 107  */ 108  public List<InetAddressBlock> getAddressBlockList() { 109  if (this.blocks.isEmpty()) { 110  return new ArrayList<>(); 111  } 112  113  //TODO(mmarquez): we need to check if this is thread safe 114  InetAddressBlock[] bs = this.blocks.toArray(new InetAddressBlock[0]); 115  List<InetAddressBlock> list = new ArrayList<>(bs.length); 116  117  for (InetAddressBlock inetAddressBlock : bs) { 118  list.add(inetAddressBlock); 119  } 120  121  return list; 122  } 123 }