<commit_before>'use strict'
var mosca = require('mosca')
var consoleLogger = require('../../util/logger.js')

/**
 * Constructor called by logagent, when the config file contains tis entry:
 * input
 *  mqtt-broker:
 *    module: mqtt-broker
 *    port: 1883
 *
 * @config cli arguments and config entries
 * @eventEmitter logent eventEmitter object
 */
function InputMqttBroker (config, eventEmitter) {
  this.config = config
  this.eventEmitter = eventEmitter
}
module.exports = InputMqttBroker
/**
 * Plugin start function, called after constructor
 *
 */
InputMqttBroker.prototype.start = function () {
  if (!this.started) {
    this.createServer()
    this.started = true
  }
}

/**
 * Plugin stop function, called when logagent terminates
 * we close the server socket here.
 */
InputMqttBroker.prototype.stop = function (cb) {
  this.server.close(cb)
}

InputMqttBroker.prototype.createServer = function () {
  var self = this
  if (!this.config.port) {
    this.config.port = 1883
  }
  var server = new mosca.Server({
    port: this.config.port,
    backend: this.config.backend
  })
  this.server = server
  server.on('published', function (packet, client) {
    if (packet.topic && /^\$SYS/.test(packet.topic)) {
      return
    }
    var context = {
      name: 'input.mqtt',
      port: self.config.port,
      sourceName: packet.topic,
      topic: packet.topic,
      qos: packet.qos,
      retain: packet.retain
    }
    if (packet.payload) {
      self.eventEmitter.emit('data.raw', packet.payload.toString(), context)
    }<commit_msg>add comment about Mosca  topic<commit_after>'use strict'
var mosca = require('mosca')
var consoleLogger = require('../../util/logger.js')

/**
 * Constructor called by logagent, when the config file contains tis entry:
 * input
 *  mqtt-broker:
 *    module: mqtt-broker
 *    port: 1883
 *
 * @config cli arguments and config entries
 * @eventEmitter logent eventEmitter object
 */
function InputMqttBroker (config, eventEmitter) {
  this.config = config
  this.eventEmitter = eventEmitter
}
module.exports = InputMqttBroker
/**
 * Plugin start function, called after constructor
 *
 */
InputMqttBroker.prototype.start = function () {
  if (!this.started) {
    this.createServer()
    this.started = true
  }
}

/**
 * Plugin stop function, called when logagent terminates
 * we close the server socket here.
 */
InputMqttBroker.prototype.stop = function (cb) {
  this.server.close(cb)
}

InputMqttBroker.prototype.createServer = function () {
  var self = this
  if (!this.config.port) {
    this.config.port = 1883
  }
  var server = new mosca.Server({
    port: this.config.port,
    backend: this.config.backend
  })
  this.server = server
  server.on('published', function (packet, client) {
    // ignore messages from 'internal' $SYS topic (e.g. client connect/disconnect)
    if (packet.topic && /^\$SYS/.test(packet.topic)) {
      return
    }
    var context = {
      name: 'input.mqtt',
      port: self.config.port,
      sourceName: packet.topic,
      topic: packet.topic,
      qos: packet.qos,
      retain: packet.retain
    }
    if (packet.payload) {
      self.eventEmitter.emit('data.raw', packet.payload.toString(), context)
    }
{'fraction_non_alphanumeric': 0.12764632627646327, 'fraction_numerical': 0.0049813200498132005, 'mean_word_length': 3.135135135135135, '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': '341938', 'n_tokens_mistral': 1036, 'n_tokens_neox': 1033, 'n_words': 255}