70{
71 SocketIO sio;
72 MsgHandler msghdl(0);
73 char mbstr[100];
74 time_t now = time(0);
75 char* buffer = new char[c_maxBufSize];
76
77 int loop_counter = 0;
78 bool updated = false;
80 while (m_force_exit == 0) {
81 fflush(stdout);
82 {
83 vector<TObject*> objlist;
84 vector<string> strlist;
85 int root_fd = -1;
86 if (recv_root_message(objlist, strlist, root_fd)) {
87 printf("HistoServer2: ROOT received nobjs=%zu\n", objlist.size());
88 handle_objects(objlist, strlist, root_fd, updated);
89 }
90
91 for (auto* obj : objlist) {
92 delete obj;
93 }
94 }
95 int exam_stat = m_man->examine();
96 if (exam_stat == 0) {
97 } else if (exam_stat == 1) {
98
99 vector<int>& recvsock = m_man->connected_socket_list();
100 for (vector<int>::iterator it = recvsock.begin();
101 it != recvsock.end(); ++it) {
102 int fd = *it;
103 if (m_man->connected(fd)) {
104 struct sockaddr_in isa;
105 socklen_t isize = sizeof(isa);
106 getpeername(fd, (struct sockaddr*)&isa, &isize);
107 char address[INET_ADDRSTRLEN];
108 strcpy(address, inet_ntoa(isa.sin_addr));
109 char* ptr = strrchr(address, '.');
110 int nr = -1;
111 if (ptr) {
112 nr = atoi(ptr + 1);
113 }
114 now = time(0);
115 m_unit_last_conn_time[address] = now;
116
117 int is = sio.get(fd, buffer, c_maxBufSize);
118 if (is <= 0) {
119 now = time(0);
120 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
121 printf("[%s] HistoServer2: fd %d / %s disconnected\n", mbstr, fd, address);
122 m_man->remove(fd);
124 break;
125 }
127
128
129 EvtMessage* hmsg = new EvtMessage(buffer);
130 vector<TObject*> objlist;
131 vector<string> strlist;
132 msghdl.decode_msg(hmsg, objlist, strlist);
133 int nobjs = (hmsg->
header())->reserved[1];
134
135 string subdir = "";
136 now = time(0);
137 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
138 m_unit_last_packet_time[address] = now;
139 printf("[%s] HistoServer2 : received nobjs = %d from %s\n", mbstr, nobjs, address);
140 if (nobjs > 0) m_unit_last_content_time[address] = now;
141 for (int i = 0; i < nobjs; i++) {
142
143
144 string objname = strlist.at(i);
145 auto lpos = objname.find("SUBDIR:");
146 if (lpos != string::npos) {
147 subdir = objname.substr(7);
148 if (subdir == "EXIT") subdir = "";
149
150
151 } else {
152 m_hman->update(subdir, strlist.at(i), fd, (TH1*)objlist.at(i));
153 updated = true;
154 }
155 }
156 }
157 }
158 }
159 usleep(1000);
160 loop_counter++;
161 if (loop_counter % c_mergeIntervall == 0) {
162 if (updated) {
163 m_last_merge_time = time(0);
164 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&m_last_merge_time));
165 printf("[%s] HistoServer2: merging histograms\n", mbstr);
166 m_hman->merge();
167 updated = false;
168 }
170 }
171 }
172 return 0;
173}
EvtHeader * header()
Get pointer to EvtHeader.
void write_state(void)
Write connection state to a file.