void jack_finish(void) { intclient_running = 0; socket_finish(); pthread_join(intclient_socket_thread, NULL); effects_finish(0); protocol_remove_commands(); }
static void quit_cb(proto_t *proto) { protocol_response("resp 0", proto); protocol_remove_commands(); socket_finish(); effects_finish(1); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { /* Command line options */ static struct option long_options[] = { {"nofork", no_argument, 0, 'n'}, {"verbose", no_argument, 0, 'v'}, {"socket-port", required_argument, 0, 'p'}, {"interactive", no_argument, 0, 'i'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; int opt, opt_index = 0; /* parse command line options */ int nofork = 0, verbose = 0, socket_port = SOCKET_DEFAULT_PORT, interactive = 0; while ((opt = getopt_long(argc, argv, "nvp:iVh", long_options, &opt_index)) != -1) { switch (opt) { case 'n': nofork = 1; break; case 'v': verbose = 1; nofork = 1; break; case 'p': socket_port = atoi(optarg); break; case 'i': interactive = 1; nofork = 1; break; case 'V': printf( "%s version: %s\n" "source code: https://github.com/moddevices/mod-host\n", argv[0], version); exit(EXIT_SUCCESS); break; case 'h': printf( "Usage: %s [-vih] [-p <port>]\n" " -v, --verbose verbose messages\n" " -p, --socket-port=<port> socket port definition\n" " -i, --interactive interactive mode\n" " -V, --version print program version and exit\n" " -h, --help print this help and exit\n", argv[0]); exit(EXIT_SUCCESS); } } if (! nofork) { int pid; pid = fork(); if (pid != 0) { printf("Forking... child PID: %d\n", pid); FILE *fd; fd = fopen(PID_FILE, "w"); if (fd == NULL) { fprintf(stderr, "can't open PID File\n"); } else { fprintf(fd, "%d\n", pid); fclose(fd); } exit(EXIT_SUCCESS); } } if (mod_host_init(NULL, socket_port) != 0) { exit(EXIT_FAILURE); return 1; } /* Interactice mode */ if (interactive) interactive_mode(); /* Verbose */ protocol_verbose(verbose); /* Report ready */ printf("mod-host ready!\n"); fflush(stdout); while (1) socket_run(1); socket_finish(); effects_finish(1); protocol_remove_commands(); return 0; }
int main(int argc, char **argv) { int verbose, socket_port, interactive; /* Command line options */ struct arg_lit *_verbose = arg_lit0("v", "verbose,debug", "verbose messages"); struct arg_int *_socket = arg_int0("p", "socket-port", "<port>", "socket port definition"); struct arg_lit *_interactive = arg_lit0("i", "interactive", "interactive mode"); struct arg_lit *_help = arg_lit0("h", "help", "print this help and exit"); struct arg_end *_end = arg_end(20); void *argtable[] = {_verbose, _socket, _interactive, _help, _end}; if (arg_nullcheck(argtable)) { fprintf(stderr, "argtable error: insufficient memory\n"); exit(EXIT_FAILURE); } /* Default value of command line arguments */ _socket->ival[0] = SOCKET_DEFAULT_PORT; /* Run the argument parser */ if (arg_parse(argc, argv, argtable) == 0) { if (_help->count > 0) { fprintf(stdout, "Usage: %s", argv[0]); arg_print_syntax(stdout, argtable, "\n"); arg_print_glossary(stdout, argtable, " %-30s %s\n"); exit(EXIT_SUCCESS); } verbose = _verbose->count; socket_port = _socket->ival[0]; interactive = _interactive->count; } else { arg_print_errors(stderr, _end, argv[0]); exit(EXIT_FAILURE); } arg_freetable(argtable, sizeof(argtable)/sizeof(argtable[0])); /* If verbose or interactive, don't fork */ if (!verbose && !interactive) { int pid; pid = fork(); if (pid != 0) { printf("Forking... child PID: %d\n", pid); FILE *fd; fd = fopen(PID_FILE, "w"); if (fd == NULL) { fprintf(stderr, "can't open PID File\n"); } else { fprintf(fd, "%d\n", pid); fclose(fd); } exit(EXIT_SUCCESS); } } /* Setup the protocol */ protocol_add_command(EFFECT_ADD, effects_add_cb); protocol_add_command(EFFECT_REMOVE, effects_remove_cb); protocol_add_command(EFFECT_CONNECT, effects_connect_cb); protocol_add_command(EFFECT_DISCONNECT, effects_disconnect_cb); protocol_add_command(EFFECT_BYPASS, effects_bypass_cb); protocol_add_command(EFFECT_PARAM_SET, effects_set_param_cb); protocol_add_command(EFFECT_PARAM_GET, effects_get_param_cb); protocol_add_command(EFFECT_PARAM_MON, effects_monitor_param_cb); protocol_add_command(MONITOR_ADDR_SET, monitor_addr_set_cb); protocol_add_command(LOAD_COMMANDS, load_cb); protocol_add_command(SAVE_COMMANDS, save_cb); protocol_add_command(HELP, help_cb); protocol_add_command(QUIT, quit_cb); /* Startup the effects */ if (effects_init()) return -1; /* Setup the socket */ if (socket_start(socket_port, SOCKET_MSG_BUFFER_SIZE) < 0) { exit(EXIT_FAILURE); } socket_set_receive_cb(protocol_parse); /* Interactice mode */ if (interactive) interactive_mode(); /* Verbose */ protocol_verbose(verbose); while (1) socket_run(); protocol_remove_commands(); socket_finish(); effects_finish(); return 0; }