Permalink
Browse files

minimalist sinatra restmq

  • Loading branch information...
gleicon committed Aug 13, 2010
1 parent e8bf269 commit ceaf035947d868d9c242528b1d834e796990a0e5
Showing with 36 additions and 66 deletions.
  1. +0 −65 node/frontend.js
  2. +0 −1 node/redis-node-client
  3. +36 −0 ruby/restmq.rb
View
@@ -1,65 +0,0 @@
-// RestMQ frontend for node.js
-// provides comet and websockets route
-// it doesnt polls as the original python version (due to internal hacking)
-// ideas: use pub/sub to speed new message handling
-// websockets !
-// as there is no common proxy for ws and comet handling, a remote node.js node would help distributing load, security
-// and of course, node.js is cool :D
-// requires: http://github.com/fictorial/redis-node-client
-// redis + node.js
-//
-
-
-var path = require("path");
-var url = require("url");
-var sys = require("sys");
-var http = require("http");
-var connections={};
-var redis=require("./redis-node-client/lib/redis-client").createClient();
-
-client.info(function (err, info) {
- if (err) throw new Error(err);
- sys.puts("Redis Version is: " + info.redis_version);
- client.close();
-});
-
-
-function hasProperties(o) { for (var p in o) if (o.hasOwnProperty(p)) return true; return false; }
-
-function update() {
- if (hasProperties(connections)) {
- for (var c in connections) {
- sys.puts(connections[c].length);
- for (var q in connections[c]) {
- connections[c][q].sendBody('oi');
- }
- }
- }
- setTimeout(update, 1000);
-}
-
-setTimeout(update, 1000);
-
-function addQueue(q, r) {
- if (connections[q] == null) {
- connections[q] = [];
- }
- connections[q].push(r);
-}
-
-
-http.createServer(function(req, res) {
- r = url.parse(req.url);
- queue = path.basename(r.pathname);
- if (path.dirname(r.pathname) == '/c') {
- req.connection.setTimeout(0); // endless comet
- res.sendHeader(200, {'Content-type':'text/plain'});
- addQueue(queue, res);
- } else {
- res.sendHeader(404, {'Content-type':'text/plain'});
- res.sendBody('not found');
- res.finish();
- }
- }).listen(8888);
-
-
Submodule redis-node-client deleted from 9c5ea3
View
@@ -0,0 +1,36 @@
+# Sinatra minimalist RestMQ
+# no COMET, just /q/ routes and queue logic
+# the core of RestMQ is how it uses Redis' data types
+
+require 'rubygems'
+require 'sinatra'
+require 'redis'
+
+QUEUESET = 'QUEUESET' # queue index
+UUID_SUFFIX = ':UUID' # queue unique id
+QUEUE_SUFFIX = ':queue' # suffix to identify each queue's LIST
+
+reds = Redis.new
+
+get '/q/*' do
+ queue = params['splat'].to_s
+ throw :halt, [404, 'Not found\n'] if queue == nil
+ queue = queue + QUEUE_SUFFIX
+ b = reds.rpop queue
+ throw :halt, [404, 'Not found (empty queue)\n'] if b == nil
+ v = reds.get b
+ "{'value':" + v + "'key':" + b + "}"
+end
+
+post '/q/*' do
+ queue = params['splat'].to_s
+ value = params['value'].to_s
+ throw :halt, [404, "Not found"] if queue == nil
+ q1 = queue + QUEUE_SUFFIX
+ uuid = reds.incr queue + UUID_SUFFIX
+ reds.sadd QUEUESET, q1
+ lkey = queue + ':' + uuid.to_s
+ reds.set lkey, value
+ reds.lpush q1, lkey
+ '{ok, '+lkey+'}'
+end

0 comments on commit ceaf035

Please sign in to comment.