<commit_before>var Logger = require('./Logger');

module.exports = (function() {

  var DeviceSockets = { users: {} },
      connected = [],
      users = [],
      uuid = (function() {
        var i = 0;
        return (function() { return ['_', ++i, '_'].join(''); });
      })(),
      log = Logger('::DeviceSockets');

  function clean() {
    var i = 0,
        c = connected.length;

    for(i; i < c; i++) {
      if(connected[i] && !connected[i].socket.writable) connected[i].cleanup();
    }
  }

  DeviceSockets.users.add = function(user, device, socket) {
    var new_id = uuid();

    users.push({
      user: user,
      device: device,
      socket: socket,
      id: new_id
    });
  };

  DeviceSockets.users.remove = function(user_id, callback) {
    log('removing user socket');
    var i = 0, l = users.length, f = -1;

    for(i; i < l; i++) {
      var us = users[i];
      if(us.user === user_id) f = i;
    }

    if(f < 0) return callback();

    users.splice(f, 1);
    callback();
    log('successfully removed socket for user: ' + user_id);
  };

  DeviceSockets.users.broadcast = function(device_id, message) {
    var i = 0, l = users.length;

    function send(user) {
      log("broadcasting message["+message+"] to user["+user.user+"]");
      user.socket.emit('update', message);
    }

    for(i; i < l; i++) {
      if(users[i].device === device_id) send(users[i]);
    }
  };

  DeviceSockets.add = function(socket, device_id) {
    var id = uuid(),
        noop = function() { };

    clean();

    function remove() {
      var i = 0,
          n = -1;

      for(i; i < connected.length; i++) {
        var s = connected[i];
        if(s.id === id) { n = i; break; }
      }

      if(n < 0) return;

      log("removing socket at ["+n+"]");
      connected.splice(n, 1);
      DeviceSockets.users.broadcast(device_id, "DEVICE_DISCONNECTED");
      DeviceStateService.update(device_id, {connected: false}, noop);<commit_msg>[LFTAPI-30] resetting device state via service when socket is noticed to be dead<commit_after>var Logger = require('./Logger');

module.exports = (function() {

  var DeviceSockets = { users: {} },
      connected = [],
      users = [],
      uuid = (function() {
        var i = 0;
        return (function() { return ['_', ++i, '_'].join(''); });
      })(),
      log = Logger('::DeviceSockets'),
      STATE_RESET = {
        connected: false,
        playback: 0,
        current_track: false
      };

  function clean() {
    var i = 0,
        c = connected.length;

    for(i; i < c; i++) {
      if(connected[i] && !connected[i].socket.writable) connected[i].cleanup();
    }
  }

  DeviceSockets.users.add = function(user, device, socket) {
    var new_id = uuid();

    users.push({
      user: user,
      device: device,
      socket: socket,
      id: new_id
    });
  };

  DeviceSockets.users.remove = function(user_id, callback) {
    log('removing user socket');
    var i = 0, l = users.length, f = -1;

    for(i; i < l; i++) {
      var us = users[i];
      if(us.user === user_id) f = i;
    }

    if(f < 0) return callback();

    users.splice(f, 1);
    callback();
    log('successfully removed socket for user: ' + user_id);
  };

  DeviceSockets.users.broadcast = function(device_id, message) {
    var i = 0, l = users.length;

    function send(user) {
      log("broadcasting message["+message+"] to user["+user.user+"]");
      user.socket.emit('update', message);
    }

    for(i; i < l; i++) {
      if(users[i].device === device_id) send(users[i]);
    }
  };

  DeviceSockets.add = function(socket, device_id) {
    var id = uuid(),
        noop = function() { };

    clean();

    function remove() {
      var i = 0,
          n = -1;

      for(i; i < connected.length; i++) {
        var s = connected[i];
        if(s.id === id) { n = i; break; }
      }

      if(n < 0) return;

      function finish() {
        DeviceSockets.users.broadcast(device_id, "DEVICE_DISCONNECTED");
      }

      log("removing socket at ["+n+"] and updating state");
      connected.splice(n, 1);
      DeviceStateService.update(device_id, STATE_RESET, finish);
{'fraction_non_alphanumeric': 0.17465670922669235, 'fraction_numerical': 0.006022645145748013, 'mean_word_length': 2.3189448441247, 'pattern_counts': {'":': 0, '<': 15, '<?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': '2202844', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1374, 'n_words': 332}