Belle II Software development
file2socket.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <fcntl.h>
13#include <netinet/in.h>
14#include <fstream>
15
16#include <daq/storage/BinData.h>
17
18#include "daq/slc/system/TCPServerSocket.h"
19#include "daq/slc/system/Time.h"
20
21using namespace Belle2;
22using namespace std;
23
24#define MAXBUF 400000000
25
26int main(int argc, char** argv)
27{
28 if (argc < 4) {
29 printf("ds_socket2rb : filename hostname port interval\n");
30 return 1;
31 }
32
33 int fd = ::open(argv[1], O_RDONLY);
34 if (fd < 0) {
35 perror("open");
36 return 1;
37 }
38
39 printf("file2socket::start\n");
40 TCPServerSocket server_socket;
41 const std::string host = argv[2];
42 server_socket.open(host, atoi(argv[3]));
43 // const int ninterval = atoi(argv[4]);
44 printf("file2socket::accepting\n");
45 TCPSocket socket = server_socket.accept();
46 printf("file2socket::accepted\n");
47 //socket.setBufferSize(32 * 1024 * 1024);
48 //char* buf = new char[MAXBUF];
49 int nrec = 0;
50 Time t0, t1;
51 double datasize = 0;
52 int* evtbuf = new int[10000000];
53 BinData data;
54 //data.setBuffer(evtbuf);
55 bool newrun = true;
56 while (true) {
57 //int sstat = read(fd, data.getBuffer(), sizeof(int));
58 int sstat = read(fd, evtbuf, sizeof(int));
59 if (sstat <= 0) {
60 lseek(fd, 0, SEEK_SET);
61 continue;
62 }
63 if (newrun) {
64 newrun = false;
65 continue;
66 }
67 //unsigned int nbyte = data.getByteSize() - sizeof(int);
68 //int rstat = read(fd, (data.getBuffer() + 1), nbyte);
69 unsigned int nbyte = evtbuf[0];
70 int rstat = read(fd, (evtbuf + 1), nbyte - sizeof(int));
71 if (rstat <= 0) continue;
72 if (sstat + rstat != evtbuf[0]) continue;
73 unsigned int nbyte_hton = htonl(nbyte);
74 //socket.write(data.getBuffer()+1, nbyte + sizeof(int));
75 printf("nbyte = %u\n", nbyte);
76 socket.write(&nbyte_hton, sizeof(int));
77 socket.write(evtbuf, nbyte);
78 nrec++;
79 datasize += sstat;
80 datasize += rstat;
81 const int nth = 10000;
82 if (nrec % nth == 0) {
83 Time t;
84 double dt = (t.get() - t0.get());
85 double freq = nth / dt / 1000.;
86 double rate = datasize / dt / 1000. / 1000.;
87 printf("Serial = %d Freq = %f [kHz] (%f [kHz]), Rate = %f [MB/s], DataSize = %f [kB/event]\n",
88 nrec, freq, (nrec / (t.get() - t1.get()) / 1000), rate, datasize / 1000. / nth);
89 t0 = t;
90 datasize = 0;
91 }
92 //if (ninterval > 0 && nrec % ninterval == 0) usleep(1000);
93 sleep(1);
94 }
95 socket.close();
96 return 0;
97}
98
Abstract base class for different kinds of events.
STL namespace.