1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/client/connection-manager.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,291 @@ 1.4 +/* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +"use strict"; 1.11 + 1.12 +const {Cc, Ci, Cu} = require("chrome"); 1.13 +const {setTimeout, clearTimeout} = require('sdk/timers'); 1.14 +const EventEmitter = require("devtools/toolkit/event-emitter"); 1.15 + 1.16 +Cu.import("resource://gre/modules/Services.jsm"); 1.17 +Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); 1.18 +Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); 1.19 + 1.20 +/** 1.21 + * Connection Manager. 1.22 + * 1.23 + * To use this module: 1.24 + * const {ConnectionManager} = require("devtools/client/connection-manager"); 1.25 + * 1.26 + * # ConnectionManager 1.27 + * 1.28 + * Methods: 1.29 + * ⬩ Connection createConnection(host, port) 1.30 + * ⬩ void destroyConnection(connection) 1.31 + * ⬩ Number getFreeTCPPort() 1.32 + * 1.33 + * Properties: 1.34 + * ⬩ Array connections 1.35 + * 1.36 + * # Connection 1.37 + * 1.38 + * A connection is a wrapper around a debugger client. It has a simple 1.39 + * API to instantiate a connection to a debugger server. Once disconnected, 1.40 + * no need to re-create a Connection object. Calling `connect()` again 1.41 + * will re-create a debugger client. 1.42 + * 1.43 + * Methods: 1.44 + * ⬩ connect() Connect to host:port. Expect a "connecting" event. If 1.45 + * host is not specified, a local pipe is used 1.46 + * ⬩ disconnect() Disconnect if connected. Expect a "disconnecting" event 1.47 + * 1.48 + * Properties: 1.49 + * ⬩ host IP address or hostname 1.50 + * ⬩ port Port 1.51 + * ⬩ logs Current logs. "newlog" event notifies new available logs 1.52 + * ⬩ store Reference to a local data store (see below) 1.53 + * ⬩ keepConnecting Should the connection keep trying connecting 1.54 + * ⬩ status Connection status: 1.55 + * Connection.Status.CONNECTED 1.56 + * Connection.Status.DISCONNECTED 1.57 + * Connection.Status.CONNECTING 1.58 + * Connection.Status.DISCONNECTING 1.59 + * Connection.Status.DESTROYED 1.60 + * 1.61 + * Events (as in event-emitter.js): 1.62 + * ⬩ Connection.Events.CONNECTING Trying to connect to host:port 1.63 + * ⬩ Connection.Events.CONNECTED Connection is successful 1.64 + * ⬩ Connection.Events.DISCONNECTING Trying to disconnect from server 1.65 + * ⬩ Connection.Events.DISCONNECTED Disconnected (at client request, or because of a timeout or connection error) 1.66 + * ⬩ Connection.Events.STATUS_CHANGED The connection status (connection.status) has changed 1.67 + * ⬩ Connection.Events.TIMEOUT Connection timeout 1.68 + * ⬩ Connection.Events.HOST_CHANGED Host has changed 1.69 + * ⬩ Connection.Events.PORT_CHANGED Port has changed 1.70 + * ⬩ Connection.Events.NEW_LOG A new log line is available 1.71 + * 1.72 + */ 1.73 + 1.74 +let ConnectionManager = { 1.75 + _connections: new Set(), 1.76 + createConnection: function(host, port) { 1.77 + let c = new Connection(host, port); 1.78 + c.once("destroy", (event) => this.destroyConnection(c)); 1.79 + this._connections.add(c); 1.80 + this.emit("new", c); 1.81 + return c; 1.82 + }, 1.83 + destroyConnection: function(connection) { 1.84 + if (this._connections.has(connection)) { 1.85 + this._connections.delete(connection); 1.86 + if (connection.status != Connection.Status.DESTROYED) { 1.87 + connection.destroy(); 1.88 + } 1.89 + } 1.90 + }, 1.91 + get connections() { 1.92 + return [c for (c of this._connections)]; 1.93 + }, 1.94 + getFreeTCPPort: function () { 1.95 + let serv = Cc['@mozilla.org/network/server-socket;1'] 1.96 + .createInstance(Ci.nsIServerSocket); 1.97 + serv.init(-1, true, -1); 1.98 + let port = serv.port; 1.99 + serv.close(); 1.100 + return port; 1.101 + }, 1.102 +} 1.103 + 1.104 +EventEmitter.decorate(ConnectionManager); 1.105 + 1.106 +let lastID = -1; 1.107 + 1.108 +function Connection(host, port) { 1.109 + EventEmitter.decorate(this); 1.110 + this.uid = ++lastID; 1.111 + this.host = host; 1.112 + this.port = port; 1.113 + this._setStatus(Connection.Status.DISCONNECTED); 1.114 + this._onDisconnected = this._onDisconnected.bind(this); 1.115 + this._onConnected = this._onConnected.bind(this); 1.116 + this._onTimeout = this._onTimeout.bind(this); 1.117 + this.keepConnecting = false; 1.118 +} 1.119 + 1.120 +Connection.Status = { 1.121 + CONNECTED: "connected", 1.122 + DISCONNECTED: "disconnected", 1.123 + CONNECTING: "connecting", 1.124 + DISCONNECTING: "disconnecting", 1.125 + DESTROYED: "destroyed", 1.126 +} 1.127 + 1.128 +Connection.Events = { 1.129 + CONNECTED: Connection.Status.CONNECTED, 1.130 + DISCONNECTED: Connection.Status.DISCONNECTED, 1.131 + CONNECTING: Connection.Status.CONNECTING, 1.132 + DISCONNECTING: Connection.Status.DISCONNECTING, 1.133 + DESTROYED: Connection.Status.DESTROYED, 1.134 + TIMEOUT: "timeout", 1.135 + STATUS_CHANGED: "status-changed", 1.136 + HOST_CHANGED: "host-changed", 1.137 + PORT_CHANGED: "port-changed", 1.138 + NEW_LOG: "new_log" 1.139 +} 1.140 + 1.141 +Connection.prototype = { 1.142 + logs: "", 1.143 + log: function(str) { 1.144 + let d = new Date(); 1.145 + let hours = ("0" + d.getHours()).slice(-2); 1.146 + let minutes = ("0" + d.getMinutes()).slice(-2); 1.147 + let seconds = ("0" + d.getSeconds()).slice(-2); 1.148 + let timestamp = [hours, minutes, seconds].join(":") + ": "; 1.149 + str = timestamp + str; 1.150 + this.logs += "\n" + str; 1.151 + this.emit(Connection.Events.NEW_LOG, str); 1.152 + }, 1.153 + 1.154 + get client() { 1.155 + return this._client 1.156 + }, 1.157 + 1.158 + get host() { 1.159 + return this._host 1.160 + }, 1.161 + 1.162 + set host(value) { 1.163 + if (this._host && this._host == value) 1.164 + return; 1.165 + this._host = value; 1.166 + this.emit(Connection.Events.HOST_CHANGED); 1.167 + }, 1.168 + 1.169 + get port() { 1.170 + return this._port 1.171 + }, 1.172 + 1.173 + set port(value) { 1.174 + if (this._port && this._port == value) 1.175 + return; 1.176 + this._port = value; 1.177 + this.emit(Connection.Events.PORT_CHANGED); 1.178 + }, 1.179 + 1.180 + disconnect: function(force) { 1.181 + if (this.status == Connection.Status.DESTROYED) { 1.182 + return; 1.183 + } 1.184 + clearTimeout(this._timeoutID); 1.185 + if (this.status == Connection.Status.CONNECTED || 1.186 + this.status == Connection.Status.CONNECTING) { 1.187 + this.log("disconnecting"); 1.188 + this._setStatus(Connection.Status.DISCONNECTING); 1.189 + this._client.close(); 1.190 + } 1.191 + }, 1.192 + 1.193 + connect: function() { 1.194 + if (this.status == Connection.Status.DESTROYED) { 1.195 + return; 1.196 + } 1.197 + if (!this._client) { 1.198 + this.log("connecting to " + this.host + ":" + this.port); 1.199 + this._setStatus(Connection.Status.CONNECTING); 1.200 + let delay = Services.prefs.getIntPref("devtools.debugger.remote-timeout"); 1.201 + this._timeoutID = setTimeout(this._onTimeout, delay); 1.202 + 1.203 + this._clientConnect(); 1.204 + } else { 1.205 + let msg = "Can't connect. Client is not fully disconnected"; 1.206 + this.log(msg); 1.207 + throw new Error(msg); 1.208 + } 1.209 + }, 1.210 + 1.211 + destroy: function() { 1.212 + this.log("killing connection"); 1.213 + clearTimeout(this._timeoutID); 1.214 + this.keepConnecting = false; 1.215 + if (this._client) { 1.216 + this._client.close(); 1.217 + this._client = null; 1.218 + } 1.219 + this._setStatus(Connection.Status.DESTROYED); 1.220 + }, 1.221 + 1.222 + _clientConnect: function () { 1.223 + let transport; 1.224 + if (!this.host) { 1.225 + transport = DebuggerServer.connectPipe(); 1.226 + } else { 1.227 + try { 1.228 + transport = debuggerSocketConnect(this.host, this.port); 1.229 + } catch (e) { 1.230 + // In some cases, especially on Mac, the openOutputStream call in 1.231 + // debuggerSocketConnect may throw NS_ERROR_NOT_INITIALIZED. 1.232 + // It occurs when we connect agressively to the simulator, 1.233 + // and keep trying to open a socket to the server being started in 1.234 + // the simulator. 1.235 + this._onDisconnected(); 1.236 + return; 1.237 + } 1.238 + } 1.239 + this._client = new DebuggerClient(transport); 1.240 + this._client.addOneTimeListener("closed", this._onDisconnected); 1.241 + this._client.connect(this._onConnected); 1.242 + }, 1.243 + 1.244 + get status() { 1.245 + return this._status 1.246 + }, 1.247 + 1.248 + _setStatus: function(value) { 1.249 + if (this._status && this._status == value) 1.250 + return; 1.251 + this._status = value; 1.252 + this.emit(value); 1.253 + this.emit(Connection.Events.STATUS_CHANGED, value); 1.254 + }, 1.255 + 1.256 + _onDisconnected: function() { 1.257 + this._client = null; 1.258 + 1.259 + if (this._status == Connection.Status.CONNECTING && this.keepConnecting) { 1.260 + setTimeout(() => this._clientConnect(), 100); 1.261 + return; 1.262 + } 1.263 + 1.264 + clearTimeout(this._timeoutID); 1.265 + 1.266 + switch (this.status) { 1.267 + case Connection.Status.CONNECTED: 1.268 + this.log("disconnected (unexpected)"); 1.269 + break; 1.270 + case Connection.Status.CONNECTING: 1.271 + this.log("connection error. Possible causes: USB port not connected, port not forwarded (adb forward), wrong host or port, remote debugging not enabled on the device."); 1.272 + break; 1.273 + default: 1.274 + this.log("disconnected"); 1.275 + } 1.276 + this._setStatus(Connection.Status.DISCONNECTED); 1.277 + }, 1.278 + 1.279 + _onConnected: function() { 1.280 + this.log("connected"); 1.281 + clearTimeout(this._timeoutID); 1.282 + this._setStatus(Connection.Status.CONNECTED); 1.283 + }, 1.284 + 1.285 + _onTimeout: function() { 1.286 + this.log("connection timeout. Possible causes: didn't click on 'accept' (prompt)."); 1.287 + this.emit(Connection.Events.TIMEOUT); 1.288 + this.disconnect(); 1.289 + }, 1.290 +} 1.291 + 1.292 +exports.ConnectionManager = ConnectionManager; 1.293 +exports.Connection = Connection; 1.294 +