Fixed and working basic Cortex

This commit is contained in:
Dan Ballard 2011-01-06 08:46:26 -08:00
parent 1123dc8d0e
commit fa74d77158
4 changed files with 79 additions and 51 deletions

View File

@ -340,7 +340,7 @@ class ConnHandler implements Runnable {
userAgent = header; userAgent = header;
} }
System.out.println(i + ": '" + header + "'"); //System.out.println(i + ": '" + header + "'");
i++; i++;
header = sin.readLine(); header = sin.readLine();
} }
@ -353,6 +353,7 @@ class ConnHandler implements Runnable {
byte[] get_resp= null; byte[] get_resp= null;
String CODE = "200 OK"; String CODE = "200 OK";
int resp_length = 0; int resp_length = 0;
String content_type = "text/html";
if (type == TYPE_GET) { if (type == TYPE_GET) {
reqLine = reqLine.substring(4); reqLine = reqLine.substring(4);
res.log("GET " + reqLine); res.log("GET " + reqLine);
@ -361,6 +362,11 @@ class ConnHandler implements Runnable {
CODE = "404 Not Found"; CODE = "404 Not Found";
} }
resp_length = get_resp.length; resp_length = get_resp.length;
String end = reqLine.substring(reqLine.length()-4);
if (end == ".js")
content_type = "text/javascript";
else if (end == "css")
content_type= "text/css";
} else { } else {
// No forward // No forward
if (reqLine.equals("")) { if (reqLine.equals("")) {
@ -385,7 +391,7 @@ class ConnHandler implements Runnable {
SimpleDateFormat formater = new SimpleDateFormat("E, d M y H:m:s z"); SimpleDateFormat formater = new SimpleDateFormat("E, d M y H:m:s z");
sout.print("HTTP/1.1 " + CODE + "\r\n"+ sout.print("HTTP/1.1 " + CODE + "\r\n"+
"Date: " + formater.format(now) + "\r\n" + "Date: " + formater.format(now) + "\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n" + "Content-Type: " + content_type + "; charset=UTF-8\r\n" +
"Server: Cortex @ " + Integer.toString(res.PORT) + "\r\n"); "Server: Cortex @ " + Integer.toString(res.PORT) + "\r\n");
// Print response // Print response

View File

@ -248,26 +248,7 @@ function packResults(res) {
return str; return str;
} }
function packObject(m, defaultValue) {
var str = "";
for (var i in m) {
if (m[i] == '')
{
if (defaultValue == null) {
continue;
} else {
m[i] = defaultValue;
}
}
if (i == "results") {
str += i + "=" + packResults(m[i]) + "\n";
} else {
str += i + "=" + m[i] + "\n";
}
}
return str;
}
function removeNodeRangeLocks(addr) { function removeNodeRangeLocks(addr) {
for(var i in rangeLocks) { for(var i in rangeLocks) {

View File

@ -97,8 +97,7 @@ function extractHost(url) {
} }
//var logCheckElem = null; var logging = false
var logging = false;
function log(str) { function log(str) {
//if (logCheckElem == null) //if (logCheckElem == null)
@ -141,6 +140,7 @@ function ping() {
var retfn = returnfn(http, var retfn = returnfn(http,
function(resp) { function(resp) {
//log("pong!");
if (!connected) { if (!connected) {
connected = true; connected = true;
greet(); greet();
@ -149,17 +149,19 @@ function ping() {
function(resp) { function(resp) {
connected = false; connected = false;
var html = "Connection to local Node failed. Try reloading from <a href=\"" + originURL + "\" target=\"window\">" + originURL + "</a>"; var html = "Connection to local Node failed. Try reloading from <a href=\"" + originURL + "\" target=\"window\">" + originURL + "</a>";
stl = document.getElementById('statusLabel'); stl = document.getElementById('statusLabel');
if (stl) if (stl)
stl.innerHTML = html; stl.innerHTML = html;
} }
); );
//log("ping!");
ajaxSend(http, "cmd=ping\n\n", retfn); ajaxSend(http, "cmd=ping\n\n", retfn);
} }
function init() { function init() {
log("init");
if (connected == false) if (connected == false)
return; return;
numberOfNodes = 1; numberOfNodes = 1;
@ -183,6 +185,7 @@ function greet() {
var retfn = returnfn(http, var retfn = returnfn(http,
function(resp) { function(resp) {
log("greet returned");
var arr = resp.split(" "); var arr = resp.split(" ");
localNodeAddr = arr[0]; localNodeAddr = arr[0];
originURL = arr[1]; originURL = arr[1];
@ -192,6 +195,7 @@ function greet() {
init(); init();
} }
); );
log("send greet msg");
ajaxSend(http, "cmd=greet\n\n", retfn); ajaxSend(http, "cmd=greet\n\n", retfn);
} }
@ -250,12 +254,37 @@ function getMsgHandler(msgName) {
function processMsg(resp) { function processMsg(resp) {
handler = getMsgHandler(resp["query"]); handler = getMsgHandler(resp["query"]);
if (handler) { if (handler) {
handler(resp); i=0;
while(handler[i]) {
handler[i](resp);
i++;
}
} else { } else {
log("Error: Unkown message '" + resp["query"] + "' received from " + resp["origin"]); log("Error: Unkown message '" + resp["query"] + "' received from " + resp["origin"]);
} }
} }
function packObject(m, defaultValue) {
var str = "";
for (var i in m) {
if (m[i] == '')
{
if (defaultValue == null) {
continue;
} else {
m[i] = defaultValue;
}
}
if (i == "results") {
str += i + "=" + packResults(m[i]) + "\n";
} else {
str += i + "=" + m[i] + "\n";
}
}
return str;
}
function sendMsg(m, addr, time, mtype) { function sendMsg(m, addr, time, mtype) {
if(time == null) if(time == null)
time = dlc; time = dlc;
@ -270,7 +299,7 @@ function sendMsg(m, addr, time, mtype) {
str += packObject(m); str += packObject(m);
str += "\n"; str += "\n";
log ("SEND: " + str); //log ("SEND: " + str);
var http = ajaxConnect(); var http = ajaxConnect();
var retfn = returnfn(http, null, var retfn = returnfn(http, null,
@ -279,7 +308,7 @@ function sendMsg(m, addr, time, mtype) {
announceDead(addr); announceDead(addr);
log("ERROR> message to " + addr + " not delivered: '" + str + "'"); log("ERROR> message to " + addr + " not delivered: '" + str + "'");
}); });
//log("sendMsg: " + str);
ajaxSend(http, str, retfn, addr); ajaxSend(http, str, retfn, addr);
} }
@ -372,12 +401,16 @@ function getMsgs() {
var http = ajaxConnect(); var http = ajaxConnect();
var retfn = returnfn(http, var retfn = returnfn(http,
function(resp) { function(resp) {
log("getMsgs: " + resp); //log("getMsgs: " + resp);
queueMsgs(resp); queueMsgs(resp);
// Since getMsgs returns (a call back is called later) // Since getMsgs returns (a call back is called later)
// this is tail recursive friendly // this is tail recursive friendly
getMsgs(); getMsgs();
},
function() {
log("ERROR: getMsg RETURN FAIL");
}); });
log("getMsgs");
ajaxSend(http, "cmd=getMsgs\n\n", retfn); ajaxSend(http, "cmd=getMsgs\n\n", retfn);
} }
@ -549,10 +582,10 @@ function (resp) {
} }
}); });
addMessageHandler( "deadNode", addMsgHandler( "deadNode",
function (resp) { function (resp) {
var addr = resp['addr']; var addr = resp['addr'];
log("--------------DISCONNECT " + addr + "----------"); log("DISCONNECT " + addr);
// remove from connumberOfNodesnections list // remove from connumberOfNodesnections list
for (var i=0; i < connections.length; i++) { for (var i=0; i < connections.length; i++) {
if (connections[i] == addr) { if (connections[i] == addr) {
@ -563,12 +596,12 @@ function (resp) {
} }
}); });
addMessageHandler( "new_network", addMsgHandler( "new_network",
function (resp) { function (resp) {
join_network(resp["addr"], false); join_network(resp["addr"], false);
}); });
addMessageHandler( "heartBeat", addMsgHandler( "heartBeat",
function (resp) { function (resp) {
// do nothing // do nothing
}); });
@ -576,23 +609,23 @@ function (resp) {
/***** CRON System *****/ /***** CRON System *****/
var cronTabs = new Array(); var crontabs = new Array();
/* add a function to be called repeatedly /* add a function to be called repeatedly
* interval - in 1/10s of a second * interval - in 1/10s of a second
* fn - function to be called * fn - function to be called
*/ */
function addCronTab(interval, fn) { function addCronTab(interval, fn) {
tab = cronTabs[interval]; tab = crontabs[interval];
if (!tab) { if (!tab) {
crontabs[interval] = [fn, null]; crontabs[interval] = [fn, null];
} else { } else {
i = 0; i = 0;
while(crontabs[i]) { while(tab[i]) {
i++; i++;
} }
crontabs[i] = fn; tab[i] = fn;
crontabs[i+1]=null; tab[i+1]= null;
} }
} }
@ -602,24 +635,26 @@ function cron() {
if (connected == false) if (connected == false)
return; return;
for (time in cronTabs) { for (time in crontabs) {
i = Number(time); if (cronI % Number(time) == 0) {
if (cronI % i == 0) { tab = crontabs[time];
tab = cronTabs[time];
j=0; j=0;
while(tab[j]) { while(tab[j]) { // cannot read proprty '0' of null
tab[j](); tab[j]();
j++;
} }
} }
} }
cronI++;
cronI += 1;
// Reset to 0 after a day // Reset to 0 after a day
if (cronI > 86400) { if (cronI > 864000) {
cronI = 0; cronI = 0;
} }
}
addCronTab(10, ping); addCronTab(10, ping);
addCronTab(300, heatbeat); addCronTab(300, heartBeat);
/***** UI *****/ /***** UI *****/
@ -631,7 +666,7 @@ function setStatus() {
if (c) if (c)
c.innerHTML = "Connected to: " + genNodeList(); c.innerHTML = "Connected to: " + genNodeList();
var dlcE = document.getElementById('dlc'); var dlcE = document.getElementById('dlc');
if (dlvE) if (dlcE)
dlcE.innerHTML = "DLC: " + dlc; dlcE.innerHTML = "DLC: " + dlc;
} }
@ -639,9 +674,17 @@ addCronTab(10, setStatus);
/**** INIT *****/ /**** INIT *****/
function cortex_start() { function cortex_start(debug) {
if(debug)
logging = true;
ping(); ping();
var cronID = setInterval("cron()", 100); var cronID = setInterval("cron()", 100);
getMsgs(); getMsgs();
getLog(); getLog();
} }
/********** AND THAT's THE BASIS OF THE CORTEX P2P NETWORK **********/
/* We now have dumb do nothing nodes that can connect to each other
* and maintain state */
/********** NOW TO ADD SOME USEFUL THINGS ON TOP OF IT **********/

View File

@ -3,8 +3,6 @@
<head> <head>
<title>Client</title> <title>Client</title>
<script src="ajax.js" ></script>
<script> <script>
function load() { function load() {
port = document.getElementById('port').value; port = document.getElementById('port').value;