<commit_before>module.exports = SimpleSignalServer

var inherits = require('inherits')
var EventEmitter = require('nanobus')

inherits(SimpleSignalServer, EventEmitter)

function SimpleSignalServer (io) {
  if (!(this instanceof SimpleSignalServer)) return new SimpleSignalServer(io)

  EventEmitter.call(this)

  this._sockets = {}

  io.on('connection', (socket) => {
    socket.on('simple-signal[discover]', this._onDiscover.bind(this, socket))
  })
}

SimpleSignalServer.prototype._onDiscover = function (socket, discoveryData) {
  const discoveryRequest = { socket, discoveryData }
  discoveryRequest.discover = (id=socket.id, discoveryData={}) => {
    this._sockets[id] = socket
    socket.clientId = id

    socket.on('disconnect', () => {
      this.emit('disconnect', socket)
    })

    socket.emit('simple-signal[discover]', { id, discoveryData })

    socket.on('simple-signal[offer]', this._onOffer.bind(this, socket))
    socket.on('simple-signal[signal]', this._onSignal.bind(this, socket))
  }

  if (this.listeners('discover').length === 0) {
    discoveryRequest.discover() // defaults to using socket.id for identification
  } else {
    this.emit('discover', discoveryRequest)
  }
}

SimpleSignalServer.prototype._onOffer = function (socket, { sessionId, signal, target, metadata }) {
  const request = { initiator: socket.clientId, target, metadata, socket } 
  request.forward = (target=request.target, metadata=request.metadata) => {
    if (!this._sockets[target]) return
    this._sockets[target].emit('simple-signal[offer]', {
      initiator: socket.clientId, sessionId, signal, metadata
    })
  }

  if (this.listeners('request').length === 0) {
    request.forward()
  } else {
    this.emit('request', request)
  }
}

SimpleSignalServer.prototype._onSignal = function (socket, { target, sessionId, signal, metadata }) {
  if (!this._sockets[target]) return

  // misc. signaling data is always forwarded
  this._sockets[target].emit('simple-signal[signal]', {
    sessionId, signal, metadata
  })
}<commit_msg>Added support for multiple discovery<commit_after>module.exports = SimpleSignalServer

var inherits = require('inherits')
var EventEmitter = require('nanobus')

inherits(SimpleSignalServer, EventEmitter)

function SimpleSignalServer (io) {
  if (!(this instanceof SimpleSignalServer)) return new SimpleSignalServer(io)

  EventEmitter.call(this)

  this._sockets = {}

  io.on('connection', (socket) => {
    socket.on('simple-signal[discover]', this._onDiscover.bind(this, socket))
  })
}

SimpleSignalServer.prototype._onDiscover = function (socket, discoveryData) {
  const discoveryRequest = { socket, discoveryData }
  discoveryRequest.discover = (id=socket.id, discoveryData={}) => {
    this._sockets[id] = socket
    socket.clientId = id

    socket.removeAllListeners('disconnect');
    socket.removeAllListeners('simple-signal[offer]');
    socket.removeAllListeners('simple-signal[signal]');

    socket.on('disconnect', this._onDisconnect.bind(this, socket));

    socket.emit('simple-signal[discover]', { id, discoveryData })

    socket.on('simple-signal[offer]', this._onOffer.bind(this, socket))
    socket.on('simple-signal[signal]', this._onSignal.bind(this, socket))
  }

  if (this.listeners('discover').length === 0) {
    discoveryRequest.discover() // defaults to using socket.id for identification
  } else {
    this.emit('discover', discoveryRequest)
  }
}

SimpleSignalServer.prototype._onOffer = function (socket, { sessionId, signal, target, metadata }) {
  const request = { initiator: socket.clientId, target, metadata, socket }
  request.forward = (target=request.target, metadata=request.metadata) => {
    if (!this._sockets[target]) return
    this._sockets[target].emit('simple-signal[offer]', {
      initiator: socket.clientId, sessionId, signal, metadata
    })
  }

  if (this.listeners('request').length === 0) {
    request.forward()
  } else {
    this.emit('request', request)
  }
}

SimpleSignalServer.prototype._onSignal = function (socket, { target, sessionId, signal, metadata }) {
  if (!this._sockets[target]) return

  // misc. signaling data is always forwarded
  this._sockets[target].emit('simple-signal[signal]', {
    sessionId, signal, metadata
  })
}

SimpleSignalServer.prototype._onDisconnect = function (socket) {
  this.emit('disconnect', socket)
}
{'fraction_non_alphanumeric': 0.1449908088235294, 'fraction_numerical': 0.0009191176470588235, 'mean_word_length': 4.66796875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '6050923', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1292, 'n_words': 252}