Skip to content

Commit

Permalink
⚡ add tool for getting property tree from fgfs
Browse files Browse the repository at this point in the history
by using --httpd in fgfs
  • Loading branch information
ValKmjolnir committed Nov 6, 2023
1 parent 2ea9e03 commit 433743f
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 76 deletions.
149 changes: 75 additions & 74 deletions module/libsock.nas
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use std.dylib;

var socket=func(){
var lib=dylib.dlopen("libnasock"~(os.platform()=="windows"?".dll":".so"));
var socket = func() {
var lib = dylib.dlopen("libnasock"~(os.platform()=="windows"? ".dll":".so"));

var sock=lib.nas_socket;
var closesocket=lib.nas_closesocket;
var shutdown=lib.nas_shutdown;
var bind=lib.nas_bind;
var listen=lib.nas_listen;
var connect=lib.nas_connect;
var accept=lib.nas_accept;
var send=lib.nas_send;
var sendto=lib.nas_sendto;
var recv=lib.nas_recv;
var recvfrom=lib.nas_recvfrom;
var errno=lib.nas_errno;
var sock = lib.nas_socket;
var closesocket = lib.nas_closesocket;
var shutdown = lib.nas_shutdown;
var bind = lib.nas_bind;
var listen = lib.nas_listen;
var connect = lib.nas_connect;
var accept = lib.nas_accept;
var send = lib.nas_send;
var sendto = lib.nas_sendto;
var recv = lib.nas_recv;
var recvfrom = lib.nas_recvfrom;
var errno = lib.nas_errno;

var (invoke,invoke_i,invoke_ii,invoke_iii,invoke_iiii,invoke_iiiii)=(
var (invoke, invoke_i, invoke_ii, invoke_iii, invoke_iiii, invoke_iiiii) = (
dylib.limitcall(0),
dylib.limitcall(1),
dylib.limitcall(2),
Expand All @@ -26,36 +26,37 @@ var socket=func(){
);
return {
AF_UNSPEC:0,
AF_UNIX:1,
AF_INET:2,
AF_IMPLINK:3,
AF_PUP:4,
AF_CHAOS:5,
AF_IPX:6,
AF_NS:6,
AF_ISO:7,
AF_OSI:7,
AF_ECMA:8,
AF_DATAKIT:9,
AF_CCITT:10,
AF_SNA:11,
AF_DECnet:12,
AF_DLI:13,
AF_LAT:14,
AF_HYLINK:15,
AF_APPLETALK:16,
AF_NETBIOS:17,
AF_VOICEVIEW:18,
AF_FIREFOX:19,
AF_UNKNOWN1:20,
AF_BAN:21,
AF_MAX:22,
AF_UNIX: 1,
AF_INET: 2,
AF_IMPLINK: 3,
AF_PUP: 4,
AF_CHAOS: 5,
AF_IPX: 6,
AF_NS: 6,
AF_ISO: 7,
AF_OSI: 7,
AF_ECMA: 8,
AF_DATAKIT: 9,
AF_CCITT: 10,
AF_SNA: 11,
AF_DECnet: 12,
AF_DLI: 13,
AF_LAT: 14,
AF_HYLINK: 15,
AF_APPLETALK: 16,
AF_NETBIOS: 17,
AF_VOICEVIEW: 18,
AF_FIREFOX: 19,
AF_UNKNOWN1: 20,
AF_BAN: 21,
AF_MAX: 22,

SOCK_STREAM:1,
SOCK_DGRAM:2,
SOCK_RAW:3,
SOCK_RDM:4,
SOCK_SEQPACKET:5,
SOCKET_ERROR: -1,
SOCK_STREAM: 1,
SOCK_DGRAM: 2,
SOCK_RAW: 3,
SOCK_RDM: 4,
SOCK_SEQPACKET: 5,

IPPROTO_IP:0,IPPROTO_ICMP:1,IPPROTO_IGMP:2,IPPROTO_GGP:3,
IPPROTO_TCP:6,IPPROTO_PUP:12,IPPROTO_UDP:17,IPPROTO_IDP:22,
Expand All @@ -69,49 +70,49 @@ var socket=func(){
IPPORT_EFSSERVER:520,IPPORT_BIFFUDP:512,IPPORT_WHOSERVER:513,IPPORT_ROUTESERVER:520,
IPPORT_RESERVED:1024,

SHUT_RD :0x00,
SHUT_WR :0x01,
SHUT_RDWR:0x02,
SHUT_RD : 0x00,
SHUT_WR : 0x01,
SHUT_RDWR: 0x02,

MSG_OOB:0x1,
MSG_PEEK:0x2,
MSG_DONTROUTE:0x4,
MSG_DONTWAIT:0x40,
MSG_OOB: 0x1,
MSG_PEEK: 0x2,
MSG_DONTROUTE: 0x4,
MSG_DONTWAIT: 0x40,

socket:func(af,type,proto = 0){
return invoke_iii(sock,af,type,proto);
socket: func(af, type, proto = 0) {
return invoke_iii(sock, af, type, proto);
},
closesocket:func(sd){
return invoke_i(closesocket,sd);
closesocket: func(sd) {
return invoke_i(closesocket, sd);
},
shutdown: func(sd,how){
return invoke_ii(shutdown,sd,how);
shutdown: func(sd, how) {
return invoke_ii(shutdown, sd, how);
},
bind: func(sd,ip,port){
return invoke_iii(bind,sd,ip,port);
bind: func(sd, ip, port) {
return invoke_iii(bind, sd, ip, port);
},
listen: func(sd,backlog){
return invoke_ii(listen,sd,backlog);
listen: func(sd, backlog) {
return invoke_ii(listen, sd, backlog);
},
connect: func(sd,hostname,port){
return invoke_iii(connect,sd,hostname,port);
connect: func(sd, hostname, port) {
return invoke_iii(connect, sd, hostname, port);
},
accept: func(sd){
return invoke_i(accept,sd);
accept: func(sd) {
return invoke_i(accept, sd);
},
send: func(sd,buff,flags=0){
return invoke_iii(send,sd,buff,flags);
send: func(sd, buff, flags = 0) {
return invoke_iii(send, sd, buff, flags);
},
sendto: func(sd,hostname,port,buff,flags=0){
return invoke_iiiii(sendto,sd,hostname,port,buff,flags);
sendto: func(sd, hostname, port, buff, flags = 0) {
return invoke_iiiii(sendto, sd, hostname, port, buff, flags);
},
recv: func(sd,len,flags=0){
return invoke_iii(recv,sd,len,flags);
recv: func(sd, len, flags = 0) {
return invoke_iii(recv, sd, len, flags);
},
recvfrom: func(sd,len,flags=0){
return invoke_iii(recvfrom,sd,len,flags);
recvfrom: func(sd, len, flags = 0) {
return invoke_iii(recvfrom, sd, len, flags);
},
errno: func(){
errno: func() {
return invoke(errno);
}
};
Expand Down
2 changes: 1 addition & 1 deletion module/nasocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ var nas_recv(var* args, usize size, gc* ngc) {
var res = ngc->temp = ngc->alloc(vm_hash);
auto& hash = res.hash().elems;
char* buf = new char[static_cast<int>(args[1].num())];
auto recvsize = recv(args[0].num(), buf,args[1].num(), args[2].num());
auto recvsize = recv(args[0].num(), buf, args[1].num(), args[2].num());
hash["size"] = var::num(static_cast<double>(recvsize));
buf[recvsize>=0? recvsize:0] = 0;
hash["str"] = ngc->newstr(buf);
Expand Down
19 changes: 18 additions & 1 deletion std/json.nas
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ var _j_content = [
"identifier"
];

var parse = func() {
var _parse_error = 0;

var parse = func() {
_parse_error = 0;
var text = "";
var line = 1;
var text_size = 0;
Expand Down Expand Up @@ -75,6 +77,7 @@ var parse = func() {
init();
if (!size(str)) {
println("json::parse: empty string");
_parse_error += 1;
str = "[]";
}
text = str;
Expand Down Expand Up @@ -156,6 +159,7 @@ var parse = func() {
var match = func(type) {
if(token.type!=type) {
println("json::parse: line ",line,": expect ",_j_content[type]," but get `",token.content,"`.");
_parse_error += 1;
}
next();
return;
Expand Down Expand Up @@ -224,6 +228,7 @@ var parse = func() {
return func(source) {
if(typeof(source)!="str") {
println("json::parse: must use string but get", typeof(str));
_parse_error += 1;
return [];
}

Expand Down Expand Up @@ -296,4 +301,16 @@ var stringify = func(object) {
hgen(object);
}
return s;
}

var get_error = func() {
return _parse_error;
}

var check_error = func() {
if (_parse_error==0) {
return;
}
println("json: encounter ", _parse_error, " error(s), stop.");
exit(-1);
}
58 changes: 58 additions & 0 deletions tools/fgfs_props_getter.nas
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# used to get property tree from fgfs by httpd
# use --httpd=5500 to start fgfs
# and use this script to get property tree
# 2023/11/06 ValKmjolnir

use module.libsock;
use std.json;

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");


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;
var total_size = message.size;

while(message.size==1024) {
message = socket.recv(sd, 1024);
total_source ~= message.str;
total_size += message.size;
}

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();

var dfs = func(tree, indent = "") {
println(indent, "---------");
println(indent, "path : \"", tree.path, "\"");
println(indent, "name : \"", tree.name, "\"");
println(indent, "index : \"", tree.index, "\"");
println(indent, "type : \"", tree.type, "\"");
println(indent, "nChildren : \"", tree.nChildren, "\"");
println(indent, "---------");

if (contains(tree, "children")) {
println(indent, "children :");
foreach(var i; tree.children) {
dfs(i, indent~" ");
}
}

}

dfs(props);

0 comments on commit 433743f

Please sign in to comment.