Code coverage report for src/connection.js

Statements: 100% (32 / 32)      Branches: 100% (10 / 10)      Functions: 100% (5 / 5)      Lines: 100% (32 / 32)      Ignored: none     

All files » src/ » connection.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 861                                           1 140   140 140 140 140 140 140   140 140       140 102       1           1 3               1 1 1 2 1                   1 8 8 8 8 8 10 6 6     8     1
var Bitstream = require('./bitstream');
 
/**
 * Represents a connection to another Wasabi instance.
 *
 * Connections are sent objects according to their visibility groups, which can
 * be set using `addGroup` and `removeGroup`, and created with
 * `Wasabi.createGroup`. Connections start with no visibility groups. A
 * connection with zero visibility groups will be sent **all** objects known to
 * its Wasabi instance. If you want to send **no** objects to a connection, add
 * an empty group to it.
 *
 * @class Connection
 *
 * @constructor
 * @param {Socket} socket The socket.io socket used to communicate with the
 *     remote host
 * @param {Boolean} ghostFrom Accept ghosted NetObject from this client
 * @param {Boolean} ghostTo Ghost NetObjects to this client
 * @private
 */
 
function Connection(socket, ghostFrom, ghostTo) {
    var receiveBitstream = new Bitstream();
 
    this._socket = socket;
    this._rpcQueue = [];
    this._sendBitstream = new Bitstream();
    this._receiveBitstream = receiveBitstream;
    this._ghostFrom = ghostFrom || false;
    this._ghostTo = ghostTo || false;
 
    this._visibleObjects = {};
    this._groups = [];
 
    // configure socket to dump received data into the receive bitstream
    // currently assumes that it receives a socket.io socket
    socket.onmessage = function (data) {
        receiveBitstream.appendChars(data.data || data);
    };
}
 
Connection.prototype = {};
/**
 * Add a group to this connection
 * @method addGroup
 * @param {Group} group The group to add
 */
Connection.prototype.addGroup = function (group) {
    this._groups.push(group);
};
 
/**
 * Remove a group from this connection
 * @method removeGroup
 * @param {Group|Number} group The group or index to remove
 */
Connection.prototype.removeGroup = function (group) {
    var i;
    for (i = this._groups.length - 1; i >= 0; i--) {
        if (group === i || this._groups[i] === group) {
            this._groups.splice(i, 1);
        }
    }
};
 
/**
 * Get a collection of all objects in the connection's groups
 * @method getObjectsInGroups
 * @return {Object} A hash of serial numbers -> objects
 */
Connection.prototype.getObjectsInGroups = function () {
    var i;
    var j;
    var obj;
    var result = {};
    for (i = 0; i < this._groups.length; i++) {
        for (j = 0; j < this._groups[i]._objects.length; j++) {
            obj = this._groups[i]._objects[j];
            result[obj.wsbSerialNumber] = obj;
        }
    }
    return result;
};
 
module.exports = Connection;