<commit_before>class Kontrol extends KDObject

  bound: Bongo.bound

  [NOTREADY, READY, CLOSED] = [0,1,3]

  kontrolEndpoint = "http://#{KD.config.newkontrol.host}:#{KD.config.newkontrol.port}/request"

  constructor: (options)->
    super
    @readyState = NOTREADY
    @addr = "ws://#{KD.config.newkontrol.host}:#{KD.config.newkontrol.port}/_moh_/pub" #kontrol addr
    @connect()

  getKites: (kitename, callback)->
    queryData =
      username       : "#{KD.nick()}"
      kitename       : kitename
      authentication :
        type         : "browser"
        key          : KD.remote.getSessionToken()

    xhr = new XMLHttpRequest
    xhr.open "POST", kontrolEndpoint, yes
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
    xhr.send JSON.stringify queryData
    xhr.onload = =>
      if xhr.status is 200
        data = JSON.parse xhr.responseText
        callback null, data
      else
        callback xhr.responseText, null

  connect:->
    @ws = new WebSocket @addr, KD.remote.getSessionToken()
    @ws.onopen    = @bound 'onOpen'
    @ws.onclose   = @bound 'onClose'
    @ws.onmessage = @bound 'onMessage'
    @ws.onerror   = @bound 'onError'

  onOpen:->
    log "I'm connected to #{@addr}. Yayyy!"
    @readyState = READY
    @emit 'ready'
    @subscribe()

  onClose: (evt) ->
    log "Disconnected from #{@addr}, trying to reconnect"
    @readyState = CLOSED

  onMessage: (evt) ->
    @blobToString evt.data, (msg) =>
      try
        msg = JSON.parse msg
        log "Message from Kontrol", {msg}
      catch e
        log "json parse error: ", e, msg
        return

      switch msg.type
        when "KITE_REGISTERED"
          @emit "KiteRegistered", msg.args.kite
        when "KITE_DISCONNECTED"
          @emit "KiteDisconnected", msg.args.kite

  onError: (evt) ->
    log "kontrol: error #{evt.data}"

  ready: (callback)->
    return KD.utils.defer callback  if @readyState
    @once 'ready', callback

  subscribe: ->
    @send
      name: "subscribe"
      args:
        key: "kite.start.#{KD.nick()}"

  send: (data) ->
    @ready =>
      try @ws.send JSON.stringify data
      catch e then log e

  blobToString: (blob, callback) ->
    reader = new FileReader()
    reader.readAsText(blob)
    reader.onloadend = ->
      callback reader.result


<commit_msg>kontrol: now can validate requests and make queries based on the incoming KontrolQuery struct<commit_after>class Kontrol extends KDObject

  bound: Bongo.bound

  [NOTREADY, READY, CLOSED] = [0,1,3]

  kontrolEndpoint = "http://#{KD.config.newkontrol.host}:#{KD.config.newkontrol.port}/query"

  constructor: (options)->
    super
    @readyState = NOTREADY
    @addr = "ws://#{KD.config.newkontrol.host}:#{KD.config.newkontrol.port}/_moh_/pub" #kontrol addr
    @connect()

  getKites: (kitename, callback)->
    # find kites that belongs to username.
    queryData =
      username       : "#{KD.nick()}"
      name           : kitename
      authentication :
        type         : "browser"
        key          : KD.remote.getSessionToken()

    xhr = new XMLHttpRequest
    xhr.open "POST", kontrolEndpoint, yes
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
    xhr.send JSON.stringify queryData
    xhr.onload = =>
      if xhr.status is 200
        data = JSON.parse xhr.responseText
        callback null, data
      else
        callback xhr.responseText, null

  connect:->
    @ws = new WebSocket @addr, KD.remote.getSessionToken()
    @ws.onopen    = @bound 'onOpen'
    @ws.onclose   = @bound 'onClose'
    @ws.onmessage = @bound 'onMessage'
    @ws.onerror   = @bound 'onError'

  onOpen:->
    log "I'm connected to #{@addr}. Yayyy!"
    @readyState = READY
    @emit 'ready'
    @subscribe()

  onClose: (evt) ->
    log "Disconnected from #{@addr}, trying to reconnect"
    @readyState = CLOSED

  onMessage: (evt) ->
    @blobToString evt.data, (msg) =>
      try
        msg = JSON.parse msg
        log "Message from Kontrol", {msg}
      catch e
        log "json parse error: ", e, msg
        return

      switch msg.type
        when "KITE_REGISTERED"
          @emit "KiteRegistered", msg.args.kite
        when "KITE_DISCONNECTED"
          @emit "KiteDisconnected", msg.args.kite

  onError: (evt) ->
    log "kontrol: error #{evt.data}"

  ready: (callback)->
    return KD.utils.defer callback  if @readyState
    @once 'ready', callback

  subscribe: ->
    @send
      name: "subscribe"
      args:
        key: "kite.start.#{KD.nick()}"

  send: (data) ->
    @ready =>
      try @ws.send JSON.stringify data
      catch e then log e

  blobToString: (blob, callback) ->
    reader = new FileReader()
    reader.readAsText(blob)
    reader.onloadend = ->
      callback reader.result



{'fraction_non_alphanumeric': 0.1285803888772737, 'fraction_numerical': 0.0025088856366297305, 'mean_word_length': 2.402560455192034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6978342', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1568, 'n_words': 385}