Permalink
Browse files

V0.3

  • Loading branch information...
1 parent c8a4197 commit ac0fd4b55283c146ab85fa9b241eed72fe5b6cce @mrohad committed Aug 19, 2010
View
15 WWW/counter/applicationLogic.js
@@ -0,0 +1,15 @@
+(function(log,lib,application,request,responseHead,commands,session) {commands.writeEscapedText("");
+ var counter = 1;
+ application.get("counter",function(value){
+ log.debug("ApplicationLOGIC.JSSP, value - " +value);
+ if(value == undefined){
+ application.set("counter",1);
+ }else{
+ counter = value+1;
+ application.set("counter",counter);
+ }
+ request.parameters.counter = counter;
+ commands.forward("counter/view.jssp");
+ });
+
+})
View
14 WWW/counter/applicationLogic.jssp
@@ -0,0 +1,14 @@
+<?
+ var counter = 1;
+ application.get("counter",function(value){
+ log.debug("ApplicationLOGIC.JSSP, value - " +value);
+ if(value == undefined){
+ application.set("counter",1);
+ }else{
+ counter = value+1;
+ application.set("counter",counter);
+ }
+ request.parameters.counter = counter;
+ commands.forward("counter/view.jssp");
+ });
+?>
View
15 WWW/counter/logic.js
@@ -0,0 +1,15 @@
+(function(log,lib,application,request,responseHead,commands,session) {commands.writeEscapedText("");
+ var counter = 1;
+ application.get("counter",function(value){
+ log.debug("COUNTER.JSSP, value - " +value);
+ if(value == undefined){
+ application.set("counter",1);
+ }else{
+ counter = value+1;
+ application.set("counter",counter);
+ }
+ request.parameters.counter = counter;
+ commands.forward("counter/view.jssp");
+ });
+
+})
View
15 WWW/counter/sessionLogic.js
@@ -0,0 +1,15 @@
+(function(log,lib,application,request,responseHead,commands,session) {commands.writeEscapedText("");
+ var counter = 1;
+ session.get("counter",function(value){
+ log.debug("SESSIONLOGIC.JSSP, value - " +value);
+ if(value == undefined){
+ session.set("counter",1);
+ }else{
+ counter = value+1;
+ session.set("counter",counter);
+ }
+ request.parameters.counter = counter;
+ commands.forward("counter/view.jssp");
+ });
+
+})
View
14 WWW/counter/sessionLogic.jssp
@@ -0,0 +1,14 @@
+<?
+ var counter = 1;
+ session.get("counter",function(value){
+ log.debug("SESSIONLOGIC.JSSP, value - " +value);
+ if(value == undefined){
+ session.set("counter",1);
+ }else{
+ counter = value+1;
+ session.set("counter",counter);
+ }
+ request.parameters.counter = counter;
+ commands.forward("counter/view.jssp");
+ });
+?>
View
13 WWW/counter/view.js
@@ -0,0 +1,13 @@
+(function(log,lib,application,request,responseHead,commands,session) {commands.writeEscapedText("%3CHTML%3E%0A");
+commands.writeEscapedText("%09%3CHEAD%3E%3CTITLE%3EApplication%20Scope%20Counter%20Tester%3C/TITLE%3E%3C/HEAD%3E%0A");
+commands.writeEscapedText("%09%3CBODY%3E%0A");
+commands.writeEscapedText("%09");
+ var counter = request.parameters.counter;
+ if(counter==1)
+ commands.write("First Time");
+ else
+ commands.write("Number of users:" + counter);
+
+commands.writeEscapedText("%09%3C/BODY%3E%0A");
+commands.writeEscapedText("%3C/HTML%3E%0A");
+})
View
12 WWW/counter/view.jssp
@@ -0,0 +1,12 @@
+<HTML>
+ <HEAD><TITLE>Application Scope Counter Tester</TITLE></HEAD>
+ <BODY>
+ <?
+ var counter = request.parameters.counter;
+ if(counter==1)
+ commands.write("First Time");
+ else
+ commands.write("Number of users:" + counter);
+ ?>
+ </BODY>
+</HTML>
View
10 lib/application.js
@@ -75,24 +75,24 @@ function warpCallBackWithAsemaphore(cb,asemaphore){
};
}
}
-exports.getManager = function(currentAsemaphore){
+exports.getManager = function(seprator,currentAsemaphore){
return {
get : function(key,cb){
if(instance==undefined)
throw "You have to call start() first";
if(cb!=undefined)
- instance.get("", key, asemaphore.warpCallBack(currentAsemaphore,cb));
+ instance.get(seprator, key, asemaphore.warpCallBack(currentAsemaphore,cb));
else
- instance.get("", key, undefined);
+ instance.get(seprator, key, undefined);
},
set : function(key,value,cb){
if(instance==undefined)
throw "You have to call start() first";
if(cb!=undefined)
- instance.set("", key, value, asemaphore.warpCallBack(currentAsemaphore,cb));
+ instance.set(seprator, key, value, asemaphore.warpCallBack(currentAsemaphore,cb));
else
- instance.set("", key, value, undefined);
+ instance.set(seprator, key, value, undefined);
},
remove : function(key,cb){
View
4 lib/asemaphore.js
@@ -1,3 +1,4 @@
+//Async semaphore
var log = require('./log');
function asemaphore(counter,fireFunc){
@@ -32,9 +33,12 @@ exports.warpCallBack = function(asemaphore,cb){
if(cb == undefined)
return undefined;
else{
+ log.debug("A"+cb);
asemaphore.v();
return function(){
+ log.debug("ARRA "+Array.prototype.slice.call(arguments, 0));
cb.apply(this, Array.prototype.slice.call(arguments, 0));
+ log.debug("B");
asemaphore.p();
};
}
View
31 lib/jssp.js
@@ -87,7 +87,7 @@ exports.start = function(settings) {
console.log('Server running at port '+globalSettings.port);
};
-function handleRequest(req,res,cleanPathname){
+function handleRequest(req,res,cleanPathname,newSessionId){
var root = globalSettings.path.root;
var path = pathlib.join(root, cleanPathname);
log.info("Handling request to: " +path + " pid("+process.pid+")");
@@ -108,7 +108,10 @@ function handleRequest(req,res,cleanPathname){
path.join(path, "index.html");
var cookie = req.headers["cookie"];
var sessionId = utils.getSessionId(cookie);
- sessionScope.hello(sessionId);
+ if(newSessionId!=undefined)//forward
+ sessionId = newSessionId;
+ else
+ sessionScope.hello(sessionId);
if(!path.endsWith("."+globalSettings.server_script.ext)){
sendHeaders(req, res,undefined, stats.size, mime.mimeType(path), stats.mtime);
var readStream = fs.createReadStream(path);
@@ -185,22 +188,13 @@ function serverSideRunning(newfileName,request,response,file,lastMod,sessionId){
afterEval.push(unescape(text));
},
forward :function(resource){
- handleRequest(request,response,resource);
+ handleRequest(request,response,resource,result.sessionId);
allGood = false;
},
sendRedirect:function(url){
responseHead.status = 301;
responseHead.headers["location"] = url;
}
-// runToNextWaitPoint : function(){
-// currentAsemaphore.v();
-// },
-// waitPoint : function(){
-// currentAsemaphore.p();
-// },
-// addFunc : function(func){
-// currentAsemaphore.addFunction(func);
-// }
};
//Waiting for a bug fix(V8)
/*try{
@@ -233,14 +227,14 @@ function serverSideRunning(newfileName,request,response,file,lastMod,sessionId){
if (errRead) throw errRead;
//Handling session and application with asemphore
result.sessionId = sessionId;
- var application = applicationManager.getManager(currentAsemaphore);
- log.debug(application);
- var session = sessionManager.getManager(sessionId,currentAsemaphore,application);
+ var application = applicationManager.getManager("",currentAsemaphore);
+ var session = sessionManager.getManager(sessionId,currentAsemaphore,applicationManager,result);
//-----------
command = ["functoRun = ",data.toString()].join("");
eval(command);
functoRun(log,lib,application,request,responseHead,commands,session);
currentAsemaphore.p();
+ log.debug("sessionId - "+result.sessionId);
}catch(err){
log.warn("parse problem:"+err);
allGood = true;//yes all is good!!! :(
@@ -252,8 +246,8 @@ function serverSideRunning(newfileName,request,response,file,lastMod,sessionId){
result.html += erStr+err.stack;
}
else
- result.html += "Could not parse jssp";
- currentAsemaphore.p();
+ result.html += "Could not parse jssp";
+ while(currentAsemaphore.p()>0);
}
});
}
@@ -327,6 +321,7 @@ function serverSideProcessing(str,request,response,file,lastMod,sessionId){
}
function sendHeaders(req, res,responseHead, length, content_type, modified_time,sessionId) {
+ log.debug("send headers: sessionId - "+sessionId);
if(responseHead==undefined)
responseHead = new Object();
if(responseHead.status==undefined)
@@ -346,7 +341,7 @@ function sendHeaders(req, res,responseHead, length, content_type, modified_time,
responseHead.headers["Content-Type"] = content_type || "application/octet-stream";
if (modified_time)
responseHead.headers["Last-Modified"] = modified_time.toUTCString();
- //log.debug("RESPONSE Headers :"+utils.arrayToString(responseHead.headers)+" +++ RESPONSE Status :"+responseHead.status);
+ log.debug("RESPONSE Headers :"+utils.arrayToString(responseHead.headers)+" +++ RESPONSE Status :"+responseHead.status);
res.writeHead(responseHead.status, responseHead.headers);
log.info(req.connection.remoteAddress,req.method,responseHead.status,length);
}
View
67 lib/session.js
@@ -11,7 +11,7 @@ function createUUID() {
var uuid = s.join("");
return uuid;
}
-var seprator = "[ssn]";//make sure web app can't contain the char']'
+
function sessionScope(sessionTimeOutMinutes,webAppName){
this.webAppName = webAppName;
@@ -23,19 +23,19 @@ function sessionScope(sessionTimeOutMinutes,webAppName){
this.hello = function(uid){
if(uid != undefined && this.timers[this.webAppName+uid] != undefined){
clearTimeout(this.timers[this.webAppName+uid]);
- this.timers[webAppName+uid] = this.setSessionTimeout(uid);
+ this.timers[this.webAppName+uid] = this.setSessionTimeout(uid);
}
};
- this.setSessionTimeout = function(uid){//adding *minutes* to the uid timeout
- return setTimeout((function(sid){
+ this.setSessionTimeout = function(application,uid){//adding *minutes* to the uid timeout
+ return setTimeout((function(application,sid){
if(sid!=undefined){
- this.remove(sid);
+ this.remove(application,sid);
this.timers[this.webAppName+sid] = undefined;
log.info("Clearing session: "+this.webAppName+sid);
}
})
- ,this.numberOfMili,uid);
+ ,this.numberOfMili,application,uid);
};
this.remove = function(application,nuid,cb){
@@ -44,44 +44,48 @@ function sessionScope(sessionTimeOutMinutes,webAppName){
this.saveObjectInSession =function(application,key,value,uid,obj,cb){
obj[key] = value;
- application.set(seprator,uid,obj,cb);
+ application.set(uid,obj,cb);
};
this.set = function(application,key,value,uid,cb){
log.debug("sessionMap.set: key-"+key+" value-" +value+" uid-"+uid);
var cont = true;
if(uid==undefined){
uid = createUUID();
- application.get(seprator,uid,function(result){
- if(result!=undefined)
- this.set(key, value, undefined, cb);
- else{
+ //application.get(seprator,uid,function(result){
+ //if(result!=undefined)
+ // this.set(key, value, undefined, cb);
+ //else{
log.debug("New session UID:"+uid);
- this.setSessionTimeout(uid);
+ this.setSessionTimeout(application,uid);
this.saveObjectInSession(application,key,value,uid,new Object(),cb);
- }
- });
+ //}
+ //});
}else{
- application.get(seprator,uid,function(result){
+ var saveFunc = this.saveObjectInSession;
+ application.get(uid,function(result){
if(result!=undefined)
- this.saveObjectInSession(application,key,value,uid,result,cb);
+ saveFunc(application,key,value,uid,result,cb);
else
- throw "InvalidSession";
+ saveFunc(application,key,value,uid,new Object(),cb);
});
}
+ log.debug("sessionMap.set: return uid-"+uid);
+ return uid;
};
this.get = function(application,key,uid,cb){
log.debug("sessionMap.get: key-"+key+" uid-"+uid);
if(uid==undefined)
- throw "InvalidSession";
+ cb(undefined);
else{
- application.get(seprator,uid,function(result){
- if(result!=undefined)
- cb(result[key]);
- else
- throw "InvalidSession";
+ application.get(uid,function(obj){
+ if(obj!=undefined){
+ log.debug("appGet@Session - "+obj);
+ cb(obj[key]);
+ }else
+ cb(undefined);
});
}
};
@@ -94,25 +98,28 @@ exports.start = function(sessionTimeOutMinutes,webAppName){
return instance;
};
-
-exports.getManager = function(uid,currentAsemaphore,application){
+exports.getManager = function(uid,currentAsemaphore,applicationManager,result){
+ var application = applicationManager.getManager("ssn-",currentAsemaphore);
return {
get : function(key,cb){
if(instance==undefined)
throw "You have to call start() first";
- if(cb!=undefined)
+ if(cb!=undefined){
+ //log.debug("CB"+cb);
instance.get(application,key, uid, asemaphore.warpCallBack(currentAsemaphore,cb));
- else
+ }else
instance.get(application,key, uid, undefined);
},
set : function(key,value,cb){
if(instance==undefined)
throw "You have to call start() first";
if(cb!=undefined)
- instance.set(application,key, value, uid, asemaphore.warpCallBack(currentAsemaphore,cb));
- else
- instance.set(application,key, value, uid, undefined);
+ result.sessionId = instance.set(application,key, value, uid, asemaphore.warpCallBack(currentAsemaphore,cb));
+ else{
+ result.sessionId = instance.set(application,key, value, uid, undefined);
+ log.debug("sessionManager.set: result.sessionId - "+result.sessionId);
+ }
}
};
};
View
4 settings.json
@@ -12,9 +12,9 @@
"end":"?>",
"session_minutes":1,
"memcached":{
- "enable":1,
+ "enable":0,
"server":"localhost",
- "port":11212
+ "port":11211
}
},
"debug_mode" : 1,

0 comments on commit ac0fd4b

Please sign in to comment.