CMS 3D CMS Logo

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