Example #1
0
exports.init = function (nodeName, onReadyCallback,onSleepCanExecuteCallback) {
    cprint("Starting adaptor " + nodeName);

    thisAdaptor                 = new AdaptorBase(nodeName);
    thisAdaptor.onReadyCallback = onReadyCallback;

    thisAdaptor.instaceUID      =   uuid.v4();

    if(onSleepCanExecuteCallback == undefined){
        thisAdaptor.onSleepCanExecuteCallback = default_onSleepCanExecute;
    }
    thisAdaptor.isSleeping      = false;


    var basePath = process.env.SWARM_PATH;
    if(process.env.SWARM_PATH == undefined){
        console.log("Please set SWARM_PATH variable to your installation folder");
        process.exit(-1);
    }

    util.addGlobalErrorHandler();

    var basicConfigFile             = basePath + "/etc/config";
    thisAdaptor.config              = util.readConfig(basicConfigFile);
    thisAdaptor.redisHost           = thisAdaptor.config.Core.redisHost;
    thisAdaptor.redisPort           = thisAdaptor.config.Core.redisPort;
    thisAdaptor.coreId              = thisAdaptor.config.Core.coreId;

    redisClient             = redis.createClient(thisAdaptor.redisPort, thisAdaptor.redisHost);

    redisClient.on("error",onRedisError);
    redisClient.on("connect",onRedisConnect);

    pubsubRedisClient       = redis.createClient(thisAdaptor.redisPort, thisAdaptor.redisHost);

    pubsubRedisClient.on("error",onRedisError);
    pubsubRedisClient.on("connect",onRedisConnect);


    thisAdaptor.compiledSwarmingDescriptions    = [];
    thisAdaptor.connectedOutlets                ={};

    thisAdaptor.msgCounter                      = 0;

    var channel = thisAdaptor.coreId + nodeName;
    dprint("Subscribing to channel " + channel );
    pubsubRedisClient.subscribe(channel);


    // handle messages from redis
    pubsubRedisClient.on("message", function (channel, message) {
        //continue swarmingPhase
        var initVars = JSON.parse(message);
        if(!thisAdaptor.isSleeping || thisAdaptor.onSleepCanExecuteCallback(initVars)){
            onMessageFromQueue(initVars, message);
        }
    });

    if (nodeName == "Core") {
        thisAdaptor.descriptionsFolder = basePath + "/" + thisAdaptor.config.Core.swarmsfolder;
        uploadDescriptions();
        loadSwarmingCode();
    }

    return thisAdaptor;
}
Example #2
0
exports.init = function (nodeName, onReadyCallback,onSleepCanExecuteCallback,verbose) {
    globalVerbosity = verbose;
    cprint("Starting adapter " + nodeName);

    thisAdapter                 = new AdapterBase(nodeName);
    thisAdapter.onReadyCallback = onReadyCallback;

    thisAdapter.instaceUID      =   uuid.v4();

    if(onSleepCanExecuteCallback == undefined){
        thisAdapter.onSleepCanExecuteCallback = default_onSleepCanExecute;
    }

    var basePath = process.env.SWARM_PATH;
    if(process.env.SWARM_PATH == undefined){
        util.delayExit("Please set SWARM_PATH variable to your installation folder",1000);
    }

    util.addGlobalErrorHandler();

    var basicConfigFile             = basePath + "/etc/config";
    thisAdapter.config              = util.readConfig(basicConfigFile);
    thisAdapter.redisHost           = thisAdapter.config.Core.redisHost;
    thisAdapter.redisPort           = thisAdapter.config.Core.redisPort;
    thisAdapter.coreId              = thisAdapter.config.Core.coreId;

    redisClient             = redis.createClient(thisAdapter.redisPort, thisAdapter.redisHost);

    redisClient.on("error",onRedisError);
    redisClient.on("ready",onCmdRedisReady);

    pubsubRedisClient       = redis.createClient(thisAdapter.redisPort, thisAdapter.redisHost);

    pubsubRedisClient.on("error",onRedisError);

    thisAdapter.compiledSwarmingDescriptions    = [];
    thisAdapter.connectedOutlets                = {};

    thisAdapter.msgCounter                      = 0;

    var channel = util.mkChannelUri(nodeName);
    dprint("Subscribing to channel " + channel );
    pubsubRedisClient.subscribe(channel);
    pubsubRedisClient.on("subscribe",onPubSubRedisReady);

    // handle messages from redis
    pubsubRedisClient.on("message", function (channel, message) {
        //continue swarmingPhase
        var initVars = JSON.parse(message);
        if(thisAdapter.nodeName == "Null*"){
            cprint("Error: Null adapter received " + message);
        }
        else
        if(!thisAdapter.isSleeping || thisAdapter.onSleepCanExecuteCallback(initVars)){
            onMessageFromQueue(initVars);
        }
    });

    thisAdapter.swarmingCodeLoaded = false;
    return thisAdapter;
}