diff --git a/std-udp-recv/test/CMakeLists.txt b/std-udp-recv/test/CMakeLists.txt index e947c2d..aac6abd 100644 --- a/std-udp-recv/test/CMakeLists.txt +++ b/std-udp-recv/test/CMakeLists.txt @@ -1,8 +1,13 @@ add_executable(std-udp-recv-tests main.cpp) + target_link_libraries(std-udp-recv-tests core-buffer-lib std-udp-recv-lib gtest ) +add_executable(std-udp-sim simulator.cpp) +target_link_libraries(std-udp-sim + core-buffer-lib + ) diff --git a/std-udp-recv/test/simulator.cpp b/std-udp-recv/test/simulator.cpp new file mode 100644 index 0000000..3fb5fd6 --- /dev/null +++ b/std-udp-recv/test/simulator.cpp @@ -0,0 +1,81 @@ +#include +#include +#include "../include/UdpRecvConfig.hpp" +#include +#include "mock/udp.hpp" + +#ifdef USE_EIGER +#include "eiger.hpp" +#else +#include "jungfrau.hpp" +#endif + +const int MAX_IMAGE_ID = 10000; + +using namespace std; + +int main(int argc, char **argv) { + + if (argc != 3) { + cout << endl; + cout << "Usage: std_udp_sim [detector_json_filename] [bit_depth]"; + cout << endl; + cout << "\tdetector_json_filename: detector config file path." << endl; + cout << "\tbit_depth: bit depth of the incoming udp packets." << endl; + cout << endl; + exit(-1); + } + + const auto config = UdpRecvConfig::from_json_file(string(argv[1])); + const int bit_depth = atoi(argv[2]); + + if (DETECTOR_TYPE != config.detector_type) { + throw runtime_error("UDP recv version for " + DETECTOR_TYPE + + " but config for " + config.detector_type); + } + + const size_t FRAME_N_BYTES = MODULE_N_PIXELS * bit_depth / 8; + const size_t N_PACKETS_PER_FRAME = FRAME_N_BYTES / DATA_BYTES_PER_PACKET; + + int sockets[config.n_modules]; + sockaddr_in send_address[config.n_modules]; + + for (int i_module=0; i_module < config.n_modules; i_module++){ + send_address[i_module] = + get_server_address(config.start_udp_port + i_module); + + sockets[i_module] = socket(AF_INET,SOCK_DGRAM,0); + if (sockets[i_module] < 0) { + throw runtime_error("Cannot bind UDP socket."); + } + } + + uint64_t image_id = 0; + while (true) { + for (size_t i_packet = 0; i_packet < N_PACKETS_PER_FRAME; i_packet++) { + for (int i_module = 0; i_module < config.n_modules; i_module++) { + + det_packet send_udp_buffer; + send_udp_buffer.packetnum = i_packet; + + // framenum as image_id for the Eiger and bunchid for the JF +#ifdef USE_EIGER + send_udp_buffer.framenum = image_id; + send_udp_buffer.bunchid = image_id + 100; +#else + send_udp_buffer.framenum = image_id+100; + send_udp_buffer.bunchid = image_id; +#endif + + ::sendto( + sockets[i_module], + &send_udp_buffer, + BYTES_PER_PACKET, + -1, + (sockaddr *) &send_address[i_module], + sizeof(sockaddr_in)); + } + } + image_id = ++image_id % MAX_IMAGE_ID; + } +} \ No newline at end of file