Click here to Skip to main content
15,881,882 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 356.6K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
var BaseCommand = require('./base_command').BaseCommand,
  inherits = require('util').inherits,
  binaryutils = require('../utils');

/**
  Insert Document Command
**/
var KillCursorCommand = exports.KillCursorCommand = function(db, cursorIds) {
  BaseCommand.call(this);

  this.cursorIds = cursorIds;
  this.db = db;
};

inherits(KillCursorCommand, BaseCommand);

KillCursorCommand.OP_KILL_CURSORS = 2007;

/*
struct {
    MsgHeader header;                 // standard message header
    int32     ZERO;                   // 0 - reserved for future use
    int32     numberOfCursorIDs;      // number of cursorIDs in message
    int64[]   cursorIDs;                // array of cursorIDs to close
}
*/
KillCursorCommand.prototype.toBinary = function() {
  // Calculate total length of the document
  var totalLengthOfCommand = 4 + 4 + (4 * 4) + (this.cursorIds.length * 8);
  // Let's build the single pass buffer command
  var _index = 0;
  var _command = new Buffer(totalLengthOfCommand);
  // Write the header information to the buffer
  _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;     
  _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
  _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
  _command[_index] = totalLengthOfCommand & 0xff;
  // Adjust index
  _index = _index + 4;
  // Write the request ID
  _command[_index + 3] = (this.requestId >> 24) & 0xff;     
  _command[_index + 2] = (this.requestId >> 16) & 0xff;
  _command[_index + 1] = (this.requestId >> 8) & 0xff;
  _command[_index] = this.requestId & 0xff;
  // Adjust index
  _index = _index + 4;
  // Write zero
  _command[_index++] = 0;
  _command[_index++] = 0;
  _command[_index++] = 0;
  _command[_index++] = 0;
  // Write the op_code for the command
  _command[_index + 3] = (KillCursorCommand.OP_KILL_CURSORS >> 24) & 0xff;     
  _command[_index + 2] = (KillCursorCommand.OP_KILL_CURSORS >> 16) & 0xff;
  _command[_index + 1] = (KillCursorCommand.OP_KILL_CURSORS >> 8) & 0xff;
  _command[_index] = KillCursorCommand.OP_KILL_CURSORS & 0xff;
  // Adjust index
  _index = _index + 4;

  // Write zero
  _command[_index++] = 0;
  _command[_index++] = 0;
  _command[_index++] = 0;
  _command[_index++] = 0;

  // Number of cursors to kill
  var numberOfCursors = this.cursorIds.length;
  _command[_index + 3] = (numberOfCursors >> 24) & 0xff;     
  _command[_index + 2] = (numberOfCursors >> 16) & 0xff;
  _command[_index + 1] = (numberOfCursors >> 8) & 0xff;
  _command[_index] = numberOfCursors & 0xff;
  // Adjust index
  _index = _index + 4;

  // Encode all the cursors
  for(var i = 0; i < this.cursorIds.length; i++) {
    // Encode the cursor id
    var low_bits = this.cursorIds[i].getLowBits();
    // Encode low bits
    _command[_index + 3] = (low_bits >> 24) & 0xff;     
    _command[_index + 2] = (low_bits >> 16) & 0xff;
    _command[_index + 1] = (low_bits >> 8) & 0xff;
    _command[_index] = low_bits & 0xff;
    // Adjust index
    _index = _index + 4;
      
    var high_bits = this.cursorIds[i].getHighBits();
    // Encode high bits
    _command[_index + 3] = (high_bits >> 24) & 0xff;     
    _command[_index + 2] = (high_bits >> 16) & 0xff;
    _command[_index + 1] = (high_bits >> 8) & 0xff;
    _command[_index] = high_bits & 0xff;
    // Adjust index
    _index = _index + 4;
  }
  
  return _command;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions