<commit_before>(function (exports) {
  var $ = exports.$
  var configurationObj = exports.configurationObj
  var alert = exports.alert
  var XMLHttpRequest = exports.XMLHttpRequest
  var Coordinator = exports.Coordinator
  var Plotter = exports.Plotter
  var mainObjs = {}
  var listIndex = 0
  var topics = [
    'Internet of Things', 'P2P Systems', 'Cloud Computing', 'Big Data',
    'Gossiping', 'Optimization', 'Recommendation Systems'
  ]
  var urlMsg = false
  var PROFILE = []
  function addOneChatBox (peerId) {
    var chatbox, header, input
    chatbox = $('<div></div>')
      .addClass('connection')
      .addClass('active')
      .attr('id', peerId)
    header = $('<label></label>').html('Chat with <strong>' + peerId + '</strong>')
    input = $('<div></div>')
      .addClass('conInput')
      .addClass('active')
    input.append($('<textarea></textarea>')
      .addClass('textarea')
      .attr('id', 'textArea' + peerId))
    input.append($('<button>Send</button>')
      .addClass('chatButton')
      .attr('onclick', "sendMessageFromChatbox('" + peerId + "')"))
    chatbox.append(header)
    chatbox.append(input)
    chatbox.append('<h1></h1>')
    chatbox.append('<h1></h1>')
    $('#connections').append(chatbox)
  }
  function addChatBoxes (neighbours) {
    document.getElementById('chat').removeChild(document.getElementById('connections'))
    $('#chat').append($('<div></div>').attr('id', 'connections'))
    for (var i = 0; i < neighbours.length; i++) addOneChatBox(neighbours[i])
  }
  function sendMessageFromChatbox (peerId) {
    var txtArea = document.getElementById('textArea' + peerId)
    var msg = '' + txtArea.value
    if (msg || msg !== '') {
      mainObjs.coordi.protocols.vicinity1.sendTo(peerId, msg)
      if ($('#' + peerId)) $('#' + peerId).append('<div><span class="you">You: </span>' + msg + '</div>')
    }
    txtArea.value = ''
  }
  function resetProfile () {
    if (listIndex !== 0) {
      var topics = document.getElementById('topicsSection')
      for (var i = 0; i < listIndex; i++) topics.removeChild(document.getElementById('list' + i))
      listIndex = 0
    }
    //var msg = 'Your profile was reseted. Please, chose other subjects of your interest and ' +
    //  'click on the button <<Update Profile>>'
    //alert(msg)
  }
  function updateProfile () {
    if (listIndex == 0) alert("Your profile can't be empty")
    else {
      var profile = [], element
      for (var i = 0; i < listIndex; i++) {
        element = document.getElementById('list' + i)
        profile.push(element.options[element.selectedIndex].text)
      }
      mainObjs.coordi.updateProfile(profile)
    }
  }
  function showProfile (prof) {
    var list, listItem
    for (var i = 0; i < prof.length; i++) {
      list = $('<select></select>').attr('id', 'list' + i)
      listItem = $('<option>' + prof[i] + '</option>').attr('value', 'v' + i)
      list.append(listItem)
      $('#topicsSection').append(list)
    }
    listIndex = prof.length
  }
  function addTopicList () {
    if (listIndex === topics.length) {
      alert('You can not chose another topic, please, continue typing you peer ID')
      return
    }
    var list = $('<select></select>').attr('id', 'list' + listIndex)
    var listItem
    for (var i = 0; i < topics.length; i++) {
      listItem = $('<option>' + topics[i] + '</option>').attr('value', 'v' + i)
      list.append(listItem)
    }
    $('#topicsSection').append(list)
    listIndex++
  }
  function getPeersInOverlay () {
    var host = configurationObj.signalingService.host
    var port = configurationObj.signalingService.port
    var http = new XMLHttpRequest()
    var url = 'http://' + host + ':' + port + '/getGraph'
    http.open('get', url, true)
    http.onerror = function (e) { console.log('ERROR: While trying to get peers in overlay') }
    http.onreadystatechange = function () {
      if (http.readyState !== 4) return
      if (http.status !== 200) {
        http.onerror()
        return
      }
      console.log('PEERS IN OVERLAY: ' + http.responseText)
      mainObjs.overlay = JSON.parse(http.responseText)
    }
    http.send(null)
  }
  function postKeepAlive () {
    var host = configurationObj.signalingService.host
    var port = configurationObj.signalingService.port
    var url = 'http://' + host + ':' + port + '/keepAlive'
    var xhr = new XMLHttpRequest()
    xhr.open('POST', url, true)
    xhr.setRequestHeader('Content-type', 'text/plain')
    xhr.onreadystatechange = function () {
      if (xhr.readyState !== 4) return
      if (xhr.status !== 200) {
        xhr.onerror()
        return
      }
      var res = JSON.parse(xhr.responseText)
      if (res.success) console.log('Keep alive was posted')
      else console.log('Keep alive was not received by the server')
    }
    xhr.onerror = function () { console.log('ERROR: While posting keep alive msg') }
    xhr.send(JSON.stringify({ id: mainObjs.peerId }))
  }
  function isPeeridValid (peerId) {
    var host = configurationObj.signalingService.host
    var port = configurationObj.signalingService.port
    var url = 'http://' + host + ':' + port + '/checkPeerId'
    var xhr = new XMLHttpRequest()
    var self = this
    xhr.open('POST', url, true)
    xhr.setRequestHeader('Content-type', 'text/plain')
    xhr.onreadystatechange = function () {
      if (xhr.readyState !== 4) return
      if (xhr.status !== 200) {
        xhr.onerror()
        return
      }
      console.log('IsPeerValid ? ' + xhr.responseText)
      var res = JSON.parse(xhr.responseText)
      if (!res.answer) self.launchPeer(peerId, host, port)
      else {
        alert('The peerId you typed was already taken, please give another one')
        $('#user').val('')
        $('#user').focus()
      }
    }
    xhr.onerror = function () { console.log('ERROR: While checking if peerId is valid') }
    var msg = { id: peerId }
    xhr.send(JSON.stringify(msg))
  }
  function launchPeer (peerId, host, port) {
    mainObjs['peerId'] = peerId
    mainObjs.firstCall = false
    document.getElementById('start').disabled = true
    document.getElementById('updateProfile').disabled = false
    if (PROFILE.length === 0) {
      var element
      for (var i = 0; i < listIndex; i++) {
        element = document.getElementById('list' + i)
        profile.push(element.options[element.selectedIndex].text)
      }
    }
    var coordi = new Coordinator(configurationObj, peerId, PROFILE)
    mainObjs['coordi'] = coordi
    coordi.on('msgReception', function (emitter, data) {
      if (!$('#' + emitter)) addOneChatBox(emitter)
      $('#' + emitter).append('<div><span class="peer">' + emitter + ': </span>' + data + '</div>')
    })
    coordi.on('neighbourhood', function (neighbours, algoId, loop) {
      if (neighbours.length !== 0) {
        var container = algoId === 'cyclon1' ? 'rpsGraph' : 'clusteringGraph'<commit_msg>BugFix: peers' profile is now consider when the URL functionality is not used<commit_after>(function (exports) {
  var $ = exports.$
  var configurationObj = exports.configurationObj
  var alert = exports.alert
  var XMLHttpRequest = exports.XMLHttpRequest
  var Coordinator = exports.Coordinator
  var Plotter = exports.Plotter
  var mainObjs = {}
  var listIndex = 0
  var topics = [
    'Internet of Things', 'P2P Systems', 'Cloud Computing', 'Big Data',
    'Gossiping', 'Optimization', 'Recommendation Systems'
  ]
  var urlMsg = false
  var PROFILE = []
  function addOneChatBox (peerId) {
    var chatbox, header, input
    chatbox = $('<div></div>')
      .addClass('connection')
      .addClass('active')
      .attr('id', peerId)
    header = $('<label></label>').html('Chat with <strong>' + peerId + '</strong>')
    input = $('<div></div>')
      .addClass('conInput')
      .addClass('active')
    input.append($('<textarea></textarea>')
      .addClass('textarea')
      .attr('id', 'textArea' + peerId))
    input.append($('<button>Send</button>')
      .addClass('chatButton')
      .attr('onclick', "sendMessageFromChatbox('" + peerId + "')"))
    chatbox.append(header)
    chatbox.append(input)
    chatbox.append('<h1></h1>')
    chatbox.append('<h1></h1>')
    $('#connections').append(chatbox)
  }
  function addChatBoxes (neighbours) {
    document.getElementById('chat').removeChild(document.getElementById('connections'))
    $('#chat').append($('<div></div>').attr('id', 'connections'))
    for (var i = 0; i < neighbours.length; i++) addOneChatBox(neighbours[i])
  }
  function sendMessageFromChatbox (peerId) {
    var txtArea = document.getElementById('textArea' + peerId)
    var msg = '' + txtArea.value
    if (msg || msg !== '') {
      mainObjs.coordi.protocols.vicinity1.sendTo(peerId, msg)
      if ($('#' + peerId)) $('#' + peerId).append('<div><span class="you">You: </span>' + msg + '</div>')
    }
    txtArea.value = ''
  }
  function resetProfile () {
    if (listIndex !== 0) {
      var topics = document.getElementById('topicsSection')
      for (var i = 0; i < listIndex; i++) topics.removeChild(document.getElementById('list' + i))
      listIndex = 0
    }
    //var msg = 'Your profile was reseted. Please, chose other subjects of your interest and ' +
    //  'click on the button <<Update Profile>>'
    //alert(msg)
  }
  function updateProfile () {
    if (listIndex == 0) alert("Your profile can't be empty")
    else {
      var profile = [], element
      for (var i = 0; i < listIndex; i++) {
        element = document.getElementById('list' + i)
        profile.push(element.options[element.selectedIndex].text)
      }
      mainObjs.coordi.updateProfile(profile)
    }
  }
  function showProfile (prof) {
    var list, listItem
    for (var i = 0; i < prof.length; i++) {
      list = $('<select></select>').attr('id', 'list' + i)
      listItem = $('<option>' + prof[i] + '</option>').attr('value', 'v' + i)
      list.append(listItem)
      $('#topicsSection').append(list)
    }
    listIndex = prof.length
  }
  function addTopicList () {
    if (listIndex === topics.length) {
      alert('You can not chose another topic, please, continue typing you peer ID')
      return
    }
    var list = $('<select></select>').attr('id', 'list' + listIndex)
    var listItem
    for (var i = 0; i < topics.length; i++) {
      listItem = $('<option>' + topics[i] + '</option>').attr('value', 'v' + i)
      list.append(listItem)
    }
    $('#topicsSection').append(list)
    listIndex++
  }
  function getPeersInOverlay () {
    var host = configurationObj.signalingService.host
    var port = configurationObj.signalingService.port
    var http = new XMLHttpRequest()
    var url = 'http://' + host + ':' + port + '/getGraph'
    http.open('get', url, true)
    http.onerror = function (e) { console.log('ERROR: While trying to get peers in overlay') }
    http.onreadystatechange = function () {
      if (http.readyState !== 4) return
      if (http.status !== 200) {
        http.onerror()
        return
      }
      console.log('PEERS IN OVERLAY: ' + http.responseText)
      mainObjs.overlay = JSON.parse(http.responseText)
    }
    http.send(null)
  }
  function postKeepAlive () {
    var host = configurationObj.signalingService.host
    var port = configurationObj.signalingService.port
    var url = 'http://' + host + ':' + port + '/keepAlive'
    var xhr = new XMLHttpRequest()
    xhr.open('POST', url, true)
    xhr.setRequestHeader('Content-type', 'text/plain')
    xhr.onreadystatechange = function () {
      if (xhr.readyState !== 4) return
      if (xhr.status !== 200) {
        xhr.onerror()
        return
      }
      var res = JSON.parse(xhr.responseText)
      if (res.success) console.log('Keep alive was posted')
      else console.log('Keep alive was not received by the server')
    }
    xhr.onerror = function () { console.log('ERROR: While posting keep alive msg') }
    xhr.send(JSON.stringify({ id: mainObjs.peerId }))
  }
  function isPeeridValid (peerId) {
    var host = configurationObj.signalingService.host
    var port = configurationObj.signalingService.port
    var url = 'http://' + host + ':' + port + '/checkPeerId'
    var xhr = new XMLHttpRequest()
    var self = this
    xhr.open('POST', url, true)
    xhr.setRequestHeader('Content-type', 'text/plain')
    xhr.onreadystatechange = function () {
      if (xhr.readyState !== 4) return
      if (xhr.status !== 200) {
        xhr.onerror()
        return
      }
      console.log('IsPeerValid ? ' + xhr.responseText)
      var res = JSON.parse(xhr.responseText)
      if (!res.answer) self.launchPeer(peerId, host, port)
      else {
        alert('The peerId you typed was already taken, please give another one')
        $('#user').val('')
        $('#user').focus()
      }
    }
    xhr.onerror = function () { console.log('ERROR: While checking if peerId is valid') }
    var msg = { id: peerId }
    xhr.send(JSON.stringify(msg))
  }
  function launchPeer (peerId, host, port) {
    mainObjs['peerId'] = peerId
    mainObjs.firstCall = false
    document.getElementById('start').disabled = true
    document.getElementById('updateProfile').disabled = false
    if (PROFILE.length === 0) {
      var element
      for (var i = 0; i < listIndex; i++) {
        element = document.getElementById('list' + i)
        PROFILE.push(element.options[element.selectedIndex].text)
      }
    }
    var coordi = new Coordinator(configurationObj, peerId, PROFILE)
    mainObjs['coordi'] = coordi
    coordi.on('msgReception', function (emitter, data) {
      if (!$('#' + emitter)) addOneChatBox(emitter)
      $('#' + emitter).append('<div><span class="peer">' + emitter + ': </span>' + data + '</div>')
    })
    coordi.on('neighbourhood', function (neighbours, algoId, loop) {
      if (neighbours.length !== 0) {
        var container = algoId === 'cyclon1' ? 'rpsGraph' : 'clusteringGraph'
{'fraction_non_alphanumeric': 0.16225978904782545, 'fraction_numerical': 0.0044791215142320475, 'mean_word_length': 3.006657018813314, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 75, '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': '4517692', 'n_tokens_mistral': 4416, 'n_tokens_neox': 4322, 'n_words': 1023}