<commit_before>var PeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
var SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;

function rtcpeer(){
	this.messageListners = [];
	this.channelList = {};
	this.candidates = [];
}
rtcpeer.prototype = {
	constructor: rtcpeer,
	dataChannelOptions : {
	//	ordered: false, // do not guarantee order
	//	maxRetransmitTime: 3000, // in milliseconds
		reliable: false
	},
	configuration : {
	  'iceServers': [
		//{'url':'turn:numb.viagenie.ca','credential':'1q2w3e4r','username':'<EMAIL>'},
		//{ 'url': 'stun:stunserver.org'},
		//{ 'url': 'stun:stun.ekiga.net'},
		//{ 'url': 'stun:stun.schlund.de'},
		//{ 'url': 'stun:stun3.l.google.com:19302'},
		{ 'url': 'stun:stun.l.google.com:19302'}
		]
	},
	options : {
	    optional: [
		{DtlsSrtpKeyAgreement: true},
		{RtpDataChannels: true}
		]
	},
	mediaConstraints : {'mandatory':{
		'OfferToReceiveAudio':false,
		'OfferToReceiveVideo':false
		}
	},
	messageListners : undefined,
	channelList : undefined,
	pc : undefined,
	errorHandler : function(err){
		console.log("Error:"); console.log(err);
		console.log(new Error('dummy').stack);
	},
	init : function(callback){ //the callback is called for every icecandidate
		//this.pc = new PeerConnection(this.configuration,this.options);
		this.pc = new PeerConnection(null);
		var that = this;
		this.pc.ondatachannel = function(event){
			that.handleChannel(event.channel);
		};
		this.pc.onicecandidate = function(event) {
			if (event.candidate) {
				console.log("candidate: "+JSON.stringify(event.candidate));
				that.candidates.push(JSON.stringify(event.candidate));
				//if (JSON.parse(that.localDesc).type == 'offer')
					callback(that.localDesc,event.candidate);
				//else
				//	callback(that.remoteDesc,event.candidate);
			}
		};
	},
	candidates: undefined,
	offerCon :function(callback){
		if (!this.mainChannel)
			this.createChannel('COPY_PASTE_CHANNEL');
		var that = this;
		this.pc.createOffer(function(desc) {
			that.localDesc = JSON.stringify(desc);
			callback(JSON.stringify(desc));
			that.pc.setLocalDescription(desc,function(e){},that.errorHandler);
		}, this.errorHandler, this.mediaConstraints);
	},
	handleMessage: function(msg){
		for (var i=0;i<this.messageListners.length;i++){
			var l = this.messageListners[i];
			l(msg);
		}
	},
	addMessageListner: function(f){
		this.messageListners.push(f);
	},
	createChannel: function(name){
		this.handleChannel(undefined,name);
		//return this.handleChannel(null,name);
	},
	handleChannel: function(channel, channelName){
		if (!channelName) channelName = "CHANNEL_NAME";
		if (!channel){
			channel = this.pc.createDataChannel(channelName, this.dataChannelOptions);
			this.channelList[channelName] = channel;
		} else{
			if (this.channelList.length == 0)
				this.mainChannel = channelName;
			this.channelList[channelName] = channel;
		}
		var that = this;
		channel.onopen = function(event) {
			console.log("dataChannel opened");
			console.log(channel);
		};
		channel.onerror = this.errorHandler;
		channel.onmessage = function(event){
			//console.log(event);
			that.handleMessage(event.data);
		}
		channel.onclose = function(event){
			console.log("dataChannel is closed.");
			console.log(event);
			// TODO remove channel
		}
		return channel;
	},
	sendMessage: function(msg,channelName){
		if (channelName == 'broadcast') channelName = undefined;
		if(channelName){
			var c = this.channelList[channelName];
			var readyState = c.readyState;
			if (readyState == "open") {
				c.send(msg);
			}else{
				console.warn("dataChannel could not send message, state:"+readyState);
			}
		} else {
			var key;
			for (key in this.channelList){
				var c = this.channelList[key];
				var readyState = c.readyState;
				try{
					c.send(msg);
				} catch(err) {
					console.warn('dataChannel could not send message, state:'+readyState);
					console.err(err);
				}
			}
		}
	},
	localDesc: undefined,
	answer: function(ta, callback){
		try{
			if(!ta) {
				ta = document.getElementById('textarea').value;
				document.getElementById('textarea').value='';
			}
			this.remoteDesc = ta;
			var desc = new SessionDescription(JSON.parse(ta));
			//console.log(desc);
			var that = this;
			this.pc.setRemoteDescription(desc,function (){
					that.pc.createAnswer(function(desc) {
						that.localDesc = JSON.stringify(desc);
						callback(that.localDesc);
						that.pc.setLocalDescription(desc);
					},that.errorHandler,that.mediaConstraints);
				},this.errorHandler);
		} catch (e) {console.log(e);}
	},
	remoteDesc: undefined,
	offer: function(ta){
		if(!ta) {
			ta = document.getElementById('textarea').value;
			document.getElementById('textarea').value='';
		}
		this.remoteDesc = ta;
		var desc = new SessionDescription(JSON.parse(ta));
		this.pc.setRemoteDescription(desc,function(){console.log("done");},this.errorHandler);
	},
	cand : function(ta){
		this.pc.addIceCandidate(new IceCandidate(JSON.parse(ta)));
	}
}
function rtcnode(){}<commit_msg>small fix for remote channel<commit_after>var PeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
var SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;

function rtcpeer(){
	this.messageListners = [];
	this.channelList = {};
	this.candidates = [];
}
rtcpeer.prototype = {
	constructor: rtcpeer,
	dataChannelOptions : {
	//	ordered: false, // do not guarantee order
	//	maxRetransmitTime: 3000, // in milliseconds
		reliable: false
	},
	configuration : {
	  'iceServers': [
		//{'url':'turn:numb.viagenie.ca','credential':'1q2w3e4r','username':'<EMAIL>'},
		//{ 'url': 'stun:stunserver.org'},
		//{ 'url': 'stun:stun.ekiga.net'},
		{ 'url': 'stun:stun.schlund.de'},
		{ 'url': 'stun:stun3.l.google.com:19302'},
		{ 'url': 'stun:stun.l.google.com:19302'}
		]
	},
	options : {
	    optional: [
		{DtlsSrtpKeyAgreement: true},
		{RtpDataChannels: true}
		]
	},
	mediaConstraints : {'mandatory':{
		'OfferToReceiveAudio':false,
		'OfferToReceiveVideo':false
		}
	},
	messageListners : undefined,
	channelList : undefined,
	pc : undefined,
	errorHandler : function(err){
		console.log("Error:"); console.log(err);
		console.log(new Error('dummy').stack);
	},
	init : function(callback){ //the callback is called for every icecandidate
		//this.pc = new PeerConnection(this.configuration,this.options);
		this.pc = new PeerConnection(this.configuration);
		var that = this;
		this.pc.ondatachannel = function(event){
			that.handleChannel(event.channel);
		};
		this.pc.onicecandidate = function(event) {
			if (event.candidate) {
				console.log("candidate: "+JSON.stringify(event.candidate));
				that.candidates.push(JSON.stringify(event.candidate));
				//if (JSON.parse(that.localDesc).type == 'offer')
					callback(that.localDesc,event.candidate);
				//else
				//	callback(that.remoteDesc,event.candidate);
			}
		};
	},
	candidates: undefined,
	offerCon :function(callback){
		if (!this.mainChannel)
			this.createChannel('COPY_PASTE_CHANNEL');
		var that = this;
		this.pc.createOffer(function(desc) {
			that.localDesc = JSON.stringify(desc);
			callback(JSON.stringify(desc));
			that.pc.setLocalDescription(desc,function(e){},that.errorHandler);
		}, this.errorHandler, null);//this.mediaConstraints);
	},
	handleMessage: function(msg){
		for (var i=0;i<this.messageListners.length;i++){
			var l = this.messageListners[i];
			l(msg);
		}
	},
	addMessageListner: function(f){
		this.messageListners.push(f);
	},
	createChannel: function(name){
		this.handleChannel(undefined,name);
		//return this.handleChannel(null,name);
	},
	handleChannel: function(channel, channelName){
		if (!channelName) channelName = "CHANNEL_NAME";
		if (!channel){
			channel = this.pc.createDataChannel(channelName, this.dataChannelOptions);
			this.channelList[channelName] = channel;
		} else{
			if (this.channelList.length == 0)
				this.mainChannel = channelName;
			this.channelList[channelName] = channel;
		}
		var that = this;
		channel.onopen = function(event) {
			console.log("dataChannel opened");
			console.log(channel);
		};
		channel.onerror = this.errorHandler;
		channel.onmessage = function(event){
			//console.log(event);
			that.handleMessage(event.data);
		}
		channel.onclose = function(event){
			console.log("dataChannel is closed.");
			console.log(event);
			// TODO remove channel
		}
		return channel;
	},
	sendMessage: function(msg,channelName){
		if (channelName == 'broadcast') channelName = undefined;
		if(channelName){
			var c = this.channelList[channelName];
			var readyState = c.readyState;
			if (readyState == "open") {
				c.send(msg);
			}else{
				console.warn("dataChannel could not send message, state:"+readyState);
			}
		} else {
			var key;
			for (key in this.channelList){
				var c = this.channelList[key];
				var readyState = c.readyState;
				try{
					c.send(msg);
				} catch(err) {
					console.warn('dataChannel could not send message, state:'+readyState);
					console.err(err);
				}
			}
		}
	},
	localDesc: undefined,
	answer: function(ta, callback){
		try{
			if(!ta) {
				ta = document.getElementById('textarea').value;
				document.getElementById('textarea').value='';
			}
			this.remoteDesc = ta;
			var desc = new SessionDescription(JSON.parse(ta));
			//console.log(desc);
			var that = this;
			this.pc.setRemoteDescription(desc,function (){
					that.pc.createAnswer(function(desc) {
						that.localDesc = JSON.stringify(desc);
						callback(that.localDesc);
						that.pc.setLocalDescription(desc);
					},that.errorHandler,null);//that.mediaConstraints);
				},this.errorHandler);
		} catch (e) {console.log(e);}
	},
	remoteDesc: undefined,
	offer: function(ta){
		if(!ta) {
			ta = document.getElementById('textarea').value;
			document.getElementById('textarea').value='';
		}
		this.remoteDesc = ta;
		var desc = new SessionDescription(JSON.parse(ta));
		this.pc.setRemoteDescription(desc,function(){console.log("done");},this.errorHandler);
	},
	cand : function(ta){
		this.pc.addIceCandidate(new IceCandidate(JSON.parse(ta)),function(a){console.log('success:'+a);},function(a){console.log('fail:'+a);});
	}
}
function rtcnode(){}
{'fraction_non_alphanumeric': 0.15878570077548704, 'fraction_numerical': 0.00397200680915453, 'mean_word_length': 4.389908256880734, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5181576', 'n_tokens_mistral': 4025, 'n_tokens_neox': 3497, 'n_words': 577}