static void js_srv(struct v7 *v7, struct v7_val *result, ns_callback_t cb, struct v7_val **args, int num_args) { struct v7_val *listening_port; struct ns_server *srv; if (num_args < 1 || args[0]->type != V7_OBJ || (listening_port = v7_lookup(args[0], "listening_port")) == NULL) return; result->type = V7_OBJ; srv = (struct ns_server *) calloc(1, sizeof(*srv)); ns_server_init(srv, result, cb); //v7_set_num(v7, result, "_priv", (unsigned long) srv); v7_set_obj(v7, result, "options", args[0]); switch (listening_port->type) { case V7_NUM: { char buf[100]; snprintf(buf, sizeof(buf), "%d", (int) listening_port->v.num); ns_bind(srv, buf); } break; case V7_STR: ns_bind(srv, listening_port->v.str.buf); break; default: fprintf(stderr, "%s\n", "Invalid listening_port"); break; } while (srv->listening_sock != INVALID_SOCKET && s_received_signal == 0) { ns_server_poll(srv, 1000); } ns_server_free(srv); }
int main(int argc, char *argv[]) { const char *script = "nsv7.js", *port = "4000"; struct ns_server server; struct v7 *v7; int i; // Parse command line options for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-f") == 0 && i + 1 < argc) { script = argv[++i]; } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { port = argv[++i]; } else { elog(1, "Usage: %s [-f FILE] [-p PORT]", argv[0]); } } signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); // Initialize scripting engine v7 = v7_create(); v7_init_stdlib(v7); if (v7_exec_file(v7, script) != V7_OK) { elog(1, "Error executing %s", script); } // Initialize server ns_server_init(&server, v7, ev_handler); ns_bind(&server, port); while (s_received_signal == 0) { ns_server_poll(&server, 1000); } printf("Existing on signal %d\n", s_received_signal); v7_destroy(&v7); ns_server_free(&server); return EXIT_SUCCESS; }