Belle II Software development
IOInfo.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#include "daq/slc/readout/IOInfo.h"
9
10#include <cstdlib>
11#include <cstdio>
12
13#include <fstream>
14#include <sstream>
15#include <arpa/inet.h>
16
17using namespace Belle2;
18
19int IOInfo::checkTCP(IOInfo& info)
20{
21 std::vector<IOInfo> info_v;
22 info_v.push_back(info);
23 int ret = checkTCP(info_v);
24 info = info_v[0];
25 return ret;
26}
27
28int IOInfo::checkTCP(std::vector<IOInfo>& info)
29{
30 size_t count = 0;
31 for (size_t i = 0; i < info.size(); i++) {
32 info[i].setState(0);
33 info[i].setTXQueue(0);
34 info[i].setRXQueue(0);
35 }
36 count = 0;
37 std::string line;
38 std::stringstream ss;
39 std::string sl, local_address, rem_address, st, queue;
40 std::ifstream fin("/proc/net/tcp");
41 getline(fin, line);
42 while (fin && getline(fin, line)) {
43 ss.str("");
44 ss << line;
45 ss >> sl >> local_address >> rem_address >> st >> queue;
46 unsigned int addr = strtoul(local_address.substr(0, 8).c_str(), NULL, 16);
47 int port = strtoul(local_address.substr(9).c_str(), NULL, 16);
48 unsigned int raddr = strtoul(rem_address.substr(0, 8).c_str(), NULL, 16);
49 int rport = strtoul(rem_address.substr(9).c_str(), NULL, 16);
50 for (size_t i = 0; i < info.size(); i++) {
51 if (info[i].getState() > 0) continue;
52 int sti = strtoul(st.c_str(), NULL, 16);
53 if (//addr == info[i].getLocalAddress() &&
54 port == info[i].getLocalPort() && sti == 1) {
55 info[i].setRemoteAddress(raddr);
56 info[i].setRemotePort(rport);
57 info[i].setState(sti);
58 info[i].setTXQueue(strtoul(queue.substr(0, 8).c_str(), NULL, 16));
59 info[i].setRXQueue(strtoul(queue.substr(9).c_str(), NULL, 16));
60 count++;
61 } else if (raddr == info[i].getRemoteAddress() &&
62 rport == info[i].getRemotePort() && sti == 1) {
63 info[i].setLocalAddress(addr);
64 info[i].setLocalPort(port);
65 info[i].setState(sti);
66 info[i].setTXQueue(strtoul(queue.substr(0, 8).c_str(), NULL, 16));
67 info[i].setRXQueue(strtoul(queue.substr(9).c_str(), NULL, 16));
68 count++;
69 }
70 if (count == info.size()) return count;
71 }
72 }
73 return count;
74}
75
76const char* IOInfo::getLocalIP() const
77{
78 sockaddr_in addr;
79 addr.sin_addr.s_addr = m_local_addr;
80 return inet_ntoa(addr.sin_addr);
81}
82
83const char* IOInfo::getRemoteIP() const
84{
85 sockaddr_in addr;
86 addr.sin_addr.s_addr = m_remote_addr;
87 return inet_ntoa(addr.sin_addr);
88}
Abstract base class for different kinds of events.