Gateway + Network reconnection methods. Jumpserver improvements
authorDarren <darren@darrenwhitlen.com>
Sat, 17 Aug 2013 23:23:58 +0000 (00:23 +0100)
committerDarren <darren@darrenwhitlen.com>
Sat, 17 Aug 2013 23:23:58 +0000 (00:23 +0100)
client/assets/src/models/application.js
client/assets/src/models/gateway.js
client/assets/src/models/network.js

index 8e9df4bcceafdef5c80bd1b5f4960483e1b9a9b1..7269e461ab471628762a80093175a94870ef98a1 100644 (file)
@@ -345,7 +345,10 @@ _kiwi.model.Application = function () {
              * Handle the reconnections to the kiwi server\r
              */\r
             (function () {\r
+                // 0 = non-reconnecting state. 1 = reconnecting state.\r
                 var gw_stat = 0;\r
+\r
+                // If the current or upcoming disconnect was planned\r
                 var unplanned_disconnect = false;\r
 \r
                 gw.on('disconnect', function (event) {\r
@@ -393,7 +396,7 @@ _kiwi.model.Application = function () {
                         that.message.text(msg, {timeout: 5000});\r
                     }\r
 \r
-                    // Mention the disconnection on every channel\r
+                    // Mention the re-connection on every channel\r
                     _kiwi.app.connections.forEach(function(connection) {\r
                         connection.panels.server.addMsg('', msg, 'action join');\r
 \r
@@ -419,17 +422,25 @@ _kiwi.model.Application = function () {
 \r
 \r
             gw.on('kiwi:jumpserver', function (data) {\r
-                // Switching kiwi server?\r
-                if (typeof data.kiwi_server !== 'undefined') {\r
-                    _kiwi.app.kiwi_server = data.kiwi_server;\r
-                    _kiwi.gateway.set('kiwi_server', data.kiwi_server);\r
-                }\r
+                var serv;\r
+                // No server set? Then nowhere to jump to.\r
+                if (typeof data.kiwi_server === 'undefined')\r
+                    return;\r
+\r
+                serv = data.kiwi_server;\r
+\r
+                // Strip any trailing slash from the end\r
+                if (serv[serv.length-1] === '/')\r
+                    serv = serv.substring(0, serv.length-1);\r
+\r
+                _kiwi.app.kiwi_server = serv;\r
 \r
                 // Force the jumpserver now?\r
                 if (data.force) {\r
                     // Get an interval around 1 minute so everyone doesn't reconnect it all at once\r
                     var jump_server_interval = Math.random() * (90 - 60) + 60;\r
 \r
+                    // Tell the user we are going to disconnect, wait a minute then do the actual reconnect\r
                     var msg = _kiwi.global.i18n.translate('client_models_application_jumpserver_prepare').fetch();\r
                     that.message.text(msg, {timeout: 10000});\r
 \r
@@ -438,7 +449,10 @@ _kiwi.model.Application = function () {
                         that.message.text(msg, {timeout: 8000});\r
 \r
                         setTimeout(function forcedReconnectPartTwo() {\r
-                            _kiwi.gateway.reconnect();\r
+                            _kiwi.gateway.reconnect(function() {\r
+                                // Reconnect all the IRC connections\r
+                                that.connections.forEach(function(con){ con.reconnect(); });\r
+                            });\r
                         }, 5000);\r
 \r
                     }, jump_server_interval * 1000);\r
index b6d4965bbd516466c101b207c3505471b4a628c9..eddbc60568ef3854f4e77f3f8fc5e7fb8a3fbc59 100644 (file)
@@ -134,9 +134,26 @@ _kiwi.model.Gateway = function () {
 \r
 \r
     this.reconnect = function (callback) {\r
+        var that = this,\r
+            transport_path;\r
+\r
         this.disconnect_requested = true;\r
         this.socket.disconnect();\r
-        this.connect(callback);\r
+\r
+        // To get around the allow-origin issues for requests, completely reload the\r
+        // transport source from the new server\r
+        window.io = null;\r
+\r
+        // Path to get the socket.io transport code\r
+        transport_path = _kiwi.app.kiwi_server + _kiwi.app.get('base_path') + '/transport/socket.io.js?ts='+(new Date().getTime());\r
+        $script(transport_path, function() {\r
+            if (!window.io) {\r
+                return callback('err_kiwi_server_not_found');\r
+            }\r
+\r
+            that.set('kiwi_server', _kiwi.app.kiwi_server + '/kiwi');\r
+            that.connect(callback);\r
+        });\r
     };\r
 \r
 \r
@@ -226,21 +243,30 @@ _kiwi.model.Gateway = function () {
     };\r
 \r
 \r
-\r
+    /**\r
+     * Return a new network object with the new connection details\r
+     */\r
     this.newConnection = function(connection_info, callback_fn) {\r
-        var that = this,\r
-            h = connection_info;\r
+        var that = this;\r
 \r
-        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) {\r
+        this.makeIrcConnection(connection_info, function(err, server_num) {\r
             var connection;\r
 \r
             if (!err) {\r
                 if (!_kiwi.app.connections.getByConnectionId(server_num)){\r
-                    connection = new _kiwi.model.Network({connection_id: server_num, nick: h.nick});\r
+                    var inf = {\r
+                        connection_id: server_num,\r
+                        nick: connection_info.nick,\r
+                        address: connection_info.host,\r
+                        port: connection_info.port,\r
+                        ssl: connection_info.ssl,\r
+                        password: connection_info.password\r
+                    };\r
+                    connection = new _kiwi.model.Network(inf);\r
                     _kiwi.app.connections.add(connection);\r
                 }\r
 \r
-                console.log("_kiwi.gateway.socket.on('connect')");\r
+                console.log("_kiwi.gateway.socket.on('connect')", connection);\r
                 callback_fn && callback_fn(err, connection);\r
 \r
             } else {\r
@@ -250,6 +276,31 @@ _kiwi.model.Gateway = function () {
         });\r
     };\r
 \r
+\r
+    /**\r
+     * Make a new IRC connection and return its connection ID\r
+     */\r
+    this.makeIrcConnection = function(connection_info, callback_fn) {\r
+        var server_info = {\r
+            command:    'connect',\r
+            nick:       connection_info.nick,\r
+            hostname:   connection_info.host,\r
+            port:       connection_info.port,\r
+            ssl:        connection_info.ssl,\r
+            password:   connection_info.password\r
+        };\r
+\r
+        this.socket.emit('kiwi', server_info, function (err, server_num) {\r
+            if (!err) {\r
+                callback_fn && callback_fn(err, server_num);\r
+\r
+            } else {\r
+                callback_fn && callback_fn(err);\r
+            }\r
+        });\r
+    };\r
+\r
+\r
     this.isConnected = function () {\r
         return this.socket.socket.connected;\r
     };\r
index 22a7c1f035f41e670e17f7e2ecb9061f74603d00..08dd97f2f0a92ee78887064c18d5feec11d5747c 100644 (file)
             */
             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
 
 
         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);
         },
 
 
+        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);});
 
             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(',');
             }
     });
 
 
-    
+
     function onDisconnect(event) {
         $.each(this.panels.models, function (index, panel) {
             panel.addMsg('', _kiwi.global.i18n.translate('client_models_network_disconnected').fetch(), 'action quit');
         members.remove(user, part_options);
 
         if (part_options.current_user_kicked) {
-            members.reset([]);        
+            members.reset([]);
         }
     }