<commit_before>
var util = require("util");
var fs = require("fs");
var urlHelpers = require("url");
var pathHelpers = require("path");

var async = require("async");
var io = require("socket.io");

var ClientInterface = require("../clientInterface");
var Client = require("../client");
var Message = require("../message");
var Resource = require("../../resources/resource");

//-----------------------------------------------------------
// Class Definition
//-----------------------------------------------------------

function SocketIO() {
  SocketIO.super_.call(this);
  this.sockets = {};
  this.socketFactory = null;
  this.serverListeners = [];
};
util.inherits(SocketIO, ClientInterface);
var p = SocketIO.prototype;

//-----------------------------------------------------------
// Initialization
//-----------------------------------------------------------

/*
 * The clientInterface component is the last to be configured, so it has access to 
 * fully configured instances of all the other components and their data.
 */
p._configure = function(config, callback) {
  var self = this;
  this.config = config; 
  this.readyCallback = callback;
 
  // intercept and respond to all server requests - replace all listeners with
  // our own function, but keep track of them so they can be called by our listener.
  this.serverListeners = this.factory.server.listeners("request");
  this.factory.server.removeAllListeners("request");
  this.factory.server.on("request", function (req, res) {
    self._handleRequest(req, res);
  });
  
  // set up Socket.IO on the server object
  this.socketFactory = io.listen(this.factory.server); 
  this.socketFactory.on("connection", function(socket) { 
    self._initializeConnection(socket);
  });   
  
  // set up resources for the main thywill page and its Javascript.
  // 
  // waterfall passes the 2nd and later parameters passed to asyncCallback
  // as arguments prior to the asyncCallback to the next function in line
  var fns = [
    function(asyncCallback) {
      self.factory.resources.getBootstrapResources(asyncCallback);
    },
    function(resources, asyncCallback) {
      
      // Load and template the main thywill HTML page, with calls to CSS and Javascript resources
      
      var filepath = pathHelpers.resolve(__dirname, "../../../client/thywill.html");
      var data = fs.readFileSync(filepath, self.config.encoding);

      var jsElementTemplate = '\n<script type="text/javascript" src="<%= path %>"></script>';
      var cssElementTemplate = '\n@import url("<%= path %>");';
        
      var js = "";
      var css= "";
      for( var i = 0, length = resources.length; i < length; i++ ) {
        if( resources[i].type == Resource.TYPE_JAVASCRIPT ) {
          elements += self.factory.template.render(jsElementTemplate, { path: resources[i].path });
        } else if( resources[i].type == Resource.TYPE_HTML ) {
          elements += self.factory.template.render(cssElementTemplate, { path: resources[i].path });
        }
      }
      data = self.factory.template.render(data, { js: js, css: css, encoding: self.config.encoding });
      self.factory.resources.defineBootstrapResource(new Resource(Resource.TYPE_HTML, self.config.basePath + "/index.html", data), asyncCallback);
    },
    function(asyncCallback) {
      var filepath = pathHelpers.resolve(__dirname, "../../../client/thywill.js");
      var data = fs.readFileSync(filepath, self.config.encoding);
      self.factory.resources.defineBootstrapResource(new Resource(Resource.TYPE_JAVASCRIPT, self.config.basePath + "/thywill.js", data), asyncCallback);
    },
  ];
  async.waterfall(fns, function(error) {
    self._announceReady(error);
  });
  
};

//-----------------------------------------------------------
// Methods
//-----------------------------------------------------------

/*
 * A new connection is made over Socket.IO, so sort out what needs to be done with it,
 * and set up its ability to send and receive.
 */
p._initializeConnection = function(socket) {
  var self = this;
  
  // set up client data for the new connection.
  socket.client = new Client();
  this.sockets[socket.client.id] = socket;
  
  // set up the necessary responses to socket events
  socket.on("message", function(encodedMessage) {<commit_msg>ordering issue in setup<commit_after>
var util = require("util");
var fs = require("fs");
var urlHelpers = require("url");
var pathHelpers = require("path");

var async = require("async");
var io = require("socket.io");

var ClientInterface = require("../clientInterface");
var Client = require("../client");
var Message = require("../message");
var Resource = require("../../resources/resource");

//-----------------------------------------------------------
// Class Definition
//-----------------------------------------------------------

function SocketIO() {
  SocketIO.super_.call(this);
  this.sockets = {};
  this.socketFactory = null;
  this.serverListeners = [];
};
util.inherits(SocketIO, ClientInterface);
var p = SocketIO.prototype;

//-----------------------------------------------------------
// Initialization
//-----------------------------------------------------------

/*
 * The clientInterface component is the last to be configured, so it has access to 
 * fully configured instances of all the other components and their data.
 */
p._configure = function(config, callback) {
  var self = this;
  this.config = config; 
  this.readyCallback = callback;
 
  // intercept and respond to all server requests - replace all listeners with
  // our own function, but keep track of them so they can be called by our listener.
  this.serverListeners = this.factory.server.listeners("request");
  this.factory.server.removeAllListeners("request");
  this.factory.server.on("request", function (req, res) {
    self._handleRequest(req, res);
  });
  
  // set up Socket.IO on the server object
  this.socketFactory = io.listen(this.factory.server); 
  this.socketFactory.on("connection", function(socket) { 
    self._initializeConnection(socket);
  });   
  
  // set up resources for the main thywill page and its Javascript.
  // 
  // waterfall passes the 2nd and later parameters passed to asyncCallback
  // as arguments prior to the asyncCallback to the next function in line
  var fns = [
    function(asyncCallback) {
      var filepath = pathHelpers.resolve(__dirname, "../../../client/thywill.js");
      var data = fs.readFileSync(filepath, self.config.encoding);
      self.factory.resources.defineBootstrapResource(new Resource(Resource.TYPE_JAVASCRIPT, self.config.basePath + "/thywill.js", data), asyncCallback);
    },
    function(asyncCallback) {
      self.factory.resources.getBootstrapResources(asyncCallback);
    },
    function(resources, asyncCallback) {
      
      // Load and template the main thywill HTML page, with calls to CSS and Javascript resources
      
      var filepath = pathHelpers.resolve(__dirname, "../../../client/thywill.html");
      var data = fs.readFileSync(filepath, self.config.encoding);

      var jsElementTemplate = '\n<script type="text/javascript" src="<%= path %>"></script>';
      var cssElementTemplate = '\n@import url("<%= path %>");';
        
      var js = "";
      var css= "";
      for( var i = 0, length = resources.length; i < length; i++ ) {
        if( resources[i].type == Resource.TYPE_JAVASCRIPT ) {
          elements += self.factory.template.render(jsElementTemplate, { path: resources[i].path });
        } else if( resources[i].type == Resource.TYPE_HTML ) {
          elements += self.factory.template.render(cssElementTemplate, { path: resources[i].path });
        }
      }
      data = self.factory.template.render(data, { js: js, css: css, encoding: self.config.encoding });
      self.factory.resources.defineBootstrapResource(new Resource(Resource.TYPE_HTML, self.config.basePath + "/index.html", data), asyncCallback);
    },
  ];
  async.waterfall(fns, function(error) {
    self._announceReady(error);
  });
  
};

//-----------------------------------------------------------
// Methods
//-----------------------------------------------------------

/*
 * A new connection is made over Socket.IO, so sort out what needs to be done with it,
 * and set up its ability to send and receive.
 */
p._initializeConnection = function(socket) {
  var self = this;
  
  // set up client data for the new connection.
  socket.client = new Client();
  this.sockets[socket.client.id] = socket;
  
  // set up the necessary responses to socket events
  socket.on("message", function(encodedMessage) {
{'fraction_non_alphanumeric': 0.2070812951316096, 'fraction_numerical': 0.0004658746797111577, 'mean_word_length': 2.4293130990415337, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '670093', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2231, 'n_words': 639}