Create node example directory

This commit is contained in:
Travis LaDuke
2017-06-28 09:35:11 -07:00
parent efe5d15143
commit fc210c9be5
12 changed files with 236 additions and 0 deletions

6
examples/node/.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
node_modules/*
darwin
zto
include
libzt-build
package-lock.json

5
examples/node/README.md Normal file
View File

@@ -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

View File

@@ -0,0 +1,8 @@
# Automatically generated file. Edits will be lost.
# Based on: autogypi.json
{
"includes": [
"node_modules/nbind/src/nbind-common.gypi"
]
}

11
examples/node/auto.gypi Normal file
View File

@@ -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"
]
}

View File

@@ -0,0 +1,6 @@
{
"dependencies": [
"nbind"
],
"includes": []
}

72
examples/node/binding.cc Normal file
View File

@@ -0,0 +1,72 @@
#include <string>
#include <iostream>
#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);
}

11
examples/node/binding.gyp Normal file
View File

@@ -0,0 +1,11 @@
{
"targets": [
{
"include_dirs": ["libzt/build/darwin", "libzt/include", "libzt/zto/include"],
"libraries": ["<!(pwd)/libzt/build/darwin/libzt.a"],
"includes": ["auto.gypi"],
"sources": ["binding.cc"]
}
],
"includes": ["auto-top.gypi"]
}

View File

@@ -0,0 +1,28 @@
var http = require('http')
var zt = require('./libzt')
var earth = '8056c2e21c000001'
var listenPort = 8766
zt.simpleStart('./tmp/' + earth, earth)
var addr = zt.getIpV4Address(earth).split('/')[0]
var socket = zt.socket()
zt.bindPort(socket, addr, listenPort)
// zt.listen(socket)
console.log('socket fd', socket)
console.log('ip a', addr)
console.log(`http://${addr}:${listenPort}`)
var server = http.createServer(function (request, response) {
response.writeHead(200, { 'Content-Type': 'text/plain' })
response.end('Hello World\n')
})
// attempt to listen to file descriptor.
// doesn't work, but would be cool!
server.listen({ fd: socket })
// listen on localhost:8765. works
// server.listen(8765)

13
examples/node/libzt.js Normal file
View File

@@ -0,0 +1,13 @@
var nbind = require('nbind')
var ZT = nbind.init().lib.ZT
module.exports = {
accept: ZT.accept,
bindPort: ZT.bind,
getDeviceId: ZT.getDeviceId,
getIpV4Address: ZT.getIpV4Address,
listen: ZT.listen,
running: ZT.running,
simpleStart: ZT.simpleStart,
socket: ZT.socket
}

View File

@@ -0,0 +1,35 @@
{
"name": "node-libzt",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"autogypi": "autogypi",
"node-gyp": "node-gyp",
"format": "prettier-standard '*.js'",
"preinstall": "mkdir -p libzt; cd libzt; ln -sf ../../../include include; ln -sf ../../../zto zto; ln -sf ../../../build build",
"postinstall": "npm run -- node-gyp rebuild"
},
"lint-staged": {
"linters": {
"src/**/*.js": [
"prettier-standard",
"git add"
]
}
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"husky": "^0.14.3",
"lint-staged": "^4.0.3",
"node-gyp": "^3.6.2",
"prettier-standard": "^6.0.0"
},
"dependencies": {
"autogypi": "^0.2.2",
"nbind": "^0.3.13"
}
}

41
examples/node/test.js Normal file
View File

@@ -0,0 +1,41 @@
var fs = require('fs')
var zt = require('./libzt')
var running = zt.running()
console.log('running', running)
var earth = '8056c2e21c000001'
var listenPort = 1234
zt.simpleStart('./tmp/' + earth, earth)
var deviceId = zt.getDeviceId()
console.log('device id', deviceId)
var addr = zt.getIpV4Address(earth).split('/')[0]
console.log('ip a', addr)
var socket = zt.socket()
console.log('socket', socket)
var bindPort = zt.bindPort(socket, addr, listenPort)
console.log('bind', bindPort)
var listen = zt.listen(socket)
console.log('listen', listen)
console.log()
console.log('ready')
console.log(`run this in another terminal:\n\tnc ${addr} ${listenPort}`)
console.log(`then type something`)
console.log()
var fd = zt.accept(socket)
fd = fs.createReadStream(null, { fd: fd })
fd.pipe(process.stdout)
// Start reading from stdin so we don't exit.
process.stdin.resume()
// lib.ZT.stop()

View File