<commit_before>/// <reference path="../../webrtc/peerconnection.d.ts" />
/// <reference path="../../logging/logging.d.ts" />

var log :Logging.Log = new Logging.Log('copypaste-socks');

var pcConfig :WebRtc.PeerConnectionConfig = {
  webrtcPcConfig: {
    iceServers: [{urls: ['stun:stun.l.google.com:19302']},
                 {urls: ['stun:stun1.l.google.com:19302']}]
  },
  peerName: 'pc'
};

function connectDataChannel(d:WebRtc.DataChannel) {
  d.dataFromPeerQueue.setSyncHandler((data:WebRtc.Data) => {
    freedom().emit('messageFromPeer', data.str);
  });

  freedom().on('messageFromPeer', (message:string) => {
    d.send({ str: message }).catch((e) => {
      log.error('error sending chat message: ' + e.message);
    });
  });
}

function makePeerConnection() : WebRtc.PeerConnection {
  var pc :WebRtc.PeerConnection = new WebRtc.PeerConnection(pcConfig);

  pc.signalForPeerQueue.setSyncHandler((signal:WebRtc.SignallingMessage) => {
    freedom().emit('signalForPeer', signal);
  });

  pc.onceConnected.then(() => {
    log.info('connected');
  });

  pc.peerOpenedChannelQueue.setSyncHandler((d:WebRtc.DataChannel) => {
    if (d.getLabel() === 'text') {
      connectDataChannel(d);
      freedom().emit('ready');
    }
  });

  return pc;
}

var pc :WebRtc.PeerConnection;

freedom().on('start', () => {
  pc = makePeerConnection();
  pc.negotiateConnection()
    .then(() => {
      return pc.openDataChannel('text');
    })
    .then(connectDataChannel)<commit_msg>update copypaste sample to use new peerconnection interface<commit_after>/// <reference path="../../freedom/typings/rtcpeerconnection.d.ts" />
/// <reference path="../../webrtc/peerconnection.d.ts" />
/// <reference path="../../logging/logging.d.ts" />

var log :Logging.Log = new Logging.Log('copypaste-socks');

var config :freedom_RTCPeerConnection.RTCConfiguration = {
  iceServers: [{urls: ['stun:stun.l.google.com:19302']},
               {urls: ['stun:stun1.l.google.com:19302']}]
};

function connectDataChannel(d:WebRtc.DataChannel) {
  d.dataFromPeerQueue.setSyncHandler((data:WebRtc.Data) => {
    freedom().emit('messageFromPeer', data.str);
  });

  freedom().on('messageFromPeer', (message:string) => {
    d.send({ str: message }).catch((e) => {
      log.error('error sending chat message: ' + e.message);
    });
  });
}

function makePeerConnection() : WebRtc.PeerConnection {
  var provider :freedom_RTCPeerConnection.RTCPeerConnection =
      freedom['core.rtcpeerconnection'](config);
  var pc = WebRtc.PeerConnection.fromRtcPeerConnection(provider);

  pc.signalForPeerQueue.setSyncHandler((signal:WebRtc.SignallingMessage) => {
    freedom().emit('signalForPeer', signal);
  });

  pc.onceConnected.then(() => {
    log.info('connected');
  });

  pc.peerOpenedChannelQueue.setSyncHandler((d:WebRtc.DataChannel) => {
    if (d.getLabel() === 'text') {
      connectDataChannel(d);
      freedom().emit('ready');
    }
  });

  return pc;
}

var pc :WebRtc.PeerConnection;

freedom().on('start', () => {
  pc = makePeerConnection();
  pc.negotiateConnection()
    .then(() => {
      return pc.openDataChannel('text');
    })
    .then(connectDataChannel)
{'fraction_non_alphanumeric': 0.1895734597156398, 'fraction_numerical': 0.0069510268562401265, 'mean_word_length': 3.9009287925696596, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 24, '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': '6533647', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1057, 'n_words': 145}