<commit_before>package se.gigurra.leavu3.interfaces

import com.twitter.finagle.FailedFastException
import com.twitter.util.Duration
import se.gigurra.heisenberg.MapDataParser
import se.gigurra.leavu3.datamodel.{Configuration, DlinkConfiguration, DlinkData, Mark, Member}
import se.gigurra.leavu3.util.{RestClient, SimpleTimer}
import se.gigurra.serviceutils.json.JSON
import se.gigurra.serviceutils.twitter.logging.Logging
import se.gigurra.serviceutils.twitter.service.ServiceException

import scala.util.{Failure, Success, Try}

/**
  * Created by kjolh on 3/20/2016.
  */
object Dlink extends Logging {

  @volatile var config: DlinkConfiguration = DlinkConfiguration()
  @volatile var dlinkClient = RestClient(config.host, config.port)
  @volatile var connected = false

  def start(appCfg: Configuration): Unit = {

    logger.info(s"Downloading datalink settings from dcs-remote ..")
    config = downloadDlinkConfig()
    CfgUpdate.handleDlinkConfig(config)
    logger.info(s"Dlink settings downloaded:\n ${JSON.write(config)}")
    In.start()
    if (appCfg.relayDlink)
      Out.start()
    CfgUpdate.start()
  }

  object CfgUpdate {

    def handleDlinkConfig(newConfig: DlinkConfiguration): Unit = {
      if (newConfig != config) {
        logger.info(s"Updating dlink settings to: \n ${JSON.write(newConfig)}")
        dlinkClient = RestClient(config.host, config.port)(dlinkClient.timer)
        config = newConfig
        In.onNewConfig()
        Out.onNewConfig()
      }
    }

    def start(): Unit = {
      SimpleTimer(Duration.fromSeconds(3)) {
        Try(downloadDlinkConfig()) match {
          case Success(newConfig) => handleDlinkConfig(newConfig)
          case Failure(e) => logger.warning(s"Unable to update data link configuration: $e")
        }
      }
    }
  }

  object In {

    @volatile var snapshot: Map[String, DlinkData] = Map.empty

    def onNewConfig(): Unit = {
      snapshot = Map.empty
    }

    def start(): Unit = {

      SimpleTimer.fromFps(1) {
        Try {
          val rawData = JSON.readMap(dlinkClient.getBlocking(config.team, cacheMaxAgeMillis = Some(10000L)))
          snapshot = rawData.collect {
            case ValidDlinkData(id, dlinkData) =>
              connected = true
              id -> dlinkData<commit_msg>Add coalition check for dlink data<commit_after>package se.gigurra.leavu3.interfaces

import com.twitter.finagle.FailedFastException
import com.twitter.util.Duration
import se.gigurra.heisenberg.MapDataParser
import se.gigurra.leavu3.datamodel.{Configuration, DlinkConfiguration, DlinkData, Mark, Member}
import se.gigurra.leavu3.util.{RestClient, SimpleTimer}
import se.gigurra.serviceutils.json.JSON
import se.gigurra.serviceutils.twitter.logging.Logging
import se.gigurra.serviceutils.twitter.service.ServiceException

import scala.util.{Failure, Success, Try}

/**
  * Created by kjolh on 3/20/2016.
  */
object Dlink extends Logging {

  @volatile var config: DlinkConfiguration = DlinkConfiguration()
  @volatile var dlinkClient = RestClient(config.host, config.port)
  @volatile var connected = false

  def start(appCfg: Configuration): Unit = {

    logger.info(s"Downloading datalink settings from dcs-remote ..")
    config = downloadDlinkConfig()
    CfgUpdate.handleDlinkConfig(config)
    logger.info(s"Dlink settings downloaded:\n ${JSON.write(config)}")
    In.start()
    if (appCfg.relayDlink)
      Out.start()
    CfgUpdate.start()
  }

  object CfgUpdate {

    def handleDlinkConfig(newConfig: DlinkConfiguration): Unit = {
      if (newConfig != config) {
        logger.info(s"Updating dlink settings to: \n ${JSON.write(newConfig)}")
        dlinkClient = RestClient(config.host, config.port)(dlinkClient.timer)
        config = newConfig
        In.onNewConfig()
        Out.onNewConfig()
      }
    }

    def start(): Unit = {
      SimpleTimer(Duration.fromSeconds(3)) {
        Try(downloadDlinkConfig()) match {
          case Success(newConfig) => handleDlinkConfig(newConfig)
          case Failure(e) => logger.warning(s"Unable to update data link configuration: $e")
        }
      }
    }
  }

  object In {

    @volatile var snapshot: Map[String, DlinkData] = Map.empty

    def onNewConfig(): Unit = {
      snapshot = Map.empty
    }

    def start(): Unit = {

      SimpleTimer.fromFps(1) {
        Try {
          val gameData = GameIn.snapshot
          if (gameData.isValid) {
            val rawData = JSON.readMap(dlinkClient.getBlocking(config.team, cacheMaxAgeMillis = Some(10000L)))
            snapshot = rawData.collect {
              case ValidDlinkData(id, dlinkData) if dlinkData.data.selfData.coalitionId == gameData.selfData.coalitionId =>
                connected = true
                id -> dlinkData
            }
{'fraction_non_alphanumeric': 0.10831234256926953, 'fraction_numerical': 0.007136859781696054, 'mean_word_length': 3.132697311361665, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2492382', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1460, 'n_words': 298}