<commit_before>'use strict'

var AbstractTransport = require('./abstract')
var freeice = require('freeice')
var merge = require('merge')
var SimplePeer = require('simple-peer')
var util = require('util')
var winston = require('winston')
var winstonWrapper = require('winston-meta-wrapper')
var wrtc = require('wrtc')

function WebRtcTransport (args) {
  if (!(this instanceof WebRtcTransport)) {
    return new WebRtcTransport(args)
  }
  // logging
  this._log = winstonWrapper(winston)
  this._log.addMeta({
    module: '1tp:transports:webrtc'
  })
  // verify signaling args
  if (args.signaling === undefined) {
    var signalingArgsError = 'incorrect args: signaling is undefined'
    this._log.error(signalingArgsError)
    throw new Error(signalingArgsError)
  }
  // merge args
  this._margs = merge(Object.create(WebRtcTransport.DEFAULTS), args)
  delete this._margs._signaling
  // init
  AbstractTransport.call(this)
  this._signaling = args.signaling
  this._listening = false
  // accept new incoming connections
  this._acceptIncomingConnections = true
  // done
  this._log.debug('created webrtc transport, config = ' + JSON.stringify(this._margs))
}

// Inherit from abstract transport
util.inherits(WebRtcTransport, AbstractTransport)

WebRtcTransport.DEFAULTS = {
  iceServers: freeice(),
  trickle: false,
  wrtc: wrtc
}

WebRtcTransport.prototype.transportType = function () {
  return 'webrtc'
}

WebRtcTransport.prototype.connectTimeout = function () {
  return 2000
}

WebRtcTransport.prototype.listen = function (listeningInfo, onSuccess, onFailure) {
  var requestedRegistrationInfo
  if (listeningInfo !== undefined) {
    // verify listeningInfo
    if (listeningInfo.transportType !== this.transportType()) {
      var transportTypeError = 'incorrect listeningInfo: unexpected transportType -- ignoring request'
      this._log.error(transportTypeError)
      this._error(transportTypeError, onFailure)
      return
    }
    if (listeningInfo.transportInfo === undefined) {
      var transportInfoUndefined = 'incorrect connectionInfo: transportInfo is undefined'
      this._log.error(transportInfoUndefined)
      this._error(transportInfoUndefined, onFailure)
      return
    }
    requestedRegistrationInfo = listeningInfo.transportInfo
  }
  // register callback with signaling connector
  var self = this
  this._signaling.registerP(this._onSignalingMessage.bind(this), requestedRegistrationInfo)
    .then(function (actualRegistrationInfo) {
      var myConnectionInfo = {<commit_msg>disable wrtc module when browserifying 1tp<commit_after>'use strict'

var AbstractTransport = require('./abstract')
var freeice = require('freeice')
var merge = require('merge')
var SimplePeer = require('simple-peer')
var util = require('util')
var winston = require('winston')
var winstonWrapper = require('winston-meta-wrapper')

function WebRtcTransport (args) {
  if (!(this instanceof WebRtcTransport)) {
    return new WebRtcTransport(args)
  }
  // logging
  this._log = winstonWrapper(winston)
  this._log.addMeta({
    module: '1tp:transports:webrtc'
  })
  // verify signaling args
  if (args.signaling === undefined) {
    var signalingArgsError = 'incorrect args: signaling is undefined'
    this._log.error(signalingArgsError)
    throw new Error(signalingArgsError)
  }
  // merge args
  this._margs = merge(Object.create(WebRtcTransport.DEFAULTS), args)
  try {
    var wrtc = require('wrtc')
    this._log.debug('using wrtc module')
    this._margs.wrtc = wrtc
  } catch (error) {
    this._log.error('cannot load wrtc module')
  }
  delete this._margs._signaling
  // init
  AbstractTransport.call(this)
  this._signaling = args.signaling
  this._listening = false
  // accept new incoming connections
  this._acceptIncomingConnections = true
  // done
  this._log.debug('created webrtc transport, config = ' + JSON.stringify(this._margs))
}

// Inherit from abstract transport
util.inherits(WebRtcTransport, AbstractTransport)

WebRtcTransport.DEFAULTS = {
  iceServers: freeice(),
  trickle: false,
}

WebRtcTransport.prototype.transportType = function () {
  return 'webrtc'
}

WebRtcTransport.prototype.connectTimeout = function () {
  return 15000
}

WebRtcTransport.prototype.listen = function (listeningInfo, onSuccess, onFailure) {
  var requestedRegistrationInfo
  if (listeningInfo !== undefined) {
    // verify listeningInfo
    if (listeningInfo.transportType !== this.transportType()) {
      var transportTypeError = 'incorrect listeningInfo: unexpected transportType -- ignoring request'
      this._log.error(transportTypeError)
      this._error(transportTypeError, onFailure)
      return
    }
    if (listeningInfo.transportInfo === undefined) {
      var transportInfoUndefined = 'incorrect connectionInfo: transportInfo is undefined'
      this._log.error(transportInfoUndefined)
      this._error(transportInfoUndefined, onFailure)
      return
    }
    requestedRegistrationInfo = listeningInfo.transportInfo
  }
  // register callback with signaling connector
  var self = this
  this._signaling.registerP(this._onSignalingMessage.bind(this), requestedRegistrationInfo)
    .then(function (actualRegistrationInfo) {
      var myConnectionInfo = {
{'fraction_non_alphanumeric': 0.10285933601995778, 'fraction_numerical': 0.002302820955670697, 'mean_word_length': 4.345641025641026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3273936', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1501, 'n_words': 339}