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

Class Class, % Method, % Line, %
PeerExplorerCleaner 0% (0/1) 0% (0/5) 0% (0/11)


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 java.util.concurrent.Executors; 22 import java.util.concurrent.ScheduledExecutorService; 23 import java.util.concurrent.TimeUnit; 24  25 /** 26  * Created by mario on 22/02/17. 27  */ 28 public class PeerExplorerCleaner { 29  30  private PeerExplorer peerExplorer; 31  private ScheduledExecutorService updateTask; 32  private long updatePeriod; 33  private long cleanPeriod; 34  private boolean running = false; 35  36  public PeerExplorerCleaner(PeerExplorer peerExplorer, long updatePeriod, long cleanPeriod) { 37  this.peerExplorer = peerExplorer; 38  this.updatePeriod = updatePeriod; 39  this.cleanPeriod = cleanPeriod; 40  // it should stay on a single thread since there are two tasks that could interfere with each other running here 41  this.updateTask = Executors.newSingleThreadScheduledExecutor(r -> new Thread(r, "PeerExplorerCleaner")); 42  } 43  44  public void run() { 45  if (!running) { 46  this.startUpdateTask(); 47  running = true; 48  } 49  } 50  51  private void startUpdateTask() { 52  updateTask.scheduleAtFixedRate(() -> peerExplorer.clean(), cleanPeriod, cleanPeriod, TimeUnit.MILLISECONDS); 53  updateTask.scheduleAtFixedRate(() -> peerExplorer.update(), updatePeriod, updatePeriod, TimeUnit.MILLISECONDS); 54  } 55  56  57 }