<commit_before>/*
  GHCJSi communication

  reads messages from stdin, sends over stderr
*/

var h$GHCJSiRecord = // true ||
      !!process.env['GHCJS_RECORD_GHCJSI'];

var h$GHCJSiPort = process.env['GHCJSI_PORT'] || 6400;

var h$GHCJSiReplay = process.argv.length > 0 &&
                     process.argv[process.argv.length-1] === 'replay';

var h$GHCJSi = { data: null
               , loadedSymbols: {}
               , current: null
               , sendMessage: h$sendMessage
               , done: h$GHCJSiDone
		 , clientHtml: ''
		 , clientJS: ''
	 , socket: null
               };

global.h$GHCJSi = h$GHCJSi;
global.require  = require;
global.module   = module;

var fs     = require('fs');
var server = require('http').createServer(h$handleHTTP);
var url    = require('url');
var io = null;
try {
  io = require('socket.io')(server);
} catch (e) { }

// start listening
function h$initGHCJSi() {
  process.stdin.setEncoding('utf8');
  process.stderr.setEncoding('binary');
  process.on('uncaughtException', function(err) {
    console.log(err);
    console.log(err.stack);
  });
  if(h$GHCJSiReplay) {
    h$replayMessage(1);
  } else {
    h$startHTTPServer();
    process.stdin.on('readable', function() {
      while(true) {
	var str = process.stdin.read();
	if(str) {
        var buf = new Buffer(str, 'hex');
          h$GHCJSi.data = h$GHCJSi.data ? Buffer.concat([h$GHCJSi.data, buf]) : buf;
          h$processInput();
	} else {
          return;
	}
      }
    });
    process.stdin.on('close', function() { process.exit(0); });
  }
}

function h$replayMessage(n) {
  try {
    var buffer  = fs.readFileSync("ghcjsimessage." + n + ".dat");
    var msgType = buffer.readUInt32BE(0);
    h$processMessage(msgType, buffer.slice(4));
    setTimeout(function() { h$replayMessage(n+1); }, 1500);
  } catch(e) { }
}

function h$startHTTPServer() {
  if(!io) {
    console.log("\nsocket.io not found, browser session not available");
    return;
  }
  io.on('connection', function(socket) {
    if(h$GHCJSi.socket !== null) {
      socket.close();
      return;
    }
    h$GHCJSi.socket = socket;
    console.log("\nbrowser connected, code runs in browser from now on");
    socket.on('msg', function(msg) {
      h$GHCJSi.sendMessage(msg.type, msg.payload);
    });
    socket.on('out', function(data) {
      process.stdout.write(data);
    });
    socket.on('disconnect', function() {
      console.log('browser disconnected');
      if(h$GHCJSi.socket === socket) h$GHCJSi.socket = null;
    });
  });
  h$GHCJSi.clientHtml = fs.readFileSync(__dirname + '/ghcjsiClient.html');
  h$GHCJSi.clientJs   = fs.readFileSync(__dirname + '/ghcjsiClient.js');<commit_msg>fix GHCJSi browser sessions when socket.io connects multiple times<commit_after>/*
  GHCJSi communication

  reads messages from stdin, sends over stderr
*/

var h$GHCJSiRecord = // true ||
      !!process.env['GHCJS_RECORD_GHCJSI'];

var h$GHCJSiPort = process.env['GHCJSI_PORT'] || 6400;

var h$GHCJSiReplay = process.argv.length > 0 &&
                     process.argv[process.argv.length-1] === 'replay';

var h$GHCJSi = { data: null
               , loadedSymbols: {}
               , current: null
               , sendMessage: h$sendMessage
               , done: h$GHCJSiDone
		 , clientHtml: ''
		 , clientJS: ''
	 , socket: null
               };

global.h$GHCJSi = h$GHCJSi;
global.require  = require;
global.module   = module;

var fs     = require('fs');
var server = require('http').createServer(h$handleHTTP);
var url    = require('url');
var io = null;
try {
  io = require('socket.io')(server);
} catch (e) { }

// start listening
function h$initGHCJSi() {
  process.stdin.setEncoding('utf8');
  process.stderr.setEncoding('binary');
  process.on('uncaughtException', function(err) {
    console.log(err);
    console.log(err.stack);
  });
  if(h$GHCJSiReplay) {
    h$replayMessage(1);
  } else {
    h$startHTTPServer();
    process.stdin.on('readable', function() {
      while(true) {
	var str = process.stdin.read();
	if(str) {
        var buf = new Buffer(str, 'hex');
          h$GHCJSi.data = h$GHCJSi.data ? Buffer.concat([h$GHCJSi.data, buf]) : buf;
          h$processInput();
	} else {
          return;
	}
      }
    });
    process.stdin.on('close', function() { process.exit(0); });
  }
}

function h$replayMessage(n) {
  try {
    var buffer  = fs.readFileSync("ghcjsimessage." + n + ".dat");
    var msgType = buffer.readUInt32BE(0);
    h$processMessage(msgType, buffer.slice(4));
    setTimeout(function() { h$replayMessage(n+1); }, 1500);
  } catch(e) { }
}

function h$startHTTPServer() {
  if(!io) {
    console.log("\nsocket.io not found, browser session not available");
    return;
  }
  io.on('connection', function(socket) {
    console.log("\nbrowser connected, code runs in browser from now on");
    h$GHCJSi.socket = io;
    socket.on('msg', function(msg) {
      h$GHCJSi.sendMessage(msg.type, msg.payload);
    });
    socket.on('out', function(data) {
      process.stdout.write(data);
    });
    socket.on('disconnect', function() {
      // console.log('browser disconnected');
    });
  });
  h$GHCJSi.clientHtml = fs.readFileSync(__dirname + '/ghcjsiClient.html');
  h$GHCJSi.clientJs   = fs.readFileSync(__dirname + '/ghcjsiClient.js');
{'fraction_non_alphanumeric': 0.1664139499620925, 'fraction_numerical': 0.006823351023502654, 'mean_word_length': 2.979638009049774, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5151826', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1849, 'n_words': 329}