57 lines
1.7 KiB
C++
57 lines
1.7 KiB
C++
// Copyright (2019-2022) Paul Scherrer Institute
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "UDPSimulator.h"
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
#include <cstring>
|
|
#include <fstream>
|
|
|
|
#include "../common/JFJochException.h"
|
|
#include "../common/Definitions.h"
|
|
|
|
UDPSimulator::UDPSimulator(const std::vector<uint16_t> &in_image) : image(in_image) {
|
|
if (image.size() != RAW_MODULE_SIZE)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Input image for simulator is wrong");
|
|
|
|
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
if (fd < 0)
|
|
throw JFJochException(JFJochExceptionCategory::UDPError,
|
|
"Cannot create UDP socket");
|
|
}
|
|
|
|
UDPSimulator::~UDPSimulator() {
|
|
close(fd);
|
|
}
|
|
|
|
void UDPSimulator::SendImage(const std::string &ipv4_dest_addr,
|
|
uint64_t frame_number,
|
|
uint16_t module_number) {
|
|
std::unique_lock<std::mutex> ul(m);
|
|
|
|
struct sockaddr_in addr {
|
|
.sin_family = AF_INET,
|
|
.sin_port = htons(8192 + module_number * 2),
|
|
.sin_addr = {
|
|
.s_addr = inet_addr(ipv4_dest_addr.c_str())
|
|
}
|
|
};
|
|
|
|
if (inet_pton(AF_INET, ipv4_dest_addr.c_str(), &addr.sin_addr.s_addr) <= 0)
|
|
throw JFJochException(JFJochExceptionCategory::UDPError, "Cannot parse address " + ipv4_dest_addr);
|
|
|
|
RAW_JF_Packet packet{
|
|
.framenum = frame_number
|
|
};
|
|
|
|
for (int i = 0; i < 128; i++) {
|
|
packet.packetnum = i;
|
|
memcpy(packet.data, image.data() + 4096 * i, 4096 * sizeof(uint16_t));
|
|
sendto(fd, &packet, sizeof(RAW_JF_Packet), 0, (struct sockaddr *) &addr, sizeof(addr));
|
|
}
|
|
}
|
|
|