diff --git a/README.md b/README.md index d751ccc..24890d4 100644 --- a/README.md +++ b/README.md @@ -26,19 +26,19 @@ START SERVER INITIALIZE ```lua - hub = noobhub.new({ server = "127.0.0.1"; port = 1337; }); + hub = noobhub.new({ server = "127.0.0.1"; port = 1337; }); ``` SUBSCRIBE TO A CHANNEL AND RECEIVE CALLBACKS WHEN NEW JSON MESSAGES ARRIVE ```lua hub:subscribe({ - channel = "hello-world"; + channel = "hello-world"; callback = function(message) - - if(message.action == "ping") then + + if(message.action == "ping") then print("Pong!") end; - + end; }); ``` diff --git a/client/javascript-node-js/client.benchmarks.js b/client/javascript-node-js/client.benchmarks.js index 1924321..0968047 100644 --- a/client/javascript-node-js/client.benchmarks.js +++ b/client/javascript-node-js/client.benchmarks.js @@ -3,7 +3,7 @@ var echo = (process.argv && process.argv[2] == 'echo') , crypto = require('crypto') , Trololo = new function() { - + var host = 'localhost' , port = '1337' , self = this @@ -14,61 +14,59 @@ var echo = (process.argv && process.argv[2] == 'echo') , startTime = 0 , triesMax = 1000 , tryCurrent = 0 - , stats = { + , stats = { max: 0, min: 10000, average: 0, data: [] }; - + this.send = function() { console.log('[BENCHMARKING MODE]'); self._connectAndSubscribe(function(){ socket.on('data', self._measure); self._ping(); }); - } - + this._ping = function() { - hash = String( Math.random() * 1000 ); console.log('sending hash ', hash); startTime = new Date().getTime(); socket.write("__JSON__START__" + hash + "__JSON__END__"); } - + this._measure = function(data) { - console.log('got data ', String(data)); - + console.log('got data ', String(data)); + var str = String(data).replace(/__JSON__START__|__JSON__END__|\r|\n/g, ''); - if (str == hash) { + if (str == hash) { var lat = new Date().getTime() - startTime; console.log('latency ', lat); stats.data.push(lat); var summ = 0; if (++tryCurrent == triesMax) { - + for (var i=0, l=stats.data.length; i stats.max) { stats.max = stats.data[i] - } - + } + if (stats.data[i] < stats.min) { stats.min = stats.data[i] } summ += stats.data[i]; } - + stats.average = Number(summ / triesMax).toFixed(2); - + console.log('[RESULTS]'); console.log('max latency : ' + stats.max); console.log('min latency : ' + stats.min); console.log('average : ' + stats.average); - + } else { self._ping(); } @@ -77,14 +75,14 @@ var echo = (process.argv && process.argv[2] == 'echo') //console.log(str + " != " + hash); } } - + this.listen = function() { console.log('[ECHO MODE]'); self._connectAndSubscribe(); - + var lastResponse = null; socket.on('data', function(data){ - + if (String(data) != String(lastResponse)) { console.log('incoming data', data); lastResponse = data; @@ -92,20 +90,20 @@ var echo = (process.argv && process.argv[2] == 'echo') } }) } - + this._connectAndSubscribe = function(cb) { socket = new net.createConnection(port, host); socket.setNoDelay(true); - + socket.on('connect', function(){ console.log('connected to ', host, port); console.log(arguments); - + socket.write("__SUBSCRIBE__" + channel + "__ENDSUBSCRIBE__"); - + if (typeof(cb) == "function") cb(); }) } } -echo ? Trololo.listen() : Trololo.send(); \ No newline at end of file +echo ? Trololo.listen() : Trololo.send(); diff --git a/client/javascript-node-js/client.js b/client/javascript-node-js/client.js index a3c6c7f..215bf75 100644 --- a/client/javascript-node-js/client.js +++ b/client/javascript-node-js/client.js @@ -80,7 +80,7 @@ var Nbhb = exports.Nbhb = function() { var s = String(str).replace(/}{|\r|\n/g, '}{'); if (s == str){ - + if (typeof(self.messageCallback) === "function") self.messageCallback(str); } diff --git a/client/javascript-node-js/client.test.js b/client/javascript-node-js/client.test.js index 4709191..f41012c 100644 --- a/client/javascript-node-js/client.test.js +++ b/client/javascript-node-js/client.test.js @@ -13,11 +13,9 @@ noobhub.subscribe( } , function(s){ console.log('subscribed callback'); - noobhub.publish({ a : Math.random() }, function(){ console.log("data sent"); }); - } , function(data) { console.log("get data callback: " + data); diff --git a/client/javascript-node-js/perf.js b/client/javascript-node-js/perf.js index 0b0adf1..58bc7f3 100644 --- a/client/javascript-node-js/perf.js +++ b/client/javascript-node-js/perf.js @@ -53,18 +53,18 @@ var z = function(idx) { server: 'localhost', channel: channel }, function() { isAlive = 1; return _onSubscribed(idx); } - , function(msg) { + , function(msg) { if (msg === _myMessage) { var lat = Date.now() - startTime; console.log(idx + 'latency is : ', lat); _sendMetrics('latency:'+lat+'|ms'); } - return _onMessage(idx, msg); + return _onMessage(idx, msg); } - , function(err) { - isAlive = 0; - clearInterval(_interval); - clearInterval(_changeChannel); + , function(err) { + isAlive = 0; + clearInterval(_interval); + clearInterval(_changeChannel); return _onError(idx, err); } ); @@ -93,7 +93,7 @@ var z = function(idx) { clearInterval(_interval); clearInterval(_changeChannel); n.unsubscribe(); - n = null; + n = null; } }; @@ -114,4 +114,4 @@ for (var i=0, l=config.zerlings; itable mapping -- the table contains tokens (byte values) as keys and maps them on other -- token tables (mostly, the boolean value 'true' is used to indicate termination @@ -202,7 +202,7 @@ do return struct end function struct:to(chars) - for i=1,#chars do + for i=1,#chars do tt[chars:byte(i)] = value end return struct @@ -212,9 +212,9 @@ do return struct end end - + -- keep "named" byte values at hands - local + local c_esc, c_e, c_l, @@ -224,9 +224,9 @@ do c_a, c_s, c_slash = ("\\elrufas/"):byte(1,9) - + -- token tables - tt_doublequote_string = strDoubleQuot, tt_singlequote_string = strSingleQuot - local + local tt_object_key, tt_object_colon, tt_object_value, @@ -241,19 +241,19 @@ do tt_comment_middle, tt_ignore --< tt_ignore is special - marked tokens will be tt_ignored = {},{},{},{},{},{},{},{},{},{},{},{},{} - + -- strings to be used in certain token tables local strchars = "" -- all valid string characters (all except newlines) local allchars = "" -- all characters that are valid in comments --local escapechar = {} - for i=0,0xff do + for i=0,0xff do local c = string.char(i) if c~="\n" and c~="\r" then strchars = strchars .. c end allchars = allchars .. c --escapechar[i] = "\\" .. string.char(i) end - ---[[ + +--[[ charstounescape = "\"\'\\bfnrt/"; unescapechars = "\"'\\\b\f\n\r\t\/"; for i=1,#charstounescape do @@ -262,88 +262,87 @@ do ]]-- -- obj key reader, expects the end of the object or a quoted string as key - init_token_table (tt_object_key) "object (' or \" or } or , expected)" + init_token_table (tt_object_key) "object (' or \" or } or , expected)" :link(tt_singlequote_string) :to "'" :link(tt_doublequote_string) :to '"' :link(true) :to "}" :link(tt_object_key) :to "," :link(tt_comment_start) :to "/" :link(tt_ignore) :to " \t\r\n" - - + -- after the key, a colon is expected (or comment) - init_token_table (tt_object_colon) "object (: expected)" - :link(tt_object_value) :to ":" - :link(tt_comment_start) :to "/" + init_token_table (tt_object_colon) "object (: expected)" + :link(tt_object_value) :to ":" + :link(tt_comment_start) :to "/" :link(tt_ignore) :to" \t\r\n" - + -- as values, anything is possible, numbers, arrays, objects, boolean, null, strings init_token_table (tt_object_value) "object ({ or [ or ' or \" or number or boolean or null expected)" - :link(tt_object_key) :to "{" - :link(tt_array_seperator) :to "[" - :link(tt_singlequote_string) :to "'" - :link(tt_doublequote_string) :to '"' - :link(tt_numeric) :to "0123456789.-" - :link(tt_boolean) :to "tf" - :link(tt_null) :to "n" - :link(tt_comment_start) :to "/" + :link(tt_object_key) :to "{" + :link(tt_array_seperator) :to "[" + :link(tt_singlequote_string) :to "'" + :link(tt_doublequote_string) :to '"' + :link(tt_numeric) :to "0123456789.-" + :link(tt_boolean) :to "tf" + :link(tt_null) :to "n" + :link(tt_comment_start) :to "/" :link(tt_ignore) :to " \t\r\n" - + -- token tables for reading strings init_token_table (tt_doublequote_string) "double quoted string" :link(tt_ignore) :to (strchars) :link(c_esc) :to "\\" :link(true) :to '"' - + init_token_table (tt_singlequote_string) "single quoted string" :link(tt_ignore) :to (strchars) - :link(c_esc) :to "\\" + :link(c_esc) :to "\\" :link(true) :to "'" - + -- array reader that expects termination of the array or a comma that indicates the next value init_token_table (tt_array_value) "array (, or ] expected)" - :link(tt_array_seperator) :to "," + :link(tt_array_seperator) :to "," :link(true) :to "]" - :link(tt_comment_start) :to "/" + :link(tt_comment_start) :to "/" :link(tt_ignore) :to " \t\r\n" - + -- a value, pretty similar to tt_object_value init_token_table (tt_array_seperator) "array ({ or [ or ] or ' or \" or number or boolean or null expected)" - :link(tt_object_key) :to "{" - :link(tt_array_seperator) :to "[" - :link(tt_singlequote_string) :to "'" - :link(tt_doublequote_string) :to '"' - :link(tt_comment_start) :to "/" - :link(tt_numeric) :to "0123456789.-" - :link(tt_boolean) :to "tf" - :link(tt_null) :to "n" + :link(tt_object_key) :to "{" + :link(tt_array_seperator) :to "[" + :link(tt_singlequote_string) :to "'" + :link(tt_doublequote_string) :to '"' + :link(tt_comment_start) :to "/" + :link(tt_numeric) :to "0123456789.-" + :link(tt_boolean) :to "tf" + :link(tt_null) :to "n" :link(tt_ignore) :to " \t\r\n" :link(tt_array_value) :to "]" - + -- valid number tokens init_token_table (tt_numeric) "number" :link(tt_ignore) :to "0123456789.-Ee" - + -- once a comment has been started with /, a * is expected init_token_table (tt_comment_start) "comment start (* expected)" :link(tt_comment_middle) :to "*" - + -- now everything is allowed, watch out for * though. The next char is then checked manually init_token_table (tt_comment_middle) "comment end" :link(tt_ignore) :to (allchars) :link(true) :to "*" - + function decode (js_string) local pos = 1 -- position in the string - + -- read the next byte value local function next_byte () pos = pos + 1 return js_string:byte(pos-1) end - + -- in case of error, report the location using line numbers - local function location () + local function location () local n = ("\n"):byte() local line,lpos = 1,0 - for i=1,pos do + for i=1,pos do if js_string:byte(i) == n then line,lpos = line + 1,1 else @@ -352,18 +351,18 @@ do end return "Line "..line.." character "..lpos end - + -- debug func --local function status (str) -- print(str.." ("..s:sub(math.max(1,p-10),p+10)..")") --end - + -- read the next token, according to the passed token table local function next_token (tok) while pos <= #js_string do - local b = js_string:byte(pos) + local b = js_string:byte(pos) local t = tok[b] - if not t then + if not t then error("Unexpected character at "..location()..": ".. string.char(b).." ("..b..") when reading "..tok.name.."\nContext: \n".. js_string:sub(math.max(1,pos-30),pos+30).."\n"..(" "):rep(pos+math.min(-1,30-pos)).."^") @@ -373,14 +372,14 @@ do end error("unexpected termination of JSON while looking for "..tok.name) end - + -- read a string, double and single quoted ones local function read_string (tok) local start = pos --local returnString = {} repeat local t = next_token(tok) - if t == c_esc then + if t == c_esc then --table.insert(returnString, js_string:sub(start, pos-2)) --table.insert(returnString, escapechar[ js_string:byte(pos) ]) pos = pos + 1 @@ -391,7 +390,7 @@ do -- We consider the situation where no escaped chars were encountered separately, -- and use the fastest possible return in this case. - + --if 0 == #returnString then -- return js_string:sub(start,pos-2) --else @@ -400,7 +399,7 @@ do --end --return js_string:sub(start,pos-2) end - + local function read_num () local start = pos while pos <= #js_string do @@ -410,22 +409,22 @@ do end return tonumber(js_string:sub(start-1,pos-1)) end - + -- read_bool and read_null are both making an assumption that I have not tested: - -- I would expect that the string extraction is more expensive than actually + -- I would expect that the string extraction is more expensive than actually -- making manual comparision of the byte values - local function read_bool () + local function read_bool () pos = pos + 3 local a,b,c,d = js_string:byte(pos-3,pos) if a == c_r and b == c_u and c == c_e then return true end pos = pos + 1 - if a ~= c_a or b ~= c_l or c ~= c_s or d ~= c_e then - error("Invalid boolean: "..js_string:sub(math.max(1,pos-5),pos+5)) + if a ~= c_a or b ~= c_l or c ~= c_s or d ~= c_e then + error("Invalid boolean: "..js_string:sub(math.max(1,pos-5),pos+5)) end return false end - - -- same as read_bool: only last + + -- same as read_bool: only last local function read_null () pos = pos + 3 local u,l1,l2 = js_string:byte(pos-3,pos-1) @@ -433,23 +432,23 @@ do error("Invalid value (expected null):"..js_string:sub(pos-4,pos-1).. " ("..js_string:byte(pos-1).."="..js_string:sub(pos-1,pos-1).." / "..c_l..")") end - + local read_object_value,read_object_key,read_array,read_value,read_comment - + -- read a value depending on what token was returned, might require info what was used (in case of comments) function read_value (t,fromt) if t == tt_object_key then return read_object_key({}) end if t == tt_array_seperator then return read_array({}) end - if t == tt_singlequote_string or + if t == tt_singlequote_string or t == tt_doublequote_string then return read_string(t) end if t == tt_numeric then return read_num() end - if t == tt_boolean then return read_bool() end + if t == tt_boolean then return read_bool() end if t == tt_null then return read_null() end if t == tt_comment_start then return read_value(read_comment(fromt)) end error("unexpected termination - "..js_string:sub(math.max(1,pos-10),pos+10)) end - - -- read comments until something noncomment like surfaces, using the token reader which was + + -- read comments until something noncomment like surfaces, using the token reader which was -- used when stumbling over this comment function read_comment (fromt) while true do @@ -464,7 +463,7 @@ do end end end - + -- read arrays, empty array expected as o arg function read_array (o,i) --if not i then status "arr open" end @@ -472,8 +471,8 @@ do -- loop until ... while true do local array_token = next_token(tt_array_seperator) - if array_token == tt_array_value then -- ... we found a terminator token ']' - return o + if array_token == tt_array_value then -- ... we found a terminator token ']' + return o end o[i] = read_value(array_token, tt_array_seperator) local t = next_token(tt_array_value) @@ -482,18 +481,18 @@ do end if t == true then -- ... we found a terminator token --status "arr close" - return o + return o end - i = i + 1 + i = i + 1 end end - + -- object value reading function read_object_value (o) local t = next_token(tt_object_value) return read_value(t,tt_object_value) end - + -- object key reading, might also terminate the object function read_object_key (o) while true do @@ -504,23 +503,23 @@ do if t == true then return o end if t == tt_object_key then return read_object_key(o) end local k = read_string(t) - + if next_token(tt_object_colon) == tt_comment_start then t = read_comment(tt_object_colon) end - + local v = read_object_value(o) o[k] = v end end - + -- now let's read data from our string and pretend it's an object value local r = read_object_value() if pos<=#js_string then -- not sure about what to do with dangling characters --error("Dangling characters in JSON code ("..location()..")") end - + return r end end diff --git a/client/lua-moai/main.lua b/client/lua-moai/main.lua index 6473750..3b542f3 100644 --- a/client/lua-moai/main.lua +++ b/client/lua-moai/main.lua @@ -21,10 +21,10 @@ aMainLayer:setViewport ( aViewport ) asciiTextCodes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .,:;!?()&/-' appFonts = {} appFonts["anonymous"] = { - ttf = 'anonymous.ttf', - textcodes = asciiTextCodes, + ttf = 'anonymous.ttf', + textcodes = asciiTextCodes, font = MOAIFont.new(), - size = 10, + size = 10, dpi = 163 } @@ -90,7 +90,7 @@ hub:subscribe({ callback = function(message) --aReportMsg = "message received = "..json.encode(message) if(message.action == "ping") then ---------------------------------- - + aReportMsg = "ping received, sending pong" hub:publish({ message = { @@ -145,4 +145,4 @@ function md5 ( data ) writer:write ( data ) writer:close () return writer:getHashHex () -end \ No newline at end of file +end diff --git a/client/lua-moai/run.bat b/client/lua-moai/run.bat index 457cd2a..b8aef5f 100644 --- a/client/lua-moai/run.bat +++ b/client/lua-moai/run.bat @@ -20,7 +20,7 @@ if not exist "%MOAI_BIN%\moai.exe" ( if not exist "%MOAI_CONFIG%" ( echo. echo ------------------------------------------------------------------------------- - echo WARNING: The MOAI_CONFIG environment variable either doesn't exist or it's + echo WARNING: The MOAI_CONFIG environment variable either doesn't exist or it's echo pointing to an invalid path. Please point it at a folder containing config.lua. echo ------------------------------------------------------------------------------- echo. @@ -30,4 +30,4 @@ if not exist "%MOAI_CONFIG%" ( "%MOAI_BIN%\moai" "%MOAI_CONFIG%\config.lua" "main.lua" :end -pause \ No newline at end of file +pause diff --git a/client/php/console.php b/client/php/console.php index c078a87..15143fe 100644 --- a/client/php/console.php +++ b/client/php/console.php @@ -16,11 +16,11 @@ $except = NULL; $read_buffer = ''; while(1) { - $read = array($socket); + $read = array($socket); $num_changed_sockets = socket_select($read, $write, $except, 0,0); if ( $num_changed_sockets > 0 ) { foreach($read as $sock) - if( $r = socket_read($sock,1,PHP_NORMAL_READ) ) { + if( $r = socket_read($sock,1,PHP_NORMAL_READ) ) { $r = str_ireplace("\n",'',$r); $r = str_ireplace("\r",'',$r); $read_buffer .= $r;