CMS 3D CMS Logo

TCPReceiver.cc
Go to the documentation of this file.
1 
2 /*
3  Author: Adam Hunt
4  email: ahunt@princeton.edu
5  Date: 2007-08-24
6 */
7 
10 #include "RecoLuminosity/TCPReceiver/interface/LumiStructures.hh"
11 
12 #include <iostream>
13 
14 #include <unistd.h>
15 #include <sys/time.h>
16 
17 // srand rand
18 #include <cstdlib>
19 
20 // perror
21 #include <cstdio>
22 
23 // tcp
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <netdb.h>
29 
30 #include <cstring>
31 #include <cassert>
32 
33 namespace HCAL_HLX {
34 
36 #ifdef DEBUG
37  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
38 #endif
39 
40  acquireMode = 0;
41  servPort = 0;
42  servIP = "127.0.0.1";
43  Connected = false;
44 
45 #ifdef DEBUG
46  std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
47 #endif
48  }
49 
50  TCPReceiver::TCPReceiver(unsigned short int port, std::string IP, unsigned char mode = 0) {
51 #ifdef DEBUG
52  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
53 #endif
54  acquireMode = mode;
55  servPort = port;
56  servIP = IP;
57  Connected = false;
58 
59 #ifdef DEBUG
60  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
61 #endif
62  }
63 
65 
66  void SetupFDSets(fd_set &ReadFDs,
67  fd_set &WriteFDs,
68  fd_set &ExceptFDs,
69  int ListeningSocket = -1,
70  int connectSocket = -1) { //std::vector & gConnections) {
71  FD_ZERO(&ReadFDs);
72  FD_ZERO(&WriteFDs);
73  FD_ZERO(&ExceptFDs);
74 
75  // Add the listener socket to the read and except FD sets, if there
76  // is one.
77  if (ListeningSocket != -1) {
78  FD_SET(ListeningSocket, &ReadFDs);
79  FD_SET(ListeningSocket, &ExceptFDs);
80  }
81 
82  if (connectSocket != -1) {
83  FD_SET(connectSocket, &ReadFDs);
84  FD_SET(connectSocket, &ExceptFDs);
85  }
86  }
87 
89 #ifdef DEBUG
90  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
91 #endif
92 
93  int errorCode = 0;
94 
95  if (acquireMode == 0) { // real data
96  if (Connected == false) {
97  errorCode = 701; // not connected
98  } else {
99  unsigned int bytesRcvd, bytesToReceive, totalBytesRcvd;
100  const unsigned int Buffer_Size = 8192;
101  char *Buffer;
102  char *BigBuffer;
103 
104  // From John's code
105 
106  fd_set fdsRead, fdsWrite, fdsExcept;
107 
108  // int outputcode;
109  //int z = 0, localCount = 0;
110  time_t tempTime, curTime;
111  //int ret;
112 
113  time(&curTime);
114 
115  bytesToReceive = sizeof(localSection);
116  Buffer = new char[Buffer_Size];
117  BigBuffer = new char[bytesToReceive];
118  totalBytesRcvd = 0;
119 
120  memset(reinterpret_cast<char *>(&localSection), 0, Buffer_Size);
121  memset(Buffer, 0, Buffer_Size);
122  memset(BigBuffer, 0, bytesToReceive);
123 
124  usleep(10000);
125  assert(tcpSocket >= 0);
126  while ((totalBytesRcvd < bytesToReceive) && (errorCode == 0)) {
127  SetupFDSets(fdsRead, fdsWrite, fdsExcept, -1, tcpSocket);
128 
129  if (select(tcpSocket + 1, &fdsRead, nullptr, &fdsExcept, nullptr) > 0) {
130  if (FD_ISSET(tcpSocket, &fdsRead)) {
131  if ((bytesRcvd = recv(tcpSocket, Buffer, Buffer_Size, 0)) <= 0) {
132  perror("Recv Error");
133  errorCode = 501;
134  } else {
135  if ((totalBytesRcvd + bytesRcvd) <= bytesToReceive) {
136  memcpy(&BigBuffer[totalBytesRcvd], Buffer, bytesRcvd);
137  } else {
138  std::cout << "***** MEM OVER FLOW: Did someone forget to update LumiStructures.hh? *****"
139  << std::endl;
140  errorCode = 502;
141  }
142  totalBytesRcvd += bytesRcvd;
143  time(&tempTime);
144  }
145  }
146  }
147  }
148 
149  if (errorCode == 0) {
150  memcpy(reinterpret_cast<char *>(&localSection), BigBuffer, sizeof(localSection));
151  errorCode = 1; // success
152  }
153  delete[] Buffer;
154  delete[] BigBuffer;
155  }
156  } else if (acquireMode == 1) { // fill with fake data. Should be unique.
157  GenerateFakeData(localSection);
158  errorCode = 1;
159  } else if (acquireMode == 2) { // fill with random fake data.
160  GenerateRandomData(localSection);
161  errorCode = 1;
162  } else {
163  errorCode = 201;
164  }
165 
166 #ifdef DEBUG
167  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
168 #endif
169 
170  return errorCode;
171  }
172 
174 #ifdef DEBUG
175  std::cout << "Begin and End " << __PRETTY_FUNCTION__ << " " << Connected << std::endl;
176 #endif
177  return Connected;
178  }
179 
180  int TCPReceiver::SetPort(unsigned short int port) {
181 #ifdef DEBUG
182  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
183 #endif
184 
185  int errorCode;
186 
187  if (port < 1024) {
188  errorCode = 101;
189  } else {
190  servPort = port;
191  errorCode = 1;
192  }
193 
194 #ifdef DEBUG
195  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
196 #endif
197  return errorCode;
198  }
199 
200  int TCPReceiver::SetMode(unsigned char mode) {
201 #ifdef DEBUG
202  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
203 #endif
204 
205  int errorCode;
206 
207  if (mode > 2) {
208  errorCode = 201;
209  } else {
210  acquireMode = mode;
211  errorCode = 1;
212  }
213 
214 #ifdef DEBUG
215  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
216 #endif
217  return errorCode;
218  }
219 
221 #ifdef DEBUG
222  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
223 #endif
224  servIP = IP;
225 #ifdef DEBUG
226  std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
227 #endif
228  }
229 
231 #ifdef DEBUG
232  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
233 #endif
234 
235  int errorCode;
236 
237  if (acquireMode == 0) {
238  struct hostent *hostInfo = gethostbyname(servIP.c_str());
239 
240  if (servPort < 1024) {
241  errorCode = 101; // Protected ports
242  } else {
243 #ifdef DEBUG
244  std::cout << "Requesting connection" << std::endl;
245 #endif
246  if ((tcpSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
247  perror("Socket Error");
248  errorCode = 301; // Socket failed
249  } else {
250  memset(&servAddr, 0, sizeof(servAddr));
251  servAddr.sin_family = hostInfo->h_addrtype;
252  memcpy((char *)&servAddr.sin_addr.s_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
253  // servAddr.sin_addr.s_addr = inet_addr(servIP.c_str());
254  servAddr.sin_port = htons(servPort);
255 #ifdef DEBUG
256  std::cout << "Connecting" << std::endl;
257 #endif
258  if (connect(tcpSocket, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
259  perror("Connect Error");
260  errorCode = 302; // connect failed
261  close(tcpSocket);
262  } else {
263  Connected = true;
264  errorCode = 1; // Successful connection
265  }
266  }
267  }
268  } else if (acquireMode == 1) {
269  Connected = true;
270  errorCode = 1; // do nothing for fake data
271  } else if (acquireMode == 2) {
272  Connected = true;
273  errorCode = 1; // do nothing for random data
274  } else {
275  errorCode = 201; // Invalid acquire mode
276  }
277 
278 #ifdef DEBUG
279  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
280 #endif
281  return errorCode;
282  }
283 
285 #ifdef DEBUG
286  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
287 #endif
288 
289  int errorCode = 0;
290 
291  if (Connected) {
292  if (acquireMode == 0) {
293  if (shutdown(tcpSocket, SHUT_RDWR) < 0) {
294  perror("Shutdown Error");
295  errorCode = 601; // Disconnect Failed
296  } else {
297  errorCode = 1; // Successful Disconnect
298  }
299  }
300  Connected = false;
301  } else {
302  errorCode = 401; // Not Connected
303  }
304 
305 #ifdef DEBUG
306  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
307 #endif
308  return errorCode;
309  }
310 
312 #ifdef DEBUG
313  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
314 #endif
315  int i, j, k;
316 
317  localSection.hdr.runNumber = 1;
318  localSection.hdr.startOrbit = 2;
319  localSection.hdr.numOrbits = 3;
320  localSection.hdr.numBunches = 4;
321  localSection.hdr.numHLXs = 5;
322  localSection.hdr.bCMSLive = true;
323  localSection.hdr.sectionNumber = 120;
324 
325  timeval tvTemp;
326  gettimeofday(&tvTemp, nullptr);
327  localSection.hdr.timestamp = tvTemp.tv_sec;
328  localSection.hdr.timestamp_micros = tvTemp.tv_usec;
329 
330  localSection.lumiSummary.DeadtimeNormalization = 0.7;
331  localSection.lumiSummary.LHCNormalization = 0.75;
332  localSection.lumiSummary.OccNormalization[0] = 0.8;
333  localSection.lumiSummary.OccNormalization[1] = 0.85;
334  localSection.lumiSummary.ETNormalization = 0.8;
335  localSection.lumiSummary.InstantLumi = 0.9;
336  localSection.lumiSummary.InstantLumiErr = 0.10;
337  localSection.lumiSummary.InstantLumiQlty = 11;
338  localSection.lumiSummary.InstantETLumi = 0.12;
339  localSection.lumiSummary.InstantETLumiErr = 0.13;
340  localSection.lumiSummary.InstantETLumiQlty = 14;
341  localSection.lumiSummary.InstantOccLumi[0] = 0.15;
342  localSection.lumiSummary.InstantOccLumiErr[0] = 0.16;
343  localSection.lumiSummary.InstantOccLumiQlty[0] = 17;
344  localSection.lumiSummary.lumiNoise[0] = 0.18;
345  localSection.lumiSummary.InstantOccLumi[1] = 0.19;
346  localSection.lumiSummary.InstantOccLumiErr[1] = 0.20;
347  localSection.lumiSummary.InstantOccLumiQlty[1] = 21;
348  localSection.lumiSummary.lumiNoise[1] = 0.22;
349 
350  for (j = 0; j < 3564; j++) {
351  localSection.lumiDetail.ETBXNormalization[j] = 0.25 * j / 35640.0;
352  localSection.lumiDetail.OccBXNormalization[0][j] = 0.5 * j / 35640.0;
353  localSection.lumiDetail.OccBXNormalization[1][j] = 0.75 * j / 35640.0;
354  localSection.lumiDetail.LHCLumi[j] = 1 * j / 35640.0;
355  localSection.lumiDetail.ETLumi[j] = 2 * j / 35640.0;
356  localSection.lumiDetail.ETLumiErr[j] = 3 * j / 35640.0;
357  localSection.lumiDetail.ETLumiQlty[j] = 4 * j;
358  localSection.lumiDetail.OccLumi[0][j] = 5 * j / 35640.0;
359  localSection.lumiDetail.OccLumiErr[0][j] = 6 * j / 35640.0;
360  localSection.lumiDetail.OccLumiQlty[0][j] = 7 * j;
361  localSection.lumiDetail.OccLumi[1][j] = 8 * j / 35640.0;
362  localSection.lumiDetail.OccLumiErr[1][j] = 9 * j / 35640.0;
363  localSection.lumiDetail.OccLumiQlty[1][j] = 10 * j;
364  }
365 
366  for (i = 0; i < 36; i++) {
367  localSection.etSum[i].hdr.numNibbles = 7;
368  localSection.occupancy[i].hdr.numNibbles = 8;
369  localSection.lhc[i].hdr.numNibbles = 9;
370 
371  localSection.etSum[i].hdr.bIsComplete = true;
372  localSection.occupancy[i].hdr.bIsComplete = true;
373  localSection.lhc[i].hdr.bIsComplete = true;
374 
375  for (j = 0; j < 3564; j++) {
376  localSection.etSum[i].data[j] = 6 * j + 10 * i;
377  for (k = 0; k < 6; k++) {
378  localSection.occupancy[i].data[k][j] = k * j + 11 * i;
379  }
380  localSection.lhc[i].data[j] = 7 * j + 12 * i;
381  }
382  }
383 
384 #ifdef DEBUG
385  std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
386 #endif
387  }
388 
390 #ifdef DEBUG
391  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
392 #endif
393  int i, j, k;
394 
395  srand(time(nullptr));
396  localSection.hdr.runNumber = 55; //(rand() % 100);
397  localSection.hdr.startOrbit = (rand() % 100);
398  localSection.hdr.numOrbits = (rand() % 100);
399  localSection.hdr.numBunches = (rand() % 100);
400  localSection.hdr.numHLXs = (rand() % 100);
401  localSection.hdr.bCMSLive = true;
402  localSection.hdr.sectionNumber = (rand() % 100);
403 
404  localSection.lumiSummary.DeadtimeNormalization = (float)(rand() % 100) / 100;
405  localSection.lumiSummary.LHCNormalization = (float)(rand() % 100) / 100;
406  localSection.lumiSummary.OccNormalization[0] = (float)(rand() % 100) / 100;
407  localSection.lumiSummary.OccNormalization[1] = (float)(rand() % 100) / 100;
408  localSection.lumiSummary.ETNormalization = (float)(rand() % 100) / 100;
409  localSection.lumiSummary.InstantLumi = (float)(rand() % 100) / 100;
410  localSection.lumiSummary.InstantLumiErr = (float)(rand() % 100) / 100;
411  localSection.lumiSummary.InstantLumiQlty = (rand() % 100);
412  localSection.lumiSummary.InstantETLumi = (float)(rand() % 100) / 100;
413  localSection.lumiSummary.InstantETLumiErr = (float)(rand() % 100) / 100;
414  localSection.lumiSummary.InstantETLumiQlty = (rand() % 100);
415  localSection.lumiSummary.InstantOccLumi[0] = (float)(rand() % 100) / 100;
416  localSection.lumiSummary.InstantOccLumiErr[0] = (float)(rand() % 100) / 100;
417  localSection.lumiSummary.InstantOccLumiQlty[0] = (rand() % 100);
418  localSection.lumiSummary.lumiNoise[0] = (float)(rand() % 100) / 100;
419  localSection.lumiSummary.InstantOccLumi[1] = (float)(rand() % 100) / 100;
420  localSection.lumiSummary.InstantOccLumiErr[1] = (float)(rand() % 100) / 100;
421  localSection.lumiSummary.InstantOccLumiQlty[1] = (rand() % 100);
422  localSection.lumiSummary.lumiNoise[1] = (float)(rand() % 100) / 100;
423 
424  for (j = 0; j < 3564; j++) {
425  localSection.lumiDetail.ETBXNormalization[j] = 0.25 * j / 35640.0;
426  localSection.lumiDetail.OccBXNormalization[0][j] = 0.5 * j / 35640.0;
427  localSection.lumiDetail.OccBXNormalization[1][j] = 0.75 * j / 35640.0;
428  localSection.lumiDetail.LHCLumi[j] = (float)(rand() % 100) / 100.0;
429  localSection.lumiDetail.ETLumi[j] = (float)(rand() % 100) / 100.0;
430  localSection.lumiDetail.ETLumiErr[j] = (float)(rand() % 100) / 100.0;
431  localSection.lumiDetail.ETLumiQlty[j] = (rand() % 100);
432  localSection.lumiDetail.OccLumi[0][j] = (float)(rand() % 100) / 100.0;
433  localSection.lumiDetail.OccLumiErr[0][j] = (float)(rand() % 100) / 100.0;
434  localSection.lumiDetail.OccLumiQlty[0][j] = (rand() % 100);
435  localSection.lumiDetail.OccLumi[1][j] = (float)(rand() % 100) / 100.0;
436  localSection.lumiDetail.OccLumiErr[1][j] = (float)(rand() % 100) / 100.0;
437  localSection.lumiDetail.OccLumiQlty[1][j] = (rand() % 100);
438  }
439 
440  for (i = 0; i < 36; i++) {
441  localSection.etSum[i].hdr.numNibbles = (rand() % 100);
442  localSection.occupancy[i].hdr.numNibbles = 8 * (rand() % 100);
443  localSection.lhc[i].hdr.numNibbles = 9 * (rand() % 100);
444 
445  localSection.etSum[i].hdr.bIsComplete = true;
446  localSection.occupancy[i].hdr.bIsComplete = true;
447  localSection.lhc[i].hdr.bIsComplete = true;
448 
449  for (j = 0; j < 3564; j++) {
450  localSection.etSum[i].data[j] = 6 * (rand() % 3564);
451  for (k = 0; k < 6; k++) {
452  localSection.occupancy[i].data[k][j] = k * (rand() % 3564);
453  }
454  localSection.lhc[i].data[j] = 7 * (rand() % 3564);
455  }
456  }
457 
458 #ifdef DEBUG
459  std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
460 #endif
461  }
462 
466  return !(memcmp(&L, &localSection, sizeof(L)));
467  }
468 
469 } // namespace HCAL_HLX
void SetupFDSets(fd_set &ReadFDs, fd_set &WriteFDs, fd_set &ExceptFDs, int ListeningSocket=-1, int connectSocket=-1)
Definition: TCPReceiver.cc:66
unsigned char acquireMode
Definition: TCPReceiver.h:53
int SetPort(unsigned short int)
Definition: TCPReceiver.cc:180
int port
Definition: query.py:116
assert(be >=bs)
void SetIP(std::string IP)
Definition: TCPReceiver.cc:220
void GenerateRandomData(HCAL_HLX::LUMI_SECTION &localSection)
Definition: TCPReceiver.cc:389
LUMI_SECTION_SUB_HEADER hdr
OCCUPANCY_SECTION occupancy[36]
void GenerateFakeData(HCAL_HLX::LUMI_SECTION &localSection)
Definition: TCPReceiver.cc:311
int ReceiveLumiSection(HCAL_HLX::LUMI_SECTION &localSection)
Definition: TCPReceiver.cc:88
std::string servIP
Definition: TCPReceiver.h:57
int SetMode(unsigned char)
Definition: TCPReceiver.cc:200
LUMI_SECTION_SUB_HEADER hdr
unsigned short servPort
Definition: TCPReceiver.h:56
struct sockaddr_in servAddr
Definition: TCPReceiver.h:59
bool VerifyFakeData(HCAL_HLX::LUMI_SECTION &localSection)
Definition: TCPReceiver.cc:463