int main(int argc, char **argv) { int ret; trap_ifc_spec_t ifc_spec; //char *ifc_params[] = {"traptestshm,650000;traptestshm,650000"}; //ifc_spec.types = "mm"; char *ifc_params[3] = { argv[0], "-i", "tt;localhost,11111,65533;11111,5,65533" }; int paramno = 3; uint64_t counter = 0; uint64_t iteration = 0; time_t duration; uint16_t payload_size; char *payload = NULL; //verbose = CL_VERBOSE_LIBRARY; trap_verbose = CL_VERBOSE_OFF; VERBOSE(CL_VERBOSE_OFF, "%s [number]\nnumber - size of data to send for testing", argv[0]); ret = trap_parse_params(¶mno, ifc_params, &ifc_spec); if (ret != TRAP_E_OK) { if (ret == TRAP_E_HELP) { // "-h" was found trap_print_help(&module_info); return 0; } fprintf(stderr, "ERROR in parsing of parameters for TRAP: %s\n", trap_last_error_msg); return 1; } // Initialize TRAP library (create and init all interfaces) ret = trap_init(&module_info, ifc_spec); if (ret != TRAP_E_OK) { fprintf(stderr, "ERROR in TRAP initialization %s\n", trap_last_error_msg); return 1; } duration = time(NULL); // Read data from input, process them and write to output while(!stop) { ret = trap_get_data(0, (const void **) &payload, &payload_size, TRAP_WAIT); if (ret != TRAP_E_OK) { VERBOSE(CL_ERROR, "error: %s", trap_last_error_msg); } ret = trap_send_data(0, (void *) payload, payload_size, TRAP_WAIT); if (ret == TRAP_E_OK) { counter++; } else { VERBOSE(CL_ERROR, "error: %s", trap_last_error_msg); } iteration++; if (counter == 10) { break; } } duration = time(NULL) - duration; printf("Number of iterations: %"PRIu64"\nLast sent: %"PRIu64"\nTime: %"PRIu64"s\n", (uint64_t) iteration, (uint64_t) counter-1, (uint64_t) duration); sleep(2); // Do all necessary cleanup before exiting // (close interfaces and free allocated memory) trap_finalize(); //free(payload); return 0; }
int main(int argc, char **argv) { int ret; char *recvdata = NULL; trap_ifc_spec_t ifc_spec; char *ifc_params[] = {"localhost,12345,4096"}; ifc_spec.types = "t"; ifc_spec.params = ifc_params; int timeout; #ifndef CONTINUOUS int i; #endif //verbose = CL_VERBOSE_LIBRARY; trap_verbose = CL_VERBOSE_OFF; // Initialize TRAP library (create and init all interfaces) ret = trap_init(&module_info, ifc_spec); if (ret != TRAP_E_OK) { VERBOSE(CL_ERROR, "TRAP initialization: %s", trap_last_error_msg); return 1; } signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); //#define BLOCKING #ifdef NONBLOCKING timeout = TRAP_NO_WAIT; #elif defined(BLOCKING) timeout = TRAP_WAIT; #else timeout = 5000000; /* wait for 5 secs */ #endif VERBOSE(CL_VERBOSE_OFF, "Receiving..."); // Read data from input, process them and write to output #ifndef CONTINUOUS for (i=0; i<100; ++i) { #else while (!stop) { #endif const void *data_ptr; uint16_t data_size; #ifndef CONTINUOUS printf("%03i: ", i); #endif // Receive data from any interface, wait until data are available ret = trap_get_data(TRAP_MASK_ALL, &data_ptr, &data_size, timeout); if (ret == TRAP_E_OK) { recvdata = (char *) calloc(1, data_size + 1); memcpy(recvdata, data_ptr, data_size); #ifndef DEBUG if (data_size > 10) { printf("recv(%hu): %.5s..%.5s\n", data_size, (char *) recvdata, (char *) (recvdata + data_size - 5)); } else { printf("recv(%u): %s\n", (unsigned int) data_size, (char *) recvdata); } #endif free(recvdata); } else if ((ret == TRAP_E_TERMINATED) || (ret == TRAP_E_IO_ERROR)) { printf("terminated or IO error\n"); break; } else if (ret == TRAP_E_TIMEOUT) { printf("trap_get_data() timeouted, this shouldn't happen!\n"); #ifdef WAITING sleep(6); #endif } else { printf("Error: trap_get_data() returned %i (%s)\n", ret, trap_last_error_msg); } } // Do all necessary cleanup before exiting // (close interfaces and free allocated memory) trap_finalize(); return 0; }
int main(int argc, char **argv) { int ret; INIT_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) // Declare progress structure, pointer to this struct, initialize progress limit NMCM_PROGRESS_DEF; get_o_param(argc, argv, module_getopt_string, long_options); /* output have to be known before TRAP init */ // ***** TRAP initialization ***** TRAP_DEFAULT_INITIALIZATION(argc, argv, *module_info); // Register signal handler. TRAP_REGISTER_DEFAULT_SIGNAL_HANDLER(); // Handles SIGTERM and SIGINT signal(SIGUSR1, signal_handler); signal(SIGALRM, send_handler); // ***** Create UniRec template ***** char *unirec_specifier = "PACKETS,BYTES"; signed char opt; while ((opt = TRAP_GETOPT(argc, argv, module_getopt_string, long_options)) != -1) { switch (opt) { case 'p': NMCM_PROGRESS_INIT(atoi(optarg), return 1); break; case 'P': nmcm_progress_ptr->print_char = optarg[0]; break; case 'o': /* proccessed earlier */ break; default: fprintf(stderr, "Invalid arguments.\n"); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); TRAP_DEFAULT_FINALIZATION(); return 3; } } ur_template_t *tmplt = ur_create_input_template(0, unirec_specifier, NULL); if (tmplt == NULL) { fprintf(stderr, "Error: Invalid UniRec specifier.\n"); TRAP_DEFAULT_FINALIZATION(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); return 4; } if (send_interval) { /* in case of -o option */ /* create new output tempate */ out_tmplt = ur_create_output_template(0,"FLOWS,PACKETS,BYTES", NULL); if (!out_tmplt) { fprintf(stderr, "Error: Invalid UniRec specifier (this is implementation error, contact author of the module).\n"); trap_finalize(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); ur_free_template(tmplt); return 4; } /* allocate space for output record with no dynamic part */ out_rec = ur_create_record(out_tmplt, 0); if (!out_rec) { ur_free_template(out_tmplt); TRAP_DEFAULT_FINALIZATION(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS); ur_free_template(tmplt); ur_free_template(out_tmplt); return 4; } /* Set NO_WAIT to output interface */ ret = trap_ifcctl(TRAPIFC_OUTPUT, 0, TRAPCTL_SETTIMEOUT, TRAP_NO_WAIT); if (ret != TRAP_E_OK) { ur_free_template(tmplt); ur_free_template(out_tmplt); ur_free_record(out_rec); fprintf(stderr, "Error: trap_ifcctl.\n"); trap_finalize(); FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS) return 4; } alarm(send_interval); /* arrange SIGARLM in send_interval seconds */ }