int main() { struct ev_loop *l = ev_default_loop(0); ev_stat est; ev_stat_init(&est, stat_cb, ".", 0.); ev_stat_start(l, &est); ev_loop(l, 0); }
int main(int argc, char argv[]) { EV_P = ev_default_loop(0); ev_stat stat_watcher; ev_init(&stat_watcher, stat_callback); ev_stat_set(&stat_watcher, "/home/dj/my/workspace/my_prj/libev_prj/hello.txt", 0); ev_stat_start(EV_A, &stat_watcher); ev_run(EV_A, 0); return 0; }
int mrkthr_stat_wait(mrkthr_stat_t *st) { int res; mnhash_item_t *hit; ev_item_t *ev; assert(st->ev->ty == EV_TYPE_STAT); me->pdata.ev = st->ev; if (st->ev->ev.stat.data == NULL) { st->ev->ev.stat.data = me; } else if (st->ev->ev.stat.data != me) { /* * in this case we are not allowed to wait for this event, * sorry. */ me->co.rc = MRKTHR_CO_RC_SIMULTANEOUS; return -1; } #ifdef TRACE_VERBOSE CTRACE(FBBLUE("starting ev_stat %s/%d"), st->ev->ev.stat.path, st->ev->ev.stat.wd); #endif ev_stat_start(the_loop, &st->ev->ev.stat); /* wait for an event */ me->co.state = CO_STATE_READ; res = yield(); if (res != 0) { return -1; } if ((hit = hash_get_item(&events, st->ev)) == NULL) { FAIL("mrkthr_stat_wait"); } ev = hit->key; assert(ev == st->ev); assert(ev->ev.stat.data == me); ev->ev.stat.data = NULL; me->pdata.ev = NULL; return res; }
/** * call-seq: * Coolio::StatWatcher.attach(loop) -> Coolio::StatWatcher * * Attach the stat watcher to the given Coolio::Loop. If the watcher is already * attached to a loop, detach it from the old one and attach it to the new one. */ static VALUE Coolio_StatWatcher_attach(VALUE self, VALUE loop) { ev_tstamp interval, timeout; struct Coolio_Loop *loop_data; struct Coolio_Watcher *watcher_data; if(!rb_obj_is_kind_of(loop, cCoolio_Loop)) rb_raise(rb_eArgError, "expected loop to be an instance of Coolio::Loop"); Data_Get_Struct(loop, struct Coolio_Loop, loop_data); Data_Get_Struct(self, struct Coolio_Watcher, watcher_data); if(watcher_data->loop != Qnil) Coolio_StatWatcher_detach(self); watcher_data->loop = loop; ev_stat_start(loop_data->ev_loop, &watcher_data->event_types.ev_stat); rb_call_super(1, &loop); return self; }
int main(int argc, char *argv[]) { // read options char *dir = NULL; short badarg = 0; int opt; while ((opt = getopt(argc, argv, "d:")) != -1) { if (opt == 'd') dir = optarg; else badarg = 1; } if (badarg) { print_usage(); return 1; } // change dir dir = flon_path(argv[0], dir); if (chdir(dir) != 0) { fgaj_r("couldn't chdir to %s", dir); return 1; } fgaj_i("-d %s", dir); // load configuration if (flon_configure(".") != 0) { fgaj_r("couldn't read %s/etc/flon.json, cannot start", dir); return 1; } free(dir); // set up logging flon_setup_logging("dispatcher"); // scan once scan_dir(); // load timers flon_load_timers(); // then, ev... struct ev_loop *l = ev_default_loop(0); // watch var/spool/dis/ ev_stat est; ev_stat_init(&est, spool_cb, "var/spool/dis/", 0.); ev_stat_start(l, &est); // check from time to time too ev_periodic epe; ev_periodic_init(&epe, trigger_cb, 0., .35, trigger_reschedule_cb); ev_periodic_start(l, &epe); //ev_timer eti; //ev_timer_init(&eti, do_something_when_loop_ready_cb, 0., 0.); //ev_timer_start(l, &eti); ev_signal esi; ev_signal_init(&esi, sighup_cb, SIGHUP); ev_signal_start(l, &esi); // loop //fgaj_i("about to ev_loop..."); ev_loop(l, 0); fgaj_r("something went wrong"); }
int main(int argc, char *argv[]) { struct hostent *server_host; char *server_host_str = "localhost"; char *certificate_file = NULL; char *key_file = NULL; char *password_file = NULL; char *token_file = NULL; char *username = "******"; int port = 64738; int ret; int development_mode = 0; int socket_fd; struct sockaddr_in server_addr; SSLRead socket_watcher; ev_io user_thread_watcher; ev_timer ping_watcher; ev_signal signal_watcher; ev_loop_main = EV_DEFAULT; /* * Lua initialization */ lua = luaL_newstate(); if (lua == NULL) { fprintf(stderr, "%s: could not initialize Lua\n", PIEPAN_NAME); return 1; } luaL_openlibs(lua); if (luaL_loadbuffer(lua, (const char *)src_piepan_impl_luac, src_piepan_impl_luac_len, "piepan_impl") != LUA_OK) { fprintf(stderr, "%s: could not load piepan implementation\n", PIEPAN_NAME); return 1; } lua_call(lua, 0, 0); lua_getglobal(lua, "piepan"); lua_getfield(lua, -1, "internal"); lua_getfield(lua, -1, "api"); lua_pushcfunction(lua, api_init); lua_setfield(lua, -2, "apiInit"); lua_settop(lua, 0); /* * Argument parsing */ { int opt; int i; int show_help = 0; int show_version = 0; lua_getglobal(lua, "piepan"); lua_getfield(lua, -1, "internal"); lua_getfield(lua, -1, "events"); lua_getfield(lua, -1, "onArgument"); opterr = 0; while ((opt = getopt(argc, argv, "u:c:k:s:t:p:-:dhv")) != -1) { switch (opt) { case 'u': username = optarg; break; case 'c': certificate_file = optarg; if (key_file == NULL) { key_file = certificate_file; } break; case 'k': key_file = optarg; break; case 's': { char *port_str; server_host_str = optarg; port_str = strrchr(server_host_str, ':'); if (port_str != NULL) { *port_str = '\0'; port = atoi(++port_str); } break; } case 't': token_file = optarg; break; case 'p': password_file = optarg; break; case '-': { char *key = optarg; char *value = strchr(key, '='); if (key == value) { break; } if (value != NULL) { *value++ = 0; } lua_pushvalue(lua, -1); lua_pushstring(lua, key); lua_pushstring(lua, value); lua_call(lua, 2, 0); break; } case 'd': development_mode = 1; break; case 'h': usage(stdout); return 0; case 'v': printf("%s %s (compiled on " __DATE__ " " __TIME__ ")\n", PIEPAN_NAME, PIEPAN_VERSION); return 0; default: fprintf(stderr, "%s: unknown or incomplete option '%c'\n", PIEPAN_NAME, optopt); return 1; } } lua_settop(lua, 0); } /* * Load user scripts */ { int i; lua_getglobal(lua, "piepan"); lua_getfield(lua, -1, "internal"); lua_getfield(lua, -1, "events"); lua_getfield(lua, -1, "onLoadScript"); for (i = optind; i < argc; i++) { lua_pushvalue(lua, -1); lua_pushstring(lua, argv[i]); if (development_mode) { lua_newuserdata(lua, sizeof(ScriptStat)); } else { lua_pushnil(lua); } lua_call(lua, 2, 3); if (lua_toboolean(lua, -3)) { if (development_mode) { ScriptStat *item = lua_touserdata(lua, -1); item->lua = lua; item->id = lua_tointeger(lua, -2); item->filename = argv[i]; ev_stat_init(&item->ev, script_stat_event, item->filename, 0); ev_stat_start(ev_loop_main, &item->ev); } } else { fprintf(stderr, "%s: %s\n", PIEPAN_NAME, lua_tostring(lua, -2)); } lua_pop(lua, 3); } lua_settop(lua, 0); } /* * Initialize Opus */ { OpusEncoder *encoder; int error; lua_getglobal(lua, "piepan"); lua_getfield(lua, -1, "internal"); lua_getfield(lua, -1, "opus"); encoder = lua_newuserdata(lua, opus_encoder_get_size(1)); lua_setfield(lua, -2, "encoder"); error = opus_encoder_init(encoder, 48000, 1, OPUS_APPLICATION_AUDIO); if (error != OPUS_OK) { fprintf(stderr, "%s: could not initialize the Opus encoder: %s\n", PIEPAN_NAME, opus_strerror(error)); return 1; } opus_encoder_ctl(encoder, OPUS_SET_VBR(0)); /* TODO: set this to the server's max bitrate */ opus_encoder_ctl(encoder, OPUS_SET_BITRATE(40000)); lua_settop(lua, 0); } /* * SSL initialization */ SSL_library_init(); ssl_context = SSL_CTX_new(SSLv23_client_method()); if (ssl_context == NULL) { fprintf(stderr, "%s: could not create SSL context\n", PIEPAN_NAME); return 1; } if (certificate_file != NULL) { if (!SSL_CTX_use_certificate_chain_file(ssl_context, certificate_file) || !SSL_CTX_use_PrivateKey_file(ssl_context, key_file, SSL_FILETYPE_PEM) || !SSL_CTX_check_private_key(ssl_context)) { fprintf(stderr, "%s: could not load certificate and/or key file\n", PIEPAN_NAME); return 1; } } /* * Socket initialization and connection */ socket_fd = socket(AF_INET, SOCK_STREAM, 0); if (socket_fd < 0) { fprintf(stderr, "%s: could not create socket\n", PIEPAN_NAME); return 1; } memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_host = gethostbyname(server_host_str); if (server_host == NULL || server_host->h_addr_list[0] == NULL || server_host->h_addrtype != AF_INET) { fprintf(stderr, "%s: could not parse server address\n", PIEPAN_NAME); return 1; } memmove(&server_addr.sin_addr, server_host->h_addr_list[0], server_host->h_length); ret = connect(socket_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)); if (ret != 0) { fprintf(stderr, "%s: could not connect to server\n", PIEPAN_NAME); return 1; } ssl = SSL_new(ssl_context); if (ssl == NULL) { fprintf(stderr, "%s: could not create SSL object\n", PIEPAN_NAME); return 1; } if (SSL_set_fd(ssl, socket_fd) == 0) { fprintf(stderr, "%s: could not set SSL file descriptor\n", PIEPAN_NAME); return 1; } if (SSL_connect(ssl) != 1) { fprintf(stderr, "%s: could not create secure connection\n", PIEPAN_NAME); return 1; } /* * User thread pipe */ if (pipe(user_thread_pipe) != 0) { fprintf(stderr, "%s: could not create user thread pipe\n", PIEPAN_NAME); return 1; } /* * Trigger initial event */ lua_getglobal(lua, "piepan"); lua_getfield(lua, -1, "internal"); lua_getfield(lua, -1, "initialize"); lua_newtable(lua); lua_pushstring(lua, username); lua_setfield(lua, -2, "username"); if (password_file != NULL) { lua_pushstring(lua, password_file); lua_setfield(lua, -2, "passwordFile"); } if (token_file != NULL) { lua_pushstring(lua, token_file); lua_setfield(lua, -2, "tokenFile"); } lua_pushlightuserdata(lua, lua); lua_setfield(lua, -2, "state"); lua_call(lua, 1, 0); lua_settop(lua, 0); /* * Event loop */ ev_signal_init(&signal_watcher, signal_event, SIGINT); ev_signal_start(ev_loop_main, &signal_watcher); ev_io_init(&socket_watcher.ev, socket_read_event, socket_fd, EV_READ); socket_watcher.lua = lua; socket_watcher.ssl = ssl; ev_io_start(ev_loop_main, &socket_watcher.ev); ev_io_init(&user_thread_watcher, user_thread_event, user_thread_pipe[0], EV_READ); ev_io_start(ev_loop_main, &user_thread_watcher); ev_timer_init(&ping_watcher, ping_event, PING_TIMEOUT, PING_TIMEOUT); ev_timer_start(ev_loop_main, &ping_watcher); ev_run(ev_loop_main, 0); /* * Cleanup */ lua_getglobal(lua, "piepan"); lua_getfield(lua, -1, "internal"); lua_getfield(lua, -1, "events"); lua_getfield(lua, -1, "onDisconnect"); if (lua_isfunction(lua, -1)) { lua_newtable(lua); lua_call(lua, 1, 0); } SSL_shutdown(ssl); /* TODO: sigpipe is triggered here if connection breaks */ close(socket_fd); lua_close(lua); return 0; }
int main(int argc, char **argv) { ev_stat cfg; if (argc == 1) { printf("no cfg\n"); return 0; } if (argc == 2) { daemon(0, 0); } cfg_path = strdup(argv[1]); cfg_init(cfg_path); ev_stat_init (&cfg, cfg_cb, cfg_path, 2.); signal(SIGPIPE, SIG_IGN); struct ev_loop *loop = ev_default_loop (0); work_loop = ev_loop_new(0); int listen_fd; struct sockaddr_in listen_addr; int reuseaddr_on = 1; pthread_mutex_init(&lock, NULL); thread_create(work, NULL); listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd < 0) { perror("listen failed"); return -1; } if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on)) == -1) { perror("setsockopt failed"); return -1; } memset(&listen_addr, 0, sizeof(listen_addr)); listen_addr.sin_family = AF_INET; listen_addr.sin_addr.s_addr = INADDR_ANY; listen_addr.sin_port = htons(SERVER_PORT); if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) { perror("bind failed"); return -1; } if (listen(listen_fd, 128) < 0) { perror("listen failed"); return -1; } if (setnonblock(listen_fd) < 0) { perror("failed to set server socket to non-blocking"); return -1; } ev_io ev_accept; ev_io_init(&ev_accept, accept_cb, listen_fd, EV_READ); ev_io_start(loop, &ev_accept); if (cfg_path != NULL) ev_stat_start (loop, &cfg); ev_loop (loop, 0); return 0; }
static void stat_start (void *impl, flux_watcher_t *w) { assert (w->signature == STAT_SIG); ev_stat_start (w->r->loop, (ev_stat *)impl); }