<commit_before>/**
 * This library contains methods for a component to interact with a supervisor
 */

var SUPERVISOR_PATH_REGISTER_CAPABILITY = '/register/capability';
var SUPERVISOR_PATH_SHOW_SPECIFICATION = '/show/specification';
var SUPERVISOR_PATH_SHOW_CAPABILITY = '/show/capability';
var SUPERVISOR_PATH_SHOW_RESULT = '/show/result';
var SUPERVISOR_PATH_REGISTER_SPECIFICATION = '/register/specification';
var SUPERVISOR_PATH_REGISTER_RESULT = '/register/result';
var SUPERVISOR_PATH_INFO = '/info/all';

var https = require("https")
    ,mplane = require("mplane"),
    ssl_files = require("./ssl_files.js")
    ,request = require('request')
    ,async = require('async');

var MIME_TYPE = "application/x-mplane+json";


/**********************************************************************************************************************/

/**
 * Register an array of capabilities to a supervisor
 * On completion, calls a callback function with err, response
 * options MUST include (or an INVALID parameters error is thrown):
 *  - options.host
 *  - options.port
 *  - options.caFile
 *  - options.keyFile
 *  - options.certFile
 */
 function registerCapabilities( capabilities , options ,callback) {
    if ((!options.host) || (!options.port) || (!options.caFile) || (!options.keyFile) || (!options.certFile))
        callback(new Error("Invalid parameters"), null);
    else{
        var post_data = [];
        capabilities.forEach(function(capability , index){
            // Serialize the capability Object
            //post_data[capability.get_label()] = JSON.parse(capability.to_dict());
            post_data.push(JSON.parse(capability.to_dict()));
        });
        var post_options = {
            path: SUPERVISOR_PATH_REGISTER_CAPABILITY
            ,method: 'POST'
            ,host: options.host
            ,port: options.port
            ,ca: ssl_files.readCaChain(options.caFile)
            ,agent: false,
            key: ssl_files.readFileContent(options.keyFile)
            ,cert: ssl_files.readFileContent(options.certFile)
            ,headers: {
                //'Content-Type': 'application/json; charset=utf-8',
                'Content-Type': MIME_TYPE,
                'Content-Length': Buffer.byteLength(JSON.stringify(post_data))
                ,'Accept': '*/*'
            }
        };
        // Set up the request
        var post_req = https.request(post_options, function(res) {
            if (res.statusCode != 200){
                callback(new Error(res.statusCode), res.statusCode);
            }
            callback(null,res.statusCode );
        });
        post_req.on("error", function(err){
            callback(err,null)
        });
        post_req.write(JSON.stringify( post_data));
        post_req.end();
    }
}

/**
 * Checks if a specification is ready for a capability
 * It activates a period check and action to be done when something has been found
 *  * options MUST include (or an INVALID parameters error is thrown):
 *  - options.host
 *  - options.port
 *  - options.caFile
 *  - options.keyFile
 *  - options.certFile
 *  CheckPeriod can be choosen with period (default 10000)
 *  Verbose: if options.verbose, will log on console
 *  For each specification obtained by the supervisor, action is called with
 *      - specification
 *  On completion the callback function is called with err, null
 */
function checkSpecifications(options , action , callback){
    if ((!options.host) || (!options.port) || (!options.caFile) || (!options.keyFile) || (!options.certFile))
        callback(new Error("Invalid parameters"), null);
    else {
        var period = options.period || 10000;
        setInterval(function () {
            var get_options = {
                path: SUPERVISOR_PATH_SHOW_SPECIFICATION,
                method: 'GET',
                host: options.host,
                port: options.port,
                followRedirect: false,
                ca: ssl_files.readCaChain(options.caFile),
                key: ssl_files.readFileContent(options.keyFile),
                cert: ssl_files.readFileContent(options.certFile)
            };

            var req = https.get(get_options, function (res) {
                // For some reason we have no capability registered!
                if (res.statusCode == 428){
                    callback(new Error(428) , null);
                }else{
                    // Buffer the body entirely for processing as a whole.
                    var bodyChunks = [];
                    var spec = null;
                    res.on('data', function (chunk) {
                        bodyChunks.push(chunk);
                    }).on('end', function () {
                        body = Buffer.concat(bodyChunks);
                        body = JSON.parse(body);
                        if (body.length == 0) {
                            console.log("+");
                        } else {
                            /*
                            for (var i= 0; i<body.length; i++){
                                var spec = mplane.from_dict(body[i]);
                                action(spec);
                            }*/
                            async.eachSeries(body
                                ,function(curSpec , callback){
                                    var spec = mplane.from_dict(curSpec);
                                    action(spec , callback);
                                }
                                , function(){
                                });<commit_msg>Trying to understand why only an action is performed<commit_after>/**
 * This library contains methods for a component to interact with a supervisor
 */

var SUPERVISOR_PATH_REGISTER_CAPABILITY = '/register/capability';
var SUPERVISOR_PATH_SHOW_SPECIFICATION = '/show/specification';
var SUPERVISOR_PATH_SHOW_CAPABILITY = '/show/capability';
var SUPERVISOR_PATH_SHOW_RESULT = '/show/result';
var SUPERVISOR_PATH_REGISTER_SPECIFICATION = '/register/specification';
var SUPERVISOR_PATH_REGISTER_RESULT = '/register/result';
var SUPERVISOR_PATH_INFO = '/info/all';

var https = require("https")
    ,mplane = require("mplane"),
    ssl_files = require("./ssl_files.js")
    ,request = require('request')
    ,async = require('async');

var MIME_TYPE = "application/x-mplane+json";


/**********************************************************************************************************************/

/**
 * Register an array of capabilities to a supervisor
 * On completion, calls a callback function with err, response
 * options MUST include (or an INVALID parameters error is thrown):
 *  - options.host
 *  - options.port
 *  - options.caFile
 *  - options.keyFile
 *  - options.certFile
 */
 function registerCapabilities( capabilities , options ,callback) {
    if ((!options.host) || (!options.port) || (!options.caFile) || (!options.keyFile) || (!options.certFile))
        callback(new Error("Invalid parameters"), null);
    else{
        var post_data = [];
        capabilities.forEach(function(capability , index){
            // Serialize the capability Object
            //post_data[capability.get_label()] = JSON.parse(capability.to_dict());
            post_data.push(JSON.parse(capability.to_dict()));
        });
        var post_options = {
            path: SUPERVISOR_PATH_REGISTER_CAPABILITY
            ,method: 'POST'
            ,host: options.host
            ,port: options.port
            ,ca: ssl_files.readCaChain(options.caFile)
            ,agent: false,
            key: ssl_files.readFileContent(options.keyFile)
            ,cert: ssl_files.readFileContent(options.certFile)
            ,headers: {
                //'Content-Type': 'application/json; charset=utf-8',
                'Content-Type': MIME_TYPE,
                'Content-Length': Buffer.byteLength(JSON.stringify(post_data))
                ,'Accept': '*/*'
            }
        };
        // Set up the request
        var post_req = https.request(post_options, function(res) {
            if (res.statusCode != 200){
                callback(new Error(res.statusCode), res.statusCode);
            }
            callback(null,res.statusCode );
        });
        post_req.on("error", function(err){
            callback(err,null)
        });
        post_req.write(JSON.stringify( post_data));
        post_req.end();
    }
}

/**
 * Checks if a specification is ready for a capability
 * It activates a period check and action to be done when something has been found
 *  * options MUST include (or an INVALID parameters error is thrown):
 *  - options.host
 *  - options.port
 *  - options.caFile
 *  - options.keyFile
 *  - options.certFile
 *  CheckPeriod can be choosen with period (default 10000)
 *  Verbose: if options.verbose, will log on console
 *  For each specification obtained by the supervisor, action is called with
 *      - specification
 *  On completion the callback function is called with err, null
 */
function checkSpecifications(options , action , callback){
    if ((!options.host) || (!options.port) || (!options.caFile) || (!options.keyFile) || (!options.certFile))
        callback(new Error("Invalid parameters"), null);
    else {
        var period = options.period || 10000;
        setInterval(function () {
            var get_options = {
                path: SUPERVISOR_PATH_SHOW_SPECIFICATION,
                method: 'GET',
                host: options.host,
                port: options.port,
                followRedirect: false,
                ca: ssl_files.readCaChain(options.caFile),
                key: ssl_files.readFileContent(options.keyFile),
                cert: ssl_files.readFileContent(options.certFile)
            };

            var req = https.get(get_options, function (res) {
                // For some reason we have no capability registered!
                if (res.statusCode == 428){
                    callback(new Error(428) , null);
                }else{
                    // Buffer the body entirely for processing as a whole.
                    var bodyChunks = [];
                    var spec = null;
                    res.on('data', function (chunk) {
                        bodyChunks.push(chunk);
                    }).on('end', function () {
                        body = Buffer.concat(bodyChunks);
                        body = JSON.parse(body);
                        if (body.length == 0) {
                            console.log("+");
                        } else {
                            console.log(body);
                            /*
                            for (var i= 0; i<body.length; i++){
                                var spec = mplane.from_dict(body[i]);
                                action(spec);
                            }*/
                            async.eachSeries(body
                                ,function(curSpec , callback){
                                    var spec = mplane.from_dict(curSpec);
                                    console.log("---------");
                                    console.log(spec);
                                    console.log("---------");
                                    action(spec , callback);
                                }
                                , function(){
                                });
{'fraction_non_alphanumeric': 0.12930502950761913, 'fraction_numerical': 0.003875627587421827, 'mean_word_length': 1.797240699679724, 'pattern_counts': {'":': 0, '<': 5, '<?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': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1040335', 'n_tokens_mistral': 3027, 'n_tokens_neox': 2853, 'n_words': 686}