From d6eec6ed8f8f1c8cb185ee698ada231a20e2d06b Mon Sep 17 00:00:00 2001 From: Darren Date: Sun, 18 Aug 2013 00:23:58 +0100 Subject: [PATCH] Gateway + Network reconnection methods. Jumpserver improvements --- client/assets/src/models/application.js | 28 ++++++++--- client/assets/src/models/gateway.js | 65 ++++++++++++++++++++++--- client/assets/src/models/network.js | 59 ++++++++++++++++++++-- 3 files changed, 133 insertions(+), 19 deletions(-) diff --git a/client/assets/src/models/application.js b/client/assets/src/models/application.js index 8e9df4b..7269e46 100644 --- a/client/assets/src/models/application.js +++ b/client/assets/src/models/application.js @@ -345,7 +345,10 @@ _kiwi.model.Application = function () { * Handle the reconnections to the kiwi server */ (function () { + // 0 = non-reconnecting state. 1 = reconnecting state. var gw_stat = 0; + + // If the current or upcoming disconnect was planned var unplanned_disconnect = false; gw.on('disconnect', function (event) { @@ -393,7 +396,7 @@ _kiwi.model.Application = function () { that.message.text(msg, {timeout: 5000}); } - // Mention the disconnection on every channel + // Mention the re-connection on every channel _kiwi.app.connections.forEach(function(connection) { connection.panels.server.addMsg('', msg, 'action join'); @@ -419,17 +422,25 @@ _kiwi.model.Application = function () { gw.on('kiwi:jumpserver', function (data) { - // Switching kiwi server? - if (typeof data.kiwi_server !== 'undefined') { - _kiwi.app.kiwi_server = data.kiwi_server; - _kiwi.gateway.set('kiwi_server', data.kiwi_server); - } + var serv; + // No server set? Then nowhere to jump to. + if (typeof data.kiwi_server === 'undefined') + return; + + serv = data.kiwi_server; + + // Strip any trailing slash from the end + if (serv[serv.length-1] === '/') + serv = serv.substring(0, serv.length-1); + + _kiwi.app.kiwi_server = serv; // Force the jumpserver now? if (data.force) { // Get an interval around 1 minute so everyone doesn't reconnect it all at once var jump_server_interval = Math.random() * (90 - 60) + 60; + // Tell the user we are going to disconnect, wait a minute then do the actual reconnect var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch(); that.message.text(msg, {timeout: 10000}); @@ -438,7 +449,10 @@ _kiwi.model.Application = function () { that.message.text(msg, {timeout: 8000}); setTimeout(function forcedReconnectPartTwo() { - _kiwi.gateway.reconnect(); + _kiwi.gateway.reconnect(function() { + // Reconnect all the IRC connections + that.connections.forEach(function(con){ con.reconnect(); }); + }); }, 5000); }, jump_server_interval * 1000); diff --git a/client/assets/src/models/gateway.js b/client/assets/src/models/gateway.js index b6d4965..eddbc60 100644 --- a/client/assets/src/models/gateway.js +++ b/client/assets/src/models/gateway.js @@ -134,9 +134,26 @@ _kiwi.model.Gateway = function () { this.reconnect = function (callback) { + var that = this, + transport_path; + this.disconnect_requested = true; this.socket.disconnect(); - this.connect(callback); + + // To get around the allow-origin issues for requests, completely reload the + // transport source from the new server + window.io = null; + + // Path to get the socket.io transport code + transport_path = _kiwi.app.kiwi_server + _kiwi.app.get('base_path') + '/transport/socket.io.js?ts='+(new Date().getTime()); + $script(transport_path, function() { + if (!window.io) { + return callback('err_kiwi_server_not_found'); + } + + that.set('kiwi_server', _kiwi.app.kiwi_server + '/kiwi'); + that.connect(callback); + }); }; @@ -226,21 +243,30 @@ _kiwi.model.Gateway = function () { }; - + /** + * Return a new network object with the new connection details + */ this.newConnection = function(connection_info, callback_fn) { - var that = this, - h = connection_info; + var that = this; - this.socket.emit('kiwi', {command: 'connect', nick: h.nick, hostname: h.host, port: h.port, ssl: h.ssl, password: h.password}, function (err, server_num) { + this.makeIrcConnection(connection_info, function(err, server_num) { var connection; if (!err) { if (!_kiwi.app.connections.getByConnectionId(server_num)){ - connection = new _kiwi.model.Network({connection_id: server_num, nick: h.nick}); + var inf = { + connection_id: server_num, + nick: connection_info.nick, + address: connection_info.host, + port: connection_info.port, + ssl: connection_info.ssl, + password: connection_info.password + }; + connection = new _kiwi.model.Network(inf); _kiwi.app.connections.add(connection); } - console.log("_kiwi.gateway.socket.on('connect')"); + console.log("_kiwi.gateway.socket.on('connect')", connection); callback_fn && callback_fn(err, connection); } else { @@ -250,6 +276,31 @@ _kiwi.model.Gateway = function () { }); }; + + /** + * Make a new IRC connection and return its connection ID + */ + this.makeIrcConnection = function(connection_info, callback_fn) { + var server_info = { + command: 'connect', + nick: connection_info.nick, + hostname: connection_info.host, + port: connection_info.port, + ssl: connection_info.ssl, + password: connection_info.password + }; + + this.socket.emit('kiwi', server_info, function (err, server_num) { + if (!err) { + callback_fn && callback_fn(err, server_num); + + } else { + callback_fn && callback_fn(err); + } + }); + }; + + this.isConnected = function () { return this.socket.socket.connected; }; diff --git a/client/assets/src/models/network.js b/client/assets/src/models/network.js index 22a7c1f..08dd97f 100644 --- a/client/assets/src/models/network.js +++ b/client/assets/src/models/network.js @@ -15,6 +15,24 @@ */ address: '', + /** + * The port for the network + * @type Int + */ + port: 6667, + + /** + * If this network uses SSL + * @type Bool + */ + ssl: false, + + /** + * The password to connect to this network + * @type String + */ + password: '', + /** * The current nickname * @type String @@ -36,8 +54,11 @@ initialize: function () { - this.gateway = _kiwi.global.components.Network(this.get('connection_id')); - this.bindGatewayEvents(); + // If we already have a connection, bind our events + if (typeof this.get('connection_id') !== 'undefined') { + this.gateway = _kiwi.global.components.Network(this.get('connection_id')); + this.bindGatewayEvents(); + } // Create our panel list (tabs) this.panels = new _kiwi.model.PanelList([], this); @@ -50,6 +71,34 @@ }, + reconnect: function(callback_fn) { + var that = this, + server_info = { + nick: this.get('nick'), + host: this.get('address'), + port: this.get('port'), + ssl: this.get('ssl'), + password: this.get('password') + }; + + _kiwi.gateway.makeIrcConnection(server_info, function(err, connection_id) { + if (!err) { + that.gateway.dispose(); + + that.set('connection_id', connection_id); + that.gateway = _kiwi.global.components.Network(that.get('connection_id')); + that.bindGatewayEvents(); + + callback_fn && callback_fn(err); + + } else { + console.log("_kiwi.gateway.socket.on('error')", {reason: err}); + callback_fn && callback_fn(err); + } + }); + }, + + bindGatewayEvents: function () { //this.gateway.on('all', function() {console.log('ALL', this.get('connection_id'), arguments);}); @@ -96,7 +145,7 @@ var that = this, panels = []; - // Multiple channels may come as comma-delimited + // Multiple channels may come as comma-delimited if (typeof channels === 'string') { channels = channels.split(','); } @@ -150,7 +199,7 @@ }); - + function onDisconnect(event) { $.each(this.panels.models, function (index, panel) { panel.addMsg('', _kiwi.global.i18n.translate('client_models_network_disconnected').fetch(), 'action quit'); @@ -296,7 +345,7 @@ members.remove(user, part_options); if (part_options.current_user_kicked) { - members.reset([]); + members.reset([]); } } -- 2.25.1