{"size": 2110, "ext": "js", "max_stars_repo_path": "src/app/IpcLoggerClient.js", "max_stars_repo_name": "eviltester/icompress", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/app/IpcLoggerClient.js", "max_issues_repo_name": "eviltester/icompress", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/app/IpcLoggerClient.js", "max_forks_repo_name": "eviltester/icompress", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.5135135135, "max_line_length": 93, "alphanum_fraction": 0.3872037915}
const Ipc = require('node-ipc');
const Events = require("../logging/Events");

class IpcLoggerClient{

    constructor(clientId) {
        this.ipc =  new Ipc.IPCModule();
        this.ipc.config.id = clientId;
        this.ipc.config.retry= 1500;

    }

    connect(){
        const ipc=this.ipc;
        ipc.connectTo(
            'world',
            function(){
                ipc.of.world.on(
                    'connect',
                    function(){
                        ipc.log('## connected to world ##', ipc.config.delay);
                        ipc.of.world.emit(
                            'app.message',
                            {
                                id      : ipc.config.id,
                                message : 'connect from ' + ipc.config.id
                            }
                        );
                    }
                );
                ipc.of.world.on(
                    'disconnect',
                    function(){
                        ipc.log('disconnected from world');
                    }
                );
                ipc.of.world.on(
                    'app.message',
                    function(data){
                        ipc.log('got a message from world : ', data);
                    }
                );
                ipc.of.world.on(
                    'kill.connection',
                    function(data){
                        ipc.log('world requested kill.connection');
                        ipc.disconnect('world');
                    }
                );
            });
    }

    close(){
        ipc.disconnect('world');
    }

    // when sending a message we can add whatever properties we want to it and they
    // will be received as properties on the data e.g. data.id, data.message, data.logMessage
    logMessage(message){
        this.ipc.of.world.emit(
            'app.message',
            {
                id      : this.ipc.config.id,
                message : message,
                logMessage : true
            }
        );
    }

}

module.exports = {
    IpcLoggerClient
}