void* Daemon::entry(void* data){ struct sync* td = (struct sync*)data; Daemon* daemon = td->daemon; /* setup self-pipe */ if ( pipe2(daemon->pipe, O_NONBLOCK) != 0 ){ int saved = errno; Log::fatal("main", "pipe2() returned %d: %s\n", saved, strerror(saved)); td->value = saved; sem_post(&td->semaphore); return NULL; } td->value = daemon->init(); sem_post(&td->semaphore); if ( td->value != 0 ){ return NULL; } if ( td->barrier ){ pthread_barrier_wait(td->barrier); } daemon->run(); daemon->cleanup(); /* close pipe */ close(daemon->pipe[0]); close(daemon->pipe[1]); /* free resources */ sem_destroy(&td->semaphore); free(td); return 0; }
int main(int argc, char **argv) { openni::Device &device = __device; openni::VideoStream &depth = __depth, &color = __color; openni::Status rc; char *prefix = strrchr(argv[0], '/')+1; char *cfgfile = new char[prefix-argv[0]+strlen(rgbdsend::config_file_name)+1]; strncpy(cfgfile, argv[0], prefix-argv[0]+1); strcpy(cfgfile+(prefix-argv[0]), rgbdsend::config_file_name); Config conf; if(conf.read(cfgfile) != 1) { printf("Config: Falling back to builtin presets.\n"); conf.setDefaults(); } delete[] cfgfile; CURL *curl = init_curl(); Daemon daemon; daemon.init(conf.daemon_port, conf.daemon_timeout); atexit(atexit_handler); init_openni(&device, &depth, &color, conf); int dw, dh, cw, ch; int tmp1, tmp2; if(!depth.getCropping(&tmp1, &tmp2, &dw, &dh)) { dw = depth.getVideoMode().getResolutionX(); dh = depth.getVideoMode().getResolutionY(); } if(!color.getCropping(&tmp1, &tmp2, &cw, &ch)) { cw = color.getVideoMode().getResolutionX(); ch = color.getVideoMode().getResolutionY(); } printf("Resolution:\nDepth: %dx%d @ %d fps\nColor: %dx%d @ %d fps\n", dw, dh, depth.getVideoMode().getFps(), cw, ch, color.getVideoMode().getFps()); std::queue<char *> onilist; Command cmd; while(1) { timeval t; t.tv_sec = conf.daemon_timeout; t.tv_usec = 0; fd_set fds; FD_ZERO(&fds); FD_SET(daemon.sock, &fds); FD_SET(daemon.csock, &fds); int in = select((daemon.csock > daemon.sock ? daemon.csock : daemon.sock)+1, &fds, 0, 0, &t); if(FD_ISSET(daemon.sock, &fds)) daemon.acceptConnection(); if(FD_ISSET(daemon.csock, &fds)) { char b[5]; int r = daemon.receiveCommand(&cmd); if(r == 0) { printf("Daemon Error: Could not receive command.\n"); daemon.closeConnection(); continue; } if(r == 2) // keep alive continue; if(strncmp(cmd.header, "capt", 4) == 0) { printf("Received capture command.\n"); char *newfile = new char[rgbdsend::filename_bufsize]; if(record_oni(newfile, rgbdsend::filename_bufsize, depth, color, conf) == true) { onilist.push(newfile); } else { delete[] newfile; } daemon.sendCommand("okay", 0, 0); } else if(strncmp(cmd.header, "thmb", 4) == 0) { printf("Received thumbnail command.\n"); unsigned char *thumbbuf = NULL; long unsigned int size = 0; capture_thumbnail(&thumbbuf, &size, color); printf("Captured thumbnail. %ld bytes\n", size); daemon.sendCommand("stmb", thumbbuf, size); // delete[] thumbbuf; seems like libjpeg handles this. but I'm not sure. } else if(strncmp(cmd.header, "quit", 4) == 0) { daemon.closeConnection(); } else { printf("Daemon Error: Received undefined command.\n"); } } if(daemon.csock != -1 && in <= 0) { daemon.closeConnection(); } if(daemon.csock == -1 && !onilist.empty()) process_onis(onilist, curl, conf); } cleanup_curl(curl); cleanup_openni(device, depth, color); }