Skip to content

Commit

Permalink
Worker app pretty much complete
Browse files Browse the repository at this point in the history
  • Loading branch information
robrighter committed May 26, 2010
1 parent c8f2a2f commit 560b9c0
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions worker.js
@@ -1,5 +1,4 @@
var sys = require('sys'); var sys = require('sys');
var fs = require('fs');
var noderouter = require('./lib/node-router'); var noderouter = require('./lib/node-router');
var querystring = require('querystring'); var querystring = require('querystring');
var lpb = require('./lib/longpollingbuffer'); var lpb = require('./lib/longpollingbuffer');
Expand All @@ -8,33 +7,56 @@ var server = noderouter.getServer();
var url = require('url'); var url = require('url');


var validhash = '.[0-9A-Za-z_\-]*'; var validhash = '.[0-9A-Za-z_\-]*';
var buffersize = 15; var buffersize = 40;
var feeds = {}; var feeds = {};




//Create a feed //Create a feed
server.get("__create", function (req, res, match) { server.post("/__create", function (req, res, poststring) {
res.sendHeader(200,{"Content-Type": "application/json"});
//grab the hash from the query string //grab the hash from the query string
var hash; postvals = querystring.parse(poststring);
if(url.parse(req.url,true).hasOwnProperty('query') && url.parse(req.url,true).query.hasOwnProperty('hash')){ if(postvals.hasOwnProperty('hash')){
hash = parseInt(url.parse(req.url,true)['query']['hash']); feeds[postvals.hash] = new lpb.LongPollingBuffer(buffersize);
feeds[hash] = new lpb.LongPollingBuffer(buffersize); res.write('{"status" : "success", "feed" : "'+postvals.hash+'" }' );
return {status : "success"}
} }
else { else {
return {status:'error', message:'must provide a hash for the feed to be created'}; res.write("{'status':'error', 'message':'must provide a hash for the feed to be created'}" );
} }
}); res.end();
}, "form-url-encode");


//insert data into a feed
server.post(new RegExp("^/insert/("+validhash+")$"), function(req,res,hash,poststring){

sys.puts("hash = " + hash);
sys.puts("postJSON = " + sys.inspect(querystring.parse(poststring)));

res.sendHeader(200,{"Content-Type": "application/json"});
//if the feed exists go ahead and insert the item into the buffer
if(feeds.hasOwnProperty(hash)){
toinsert = cleaninsert(querystring.parse(poststring));
buffer = feeds[hash].push(toinsert);
res.write("{'status':'success', 'inserted': "+JSON.stringify(toinsert)+"}");
}
else{
res.write("{'status':'error', 'message':'invalid feed identifier'}");
}
res.end();

}, "form-url-encode");



//Get updates on a feed //Get updates on a feed
server.get(new RegExp("^/latest/("+validhash+")$"), function (req, res, match) { server.get(new RegExp("^/latest/("+validhash+")$"), function (req, res, hash) {


//first check to verify that the feed exists //first check to verify that the feed exists
if(!feeds.hasOwnProperty(match)){ if(!feeds.hasOwnProperty(hash)){
return {status:'error', message:'invalid feed identifier'}; return {status:'error', message:'invalid feed identifier'};
} }


buffer = feeds[match]; buffer = feeds[hash];


var thesince; var thesince;
if(url.parse(req.url,true).hasOwnProperty('query') && url.parse(req.url,true).query.hasOwnProperty('since')){ if(url.parse(req.url,true).hasOwnProperty('query') && url.parse(req.url,true).query.hasOwnProperty('since')){
Expand All @@ -52,24 +74,10 @@ server.get(new RegExp("^/latest/("+validhash+")$"), function (req, res, match) {
}); });
}); });


//insert data into a feed
server.post(new RegExp("^/insert/("+validhash+")$"), function(req,res,match){

//first check to verify that the feed exists
if(!feeds.hasOwnProperty(match)){
return {error:'true', message:'invalid feed identifier'};
}

///SOMETHING WRONG HERE WITH THE DEFINISHION OF MATCH THIS NEEDS TO BE FIXED
toinsert = cleanit(querystring.parse(match));

buffer = feeds[match];
sys.puts("ABOUT TO INSERT THE FOLLOWING RECORD:\n" + JSON.stringify(toinsert));
buffer.push(toinsert);
}, "form-url-encode");





var cleaninsert = function(toinsert){
//this is where we trim down the item to be within the terms of use
return toinsert;
}


server.listen(8080); server.listen(8081);

0 comments on commit 560b9c0

Please sign in to comment.