From 03ea39074f69b046aa9e84897dc292f9db2ef578 Mon Sep 17 00:00:00 2001 From: Bulat Shakirzyanov Date: Mon, 20 Feb 2012 15:47:09 -0800 Subject: [PATCH] adding setup instructions --- .gitignore | 1 + README.md | 20 +++++ demos/requirements.txt | 10 +++ dist/nullmq.js | 164 ++++++++++++++++++++++++----------------- package.json | 7 ++ requirements.txt | 2 + 6 files changed, 136 insertions(+), 68 deletions(-) create mode 100644 README.md create mode 100644 demos/requirements.txt create mode 100644 package.json create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index e1e1099..ce0c144 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ dist/test +node_modules/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9016472 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# NullMQ + +ZeroMQ in da browser, yo! + +## Installation + +* `git clone ...` +* `cat requirements.txt | xargs npm install -g` +* `cake build` + +### Testing + +* `cake test` + +### Demos + +* `pip install -r demos/requirements.txt` +* + +### \ No newline at end of file diff --git a/demos/requirements.txt b/demos/requirements.txt new file mode 100644 index 0000000..75da935 --- /dev/null +++ b/demos/requirements.txt @@ -0,0 +1,10 @@ +git+git://github.com/progrium/stomp4py.git#egg=stomp4py +git+git://github.com/progrium/WebSocket-for-Python#egg=ws4py +git+git://github.com/progrium/gservice.git#egg=gservice +gevent==0.13.3 +greenlet==0.3.1 +lockfile==0.9.1 +nose==1.1.2 +python-daemon==1.6 +setproctitle==1.1.2 +wsgiref==0.1.2 \ No newline at end of file diff --git a/dist/nullmq.js b/dist/nullmq.js index 1b2a56a..b96e342 100644 --- a/dist/nullmq.js +++ b/dist/nullmq.js @@ -1,11 +1,8 @@ (function() { - var Queue, Socket, Stomp, assert, nullmq; - var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __indexOf = Array.prototype.indexOf || function(item) { - for (var i = 0, l = this.length; i < l; i++) { - if (this[i] === item) return i; - } - return -1; - }; + var Queue, Socket, Stomp, assert, nullmq, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + nullmq = { PUB: 'pub', SUB: 'sub', @@ -20,52 +17,51 @@ HWM: 100, IDENTITY: 101, SUBSCRIBE: 102, - UNSUBSCRIBE: 103 + UNSUBSCRIBE: 103, + _SENDERS: ['req', 'dealer', 'push', 'pub', 'router', 'rep'] }; + assert = function(description, condition) { - if (condition == null) { - condition = false; - } - if (!condition) { - throw Error("Assertion: " + description); - } + if (condition == null) condition = false; + if (!condition) throw Error("Assertion: " + description); }; + Queue = (function() { + function Queue(maxsize) { this.maxsize = maxsize != null ? maxsize : null; this.queue = []; this.offset = 0; this.watches = []; } + Queue.prototype.getLength = function() { return this.queue.length - this.offset; }; + Queue.prototype.isEmpty = function() { return this.queue.length === 0; }; + Queue.prototype.isFull = function() { - if (this.maxsize === null) { - return false; - } + if (this.maxsize === null) return false; return this.getLength() >= this.maxsize; }; + Queue.prototype.put = function(item) { var _base; if (!this.isFull()) { this.queue.push(item); - if (typeof (_base = this.watches.shift()) === "function") { - _base(); - } + if (typeof (_base = this.watches.shift()) === "function") _base(); return item; } else { } }; + Queue.prototype.get = function() { var item; - if (this.queue.length === 0) { - return; - } + if (this.queue.length === 0) return; item = this.queue[this.offset]; if (++this.offset * 2 >= this.queue.length) { this.queue = this.queue.slice(this.offset); @@ -73,6 +69,7 @@ } return item; }; + Queue.prototype.peek = function() { if (this.queue.length > 0) { return this.queue[this.offset]; @@ -80,6 +77,7 @@ return; } }; + Queue.prototype.watch = function(fn) { if (this.queue.length === 0) { return this.watches.push(fn); @@ -87,47 +85,55 @@ return fn(); } }; + return Queue; + })(); + nullmq.Context = (function() { + function Context(url, onconnect) { + var _this = this; this.url = url; - if (onconnect == null) { - onconnect = function() {}; - } + if (onconnect == null) onconnect = function() {}; this.active = false; this.client = Stomp.client(this.url); - this.client.connect("guest", "guest", __bind(function() { + this.client.connect("guest", "guest", function() { var op, _results; - this.active = true; + _this.active = true; _results = []; - while (op = this.pending_operations.shift()) { + while (op = _this.pending_operations.shift()) { _results.push(op()); } return _results; - }, this)); + }); this.pending_operations = [onconnect]; this.sockets = []; } + Context.prototype.socket = function(type) { return new Socket(this, type); }; + Context.prototype.term = function() { - return this._when_connected(__bind(function() { + var _this = this; + return this._when_connected(function() { var socket, _i, _len, _ref; - assert("context is already connected", this.client.connected); - _ref = this.sockets; + assert("context is already connected", _this.client.connected); + _ref = _this.sockets; for (_i = 0, _len = _ref.length; _i < _len; _i++) { socket = _ref[_i]; socket.close(); } - return this.client.disconnect(); - }, this)); + return _this.client.disconnect(); + }); }; + Context.prototype._send = function(socket, destination, message) { - return this._when_connected(__bind(function() { + var _this = this; + return this._when_connected(function() { var headers, part, _i, _len; - assert("context is already connected", this.client.connected); + assert("context is already connected", _this.client.connected); headers = { 'socket': socket.type }; @@ -139,22 +145,24 @@ } if (message instanceof Array) { headers['transaction'] = Math.random() + ''; - this.client.begin(transaction); + _this.client.begin(transaction); for (_i = 0, _len = message.length; _i < _len; _i++) { part = message[_i]; - this.client.send(destination, headers, part); + _this.client.send(destination, headers, part); } - return this.client.commit(transaction); + return _this.client.commit(transaction); } else { - return this.client.send(destination, headers, message.toString()); + return _this.client.send(destination, headers, message.toString()); } - }, this)); + }); }; + Context.prototype._subscribe = function(type, socket, destination) { - return this._when_connected(__bind(function() { + var _this = this; + return this._when_connected(function() { var id; - assert("context is already connected", this.client.connected); - id = this.client.subscribe(destination, __bind(function(frame) { + assert("context is already connected", _this.client.connected); + id = _this.client.subscribe(destination, function(frame) { var envelope; envelope = { 'message': frame.body, @@ -164,19 +172,22 @@ envelope['reply_to'] = frame.headers['reply-to']; } return socket.recv_queue.put(envelope); - }, this), { + }, { 'socket': socket.type, 'type': type }); return socket.connections[destination] = id; - }, this)); + }); }; + Context.prototype._connect = function(socket, destination) { return this._subscribe('connect', socket, destination); }; + Context.prototype._bind = function(socket, destination) { return this._subscribe('bind', socket, destination); }; + Context.prototype._when_connected = function(op) { if (this.client.connected) { return op(); @@ -184,9 +195,13 @@ return this.pending_operations.push(op); } }; + return Context; + })(); + Socket = (function() { + function Socket(context, type) { var _ref; this.context = context; @@ -203,22 +218,21 @@ this.rr_index = 0; this.last_recv = void 0; this.context.sockets.push(this); - if ((_ref = this.type) === nullmq.REQ || _ref === nullmq.DEALER || _ref === nullmq.PUSH || _ref === nullmq.PUB || _ref === nullmq.ROUTER || _ref === nullmq.REP) { + if (_ref = this.type, __indexOf.call(nullmq._SENDERS, _ref) >= 0) { this.send_queue.watch(this._dispatch_outgoing); } } + Socket.prototype.connect = function(destination) { - if (__indexOf.call(Object.keys(this.connections), destination) >= 0) { - return; - } + if (__indexOf.call(Object.keys(this.connections), destination) >= 0) return; return this.context._connect(this, destination); }; + Socket.prototype.bind = function(destination) { - if (__indexOf.call(Object.keys(this.connections), destination) >= 0) { - return; - } + if (__indexOf.call(Object.keys(this.connections), destination) >= 0) return; return this.context._bind(this, destination); }; + Socket.prototype.setsockopt = function(option, value) { var _ref; switch (option) { @@ -229,17 +243,13 @@ case nullmq.LINGER: return this.linger = value; case nullmq.SUBSCRIBE: - if (this.type !== nullmq.SUB) { - return; - } + if (this.type !== nullmq.SUB) return; if (_ref = !value, __indexOf.call(this.filters, _ref) >= 0) { this.filters.push(value); } return value; case nullmq.UNSUBSCRIBE: - if (this.type !== nullmq.SUB) { - return; - } + if (this.type !== nullmq.SUB) return; if (__indexOf.call(this.filters, value) >= 0) { this.filters.splice(this.filters.indexOf(value), 1); } @@ -248,6 +258,7 @@ return; } }; + Socket.prototype.getsockopt = function(option) { switch (option) { case nullmq.HWM: @@ -260,6 +271,7 @@ return; } }; + Socket.prototype.close = function() { var destination, id, _ref; _ref = this.connections; @@ -270,6 +282,7 @@ this.connections = {}; return this.closed = true; }; + Socket.prototype.send = function(message) { var _ref; if ((_ref = this.type) === nullmq.PULL || _ref === nullmq.SUB) { @@ -277,28 +290,35 @@ } return this.send_queue.put(message); }; + Socket.prototype.recv = function(callback) { - return this.recv_queue.watch(__bind(function() { - return callback(this._recv()); - }, this)); + var _this = this; + return this.recv_queue.watch(function() { + return callback(_this._recv()); + }); }; + Socket.prototype.recvall = function(callback) { - var watcher; - watcher = __bind(function() { - callback(this._recv()); - return this.recv_queue.watch(watcher); - }, this); + var watcher, + _this = this; + watcher = function() { + callback(_this._recv()); + return _this.recv_queue.watch(watcher); + }; return this.recv_queue.watch(watcher); }; + Socket.prototype._recv = function() { var envelope; envelope = this.recv_queue.get(); this.last_recv = envelope; return envelope.message; }; + Socket.prototype._identity = function(value) { return this.identity = value; }; + Socket.prototype._deliver_round_robin = function(message) { var connection_count, destination; destination = Object.keys(this.connections)[this.rr_index]; @@ -306,6 +326,7 @@ connection_count = Object.keys(this.connections).length; return this.rr_index = ++this.rr_index % connection_count; }; + Socket.prototype._deliver_fanout = function(message) { var destination, id, _ref, _results; _ref = this.connections; @@ -316,14 +337,17 @@ } return _results; }; + Socket.prototype._deliver_routed = function(message) { var destination; destination = message.shift(); return this.context._send(this, destination, message); }; + Socket.prototype._deliver_back = function(message) { return this.context._send(this, this.last_recv.destination, message); }; + Socket.prototype._dispatch_outgoing = function() { var message; if (this.context.active) { @@ -351,8 +375,11 @@ return setTimeout(this._dispatch_outgoing, 20); } }; + return Socket; + })(); + if (typeof window !== "undefined" && window !== null) { window.nullmq = nullmq; if (!(window.Stomp != null)) { @@ -365,4 +392,5 @@ exports.Queue = Queue; Stomp = require('./lib/stomp.js').Stomp; } + }).call(this); diff --git a/package.json b/package.json new file mode 100644 index 0000000..37425b3 --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "name": "nullmq" + , "version": "0.1.0" + , "dependencies" : { + "jasmine-node": "*" + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..84962db --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +coffee-script +jasmine-node \ No newline at end of file