<commit_before>var _ = require('underscore');
var async = require('async');

/**
 * SessionManager Middleware
 *
 * Manages the clients session and calls callbacks to manage
 * the stored subscriptions for clean and unclean clients.
 *
 * Enabled Callbacks:
 * - storeSubscription
 * - lookupSubscriptions
 * - clearSubscriptions
 *
 * Required Callbacks:
 *  - subscribeTopic
 */
var SessionManager = function(){};

/**
 * Stores subscriptions if the client is unclean.
 *
 * @param ctx
 * @param callback
 */
SessionManager.prototype.subscribeTopic = function(ctx, store, callback) {
  if(ctx.client._managed_session) {
    this.stack.execute('storeSubscription', ctx, callback);
  }
};

/**
 * Checks the clean flag on connect and calls appropriate functions and
 * sets clientId as client._client_id if unclean.
 *
 * @param client
 * @param packet
 * @param next
 */
SessionManager.prototype.handle = function(client, packet, next) {
  if(packet.cmd == 'connect') {
    client._client_id = packet.clientId;
    if(packet.clean) {
      client._managed_session = false;
      this._handleCleanClient(client, packet, next);
    } else {
      client._managed_session = true;
      this._handleUncleanClient(client, packet, next);
    }
  } else {
    return next();
  }
};

/**
 * For clean clients executes 'clearSubscriptions' and sends 'connack' with
 * sessionPresent' set to false as there are no subscriptions..<commit_msg>fixed bug<commit_after>var _ = require('underscore');
var async = require('async');

/**
 * SessionManager Middleware
 *
 * Manages the clients session and calls callbacks to manage
 * the stored subscriptions for clean and unclean clients.
 *
 * Enabled Callbacks:
 * - storeSubscription
 * - lookupSubscriptions
 * - clearSubscriptions
 *
 * Required Callbacks:
 *  - subscribeTopic
 */
var SessionManager = function(){};

/**
 * Stores subscriptions if the client is unclean.
 *
 * @param ctx
 * @param callback
 */
SessionManager.prototype.subscribeTopic = function(ctx, store, callback) {
  if(ctx.client._managed_session) {
    this.stack.execute('storeSubscription', ctx, callback);
  } else {
    callback();
  }
};

/**
 * Checks the clean flag on connect and calls appropriate functions and
 * sets clientId as client._client_id if unclean.
 *
 * @param client
 * @param packet
 * @param next
 */
SessionManager.prototype.handle = function(client, packet, next) {
  if(packet.cmd == 'connect') {
    client._client_id = packet.clientId;
    if(packet.clean) {
      client._managed_session = false;
      this._handleCleanClient(client, packet, next);
    } else {
      client._managed_session = true;
      this._handleUncleanClient(client, packet, next);
    }
  } else {
    return next();
  }
};

/**
 * For clean clients executes 'clearSubscriptions' and sends 'connack' with
 * sessionPresent' set to false as there are no subscriptions..
{'fraction_non_alphanumeric': 0.1146518877727745, 'fraction_numerical': 0.0, 'mean_word_length': 3.734426229508197, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7301156', 'n_tokens_mistral': 821, 'n_tokens_neox': 816, 'n_words': 238}