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