Marionette JS Client

API Docs for: 1.7.1
Show:

File: lib/marionette/drivers/moz-tcp.js

  1. (function(module, ns) {

  2.   try {
  3.     if (!window.navigator.mozTCPSocket) {
  4.       return;
  5.     }
  6.   } catch(e) {
  7.     return;
  8.   }

  9.   var TCPSocket = navigator.mozTCPSocket;

  10.   var Responder = ns.require('responder');
  11.   var ON_REGEX = /^on/;

  12.  /**
  13.    * Horrible hack to work around
  14.    * missing stuff in TCPSocket & add
  15.    * node compatible api.
  16.    */
  17.   function SocketWrapper(host, port, options) {
  18.     var events = new Responder();
  19.     var eventMethods = [
  20.       'on',
  21.       'addEventListener',
  22.       'removeEventListener',
  23.       'once',
  24.       'emit'
  25.     ];

  26.     var rawSocket = TCPSocket.open(host, port, options);

  27.     var eventList = [
  28.       'onopen',
  29.       'ondrain',
  30.       'ondata',
  31.       'onerror',
  32.       'onclose'
  33.     ];

  34.     eventList.forEach(function(method) {
  35.       rawSocket[method] = function(method, data) {
  36.         var emitData;
  37.         if ('data' in data) {
  38.           emitData = data.data;
  39.         } else {
  40.           emitData = data;
  41.         }
  42.         events.emit(method, emitData);
  43.       }.bind(socket, method.substr(2));
  44.     });

  45.     var socket = Object.create(rawSocket);

  46.     eventMethods.forEach(function(method) {
  47.       socket[method] = events[method].bind(events);
  48.     });

  49.     return socket;
  50.   }

  51.   var Abstract, CommandStream, Responder;

  52.   Abstract = ns.require('drivers/abstract');
  53.   CommandStream = ns.require('command-stream');

  54.   /** TCP **/
  55.   Tcp.Socket = SocketWrapper;

  56.   /**
  57.    * Connects to gecko marionette server using mozTCP api.
  58.    *
  59.    *
  60.    *     // default options are fine for b2g-desktop
  61.    *     // or a device device /w port forwarding.
  62.    *     var tcp = new Marionette.Drivers.MozTcp();
  63.    *
  64.    *     tcp.connect(function() {
  65.    *       // ready to use with client
  66.    *     });
  67.    *
  68.    *
  69.    * @class Marionette.Drivers.MozTcp
  70.    * @extends Marionette.Drivers.Abstract
  71.    * @constructor
  72.    * @param {Object} options connection options.
  73.    *   @param {String} [options.host="127.0.0.1"] ip/host.
  74.    *   @param {Numeric} [options.port="2828"] marionette server port.
  75.    */
  76.   function Tcp(options) {
  77.     if (typeof(options)) {
  78.       options = {};
  79.     }
  80.     Abstract.call(this, options);


  81.     this.connectionId = 0;
  82.     this.host = options.host || '127.0.0.1';
  83.     this.port = options.port || 2828;
  84.   }

  85.   Tcp.prototype = Object.create(Abstract.prototype);

  86.   /**
  87.    * Sends a command to the server.
  88.    *
  89.    * @param {Object} cmd remote marionette command.
  90.    */
  91.   Tcp.prototype._sendCommand = function _sendCommand(cmd) {
  92.     this.client.send(cmd);
  93.   };

  94.   /**
  95.    * Opens TCP socket for marionette client.
  96.    */
  97.   Tcp.prototype._connect = function connect() {
  98.     var client, self = this;

  99.     this.socket = new Tcp.Socket(this.host, this.port);
  100.     client = this.client = new CommandStream(this.socket);
  101.     this.client.on('command', this._onClientCommand.bind(this));
  102.   };

  103.   /**
  104.    * Receives command from server.
  105.    *
  106.    * @param {Object} data response from marionette server.
  107.    */
  108.   Tcp.prototype._onClientCommand = function(data) {
  109.     this._onDeviceResponse({
  110.       id: this.connectionId,
  111.       response: data
  112.     });
  113.   };

  114.   /**
  115.    * Closes connection to marionette.
  116.    */
  117.   Tcp.prototype._close = function close() {
  118.     if (this.socket && this.socket.close) {
  119.       this.socket.close();
  120.     }
  121.   };

  122.   /** export */
  123.   module.exports = exports = Tcp;

  124. }.apply(
  125.   this,
  126.   (this.Marionette) ?
  127.     [Marionette('drivers/moz-tcp'), Marionette] :
  128.     [module, require('../../lib/marionette/marionette')]
  129. ));

  130.