add POST method to edit prop tree

This commit is contained in:
ValKmjolnir 2023-11-07 23:55:35 +08:00
parent 7b05a0a244
commit d461cbb05c
2 changed files with 111 additions and 28 deletions

View File

@ -11,8 +11,9 @@ var (
_j_colon, _j_colon,
_j_str, _j_str,
_j_num, _j_num,
_j_id _j_id,
) = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9); _j_bool
) = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var _j_content = [ var _j_content = [
"eof", "eof",
@ -24,7 +25,8 @@ var _j_content = [
"`:`", "`:`",
"string", "string",
"number", "number",
"identifier" "identifier",
"boolean"
]; ];
var _parse_error = 0; var _parse_error = 0;
@ -133,7 +135,13 @@ var parse = func() {
} elsif (isnum(c)) { } elsif (isnum(c)) {
var s = c; var s = c;
ptr += 1; ptr += 1;
while(ptr<text_size and ((isnum(char(text[ptr])) or char(text[ptr])=='.'))) { while(ptr<text_size and ((
isnum(char(text[ptr])) or
char(text[ptr])=='.' or
char(text[ptr])=='e' or
char(text[ptr])=='-' or
char(text[ptr])=='+'))
) {
s ~= char(text[ptr]); s ~= char(text[ptr]);
ptr += 1; ptr += 1;
} }
@ -150,6 +158,9 @@ var parse = func() {
ptr -= 1; ptr -= 1;
token.content = s; token.content = s;
token.type = _j_id; token.type = _j_id;
if (s=="true" or s=="false") {
token.type = _j_bool;
}
} }
ptr += 1; ptr += 1;
return; return;
@ -179,7 +190,7 @@ var parse = func() {
hash[name] = hash_gen(); hash[name] = hash_gen();
} elsif (token.type==_j_lbrkt) { } elsif (token.type==_j_lbrkt) {
hash[name] = vec_gen(); hash[name] = vec_gen();
} elsif (token.type==_j_str or token.type==_j_num) { } elsif (token.type==_j_str or token.type==_j_num or token.type==_j_bool) {
hash[name] = token.content; hash[name] = token.content;
next(); next();
} }

View File

@ -6,7 +6,8 @@
use module.libsock; use module.libsock;
use std.json; use std.json;
var new_getter = func(hostname, port) { var connection = {};
connection.new = func(hostname, port) {
var socket = libsock.socket; var socket = libsock.socket;
var sd = socket.socket( var sd = socket.socket(
socket.AF_INET, socket.AF_INET,
@ -24,35 +25,88 @@ var new_getter = func(hostname, port) {
} }
println("[", os.time(), "] connect to ", hostname, ":", port, " succeeded"); println("[", os.time(), "] connect to ", hostname, ":", port, " succeeded");
var raw = func(s) {
var v = split("", s);
var res = "";
foreach(var i; v) {
if (i=="\r") {
res ~= "\\r";
continue;
}
if (i=="\n") {
res ~= "\\n\n";
continue;
}
res ~= i;
}
return res;
}
var getprop = func(path) { var getprop = func(path) {
var header = "GET /json"~path~" HTTP/1.1\n\r\n"; var header = "GET /json"~path~" HTTP/1.1\n\r\n";
var res = socket.send(sd, header); var res = socket.send(sd, header);
var message = socket.recv(sd, 1024); var message = socket.recv(sd, 1024);
var head_vector = split("\r\n", message.str);
if (size(head_vector)<11) {
println("getprop: node \"", path, "\" not found, invalid header");
logprint(LOG_DEBUG, raw(message.str));
return {path: path};
}
var message_total_size = num("0x"~head_vector[10]);
var total_source = message.str; var total_source = message.str;
while(message.size!=0) { var total_size = message.size;
while(total_size<=message_total_size) {
message = socket.recv(sd, 1024); message = socket.recv(sd, 1024);
total_source ~= message.str; total_source ~= message.str;
total_size += message.size;
}
if (find("0\r\n\r\n", total_source)<0) {
message = socket.recv(sd, 1024);
total_source ~= message.str;
total_size += message.size;
} }
var begin_position = find("{", total_source); var begin_position = find("{", total_source);
if (begin_position<0) {
println("getprop: node \"", path, "\" not found, invalid begin token");
return {path: path};
}
var end_position = find("0\r\n\r\n", total_source); var end_position = find("0\r\n\r\n", total_source);
var props = substr(total_source, begin_position, end_position-begin_position); var length = end_position-begin_position;
props = json.parse(props); if (length<0) {
json.check_error(); println("getprop: node \"", path, "\" not found, invalid end token");
return {path: path};
}
var data = substr(total_source, begin_position, length);
var props = json.parse(data);
if (json.get_error()) {
println("getprop: encounter error when parsing \"", path, "\"");
logprint(LOG_DEBUG, data);
return {path: path};
}
if (size(props)==0) { if (size(props)==0) {
println("getprop: node \"", path, "\" not found"); println("getprop: node \"", path, "\" not found, empty tree node");
} }
return props; return props;
} }
var setprop = func(path, data) {
var header = "POST /json"~path~" HTTP/1.1\n\n";
header ~= "{\"value\":\""~data~"\"}\n\r\n";
var res = socket.send(sd, header);
var message = socket.recv(sd, 1024);
}
var close = func { var close = func {
socket.closesocket(sd); socket.closesocket(sd);
} }
return { return {
getprop: getprop, getprop: getprop,
setprop: setprop,
close: close close: close
}; };
} }
@ -61,13 +115,17 @@ var dump = func(tree, indent = "") {
if (size(tree)==0) { if (size(tree)==0) {
return; return;
} }
println(indent, "---------");
println(indent, "path : \"", tree.path, "\""); println(indent, "-------------------");
println(indent, "name : \"", tree.name, "\""); var tree_keys = keys(tree);
println(indent, "index : \"", tree.index, "\""); sort(tree_keys, func(a,b) {return cmp(a, b)<0;});
println(indent, "type : \"", tree.type, "\""); foreach(var key; tree_keys) {
println(indent, "nChildren : \"", tree.nChildren, "\""); if (key == "children") {
println(indent, "---------"); continue;
}
println(indent, key, " : \"", tree[key], "\"");
}
println(indent, "-------------------");
if (contains(tree, "children")) { if (contains(tree, "children")) {
println(indent, "children :"); println(indent, "children :");
@ -86,15 +144,29 @@ if (size(arg)>2) {
exit(-1); exit(-1);
} }
var getter = new_getter(arg[0], num(arg[1])); var connect = connection.new(arg[0], num(arg[1]));
var count = 0;
var recursive_get_prop = func(path = "/") {
count += 1;
if (math.mod(count, 100)==0) {
println("get ", count," nodes, now: \"", path, "\"");
}
var props = connect.getprop(path);
var tree = {};
tree.path = props.path;
if (!contains(props, "children")) {
return tree;
}
var get_props = func(path = "/") { tree.children = [];
var props = getter.getprop(path); foreach(var child; props.children) {
dump(props); var node = {};
node = recursive_get_prop(child.path);
append(tree.children, node);
}
return tree;
} }
get_props(); # takes about 5 min to get whole tree
get_props("/e-tron/dialog/config"); var props = recursive_get_prop("/");
get_props("/e-tron/dialog/config/a"); dump(props);
getter.close();