int sig_setup() { setup_sig_handler(SIGINT, SIG_IGN); setup_sig_handler(SIGHUP, exit_halt ); setup_sig_handler(SIGTERM, exit_halt); return 1; }
int main(int argc, char * argv[]) { int socket_id, e; char *hostname, *username; int sending = 0; if (argc < 3) { printf("Usage: client <hostname> <username>\n"); exit(1); } else { hostname = argv[1]; username = argv[2]; if (argc >= 4) { sending = 1; }; } struct user me = new_user(username); setup_sig_handler(); e = socket_id = connect_to_server(hostname, PORT); if (e < 0) check_errors("Error connecting to server", e); e = handshake(socket_id, me); if (e < 0) { printf("Username already taken\n"); running = 0; } else { printf("Handshake succesfull!\n"); } while (running) { if (sending) { e = send_messages(socket_id, me); } else { e = receive_messages(socket_id); } if (e < 0) running = 0; } cleanup(socket_id); }
SEXP mc_fork(SEXP sEstranged) { int pipefd[2]; /* write end, read end */ int sipfd[2]; pid_t pid; int estranged = (asInteger(sEstranged) > 0); SEXP res = allocVector(INTSXP, 3); int *res_i = INTEGER(res); if (!estranged) { if (pipe(pipefd)) error(_("unable to create a pipe")); if (pipe(sipfd)) { close(pipefd[0]); close(pipefd[1]); error(_("unable to create a pipe")); } #ifdef MC_DEBUG Dprintf("parent[%d] created pipes: comm (%d->%d), sir (%d->%d)\n", getpid(), pipefd[1], pipefd[0], sipfd[1], sipfd[0]); #endif } /* make sure we get SIGCHLD to clean up the child process */ setup_sig_handler(); fflush(stdout); // or children may output pending text pid = fork(); if (pid == -1) { if (!estranged) { close(pipefd[0]); close(pipefd[1]); close(sipfd[0]); close(sipfd[1]); } error(_("unable to fork, possible reason: %s"), strerror(errno)); } res_i[0] = (int) pid; if (pid == 0) { /* child */ R_isForkedChild = 1; /* don't track any children of the child by default */ signal(SIGCHLD, SIG_DFL); if (estranged) res_i[1] = res_i[2] = NA_INTEGER; else { close(pipefd[0]); /* close read end */ master_fd = res_i[1] = pipefd[1]; res_i[2] = NA_INTEGER; /* re-map stdin */ dup2(sipfd[0], STDIN_FILENO); close(sipfd[0]); } is_master = 0; /* master uses USR1 to signal that the child process can terminate */ child_exit_status = -1; if (estranged) child_can_exit = 1; else { child_can_exit = 0; signal(SIGUSR1, child_sig_handler); } #ifdef MC_DEBUG Dprintf("child process %d started\n", getpid()); #endif } else { /* master process */ child_info_t *ci; if (estranged) { /* don't even register it */ res_i[1] = res_i[2] = NA_INTEGER; return res; } close(pipefd[1]); /* close write end of the data pipe */ close(sipfd[0]); /* close read end of the child-stdin pipe */ res_i[1] = pipefd[0]; res_i[2] = sipfd[1]; #ifdef MC_DEBUG Dprintf("parent registers new child %d\n", pid); #endif /* register the new child and its pipes */ ci = (child_info_t*) malloc(sizeof(child_info_t)); if (!ci) error(_("memory allocation error")); rm_closed(); /* clean up the list by removing closed entries */ ci->pid = pid; ci->pfd = pipefd[0]; ci->sifd= sipfd[1]; ci->next = children; children = ci; } return res; /* (pid, fd of data pipe, fd of child-stdin pipe) */ }
int main(int argc, char **argv) { gzFile file; const char *filename; char header[22]; unsigned char buf[65535]; const char *args = "n:o:r:tdh"; int number = 0; /* (n) total number to emit */ struct lwes_emitter *emitter = NULL; /* (o) where to send the events */ int rate = 0; /* (r) number per second for emission */ bool use_timings = false; /* (t) using timings from file */ bool repeat = false; /* (d) rerun journals over and over */ int ret = 0; LWES_INT_64 start = 0LL; LWES_INT_64 stop = 0LL; /* turn off error messages, I'll handle them */ opterr = 0; while (1) { char c = getopt (argc, argv, args); if (c == -1) { break; } switch (c) { case 'n': number = atoi(optarg); break; case 'r': rate = atoi(optarg); break; case 'o': emitter = handle_transport_arg (optarg); if (emitter == NULL) { fprintf (stderr, "ERROR: problem with emitter\n"); ret = 1; goto cleanup; } break; case 't': use_timings = true; break; case 'd': repeat = true; break; case 'h': fprintf (stderr, "%s", help); ret = 1; goto cleanup; default: fprintf (stderr, "WARNING: unrecognized command line option -%c\n", optopt); } } if (optind >= argc) { fprintf (stderr, "ERROR: journal file is required\n"); ret = 1; goto cleanup; } /* setup sigint/sigkill/sigpipe handler */ setup_sig_handler(); start = currentTimeMillisLongLong (); int start_index = optind; int num_files = argc - optind; int offset = 0; int total_count = 0; int this_second_count = 0; unsigned long long delay_micros = rate > 0 ? 1000000 / rate : 0; bool done = false; while (! done) { /* deal with multiple files and repeating */ filename = argv[start_index + offset]; if (! repeat && offset == (num_files - 1)) { done = true; /* last file and we are not repeating, so done after it's processed */ } offset = (offset + 1) % num_files; /* skip to next file if available */ file = gzopen (filename, "rb"); struct timeval start_emit_time; micro_now (&start_emit_time); struct timeval current_emit_time = { 0, 0 }; struct timeval previous_emit_time = { 0, 0 }; unsigned long long time_between_emit_events = 0ULL; unsigned long long start_file_timestamp = 0ULL; unsigned long long end_file_timestamp = 0ULL; int file_count = 0; unsigned long long time_to_sleep = 0ULL; /* read a header from the file */ while (gzread (file, header, 22) == 22) { unsigned short size = header_payload_length (header); unsigned long long cur_file_timestamp = (unsigned long long)(header_receipt_time (header)); if (start_file_timestamp == 0ULL) { start_file_timestamp = cur_file_timestamp; } end_file_timestamp = cur_file_timestamp; /* read an event from the file */ if (gzread (file, buf, size) != size) { fprintf (stderr, "ERROR: failure reading journal\n"); done = true; ret=1; break; } /* keep track of the current emit time */ micro_now (¤t_emit_time); /* if we are using timings determine how long to sleep before * emitting the next event */ if (use_timings) { unsigned long long time_between_file_events = 0ULL; time_between_file_events = (cur_file_timestamp - start_file_timestamp) * 1000ULL; time_between_emit_events = micro_timediff (&start_emit_time, ¤t_emit_time); time_to_sleep = time_between_file_events > time_between_emit_events ? time_between_file_events - time_between_emit_events : 0ULL; /* sleep some number of microseconds */ if (time_to_sleep > 0) { usleep (time_to_sleep); } } /* if we are trying to meet a rate, determine how long to sleep * before next call */ if (rate > 0) { if (previous_emit_time.tv_sec != 0) { /* the difference in micros alway includes the time slept, * so to figure out how much time we should sleep we subtract * the previous slept time from how much time it's actually * been since the last emit. In other words sleep time plus * processing time should equal delay_ms */ unsigned long long diff = micro_timediff (&previous_emit_time, ¤t_emit_time) - time_to_sleep; time_to_sleep = delay_micros > diff ? delay_micros - diff : 0ULL; if (time_to_sleep > 0) { usleep (time_to_sleep); } } } /* then emit the event */ if (lwes_emitter_emit_bytes (emitter, buf, size) != size) { fprintf (stderr, "ERROR: failure emitting\n"); done = true; ret=1; break; } /* keep track of some counts */ file_count++; total_count++; this_second_count++; previous_emit_time = current_emit_time; /* if we are emitting a limited number we will be done if we hit * that number */ if (number > 0 && number == total_count) { done = true; ret=2; break; } if (gbl_sig) { done = true; ret=3; break; } } gzclose (file); fprintf (stderr, "emitted %d events from %s representing %lld file time " "in %llu milliseconds\n", file_count, filename, (end_file_timestamp - start_file_timestamp), micro_timediff (&start_emit_time, ¤t_emit_time) / 1000ULL); } /* if we hit the count limit (2) it is not an error */ if (ret == 2) { ret = 0; } stop = currentTimeMillisLongLong (); fprintf (stderr, "emitted %d events in %lld milliseconds\n", total_count, (stop - start)); cleanup: if (emitter != NULL) { lwes_emitter_destroy (emitter); } exit (ret); }
int main(int argc, char **argv) { // Setup signal handlers setup_sig_handler(); // Initialize CV images int width = 480; int height = 640; cv::Mat left_image(height, width, CV_8UC3); cv::Mat right_image(height, width, CV_8UC3); cv::Mat disp_image(height, width, CV_16SC3); // Create VidereCamera object vc = new VidereCamera(); // Setup ROS structures ros::init(argc, argv, "videre_camera_node", ros::init_options::AnonymousName); ros::NodeHandle vc_nh; // image_transport deals with image topics image_transport::ImageTransport it(vc_nh); image_transport::Publisher pub_left = it.advertise("videre_camera/left/image_raw", 1); image_transport::Publisher pub_right = it.advertise("videre_camera/right/image_raw", 1); // cv_bridge deals with conversion between cv::Mat and sensor_msgs::Image cv_bridge::CvImage msg_left; cv_bridge::CvImage msg_right; // regular publishers for camera info topics ros::Publisher pub_info_left = vc_nh.advertise<sensor_msgs::CameraInfo>("videre_camera/left/camera_info", 1); ros::Publisher pub_info_right = vc_nh.advertise<sensor_msgs::CameraInfo>("videre_camera/right/camera_info", 1); // camera info messages sensor_msgs::CameraInfo info_left; sensor_msgs::CameraInfo info_right; // We can set encoding prior to loop because it doesn't change msg_left.encoding = sensor_msgs::image_encodings::BGR8; msg_right.encoding = sensor_msgs::image_encodings::BGR8; // We can set camera info params prior to loop because they don't change info_left.height = vc->cp()->height(); info_left.width = vc->cp()->width(); info_left.distortion_model = vc->cp()->distortion_model(); vc->cp()->left_D(info_left.D); vc->cp()->left_K(info_left.K); vc->cp()->left_R(info_left.R); vc->cp()->left_P(info_left.P); info_right.height = vc->cp()->height(); info_right.width = vc->cp()->width(); info_right.distortion_model = vc->cp()->distortion_model(); vc->cp()->right_D(info_right.D); vc->cp()->right_K(info_right.K); vc->cp()->right_R(info_right.R); vc->cp()->right_P(info_right.P); // Set frame_ids msg_left.header.frame_id = "/videre_camera/left"; msg_right.header.frame_id = "/videre_camera/left"; info_left.header.frame_id = "/videre_camera/left"; info_right.header.frame_id = "/videre_camera/left"; // TO DO: USE CAMERAINFO_MANAGER ros::Rate loop_rate(15); while(ros::ok()) { ros::Time ts = ros::Time::now(); bool got_image = vc->GetData(); left_image = vc->left(); right_image = vc->right(); if(!got_image) { ROS_INFO("Error in camera_getimagepair, exiting program"); break; } // Set image timestamps msg_left.header.stamp = ts; msg_right.header.stamp = ts; // Set image data msg_left.image = left_image; msg_right.image = right_image; // Publish images pub_left.publish(msg_left.toImageMsg()); pub_right.publish(msg_right.toImageMsg()); // Set camera info timestamps info_left.header.stamp = ts; info_right.header.stamp = ts; // Publish camera info pub_info_left.publish(info_left); pub_info_right.publish(info_right); ros::spinOnce(); loop_rate.sleep(); } return 0; }
int main(int argc, char **argv) { unsigned int i, nb_channels; unsigned int buffer_size = SAMPLES_PER_READ; const char *arg_uri = NULL; const char *arg_ip = NULL; int c, option_index = 0; struct iio_device *dev; size_t sample_size; int timeout = -1; bool scan_for_context = false; while ((c = getopt_long(argc, argv, "+hn:u:t:b:s:T:a", options, &option_index)) != -1) { switch (c) { case 'h': usage(); return EXIT_SUCCESS; case 'n': arg_ip = optarg; break; case 'u': arg_uri = optarg; break; case 'a': scan_for_context = true; break; case 't': trigger_name = optarg; break; case 'b': buffer_size = atoi(optarg); break; case 's': num_samples = atoi(optarg); break; case 'T': timeout = atoi(optarg); break; case '?': return EXIT_FAILURE; } } if (argc == optind) { fprintf(stderr, "Incorrect number of arguments.\n\n"); usage(); return EXIT_FAILURE; } setup_sig_handler(); if (scan_for_context) ctx = scan(); else if (arg_uri) ctx = iio_create_context_from_uri(arg_uri); else if (arg_ip) ctx = iio_create_network_context(arg_ip); else ctx = iio_create_default_context(); if (!ctx) { fprintf(stderr, "Unable to create IIO context\n"); return EXIT_FAILURE; } if (timeout >= 0) iio_context_set_timeout(ctx, timeout); dev = iio_context_find_device(ctx, argv[optind]); if (!dev) { fprintf(stderr, "Device %s not found\n", argv[optind]); iio_context_destroy(ctx); return EXIT_FAILURE; } if (trigger_name) { struct iio_device *trigger = iio_context_find_device( ctx, trigger_name); if (!trigger) { fprintf(stderr, "Trigger %s not found\n", trigger_name); iio_context_destroy(ctx); return EXIT_FAILURE; } if (!iio_device_is_trigger(trigger)) { fprintf(stderr, "Specified device is not a trigger\n"); iio_context_destroy(ctx); return EXIT_FAILURE; } /* * Fixed rate for now. Try new ABI first, * fail gracefully to remain compatible. */ if (iio_device_attr_write_longlong(trigger, "sampling_frequency", DEFAULT_FREQ_HZ) < 0) iio_device_attr_write_longlong(trigger, "frequency", DEFAULT_FREQ_HZ); iio_device_set_trigger(dev, trigger); } nb_channels = iio_device_get_channels_count(dev); if (argc == optind + 1) { /* Enable all channels */ for (i = 0; i < nb_channels; i++) iio_channel_enable(iio_device_get_channel(dev, i)); } else { for (i = 0; i < nb_channels; i++) { unsigned int j; struct iio_channel *ch = iio_device_get_channel(dev, i); for (j = optind + 1; j < (unsigned int) argc; j++) { const char *n = iio_channel_get_name(ch); if (!strcmp(argv[j], iio_channel_get_id(ch)) || (n && !strcmp(n, argv[j]))) iio_channel_enable(ch); } } } sample_size = iio_device_get_sample_size(dev); buffer = iio_device_create_buffer(dev, buffer_size, false); if (!buffer) { char buf[256]; iio_strerror(errno, buf, sizeof(buf)); fprintf(stderr, "Unable to allocate buffer: %s\n", buf); iio_context_destroy(ctx); return EXIT_FAILURE; } while (app_running) { int ret = iio_buffer_refill(buffer); if (ret < 0) { if (app_running) { char buf[256]; iio_strerror(-ret, buf, sizeof(buf)); fprintf(stderr, "Unable to refill buffer: %s\n", buf); } break; } /* If there are only the samples we requested, we don't need to * demux */ if (iio_buffer_step(buffer) == sample_size) { void *start = iio_buffer_start(buffer); size_t read_len, len = (intptr_t) iio_buffer_end(buffer) - (intptr_t) start; if (num_samples && len > num_samples * sample_size) len = num_samples * sample_size; for (read_len = len; len; ) { size_t nb = fwrite(start, 1, len, stdout); if (!nb) goto err_destroy_buffer; len -= nb; start = (void *)((intptr_t) start + nb); } if (num_samples) { num_samples -= read_len / sample_size; if (!num_samples) quit_all(EXIT_SUCCESS); } } else { iio_buffer_foreach_sample(buffer, print_sample, NULL); } } err_destroy_buffer: iio_buffer_destroy(buffer); iio_context_destroy(ctx); return exit_code; }