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

Class Class, % Method, % Line, %
PeerDiscoveryRequest 0% (0/1) 0% (0/8) 0% (0/15)


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; 20  21 import co.rsk.net.discovery.message.DiscoveryMessageType; 22 import co.rsk.net.discovery.message.PeerDiscoveryMessage; 23 import org.ethereum.net.rlpx.Node; 24  25 import java.net.InetSocketAddress; 26  27 /** 28  * Created by mario on 17/02/17. 29  */ 30 public class PeerDiscoveryRequest { 31  private final String messageId; 32  private final PeerDiscoveryMessage message; 33  private final InetSocketAddress address; 34  private final DiscoveryMessageType expectedResponse; 35  private final long expirationDate; 36  private final int attemptNumber; 37  private final Node relatedNode; 38  39  public PeerDiscoveryRequest(String messageId, PeerDiscoveryMessage message, InetSocketAddress address, DiscoveryMessageType expectedResponse, Long expirationPeriod, int attemptNumber, Node relatedNode) { 40  this.messageId = messageId; 41  this.message = message; 42  this.address = address; 43  this.expectedResponse = expectedResponse; 44  this.expirationDate = System.currentTimeMillis() + expirationPeriod; 45  this.attemptNumber = attemptNumber; 46  this.relatedNode = relatedNode; 47  } 48  49  public String getMessageId() { 50  return messageId; 51  } 52  53  public PeerDiscoveryMessage getMessage() { 54  return message; 55  } 56  57  public InetSocketAddress getAddress() { 58  return address; 59  } 60  61  public int getAttemptNumber() { 62  return attemptNumber; 63  } 64  65  public Node getRelatedNode() { 66  return relatedNode; 67  } 68  69  public boolean validateMessageResponse(InetSocketAddress responseAddress, PeerDiscoveryMessage message) { 70  return this.expectedResponse == message.getMessageType() && !this.hasExpired() && getAddress().equals(responseAddress); 71  } 72  73  public boolean hasExpired() { 74  return System.currentTimeMillis() > expirationDate; 75  } 76 }