<commit_before>/* Copyright (c) 2011 <NAME>. See LICENSE for more information */

var net = require('net')
  , util = require('util');

var defaultHost = '127.0.0.1'
  , defaultPort = 1883;

var Connection = require('./connection');

var Server = module.exports.Server = function Server(listener) {
  if (!(this instanceof Server)) return new Server(listener);

  var self = this;

  net.Server.call(self);

  if (listener) {
    self.on('client', listener);
  }

  self.on('connection', function(socket) {
    self.emit('client', new Connection(socket, self));
  });

  return this;
}

util.inherits(Server, net.Server);

module.exports.createServer = function(listener) {
  return new Server(listener);
};

/* TODO: put some smarts in Client, rather than just having it wrap
 * Connection */
var Client = module.exports.Client = function Client(stream) {
  if (!(this instanceof Client)) return new Client(stream);

  return new Connection(stream, null);

}

module.exports.createClient = function(port, host, callback) {
  var host = host || defaultHost
    , port = port || defaultPort
    , callback = callback || function() {}
    , net_client, mqtt_client;

  if (typeof arguments[0] === 'function' && arguments.length === 1) {
    port = defaultPort;
    host = defaultHost;
    callback = arguments[0];
  }

  net_client = new net.Socket();

  mqtt_client = new Client(net_client, null);

  net_client.on('connect', function() {
    mqtt_client.emit('connected'); 
  });

  mqtt_client.on('connected', function() {
    callback(null, mqtt_client);
  });

  mqtt_client.on('error', function(err) {
    callback(err);
  });

  net_client.connect(port, host);

  return mqtt_client;<commit_msg>Fix for the client on(connect), first argument should be a client, not null according to the docs.<commit_after>/* Copyright (c) 2011 <NAME>. See LICENSE for more information */

var net = require('net')
  , util = require('util');

var defaultHost = '127.0.0.1'
  , defaultPort = 1883;

var Connection = require('./connection');

var Server = module.exports.Server = function Server(listener) {
  if (!(this instanceof Server)) return new Server(listener);

  var self = this;

  net.Server.call(self);

  if (listener) {
    self.on('client', listener);
  }

  self.on('connection', function(socket) {
    self.emit('client', new Connection(socket, self));
  });

  return this;
}

util.inherits(Server, net.Server);

module.exports.createServer = function(listener) {
  return new Server(listener);
};

/* TODO: put some smarts in Client, rather than just having it wrap
 * Connection */
var Client = module.exports.Client = function Client(stream) {
  if (!(this instanceof Client)) return new Client(stream);

  return new Connection(stream, null);

}

module.exports.createClient = function(port, host, callback) {
  var host = host || defaultHost
    , port = port || defaultPort
    , callback = callback || function() {}
    , net_client, mqtt_client;

  if (typeof arguments[0] === 'function' && arguments.length === 1) {
    port = defaultPort;
    host = defaultHost;
    callback = arguments[0];
  }

  net_client = new net.Socket();

  mqtt_client = new Client(net_client, null);

  net_client.on('connect', function() {
    mqtt_client.emit('connected'); 
  });

  mqtt_client.on('connected', function() {
    callback(mqtt_client);
  });

  mqtt_client.on('error', function(err) {
    callback(err);
  });

  net_client.connect(port, host);

  return mqtt_client;
{'fraction_non_alphanumeric': 0.1471687266455878, 'fraction_numerical': 0.009772923253808566, 'mean_word_length': 3.950213371266003, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2697764', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1048, 'n_words': 286}