Skip to content

Commit

Permalink
⚡ package getprop using socket prop getter
Browse files Browse the repository at this point in the history
  • Loading branch information
ValKmjolnir committed Nov 7, 2023
1 parent 433743f commit 7b05a0a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
4 changes: 3 additions & 1 deletion std/json.nas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 "[]";
}
Expand Down
94 changes: 68 additions & 26 deletions tools/fgfs_props_getter.nas
Original file line number Diff line number Diff line change
Expand Up @@ -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, "\"");
Expand All @@ -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);
getter.close();

0 comments on commit 7b05a0a

Please sign in to comment.