diff --git a/std/json.nas b/std/json.nas index 82843cc8..6e4d42b0 100644 --- a/std/json.nas +++ b/std/json.nas @@ -30,7 +30,6 @@ var _j_content = [ var _parse_error = 0; var parse = func() { - _parse_error = 0; var text = ""; var line = 1; var text_size = 0; @@ -226,6 +225,7 @@ var parse = func() { } return func(source) { + _parse_error = 0; if(typeof(source)!="str") { println("json::parse: must use string but get", typeof(str)); _parse_error += 1; @@ -247,8 +247,10 @@ var parse = func() { }(); var stringify = func(object) { + _parse_error = 0; var object_type = typeof(object); if(object_type!="hash" and object_type!="vec" and object_type!="namespace") { + _parse_error += 1; println("json::stringify: must use hashmap or vector, but get ", typeof(object)); return "[]"; } diff --git a/tools/fgfs_props_getter.nas b/tools/fgfs_props_getter.nas index c19fa2ae..e0cfa87a 100644 --- a/tools/fgfs_props_getter.nas +++ b/tools/fgfs_props_getter.nas @@ -6,38 +6,61 @@ use module.libsock; use std.json; -var socket = libsock.socket; -var sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP); +var new_getter = func(hostname, port) { + var socket = libsock.socket; + var sd = socket.socket( + socket.AF_INET, + socket.SOCK_STREAM, + socket.IPPROTO_TCP + ); -while((var err = socket.connect(sd, "127.0.0.1", 5500))==socket.SOCKET_ERROR) { - println("[", os.time(), "] connect to 127.0.0.1:5500 failed: ", socket.errno()); - unix.sleep(1); -} -println("[", os.time(), "] connect to 127.0.0.1:5500 succeeded"); + while((var err = socket.connect(sd, hostname, port))==socket.SOCKET_ERROR) { + println( + "[", os.time(), "] connect to ", + hostname, ":", port, " failed: ", + socket.errno() + ); + unix.sleep(1); + } + println("[", os.time(), "] connect to ", hostname, ":", port, " succeeded"); + var getprop = func(path) { + var header = "GET /json"~path~" HTTP/1.1\n\r\n"; + var res = socket.send(sd, header); + var message = socket.recv(sd, 1024); -var header = "GET /json/ HTTP/1.1\n\r\n"; -var res = socket.send(sd, header); -var message = socket.recv(sd, 1024); + var total_source = message.str; + while(message.size!=0) { + message = socket.recv(sd, 1024); + total_source ~= message.str; + } -var total_source = message.str; -var total_size = message.size; + var begin_position = find("{", total_source); + var end_position = find("0\r\n\r\n", total_source); + var props = substr(total_source, begin_position, end_position-begin_position); + props = json.parse(props); + json.check_error(); + if (size(props)==0) { + println("getprop: node \"", path, "\" not found"); + } -while(message.size==1024) { - message = socket.recv(sd, 1024); - total_source ~= message.str; - total_size += message.size; -} + return props; + } -socket.closesocket(sd); + var close = func { + socket.closesocket(sd); + } -var A3E_position = find("{", total_source); -var end_0_position = find("0\r\n\r\n", total_source); -var props = substr(total_source, A3E_position, end_0_position-A3E_position); -props = json.parse(props); -json.check_error(); + return { + getprop: getprop, + close: close + }; +} -var dfs = func(tree, indent = "") { +var dump = func(tree, indent = "") { + if (size(tree)==0) { + return; + } println(indent, "---------"); println(indent, "path : \"", tree.path, "\""); println(indent, "name : \"", tree.name, "\""); @@ -49,10 +72,29 @@ var dfs = func(tree, indent = "") { if (contains(tree, "children")) { println(indent, "children :"); foreach(var i; tree.children) { - dfs(i, indent~" "); + dump(i, indent~" "); } } +} +if (size(arg)<2) { + println("require hostname and port"); + exit(-1); } +if (size(arg)>2) { + println("too many arguments, only require hostname and port"); + exit(-1); +} + +var getter = new_getter(arg[0], num(arg[1])); + +var get_props = func(path = "/") { + var props = getter.getprop(path); + dump(props); +} + +get_props(); +get_props("/e-tron/dialog/config"); +get_props("/e-tron/dialog/config/a"); -dfs(props); \ No newline at end of file +getter.close(); \ No newline at end of file