diff --git a/examples/node/.gitignore b/examples/node/.gitignore new file mode 100644 index 0000000..09d6241 --- /dev/null +++ b/examples/node/.gitignore @@ -0,0 +1,6 @@ +node_modules/* +darwin +zto +include +libzt-build +package-lock.json diff --git a/examples/node/README.md b/examples/node/README.md new file mode 100644 index 0000000..1ab5a7d --- /dev/null +++ b/examples/node/README.md @@ -0,0 +1,5 @@ +# Setup +- Make sure you build libzt first +- `npm install` +- `node test.js` +- `npm run -- node-gyp rebuild` if you modify binding.cc or libzt diff --git a/examples/node/auto-top.gypi b/examples/node/auto-top.gypi new file mode 100644 index 0000000..7671b8c --- /dev/null +++ b/examples/node/auto-top.gypi @@ -0,0 +1,8 @@ +# Automatically generated file. Edits will be lost. +# Based on: autogypi.json + +{ + "includes": [ + "node_modules/nbind/src/nbind-common.gypi" + ] +} diff --git a/examples/node/auto.gypi b/examples/node/auto.gypi new file mode 100644 index 0000000..49d2b0b --- /dev/null +++ b/examples/node/auto.gypi @@ -0,0 +1,11 @@ +# Automatically generated file. Edits will be lost. +# Based on: autogypi.json + +{ + "include_dirs": [ + "node_modules/nan" + ], + "includes": [ + "node_modules/nbind/src/nbind.gypi" + ] +} diff --git a/examples/node/autogypi.json b/examples/node/autogypi.json new file mode 100644 index 0000000..a62cdbd --- /dev/null +++ b/examples/node/autogypi.json @@ -0,0 +1,6 @@ +{ + "dependencies": [ + "nbind" + ], + "includes": [] +} diff --git a/examples/node/binding.cc b/examples/node/binding.cc new file mode 100644 index 0000000..da561ed --- /dev/null +++ b/examples/node/binding.cc @@ -0,0 +1,72 @@ +#include +#include + +#include "libzt.h" + +struct ZT { + static int running() { + return zts_running(); + } + + static void simpleStart(const char *path, const char *nwid) { + zts_simple_start(path, nwid); + } + + static void stop() { + zts_stop(); + } + + static char* getDeviceId() { + char* id = new char [ZT_ID_LEN + 1]; + zts_get_device_id(id); + return id; + } + + static char* getIpV4Address(const char *nwid) { + char* addr_str = new char [ZT_MAX_IPADDR_LEN]; + zts_get_ipv4_address(nwid, addr_str, ZT_MAX_IPADDR_LEN); + return addr_str; + } + + static int socket() { + return zts_socket(AF_INET, SOCK_STREAM, 0); + } + + static int bind(int sockfd, const char *addrStr, int port) { + struct sockaddr_in addr; + + addr.sin_addr.s_addr = inet_addr(addrStr); + addr.sin_family = AF_INET; + addr.sin_port = htons( port ); + + return zts_bind(sockfd, (const struct sockaddr *)&addr, sizeof(addr)); + } + + static int listen(int sockfd) { + return zts_listen(sockfd, 1); + } + + static int accept(int sockfd) { + struct sockaddr_in client; + int c = sizeof(struct sockaddr_in); + + int accept_fd = zts_accept(sockfd, (struct sockaddr *)&client, (socklen_t*)&c); + return accept_fd; + } + +}; + + +#include "nbind/nbind.h" + +NBIND_CLASS(ZT) { + method(accept); + method(bind); + method(getDeviceId); + method(getIpV4Address); + method(running); + method(simpleStart); + method(socket); + method(stop); + method(listen); +} diff --git a/examples/node/binding.gyp b/examples/node/binding.gyp new file mode 100644 index 0000000..941a8b9 --- /dev/null +++ b/examples/node/binding.gyp @@ -0,0 +1,11 @@ +{ + "targets": [ + { + "include_dirs": ["libzt/build/darwin", "libzt/include", "libzt/zto/include"], + "libraries": ["