8#include <daq/rawdata/modules/DAQConsts.h>
9#include <daq/rawdata/modules/Serializer.h>
10#include <daq/rawdata/modules/DeSerializer.h>
12#include <netinet/tcp.h>
51 B2INFO(
"Tx: Constructor done.");
57SerializerModule::~SerializerModule()
63 signal(SIGPIPE, SIG_IGN);
71 char temp_char1[100] =
"/cpr_config";
72 char temp_char2[100] =
"/cpr_status";
73 shmOpen(temp_char1, temp_char2);
81 memset(time_array0, 0,
sizeof(time_array0));
82 memset(time_array1, 0,
sizeof(time_array1));
83 memset(time_array2, 0,
sizeof(time_array2));
86 if (status.isAvailable()) {
87 status.setOutputNBytes(0);
88 status.setOutputCount(0);
97 B2INFO(
"Tx initialized.");
104 B2INFO(
"beginRun called.");
113 B2INFO(
"endRun done.");
119 B2INFO(
"terminate called");
133 return (
int*)mmap(NULL, size_words *
sizeof(
int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
142 sprintf(err_buf,
"[FATAL] Failed to shm_open (%s). Exiting... : path %s\n",
143 strerror(errno), path_cfg);
144 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
152 sprintf(err_buf,
"[FATAL] Failed to shm_open (%s). Exiting... : path %s\n",
153 strerror(errno), path_sta);
154 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
159 int size = 4 *
sizeof(int);
170 int total_send_nwords =
184 if (total_send_nwords != (rawdblk->
GetBuffer(0))[ 0 ] + 8) {
186 sprintf(err_buf,
"[FATAL] Length error. total length %d rawdblk length %d. Exting...\n",
187 total_send_nwords, (rawdblk->
GetBuffer(0))[ 0 ]);
189 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
203 hdr->SetExpRunWord(
tmp_header.GetExpRunSubrun());
212 const char err_buf[500] =
"[FATAL] CORRUPTED DATA: No COPPER blocks in RawDataBlock. Exiting...";
213 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
222int SerializerModule::sendByWriteV(
RawDataBlock* rawdblk)
224 SendHeader send_header;
225 SendTrailer send_trailer;
226 fillSendHeaderTrailer(&send_header, &send_trailer, rawdblk);
231 struct iovec iov[ NUM_BUFFER ];
237 iov[0].iov_base =
reinterpret_cast<char*
>(send_header.
GetBuffer());
238 iov[0].iov_len =
sizeof(int) * send_header.
GetHdrNwords();
240 iov[1].iov_base =
reinterpret_cast<char*
>(rawdblk->
GetWholeBuffer());
241 iov[1].iov_len =
sizeof(int) * rawcopper_nwords;
243 iov[2].iov_base =
reinterpret_cast<char*
>(send_trailer.GetBuffer());
244 iov[2].iov_len =
sizeof(int) * send_trailer.GetTrlNwords();
251 if ((n = writev(
m_socket, iov, NUM_BUFFER)) < 0) {
252 if (errno == EINTR) {
254 }
else if (errno == EAGAIN || errno == EWOULDBLOCK) {
259 callCheckRunPause(err_str);
264 sprintf(err_buf,
"[WARNING] WRITEVa error.(%s) : sent %d bytes, header %lu bytes body %lu trailer %lu : %s %s %d\n",
265 strerror(errno), n, iov[0].iov_len, iov[1].iov_len, iov[2].iov_len,
266 __FILE__, __PRETTY_FUNCTION__, __LINE__);
270 string err_str =
"RUN_ERROR";
273 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
282 printf(
"[DEBUG] *******BODY**********\n");
283 printf(
"[DEBUG] \n%.8d : ", 0);
284 printData((
int*)(iov[1].iov_base), iov[1].iov_len);
288 int total_send_bytes =
sizeof(int) * send_header.GetTotalNwords();
294 if (n != total_send_bytes) {
295 B2WARNING(
"Serializer: Sent byte(" << n <<
"bytes) is not same as the event size (" << total_send_bytes <<
"bytes). Retryring...");
299 if (n < (
int)(iov[ 0 ].iov_len)) {
300 n +=
Send(
m_socket,
static_cast<char*
>(iov[ 0 ].iov_base) + n, iov[ 0 ].iov_len - n);
303 if (n < (
int)(iov[ 0 ].iov_len + iov[ 1 ].iov_len)) {
304 n +=
Send(
m_socket,
static_cast<char*
>(iov[ 1 ].iov_base) + (n - iov[ 0 ].iov_len), iov[ 1 ].iov_len - (n - iov[ 0 ].iov_len));
307 if (n < (
int)(iov[ 0 ].iov_len + iov[ 1 ].iov_len + iov[ 2 ].iov_len)) {
309 n +=
Send(
m_socket,
static_cast<char*
>(iov[ 2 ].iov_base) + (n - iov[ 0 ].iov_len - iov[ 1 ].iov_len),
310 iov[ 2 ].iov_len - (n - iov[ 0 ].iov_len - iov[ 1 ].iov_len));
314 B2WARNING(
"Resending ends. It takes " << retry_end - retry_start <<
"(s)");
317 return total_send_bytes;
328 if ((ret = send(socket, buf + sent_bytes, size_bytes - sent_bytes, MSG_NOSIGNAL)) < 0) {
329 if (errno == EINTR) {
331 }
else if (errno == EAGAIN || errno == EWOULDBLOCK) {
334 callCheckRunPause(err_str);
339 sprintf(err_buf,
"[ERROR] Send Error. (%s) : %s %s %d", strerror(errno), __FILE__, __PRETTY_FUNCTION__, __LINE__);
343 string err_str =
"RUN_ERROR";
346 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
352 if (sent_bytes == size_bytes)
break;
357void SerializerModule::Accept()
364 struct hostent* host;
368 sprintf(temp_buf,
"[FATAL] hostname(%s) cannot be resolved(%s). Check /etc/hosts. Exiting...\n",
370 print_err.PrintError(temp_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
382 struct sockaddr_in sock_listen;
383 sock_listen.sin_family = AF_INET;
384 sock_listen.sin_addr.s_addr = *
reinterpret_cast<unsigned int*
>(host->h_addr_list[0]);
386 socklen_t addrlen =
sizeof(sock_listen);
388 fd_listen = socket(PF_INET, SOCK_STREAM, 0);
391 int ret = setsockopt(fd_listen, SOL_SOCKET, SO_REUSEADDR, &flags, (socklen_t)
sizeof(flags));
393 perror(
"Failed to set REUSEADDR");
396 if (bind(fd_listen,
reinterpret_cast<struct sockaddr*
>(&sock_listen),
sizeof(
struct sockaddr)) < 0) {
398 sprintf(temp_char,
"[FATAL] Failed to bind.(%s) Maybe other programs have already occupied this port(%d). Exiting...",
400 print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__);
405 setsockopt(fd_listen, IPPROTO_TCP, TCP_NODELAY, &val1, (socklen_t)
sizeof(val1));
407 if (listen(fd_listen, backlog) < 0) {
409 sprintf(err_buf,
"[FATAL] Failed in listen(%s). Exting...", strerror(errno));
410 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
418 struct sockaddr_in sock_accept;
422 B2INFO(
"Accepting...");
423 if ((fd_accept = accept(fd_listen,
reinterpret_cast<struct sockaddr*
>(& (sock_accept)), &addrlen)) == 0) {
425 sprintf(err_buf,
"[FATAL] Failed to accept(%s). Exiting...", strerror(errno));
426 print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
433 struct timeval timeout;
436 ret = setsockopt(fd_accept, SOL_SOCKET, SO_SNDTIMEO, &timeout, (socklen_t)
sizeof(timeout));
438 const char temp_char[100] =
"[FATAL] Failed to set TIMEOUT. Exiting...";
439 print_err.PrintError(temp_char, __FILE__, __PRETTY_FUNCTION__, __LINE__);
448 RunInfoBuffer& status(DeSerializerModule::getStatus());
449 if (status.isAvailable()) {
452 status.setOutputPort(ntohs(sock_listen.sin_port));
453 status.setOutputAddress(sock_listen.sin_addr.s_addr);
454 printf(
"%d %x\n", (
int)ntohs(sock_listen.sin_port), (
int)sock_listen.sin_addr.s_addr);
464 gettimeofday(&t, NULL);
465 return (t.tv_sec + t.tv_usec * 1.e-6);
472 array[
event - 50000 ] =
getTimeSec() - m_start_time;
480 unsigned int checksum = 0;
481 for (
int i = 0; i < nwords; i++) {
482 checksum = checksum ^ buf[ i ];
491 for (
int i = 0; i < nwords; i++) {
492 printf(
"%.8x ", buf[ i ]);
493 if (i % 10 == 9) printf(
"\n[DEBUG]");
502void SerializerModule::openRunPauseNshm()
504 char path_shm[100] =
"/cpr_pause_resume";
505 int fd = shm_open(path_shm, O_RDONLY, 0666);
507 printf(
"[DEBUG] %s\n", path_shm);
508 perror(
"[FATAL] Failed to open shm_open");
511 m_ptr = (
int*)mmap(NULL,
sizeof(
int), PROT_READ, MAP_SHARED, fd, 0);
515int SerializerModule::checkRunPause()
519 const RunInfoBuffer& status(DeSerializerModule::getStatus());
520 if (status.getState() == status.PAUSING) {
530void SerializerModule::resumeRun()
548 ret = recv(socket, buffer,
sizeof(buffer), MSG_DONTWAIT);
551 printf(
"EOF %d\n", socket); fflush(stdout);
555 if (errno == EAGAIN) {
556 printf(
"EAGAIN %d\n", socket); fflush(stdout);
560 printf(
"ERROR %d errno %d err %s\n", socket, errno, strerror(errno)); fflush(stdout);
566 printf(
"Flushing data in socket buffer (%d bytes) : sockid = %d\n", ret, socket); fflush(stdout);
571void SerializerModule::callCheckRunPause(
string& err_str)
575 printf(
"###########(Ser) TIMEOUT during send() ###############\n");
579 if (checkRunPause()) {
582 printf(
"###########(Ser) Stop is detected after return from send ###############\n");
586 err_str =
"RUN_PAUSE";
600 if (g_run_pause == 1) {
603 printf(
"###########(Ser) Go back to Deseializer() ###############\n");
608 }
else if (g_run_resuming == 1) {
611 printf(
"###########(Ser) Run resuming...() ###############\n");
633 for (
int j = 0; j < raw_dblkarray.
getEntries(); j++) {
638 B2INFO(
"SerializerPC: Sending the 1st packet...");
642 m_totbytes += sendByWriteV(raw_dblkarray[ j ]);
644 }
catch (
const string& err_str) {
647 if (err_str ==
"RUN_PAUSE" || err_str ==
"RUN_ERROR") {
651 print_err.PrintError(
const_cast<char*
>((err_str.c_str())), __FILE__, __PRETTY_FUNCTION__, __LINE__);
685 if (status.isAvailable()) {
687 status.addOutputCount(raw_dblkarray.
getEntries());
void setDescription(const std::string &description)
Sets the description of the module.
The RawDataBlock class Base class for rawdata handling.
virtual int * GetBuffer(int n)
get nth buffer pointer
virtual int CheckFTSWID(int n)
get FTSW ID to check whether this data block is FTSW data or not
virtual int GetNumEntries()
get # of data blocks = (# of nodes)*(# of events)
virtual int GetNumNodes()
get # of data sources(e.g. # of COPPER boards) in m_buffer
virtual int CheckTLUID(int n)
get FTSW ID to check whether this data block is FTSW data or not
virtual int * GetWholeBuffer()
get pointer to buffer(m_buffer)
virtual int GetNumEvents()
get # of events in m_buffer
virtual int TotalBufNwords()
Get total length of m_buffer.
void shmOpen(char *path_cfg, char *path_sta)
open shared memory
int CheckConnection(int socket)
Check socket status.
static void printData(const int *buf, int nwords)
print data contents
unsigned long long m_totbytes
sent data size
int m_port_to
Destination port.
std::string m_hostname_local
Destination Host.
SerializerModule()
Constructor / Destructor.
void initialize() override
Module functions to be called from main process.
static int * shmGet(int fd, int size_words)
Get shared memory.
void event() override
This method is the core of the module.
int m_start_flag
start flag
int BUF_SIZE_WORD
size of buffer for one event (word)
int m_shmfd_cfg
file descripter for shm
int * m_cfg_buf
buffer for shared memory
void endRun() override
This method is called if the current run ends.
void terminate() override
This method is called at the end of the event processing.
int n_basf2evt
No. of sent events.
CprErrorMessage print_err
error message program
int m_shmfd_sta
file descripter for shm
void beginRun() override
Module functions to be called from event process.
int m_shmflag
Use shared memory.
static unsigned int calcXORChecksum(const int *buf, int nwords)
calculate checksum
void recordTime(int event, double *array)
store time info.
int * m_cfg_sta
buffer for shared memory
int m_compressionLevel
Compression parameter.
int Send(int socket, char *buf, int size_bytes)
send buffer
static double getTimeSec()
store time info.
RawHeader_latest tmp_header
which format is used
Accessor to arrays stored in the data store.
int getEntries() const
Get the number of objects in the array.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.