<commit_before>package scorex.network.peer

import java.net.InetSocketAddress

import akka.actor.Actor
import scorex.app.Application
import scorex.network._
import scorex.utils.ScorexLogging

import scala.collection.JavaConversions._
import scala.collection.mutable
import scala.util.Random

/**
  * Must be singleton
  * @param application
  */
class PeerManager(application: Application) extends Actor with ScorexLogging {

  import PeerManager._

  private val connectedPeers = mutable.Map[ConnectedPeer, Option[Handshake]]()
  private var connectingPeer: Option[InetSocketAddress] = None

  private lazy val settings = application.settings
  private lazy val networkController = application.networkController

  private lazy val peerDatabase = new PeerDatabaseImpl(application)

  settings.knownPeers.foreach{address =>
    val defaultPeerInfo = PeerInfo(System.currentTimeMillis(), None, None)
    peerDatabase.addOrUpdateKnownPeer(address, defaultPeerInfo)
  }

  private def randomPeer(): Option[InetSocketAddress] = {
    val peers = peerDatabase.knownPeers(true).keys.toSeq
    if (peers.nonEmpty) Some(peers(Random.nextInt(peers.size)))
    else None
  }

  //todo: combine AddKnownPeer & UpdatePeer?
  private def peerLists: Receive = {
    case AddKnownPeer(address) =>
      val peerInfo = PeerInfo(System.currentTimeMillis())
      peerDatabase.addOrUpdateKnownPeer(address, peerInfo)

    case UpdatePeer(address, peerNonce, peerName) =>
      val peerInfo = PeerInfo(System.currentTimeMillis(), peerNonce, peerName)
      peerDatabase.addOrUpdateKnownPeer(address, peerInfo)

    case KnownPeers =>
      sender() ! peerDatabase.knownPeers(false).keys.toSeq<commit_msg>Opt suffix for optional parameters, comment fix<commit_after>package scorex.network.peer

import java.net.InetSocketAddress

import akka.actor.Actor
import scorex.app.Application
import scorex.network._
import scorex.utils.ScorexLogging

import scala.collection.JavaConversions._
import scala.collection.mutable
import scala.util.Random

/**
  * Must be singleton
  * @param application - Scorex-based application
  */
class PeerManager(application: Application) extends Actor with ScorexLogging {

  import PeerManager._

  private val connectedPeers = mutable.Map[ConnectedPeer, Option[Handshake]]()
  private var connectingPeer: Option[InetSocketAddress] = None

  private lazy val settings = application.settings
  private lazy val networkController = application.networkController

  private lazy val peerDatabase = new PeerDatabaseImpl(application)

  settings.knownPeers.foreach{address =>
    val defaultPeerInfo = PeerInfo(System.currentTimeMillis(), None, None)
    peerDatabase.addOrUpdateKnownPeer(address, defaultPeerInfo)
  }

  private def randomPeer(): Option[InetSocketAddress] = {
    val peers = peerDatabase.knownPeers(true).keys.toSeq
    if (peers.nonEmpty) Some(peers(Random.nextInt(peers.size)))
    else None
  }

  //todo: combine AddKnownPeer & UpdatePeer?
  private def peerLists: Receive = {
    case AddKnownPeer(address) =>
      val peerInfo = PeerInfo(System.currentTimeMillis())
      peerDatabase.addOrUpdateKnownPeer(address, peerInfo)

    case UpdatePeer(address, peerNonceOpt, peerNameOpt) =>
      val peerInfo = PeerInfo(System.currentTimeMillis(), peerNonceOpt, peerNameOpt)
      peerDatabase.addOrUpdateKnownPeer(address, peerInfo)

    case KnownPeers =>
      sender() ! peerDatabase.knownPeers(false).keys.toSeq
{'fraction_non_alphanumeric': 0.08684286959047342, 'fraction_numerical': 0.0, 'mean_word_length': 4.979166666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '829055', 'n_tokens_mistral': 1004, 'n_tokens_neox': 965, 'n_words': 223}