<commit_before>/**
 * Created by noam.arad on 10/23/2017.
 */

const io = require('socket.io-client');
const util = require('util');
const BackendClient = require('./BackendClientFactory').getBackendClient();
const logger = require('../common/logger').getLogger('PushManager');
const Q = require('q');

class PushManager {

    constructor()
    {
        this.registeredObjects = {};
        this.registeredQueues = {};
    }

    registerObjectToPush(partnerId, templateName, objectId, params, callback, enabledDefault = false)
    {
        const deferred = Q.defer();
        if (!this.registeredObjects[objectId] || !this.registeredObjects[objectId].templates[templateName] || !this.registeredObjects[objectId].templates[templateName].init)
        {
            BackendClient.registerToPush(templateName, partnerId, params).then((registerResult) => {
                logger.debug(`registered successfully to push event notifications data [${util.inspect(registerResult)}]`);
                this._initSocket(registerResult.url);
                if (this.socket)
                {
                    let registeredObject = this.registeredObjects[objectId];
                    if (!registeredObject)
                    {
                        registeredObject = {
                            enabled: enabledDefault,
                            templates: {}
                        };
                        this.registeredObjects[objectId] = registeredObject;
                    }
                    let encryptedQueueKey = registerResult.queueKey; //Bug of naming in the server
                    registeredObject.encryptedQueueKey = encryptedQueueKey;
                    registeredObject.callback = callback;
                    registeredObject.templates[templateName] = {'init': true};

                    this.registeredQueues[encryptedQueueKey] = {'objectId': objectId, 'deferredObject': deferred};
                    this.socket.on('reconnect', (attemptNumber) => {
                        logger.info('Reconnected in the  [' + attemptNumber + "] attempt for Queue: " + registerResult.queueName);
                        this.socket.emit('listen', registerResult.queueName, registerResult.queueKey);
                    });

                    //when listen event is caught in pub-sub-server it emits a "connected" event.
                    this.socket.emit('listen', registerResult.queueName, registerResult.queueKey);
                }
            }).<commit_msg>PLAT-9158: 1.31 add push reconnect (#603)<commit_after>/**
 * Created by noam.arad on 10/23/2017.
 */

const io = require('socket.io-client');
const util = require('util');
const BackendClient = require('./BackendClientFactory').getBackendClient();
const logger = require('../common/logger').getLogger('PushManager');
const Q = require('q');

class PushManager {

    constructor()
    {
        this.registeredObjects = {};
        this.registeredQueues = {};
    }

    registerObjectToPush(partnerId, templateName, objectId, params, callback, enabledDefault = false)
    {
        const deferred = Q.defer();
        if (!this.registeredObjects[objectId] || !this.registeredObjects[objectId].templates[templateName] || !this.registeredObjects[objectId].templates[templateName].init)
        {
            BackendClient.registerToPush(templateName, partnerId, params).then((registerResult) => {
                logger.debug(`registered successfully to push event notifications data [${util.inspect(registerResult)}]`);
                this._initSocket(registerResult.url);
                if (this.socket)
                {
                    let registeredObject = this.registeredObjects[objectId];
                    if (!registeredObject)
                    {
                        registeredObject = {
                            enabled: enabledDefault,
                            templates: {}
                        };
                        this.registeredObjects[objectId] = registeredObject;
                    }
                    let encryptedQueueKey = registerResult.queueKey; //Bug of naming in the server
                    registeredObject.encryptedQueueKey = encryptedQueueKey;
                    registeredObject.callback = callback;
                    registeredObject.templates[templateName] = {'init': true};

                    this.registeredQueues[encryptedQueueKey] = {'objectId': objectId, 'deferredObject': deferred};
                    this.socket.on('reconnect', (attemptNumber) => {
                        if (this.registeredQueues[registerResult.queueKey])
                        {
                            logger.info('Reconnected in the  [' + attemptNumber + "] attempt for Queue: " + registerResult.queueName);
                            this.socket.emit('listen', registerResult.queueName, registerResult.queueKey);
                        }
                    });

                    //when listen event is caught in pub-sub-server it emits a "connected" event.
                    this.socket.emit('listen', registerResult.queueName, registerResult.queueKey);
                }
            }).
{'fraction_non_alphanumeric': 0.10761460761460762, 'fraction_numerical': 0.005050505050505051, 'mean_word_length': 1.9422857142857144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2179470', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1154, 'n_words': 235}