#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "fragment_dataformat.h" #define MIN_FRAGMENTS 0 #define MAX_FRAGMENTS 100 /* int rndError(float p){ float rndFloat = static_cast(rand()) / static_cast(RAND_MAX); return rndFloat < p; }*/ int makeSocket() { int sockfd; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket failed"); exit(EXIT_FAILURE); } return sockfd; } void connectTo(int sock, const char* host, int port) { struct sockaddr_in serv_addr; serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(host); serv_addr.sin_port = htons(port); if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("Connection failed"); exit(EXIT_FAILURE); } printf("Connected to %s: %d\n", host, port); } int main(int argc, char* argv[]) { //will use it later to obtain random number from hardware std::random_device rd; std::mt19937 generator(42); std::discrete_distribution discrete_distr({0.9, 0.1}); std::normal_distribution normal_distr(25., 10.); std::uniform_int_distribution unif_int_distib; std::uniform_real_distribution unif_float_distrib(0.5, 2); //commenting all old_style generation //srand (static_cast (42)); bool out_condition = true; uint32_t event_number = 0; uint32_t run_number = 0; if (argc != 4) { printf("Usage %s host portNumber runNumber\n", argv[0]); exit(EXIT_FAILURE); } const char* host = argv[1]; int port = atoi(argv[2]); run_number = atoi(argv[3]); int socket = makeSocket(); connectTo(socket, host, port); while (true) { Header header; header.runNumber = run_number; header.detectorEventNumber = event_number; event_number++; header.numberOfStatusElements = 0; //if swap to old style just change with function above and prob 0.1 for 1 uint8_t incorrectError = discrete_distr(generator); uint8_t corruptedError = discrete_distr(generator); uint8_t missingDataError = discrete_distr(generator); uint8_t timeoutError = discrete_distr(generator); uint32_t firstStatusElement = 0x0; if (incorrectError) firstStatusElement |= INCORRECT_ERROR; if (corruptedError) firstStatusElement |= CORRUPTED_ERROR; if (missingDataError) firstStatusElement |= MISSING_DATA_ERROR; if (timeoutError) firstStatusElement |= TIMEOUT_ERROR; if (firstStatusElement != 0x0) { header.numberOfStatusElements = 1; header.statusElementsArray = new uint32_t[header.numberOfStatusElements]; header.statusElementsArray[0] = firstStatusElement; } uint32_t payload_size = std::max(std::min(static_cast(std::round(normal_distr(generator))), MAX_FRAGMENTS), MIN_FRAGMENTS); header.headerSize = 6 + header.numberOfStatusElements; header.fragmentSize = header.headerSize + payload_size; Fragment fragment; fragment.header = header; fragment.payloadElements = new uint32_t[payload_size]; for (uint32_t i = 0; i < payload_size; i++) { fragment.payloadElements[i] = unif_int_distib(generator); } for (uint32_t i = 0; i < header.fragmentSize; i++) { } std::cout << std::hex << std::setw(8) << std::setfill('0'); //std::cout << fragment.header; for (uint8_t i = 0; i < fragment.header.numberOfStatusElements; i++) { std::cout << "status element " << std::setw(8) << std::setfill('0') << fragment.header.statusElementsArray[i] << std::endl; } for (uint32_t i = 0; i < payload_size; i++) { std::cout << "payload element " << std::setw(8) << std::setfill('0') << fragment.payloadElements[i] << std::endl; } std::cout << std::endl; if (header.numberOfStatusElements > 0){ delete [] fragment.header.statusElementsArray; } delete [] fragment.payloadElements; sleep(unif_float_distrib(generator)); } return 0; }