UNPKG

dl

Version:

DreamLab Libs

148 lines (117 loc) 4.63 kB
var core = require('core'); var Event = core.event.Event; var EventDispatcher = core.event.EventDispatcher; var RbmqChannel = core.client.rabbitmq.RbmqChannel; var RbmqConnection = core.client.rabbitmq.RbmqConnection; var Types = core.common.Types; var QueueHostPreferrer = require('./QueueHostPreferrer').QueueHostPreferrer; var QueueCredentials = require('./QueueCredentials').QueueCredentials; var QueueLogger = require('./QueueLogger').QueueLogger; var QueueOpalCredentials = require('./QueueOpalCredentials').QueueOpalCredentials; var QueueClient = function(credentials, immediateInitialize) { EventDispatcher.call(this); if (Types.isString(credentials)) { this._credentials = new QueueOpalCredentials(credentials); } else { this._credentials = new QueueCredentials(credentials); } this._credentials.addEventListener(QueueCredentials.Event.LOADED, this._onCredentialsLoaded, this); this._connected = false; this._logger = new QueueLogger(); this._rbmqChannel = null; this._rbmqConnection = null; this._preferred = new QueueHostPreferrer(); this._userId = null; if (immediateInitialize) { this.initialize(); } }; QueueClient.prototype = Object.create(EventDispatcher.prototype); QueueClient.prototype.initialize = function() { this._credentials.load(); }; QueueClient.prototype.destroy = function() { if (this._rbmqConnection) { this._rbmqConnection.disconnect(false); this._rbmqConnection = null; } if (this._credentials) { this._credentials.destroy(); this._credentials = null; } }; /** * Set preferred hosts * * @param {Object} hosts Host => weight */ QueueClient.prototype.setPreferredHosts = function(hosts) { this._preferred.setHosts(hosts); }; QueueClient.prototype._onCredentialsLoaded = function() { console.log('QueueClient/_onCredentialsLoaded'); var that = this; var connectionConfig = this._credentials.getConnectionConfig(); this._logger.setConfig(this._credentials.getLoggingConfig(), this._credentials.getMonitoringConfig()); this._userId = connectionConfig.login; // compare preferred hosts with config and assing host weight this._preferred.parseHosts(connectionConfig.hosts, function(err, hosts) { if (!err) { connectionConfig.hosts = hosts; } // if preferred hosts are set enable it in config if (that._preferred.hasPreferredHosts()) { connectionConfig.hostWeightEnabled = true; } if (!that._rbmqConnection) { that._rbmqConnection = new RbmqConnection(connectionConfig); that._rbmqConnection.addEventListener(RbmqConnection.Event.CONNECTED, that._onConnected, that); that._rbmqConnection.addEventListener(RbmqConnection.Event.DISCONNECTED, that._onDisconnected, that); that._rbmqConnection.connect(); } else { that._rbmqConnection.setConfig(connectionConfig); } }); }; QueueClient.prototype._onConnected = function() { this._logger.log('connection', 'connected'); this._rbmqChannel.addEventListener(RbmqChannel.Event.OPENED, this._onChannelOpened, this); this._rbmqChannel.addEventListener(RbmqChannel.Event.CLOSED, this._onChannelClosed, this); this._rbmqChannel.open(); }; QueueClient.prototype._onChannelOpened = function() { var that = this; if (!this._connected) { this._connected = true; process.nextTick(function() { that.dispatchEvent(new Event(QueueClient.Event.CONNECTED)); }); } }; QueueClient.prototype._onChannelClosed = function() { if (this._rbmqConnection && this._rbmqConnection.isConnected()) { // this branch should fire only after cancel notification! console.log('QueueClient/_onChannelClosed connection is still ok - disconnect'); this._rbmqConnection.reconnect(); } else { this._onDisconnected(); } }; QueueClient.prototype._onDisconnected = function() { this._logger.log('connection', 'disconnected'); if (this._rbmqChannel) { this._rbmqChannel.removeAllEventListeners(); this._rbmqChannel = null; } var that = this; if (this._connected) { this._connected = false; process.nextTick(function() { that.dispatchEvent(new Event(QueueClient.Event.DISCONNECTED)); }); } }; QueueClient.Event = {}; QueueClient.Event.CONNECTED = 'QueueClient.Event.CONNECTED'; QueueClient.Event.DISCONNECTED = 'QueueClient.Event.DISCONNECTED'; exports.QueueClient = QueueClient;