<commit_before>var os = require('os');
var uuid = require('node-uuid');
var AutoScout = require('zetta-auto-scout');
var async = require('async');
var Device = require('zetta-device');
var HttpDevice = require('zetta-http-device');
var HttpScout = require('./lib/http_scout');
var HttpServer = require('./lib/http_server');
var Logger = require('./lib/logger');
var PeerClient = require('./lib/peer_client');
var PeerRegistry = require('./lib/peer_registry');
var PubSub = require('./lib/pubsub_service');
var Runtime = require('./lib/runtime');
var Scout = require('zetta-scout');
var scientist = require('zetta-scientist');

var Zetta = module.exports = function(opts) {
  if (!(this instanceof Zetta)) {
    return new Zetta(opts);
  }

  opts = opts || {};

  this.id = uuid.v4(); // unique id of server
  this._name = os.hostname(); // optional name, defaults to OS hostname

  this._exposeQuery = '';
  this._scouts = [];
  this._apps = [];
  this._peers = [];
  
  this.peerRegistry = opts.peerRegistry || new PeerRegistry();

  this.pubsub = opts.pubsub || new PubSub();
  this.log = opts.log || new Logger({ pubsub: this.pubsub });

  var runtimeOpts = { pubsub: this.pubsub, log: this.log };
  if (opts && opts.registry) {
    runtimeOpts.registry = opts.registry;
  }
  this.runtime = new Runtime(runtimeOpts);
  this.httpServer = new HttpServer(this);

  var httpScout = scientist.create.apply(null, [HttpScout]);
  httpScout.server = this.runtime;
  this.httpScout = httpScout;
  this._scouts.push(httpScout);

};

Zetta.prototype.name = function(name) {
  this._name = name;
  return this;
};

Zetta.prototype.use = function() {
  var args = Array.prototype.slice.call(arguments);
  var constructor = args[0];

  var self = this;
  function addScout(scout) {
    scout.server = self.runtime;
    self._scouts.push(scout);
  }

  function init() {
    var instance = Object.create(constructor.prototype);
    constructor.apply(instance, args.slice(1));
    return scientist.config(instance);
  }

  function walk(proto) {
    if (!proto || !proto.__proto__) {
      self.load(constructor);
    } else if (proto.__proto__.constructor.name === 'HttpDevice') {
      var instance = init();
      self.httpScout.driverFunctions[instance._type] = constructor;
    } else if (proto.__proto__.constructor.name === 'Device') {
      var instance = init();
      args.unshift(instance._type);
      var scout = Object.create(AutoScout.prototype);
      AutoScout.apply(scout, args);
      addScout(scout);
    } else if (proto.__proto__.constructor.name === 'Scout') {
      var scout = scientist.create.apply(null, args);
      addScout(scout);
    } else {
      walk(proto.__proto__);
    }
  }

  walk(constructor.prototype);

  return this;
};

Zetta.prototype.expose = function(query) {
  this._exposeQuery = query;
  this.runtime.expose(query);
  return this;
};

Zetta.prototype.load = function(app) {
  this._apps.push(app);
  return this;
};

Zetta.prototype.link = function(peers) {
  var self = this;
  if(!Array.isArray(peers)) {
    peers = [peers];
  }

  peers.forEach(function(peer) {
    //self._peers.push(new PeerClient(peer, self));
    self._peers.push(peer);
  });

  return this;
};


Zetta.prototype.listen = function() {
  var self = this;

  var args = Array.prototype.slice.call(arguments);

  var last = args[args.length - 1];

  var callback;
  if (typeof last === 'function') {
    callback = last;
  }

  this._run(function(err){
    if(err) {
      if (callback) {
        return callback(err);
      } else {
        throw err;
      }
    }

    var cb = function(err) {
      if (err) {
        if (callback) {
          callback(err);
        } else {
          throw err;
        }
      }

      var host;
      if (typeof args[0] === 'string') {
        host = args[0]; // UNIX socket
      } else if (typeof args[0] === 'number') {
        if (args.length > 1 && typeof args[1] === 'string') {
          host = args[1] + ':' + args[0]; // host + port
        } else {
          host = '0.0.0.0:' + args[0]; // just port
        }
      } else if (typeof args[0] === 'object' && args[0].fd) {
        host = 'fd: ' + args[0].fd; // handle
      } else {
        host = '<unknown>';
      }

      self.log.emit('log', 'server', 'Server (' + self._name + ') ' + self.id + ' listening on ' + host);

      if (callback) {
        callback(err);
      }
    };

    if (!callback) {<commit_msg>Adding http:// prefix to host/port URLs<commit_after>var os = require('os');
var uuid = require('node-uuid');
var AutoScout = require('zetta-auto-scout');
var async = require('async');
var Device = require('zetta-device');
var HttpDevice = require('zetta-http-device');
var HttpScout = require('./lib/http_scout');
var HttpServer = require('./lib/http_server');
var Logger = require('./lib/logger');
var PeerClient = require('./lib/peer_client');
var PeerRegistry = require('./lib/peer_registry');
var PubSub = require('./lib/pubsub_service');
var Runtime = require('./lib/runtime');
var Scout = require('zetta-scout');
var scientist = require('zetta-scientist');

var Zetta = module.exports = function(opts) {
  if (!(this instanceof Zetta)) {
    return new Zetta(opts);
  }

  opts = opts || {};

  this.id = uuid.v4(); // unique id of server
  this._name = os.hostname(); // optional name, defaults to OS hostname

  this._exposeQuery = '';
  this._scouts = [];
  this._apps = [];
  this._peers = [];
  
  this.peerRegistry = opts.peerRegistry || new PeerRegistry();

  this.pubsub = opts.pubsub || new PubSub();
  this.log = opts.log || new Logger({ pubsub: this.pubsub });

  var runtimeOpts = { pubsub: this.pubsub, log: this.log };
  if (opts && opts.registry) {
    runtimeOpts.registry = opts.registry;
  }
  this.runtime = new Runtime(runtimeOpts);
  this.httpServer = new HttpServer(this);

  var httpScout = scientist.create.apply(null, [HttpScout]);
  httpScout.server = this.runtime;
  this.httpScout = httpScout;
  this._scouts.push(httpScout);

};

Zetta.prototype.name = function(name) {
  this._name = name;
  return this;
};

Zetta.prototype.use = function() {
  var args = Array.prototype.slice.call(arguments);
  var constructor = args[0];

  var self = this;
  function addScout(scout) {
    scout.server = self.runtime;
    self._scouts.push(scout);
  }

  function init() {
    var instance = Object.create(constructor.prototype);
    constructor.apply(instance, args.slice(1));
    return scientist.config(instance);
  }

  function walk(proto) {
    if (!proto || !proto.__proto__) {
      self.load(constructor);
    } else if (proto.__proto__.constructor.name === 'HttpDevice') {
      var instance = init();
      self.httpScout.driverFunctions[instance._type] = constructor;
    } else if (proto.__proto__.constructor.name === 'Device') {
      var instance = init();
      args.unshift(instance._type);
      var scout = Object.create(AutoScout.prototype);
      AutoScout.apply(scout, args);
      addScout(scout);
    } else if (proto.__proto__.constructor.name === 'Scout') {
      var scout = scientist.create.apply(null, args);
      addScout(scout);
    } else {
      walk(proto.__proto__);
    }
  }

  walk(constructor.prototype);

  return this;
};

Zetta.prototype.expose = function(query) {
  this._exposeQuery = query;
  this.runtime.expose(query);
  return this;
};

Zetta.prototype.load = function(app) {
  this._apps.push(app);
  return this;
};

Zetta.prototype.link = function(peers) {
  var self = this;
  if(!Array.isArray(peers)) {
    peers = [peers];
  }

  peers.forEach(function(peer) {
    //self._peers.push(new PeerClient(peer, self));
    self._peers.push(peer);
  });

  return this;
};


Zetta.prototype.listen = function() {
  var self = this;

  var args = Array.prototype.slice.call(arguments);

  var last = args[args.length - 1];

  var callback;
  if (typeof last === 'function') {
    callback = last;
  }

  this._run(function(err){
    if(err) {
      if (callback) {
        return callback(err);
      } else {
        throw err;
      }
    }

    var cb = function(err) {
      if (err) {
        if (callback) {
          callback(err);
        } else {
          throw err;
        }
      }

      var host;
      if (typeof args[0] === 'string') {
        host = args[0]; // UNIX socket
      } else if (typeof args[0] === 'number') {
        if (args.length > 1 && typeof args[1] === 'string') {
          host = 'http://' + args[1] + ':' + args[0]; // host + port
        } else {
          host = 'http://127.0.0.1:' + args[0]; // just port
        }
      } else if (typeof args[0] === 'object' && args[0].fd) {
        host = 'fd: ' + args[0].fd; // handle
      } else {
        host = '<unknown>';
      }

      self.log.emit('log', 'server', 'Server (' + self._name + ') ' + self.id + ' listening on ' + host);

      if (callback) {
        callback(err);
      }
    };

    if (!callback) {
{'fraction_non_alphanumeric': 0.1656386677133565, 'fraction_numerical': 0.00448581361444432, 'mean_word_length': 2.835698924731183, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7258383', 'n_tokens_mistral': 2936, 'n_tokens_neox': 2885, 'n_words': 636}