Coverage Summary for Class: NodeFilter (org.ethereum.config)

Class Method, % Line, %
NodeFilter 0% (0/3) 0% (0/5)
NodeFilter$Entry 0% (0/3) 0% (0/13)
Total 0% (0/6) 0% (0/18)


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.config; 21  22 import co.rsk.net.NodeID; 23 import org.ethereum.net.rlpx.Node; 24  25 import java.net.InetAddress; 26 import java.net.UnknownHostException; 27 import java.util.ArrayList; 28 import java.util.List; 29  30 /** 31  * Created by Anton Nashatyrev on 14.01.2016. 32  */ 33 public class NodeFilter { 34  private List<Entry> entries = new ArrayList<>(); 35  36  public void add(byte[] nodeId, String hostIpPattern) { 37  entries.add(new Entry(nodeId, hostIpPattern)); 38  } 39  40  public boolean accept(Node node) { 41  return entries.stream().anyMatch(entry -> entry.accept(node)); 42  } 43  44  private class Entry { 45  private NodeID nodeId; 46  private String hostIpPattern; 47  48  public Entry(byte[] nodeId, String hostIpPattern) { 49  this.nodeId = new NodeID(nodeId); 50  if (hostIpPattern != null) { 51  int idx = hostIpPattern.indexOf("*"); 52  if (idx > 0) { 53  hostIpPattern = hostIpPattern.substring(0, idx); 54  } 55  } 56  this.hostIpPattern = hostIpPattern; 57  } 58  59  public boolean accept(InetAddress nodeAddr) { 60  String ip = nodeAddr.getHostAddress(); 61  return hostIpPattern != null && ip.startsWith(hostIpPattern); 62  } 63  64  public boolean accept(Node node) { 65  try { 66  return (nodeId == null || nodeId.equals(node.getId())) 67  && (hostIpPattern == null || accept(InetAddress.getByName(node.getHost()))); 68  } catch (UnknownHostException e) { 69  return false; 70  } 71  } 72  } 73 }