2023-08-06 15:10:18 +00:00
|
|
|
#include <algorithm>
|
2023-08-07 20:27:10 +00:00
|
|
|
#include <arpa/inet.h>
|
2023-08-06 15:10:18 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdint>
|
2023-08-07 20:27:10 +00:00
|
|
|
#include <cstdio>
|
2023-08-06 15:10:18 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2023-08-07 20:27:10 +00:00
|
|
|
#include <netinet/in.h>
|
2023-08-06 15:10:18 +00:00
|
|
|
#include <random>
|
2023-08-07 20:27:10 +00:00
|
|
|
#include <sys/socket.h>
|
2023-08-06 15:10:18 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2023-08-09 21:10:42 +00:00
|
|
|
#include <csignal>
|
2023-08-06 15:10:18 +00:00
|
|
|
|
|
|
|
|
2023-08-07 20:27:10 +00:00
|
|
|
#include "fragment_dataformat.h"
|
2023-08-06 15:10:18 +00:00
|
|
|
|
|
|
|
#define MIN_FRAGMENTS 0
|
|
|
|
#define MAX_FRAGMENTS 100
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
int rndError(float p){
|
|
|
|
float rndFloat = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
|
|
|
|
return rndFloat < p;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
2023-08-07 20:27:10 +00:00
|
|
|
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);
|
|
|
|
}
|
2023-08-06 15:10:18 +00:00
|
|
|
|
|
|
|
|
2023-08-09 21:10:42 +00:00
|
|
|
void term_handler(int signal) {
|
|
|
|
printf("Terminated, received SIGNAL %d", signal);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-06 15:10:18 +00:00
|
|
|
int main(int argc, char* argv[]) {
|
2023-08-09 21:10:42 +00:00
|
|
|
signal(SIGTERM, term_handler);
|
|
|
|
signal(SIGINT, term_handler);
|
|
|
|
|
2023-08-06 15:10:18 +00:00
|
|
|
//will use it later to obtain random number from hardware
|
|
|
|
std::random_device rd;
|
|
|
|
std::mt19937 generator(42);
|
|
|
|
std::discrete_distribution<uint8_t> discrete_distr({0.9, 0.1});
|
|
|
|
std::normal_distribution<float> normal_distr(25., 10.);
|
|
|
|
std::uniform_int_distribution<uint32_t> unif_int_distib;
|
|
|
|
std::uniform_real_distribution<float> unif_float_distrib(0.5, 2);
|
|
|
|
|
|
|
|
//commenting all old_style generation
|
|
|
|
//srand (static_cast <unsigned> (42));
|
|
|
|
|
|
|
|
bool out_condition = true;
|
|
|
|
uint32_t event_number = 0;
|
|
|
|
|
|
|
|
uint32_t run_number = 0;
|
2023-08-09 21:10:42 +00:00
|
|
|
uint32_t source_id = 0;
|
2023-08-08 21:00:41 +00:00
|
|
|
|
2023-08-09 21:10:42 +00:00
|
|
|
if (argc != 5) {
|
|
|
|
printf("Usage %s host portNumber sourceId runNumber\n", argv[0]);
|
2023-08-08 21:00:41 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2023-08-06 15:10:18 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 21:00:41 +00:00
|
|
|
const char* host = argv[1];
|
|
|
|
int port = atoi(argv[2]);
|
2023-08-09 21:10:42 +00:00
|
|
|
source_id = atoi(argv[3]);
|
|
|
|
run_number = atoi(argv[4]);
|
2023-08-08 21:00:41 +00:00
|
|
|
|
|
|
|
int socket = makeSocket();
|
|
|
|
connectTo(socket, host, port);
|
|
|
|
|
2023-08-06 15:10:18 +00:00
|
|
|
while (true) {
|
|
|
|
Header header;
|
2023-08-09 21:10:42 +00:00
|
|
|
header.sourceIdentifier = source_id;
|
2023-08-06 15:10:18 +00:00
|
|
|
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<int>(std::round(normal_distr(generator))), MAX_FRAGMENTS), MIN_FRAGMENTS);
|
2023-08-09 21:10:42 +00:00
|
|
|
header.headerSize = 7 + header.numberOfStatusElements;
|
2023-08-06 15:10:18 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-08-09 21:10:42 +00:00
|
|
|
uint32_t buffer[header.fragmentSize];
|
|
|
|
encode_fragment(buffer, fragment);
|
|
|
|
ssize_t bytes = send(socket, reinterpret_cast<char*>(buffer), sizeof(buffer), 0);
|
|
|
|
if (bytes != header.fragmentSize * sizeof(uint32_t)) {
|
2023-08-21 21:39:16 +00:00
|
|
|
perror("Send failed: num bytes not matching");
|
2023-08-09 21:10:42 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2023-08-07 20:27:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 21:10:42 +00:00
|
|
|
|
|
|
|
//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;
|
2023-08-06 15:10:18 +00:00
|
|
|
|
|
|
|
if (header.numberOfStatusElements > 0){
|
|
|
|
delete [] fragment.header.statusElementsArray;
|
|
|
|
}
|
|
|
|
delete [] fragment.payloadElements;
|
|
|
|
|
|
|
|
|
|
|
|
sleep(unif_float_distrib(generator));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|