<commit_before>/**
 *      ioBroker.ctrl (ioBroker Controller)
 *
 *      Controls Adapter-Processes
 *
 *
 */

// Change version in io-package.json and start grunt task to modify the version
var version = '0.0.15';
var title = 'io.controller';
process.title = title;


var schedule =      require('node-schedule');
var os =            require('os');
var fs =            require('fs');
var cp =            require('child_process');
var ObjectsCouch =  require(__dirname + '/lib/couch.js');
var StatesRedis =   require(__dirname + '/lib/redis.js');

var logger;
var isDaemon;
var hostname =      os.hostname();


if (process.argv.indexOf('start') !== -1) {
    isDaemon =      true;
    logger =        require(__dirname + '/lib/logger.js')('info', ['iobroker.log'], true);
} else {
    logger =        require(__dirname + '/lib/logger.js')('info', ['iobroker.log']);
}

var config;
if (!fs.existsSync(__dirname + '/conf/iobroker.json')) {
    logger.error('controller conf/iobroker.json missing - call node iobroker.js setup');
    process.exit(1);
} else {
    config = JSON.parse(fs.readFileSync(__dirname + '/conf/iobroker.json'));
}


// Find first non-loopback ip
var ifaces = os.networkInterfaces();
var ipArr = [];
for (var dev in ifaces) {
    /*jshint loopfunc:true */
    ifaces[dev].forEach(function (details) {
        if (!details.internal) ipArr.push(details.address);
    });
}
var firstIp = ipArr[0];

logger.info('ioBroker.nodejs version ' + version);
logger.info('copyright 2014 hobbyquaker, bluefox');
logger.info(title + ' starting');
logger.info('controller ip: ' + ipArr.join(' '));



var procs     = {};
var subscribe = {};

var states = new StatesRedis({

    redis: {
        host:    config.redis.host,
        port:    config.redis.port,
        options: config.redis.options
    },
    logger: logger,
    change: function (id, state) {
        if (subscribe[id]) {
            for (var i = 0; i < subscribe[id].length; i++) {
                // wake up adapter
                if (procs[subscribe[id][i]]) {
                    console.log("Wake up " + id +' ' + JSON.stringify(state));
                    startInstance(subscribe[id][i], true);
                } else {
                    logger.warn("Adapter subscribed on " + id + " does not exist!");
                }
            }
        }
    }
});
//states.subscribe('*');

var objects = new ObjectsCouch({
    host: config.couch.host,
    port: config.couch.port,
    user: config.couch.user,
    pass: <PASSWORD>,
    logger: logger,
    connected: function () {
        logger.info('controller couchdb connected');
        setMeta();
        getInstances();
        startAliveInterval();
    },
    change: function (id, obj) {
        if (!id.match(/^system\.adapter\.[a-zA-Z0-9-_]+\.[0-9]+$/)) return;
        logger.info('controller object change ' + id);
        if (procs[id]) {
            // known adapter
            procs[id].config = obj;
            if (procs[id].process) {
                stopInstance(id, function () {
                    if (ipArr.indexOf(obj.common.host) !== -1) {
                        if (obj.common.enabled) startInstance(id);
                    } else {
                        delete procs[id];
                    }
                });
            } else {
                if (ipArr.indexOf(obj.common.host) !== -1) {
                    if (obj.common.enabled) startInstance(id);
                } else {
                    delete procs[id];
                }
            }

        } else {
            // unknown adapter
            if (ipArr.indexOf(obj.common.host) !== -1) {
                procs[id] = {config: obj};
                if (obj.common.enabled) startInstance(id);
            }
        }
    }
<commit_msg>accept hostname in instance host attribute<commit_after>/**
 *      ioBroker.ctrl (ioBroker Controller)
 *
 *      Controls Adapter-Processes
 *
 *
 */

// Change version in io-package.json and start grunt task to modify the version
var version = '0.0.15';
var title = 'io.controller';
process.title = title;


var schedule =      require('node-schedule');
var os =            require('os');
var fs =            require('fs');
var cp =            require('child_process');
var ObjectsCouch =  require(__dirname + '/lib/couch.js');
var StatesRedis =   require(__dirname + '/lib/redis.js');

var logger;
var isDaemon;
var hostname =      os.hostname();


if (process.argv.indexOf('start') !== -1) {
    isDaemon =      true;
    logger =        require(__dirname + '/lib/logger.js')('info', ['iobroker.log'], true);
} else {
    logger =        require(__dirname + '/lib/logger.js')('info', ['iobroker.log']);
}

var config;
if (!fs.existsSync(__dirname + '/conf/iobroker.json')) {
    logger.error('controller conf/iobroker.json missing - call node iobroker.js setup');
    process.exit(1);
} else {
    config = JSON.parse(fs.readFileSync(__dirname + '/conf/iobroker.json'));
}


// Find first non-loopback ip
var ifaces = os.networkInterfaces();
var ipArr = [];
for (var dev in ifaces) {
    /*jshint loopfunc:true */
    ifaces[dev].forEach(function (details) {
        if (!details.internal) ipArr.push(details.address);
    });
}
var firstIp = ipArr[0];

logger.info('ioBroker.nodejs version ' + version);
logger.info('copyright 2014 hobbyquaker, bluefox');
logger.info(title + ' starting');
logger.info('controller ip: ' + ipArr.join(' '));



var procs     = {};
var subscribe = {};

var states = new StatesRedis({

    redis: {
        host:    config.redis.host,
        port:    config.redis.port,
        options: config.redis.options
    },
    logger: logger,
    change: function (id, state) {
        if (subscribe[id]) {
            for (var i = 0; i < subscribe[id].length; i++) {
                // wake up adapter
                if (procs[subscribe[id][i]]) {
                    console.log("Wake up " + id +' ' + JSON.stringify(state));
                    startInstance(subscribe[id][i], true);
                } else {
                    logger.warn("Adapter subscribed on " + id + " does not exist!");
                }
            }
        }
    }
});
//states.subscribe('*');

var objects = new ObjectsCouch({
    host: config.couch.host,
    port: config.couch.port,
    user: config.couch.user,
    pass: config.couch.pass,
    logger: logger,
    connected: function () {
        logger.info('controller couchdb connected');
        setMeta();
        getInstances();
        startAliveInterval();
    },
    change: function (id, obj) {
        if (!id.match(/^system\.adapter\.[a-zA-Z0-9-_]+\.[0-9]+$/)) return;
        logger.info('controller object change ' + id);
        if (procs[id]) {
            // known adapter
            procs[id].config = obj;
            if (procs[id].process) {
                stopInstance(id, function () {
                    if (ipArr.indexOf(obj.common.host) !== -1 || obj.common.host == hostname) {
                        if (obj.common.enabled) startInstance(id);
                    } else {
                        delete procs[id];
                    }
                });
            } else {
                if (ipArr.indexOf(obj.common.host) !== -1) {
                    if (obj.common.enabled) startInstance(id);
                } else {
                    delete procs[id];
                }
            }

        } else {
            // unknown adapter
            if (ipArr.indexOf(obj.common.host) !== -1) {
                procs[id] = {config: obj};
                if (obj.common.enabled) startInstance(id);
            }
        }
    }

{'fraction_non_alphanumeric': 0.1525892739491369, 'fraction_numerical': 0.005007247331664251, 'mean_word_length': 1.8771796815769521, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, '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': '7434589', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2256, 'n_words': 497}