CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/RecoLuminosity/TCPReceiver/src/TCPReceiver.cc

Go to the documentation of this file.
00001 
00002 /*
00003   Author: Adam Hunt
00004   email:  ahunt@princeton.edu
00005   Date:   2007-08-24
00006 */
00007 
00008 #include "RecoLuminosity/TCPReceiver/interface/TCPReceiver.h"
00009 #include "RecoLuminosity/TCPReceiver/interface/TimeStamp.h"
00010 #include "RecoLuminosity/TCPReceiver/interface/LumiStructures.hh"
00011 
00012 #include <iostream>
00013 
00014 #include <unistd.h>
00015 #include <sys/time.h>
00016 
00017 // srand rand
00018 #include <cstdlib>
00019 
00020 // perror
00021 #include <cstdio>
00022 
00023 // tcp
00024 #include <sys/types.h>
00025 #include <sys/socket.h>
00026 #include <arpa/inet.h>
00027 #include <netinet/in.h>
00028 #include <netdb.h>
00029 
00030 #include <cstring>
00031 
00032 namespace HCAL_HLX{
00033 
00034   TCPReceiver::TCPReceiver(){
00035 #ifdef DEBUG
00036     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00037 #endif
00038 
00039     acquireMode = 0;
00040     servPort = 0;
00041     servIP = "127.0.0.1";
00042     Connected = false;
00043 
00044 #ifdef DEBUG
00045     std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00046 #endif
00047   }
00048  
00049   TCPReceiver::TCPReceiver(unsigned short int port, std::string IP, unsigned char mode = 0){
00050 #ifdef DEBUG
00051     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00052 #endif
00053     acquireMode = mode;
00054     servPort = port;
00055     servIP = IP;
00056     Connected = false;
00057 
00058 #ifdef DEBUG
00059     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00060 #endif
00061   }
00062 
00063   TCPReceiver::~TCPReceiver(){
00064     Disconnect();
00065   }
00066 
00067 void SetupFDSets(fd_set& ReadFDs, fd_set& WriteFDs,
00068                  fd_set& ExceptFDs, int ListeningSocket = -1,
00069                  int connectSocket = -1) { //std::vector & gConnections) {
00070     FD_ZERO(&ReadFDs);
00071     FD_ZERO(&WriteFDs);
00072     FD_ZERO(&ExceptFDs);
00073 
00074     // Add the listener socket to the read and except FD sets, if there
00075     // is one.
00076     if (ListeningSocket != -1) {
00077         FD_SET(ListeningSocket, &ReadFDs);
00078         FD_SET(ListeningSocket, &ExceptFDs);
00079     }
00080 
00081     if (connectSocket != -1) {
00082       FD_SET(connectSocket, &ReadFDs);
00083       FD_SET(connectSocket, &ExceptFDs);
00084     }
00085 }
00086 
00087 
00088   int TCPReceiver::ReceiveLumiSection(HCAL_HLX::LUMI_SECTION &localSection){
00089 #ifdef DEBUG
00090     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00091 #endif
00092 
00093     int errorCode = 0;
00094  
00095     if(acquireMode == 0){  // real data
00096       if(Connected == false){
00097         errorCode = 701;  // not connected
00098       } else {
00099         unsigned int bytesRcvd, bytesToReceive, totalBytesRcvd;
00100         const unsigned int Buffer_Size = 8192;
00101         char *Buffer;
00102         char *BigBuffer;
00103 
00104         // From John's code
00105 
00106         fd_set fdsRead, fdsWrite, fdsExcept;
00107         struct timeval tv;
00108 
00109         tv.tv_sec = 1;
00110         tv.tv_usec = 0;
00111 
00112         //      int outputcode;
00113         //int z = 0, localCount = 0;
00114         time_t tempTime, curTime;
00115         //int ret;
00116 
00117         time(&curTime);
00118       
00119         bytesToReceive = sizeof(localSection);
00120         Buffer = new char[Buffer_Size];
00121         BigBuffer = new char[bytesToReceive];
00122         totalBytesRcvd = 0;
00123         
00124         memset(reinterpret_cast<char *>(&localSection), 0, Buffer_Size);
00125         memset(Buffer, 0, Buffer_Size);
00126         memset(BigBuffer, 0, bytesToReceive);
00127         
00128         usleep(10000);
00129         
00130         while((totalBytesRcvd < bytesToReceive) && (errorCode == 0)){
00131           
00132           SetupFDSets(fdsRead, fdsWrite, fdsExcept, -1, tcpSocket);
00133           
00134           if(select(tcpSocket+1, &fdsRead, 0, &fdsExcept, 0)> 0){
00135             
00136             if (FD_ISSET(tcpSocket, &fdsRead)) {
00137               
00138               if((bytesRcvd = recv(tcpSocket, Buffer, Buffer_Size, 0))<=0){
00139                 perror("Recv Error"); 
00140                 errorCode = 501;
00141               }else{
00142                 if((totalBytesRcvd + bytesRcvd)<= bytesToReceive){
00143                   memcpy(&BigBuffer[totalBytesRcvd], Buffer, bytesRcvd);
00144                 }else{
00145                   std::cout << "***** MEM OVER FLOW: Did someone forget to update LumiStructures.hh? *****" << std::endl;
00146                   errorCode = 502;
00147                 }
00148                 totalBytesRcvd += bytesRcvd;
00149                 time(&tempTime);
00150               }
00151             }
00152           }
00153         }
00154         
00155         if(errorCode == 0){
00156           memcpy(reinterpret_cast<char *>(&localSection), BigBuffer, sizeof(localSection));
00157           errorCode = 1; // success
00158         }  
00159         delete [] Buffer;
00160         delete [] BigBuffer;
00161       }
00162     } else if(acquireMode == 1){ // fill with fake data. Should be unique.
00163       GenerateFakeData(localSection);
00164       errorCode = 1;
00165     } else if(acquireMode == 2){ // fill with random fake data.
00166       GenerateRandomData(localSection);
00167       errorCode = 1;
00168     } else {
00169       errorCode = 201;
00170     }
00171     
00172 #ifdef DEBUG
00173     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00174 #endif
00175     
00176     return errorCode;
00177   }
00178   
00179   bool TCPReceiver::IsConnected(){
00180 #ifdef DEBUG
00181     std::cout << "Begin and End  " << __PRETTY_FUNCTION__ << " " << Connected << std::endl;
00182 #endif
00183     return Connected;
00184   }
00185   
00186   int TCPReceiver::SetPort(unsigned short int port){
00187 #ifdef DEBUG
00188     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00189 #endif
00190     
00191     int errorCode;
00192     
00193     if(port < 1024){
00194       errorCode = 101;
00195     }else{
00196       servPort = port;
00197       errorCode = 1;
00198     }
00199     
00200 #ifdef DEBUG
00201     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00202 #endif
00203     return errorCode;
00204   }
00205   
00206   int TCPReceiver::SetMode(unsigned char mode){
00207 #ifdef DEBUG
00208     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00209 #endif
00210     
00211     int errorCode;
00212     
00213     if(mode > 2){
00214       errorCode = 201;
00215     } else {
00216       acquireMode = mode;
00217       errorCode = 1;
00218     }
00219     
00220 #ifdef DEBUG
00221     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00222 #endif
00223     return errorCode;
00224   }
00225   
00226   void TCPReceiver::SetIP(std::string IP){
00227 #ifdef DEBUG
00228     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00229 #endif
00230     servIP = IP;
00231 #ifdef DEBUG
00232     std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00233 #endif
00234   }
00235   
00236   int TCPReceiver::Connect(){
00237 #ifdef DEBUG
00238     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00239 #endif  
00240     
00241     int errorCode;
00242     
00243     if(acquireMode == 0){
00244       struct hostent * hostInfo = gethostbyname(servIP.c_str());
00245       
00246       if(servPort < 1024){
00247         errorCode = 101;  // Protected ports
00248       } else {
00249 #ifdef DEBUG    
00250         std::cout << "Requesting connection" << std::endl;
00251 #endif
00252         if((tcpSocket = socket(AF_INET, SOCK_STREAM, 0))<0){
00253           perror("Socket Error");
00254           errorCode = 301; // Socket failed
00255         }else{
00256           memset(&servAddr, 0, sizeof(servAddr)); 
00257           servAddr.sin_family = hostInfo->h_addrtype;
00258           memcpy((char *) &servAddr.sin_addr.s_addr,
00259                  hostInfo->h_addr_list[0],
00260                  hostInfo->h_length);     
00261           //  servAddr.sin_addr.s_addr = inet_addr(servIP.c_str());
00262           servAddr.sin_port = htons(servPort);
00263 #ifdef DEBUG
00264           std::cout << "Connecting" << std::endl;
00265 #endif
00266           if(connect(tcpSocket, (struct sockaddr *) &servAddr, sizeof(servAddr))<0){
00267             perror("Connect Error");
00268             errorCode = 302; // connect failed
00269             close(tcpSocket);
00270           } else{
00271             Connected = true;
00272             errorCode = 1;  // Successful connection
00273           }
00274         }
00275       }
00276     } else if(acquireMode == 1) {
00277       Connected = true; 
00278       errorCode = 1;       // do nothing for fake data
00279     } else  if(acquireMode == 2) {
00280       Connected = true; 
00281       errorCode = 1;       // do nothing for random data
00282     } else {
00283       errorCode = 201;     // Invalid acquire mode
00284     }
00285     
00286 #ifdef DEBUG
00287     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00288 #endif
00289     return errorCode;
00290   }
00291   
00292   int TCPReceiver::Disconnect(){
00293 #ifdef DEBUG
00294     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00295 #endif
00296 
00297     int errorCode = 0;
00298 
00299     if(Connected){
00300       if( acquireMode == 0 ){
00301         if(shutdown(tcpSocket,SHUT_RDWR)<0){
00302           perror("Shutdown Error");
00303           errorCode = 601; // Disconnect Failed
00304         } else {
00305           
00306           errorCode = 1;  // Successful Disconnect
00307         }
00308       }
00309       Connected = false;
00310     } else {
00311       errorCode = 401;  // Not Connected
00312     }
00313     
00314 #ifdef DEBUG
00315     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00316 #endif
00317     return errorCode;
00318   }
00319 
00320   void TCPReceiver::GenerateFakeData(HCAL_HLX::LUMI_SECTION & localSection){
00321 #ifdef DEBUG
00322     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00323 #endif
00324     int i, j, k;
00325 
00326     localSection.hdr.runNumber     = 1;
00327     localSection.hdr.startOrbit    = 2;
00328     localSection.hdr.numOrbits     = 3;
00329     localSection.hdr.numBunches    = 4;
00330     localSection.hdr.numHLXs       = 5;
00331     localSection.hdr.bCMSLive      = true;
00332     localSection.hdr.sectionNumber = 120;
00333     
00334     timeval tvTemp;
00335     gettimeofday(&tvTemp, NULL);
00336     localSection.hdr.timestamp = tvTemp.tv_sec;
00337     localSection.hdr.timestamp_micros = tvTemp.tv_usec;
00338 
00339     localSection.lumiSummary.DeadtimeNormalization = 0.7;
00340     localSection.lumiSummary.LHCNormalization      = 0.75;
00341     localSection.lumiSummary.OccNormalization[0]   = 0.8;
00342     localSection.lumiSummary.OccNormalization[1]   = 0.85;
00343     localSection.lumiSummary.ETNormalization       = 0.8;
00344     localSection.lumiSummary.InstantLumi           = 0.9;
00345     localSection.lumiSummary.InstantLumiErr        = 0.10;
00346     localSection.lumiSummary.InstantLumiQlty       = 11;
00347     localSection.lumiSummary.InstantETLumi         = 0.12;
00348     localSection.lumiSummary.InstantETLumiErr      = 0.13;
00349     localSection.lumiSummary.InstantETLumiQlty     = 14;
00350     localSection.lumiSummary.InstantOccLumi[0]     = 0.15;
00351     localSection.lumiSummary.InstantOccLumiErr[0]  = 0.16;
00352     localSection.lumiSummary.InstantOccLumiQlty[0] = 17;
00353     localSection.lumiSummary.lumiNoise[0]          = 0.18;
00354     localSection.lumiSummary.InstantOccLumi[1]     = 0.19;
00355     localSection.lumiSummary.InstantOccLumiErr[1]  = 0.20;
00356     localSection.lumiSummary.InstantOccLumiQlty[1] = 21;
00357     localSection.lumiSummary.lumiNoise[1]          = 0.22;
00358     
00359     for(j=0; j < 3564; j++){
00360       localSection.lumiDetail.ETBXNormalization[j]     = 0.25*j/35640.0;
00361       localSection.lumiDetail.OccBXNormalization[0][j] = 0.5*j/35640.0;
00362       localSection.lumiDetail.OccBXNormalization[1][j] = 0.75*j/35640.0;      
00363       localSection.lumiDetail.LHCLumi[j]               = 1*j/35640.0; 
00364       localSection.lumiDetail.ETLumi[j]                = 2*j/35640.0;
00365       localSection.lumiDetail.ETLumiErr[j]             = 3*j/35640.0;
00366       localSection.lumiDetail.ETLumiQlty[j]            = 4*j;
00367       localSection.lumiDetail.OccLumi[0][j]            = 5*j/35640.0;
00368       localSection.lumiDetail.OccLumiErr[0][j]         = 6*j/35640.0;
00369       localSection.lumiDetail.OccLumiQlty[0][j]        = 7*j;
00370       localSection.lumiDetail.OccLumi[1][j]            = 8*j/35640.0;
00371       localSection.lumiDetail.OccLumiErr[1][j]         = 9*j/35640.0;
00372       localSection.lumiDetail.OccLumiQlty[1][j]        = 10*j;
00373     }
00374     
00375     for(i=0; i<36; i ++){
00376       localSection.etSum[i].hdr.numNibbles     = 7;
00377       localSection.occupancy[i].hdr.numNibbles = 8;
00378       localSection.lhc[i].hdr.numNibbles       = 9;
00379       
00380       localSection.etSum[i].hdr.bIsComplete     = true;
00381       localSection.occupancy[i].hdr.bIsComplete = true;
00382       localSection.lhc[i].hdr.bIsComplete       = true;
00383       
00384       for(j=0; j < 3564; j ++){
00385         localSection.etSum[i].data[j]          = 6*j+ 10*i;
00386         for(k=0; k < 6; k++){
00387           localSection.occupancy[i].data[k][j] = k*j + 11*i;
00388         }
00389         localSection.lhc[i].data[j]            = 7*j + 12*i;
00390       }
00391     }
00392 
00393 #ifdef DEBUG
00394     std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00395 #endif
00396 
00397   }
00398 
00399   void TCPReceiver::GenerateRandomData(HCAL_HLX::LUMI_SECTION & localSection){
00400 #ifdef DEBUG
00401     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00402 #endif
00403     int i, j, k;
00404 
00405     srand(time(NULL));
00406     localSection.hdr.runNumber     = 55; //(rand() % 100);
00407     localSection.hdr.startOrbit    = (rand() % 100);
00408     localSection.hdr.numOrbits     = (rand() % 100);
00409     localSection.hdr.numBunches    = (rand() % 100);
00410     localSection.hdr.numHLXs       = (rand() % 100);
00411     localSection.hdr.bCMSLive      = true;
00412     localSection.hdr.sectionNumber = (rand() %100);
00413     
00414     localSection.lumiSummary.DeadtimeNormalization = (float)(rand() % 100)/100;
00415     localSection.lumiSummary.LHCNormalization      = (float)(rand() % 100)/100;
00416     localSection.lumiSummary.OccNormalization[0]   = (float)(rand() % 100)/100;
00417     localSection.lumiSummary.OccNormalization[1]   = (float)(rand() % 100)/100;
00418     localSection.lumiSummary.ETNormalization       = (float)(rand() % 100)/100;
00419     localSection.lumiSummary.InstantLumi           = (float)(rand() % 100)/100;
00420     localSection.lumiSummary.InstantLumiErr        = (float)(rand() % 100)/100;
00421     localSection.lumiSummary.InstantLumiQlty       = (rand() % 100);
00422     localSection.lumiSummary.InstantETLumi         = (float)(rand() % 100)/100;
00423     localSection.lumiSummary.InstantETLumiErr      = (float)(rand() % 100)/100;
00424     localSection.lumiSummary.InstantETLumiQlty     = (rand() % 100);
00425     localSection.lumiSummary.InstantOccLumi[0]     = (float)(rand() % 100)/100;
00426     localSection.lumiSummary.InstantOccLumiErr[0]  = (float)(rand() % 100)/100;
00427     localSection.lumiSummary.InstantOccLumiQlty[0] = (rand() % 100);
00428     localSection.lumiSummary.lumiNoise[0]          = (float)(rand() % 100)/100;
00429     localSection.lumiSummary.InstantOccLumi[1]     = (float)(rand() % 100)/100;
00430     localSection.lumiSummary.InstantOccLumiErr[1]  = (float)(rand() % 100)/100;
00431     localSection.lumiSummary.InstantOccLumiQlty[1] = (rand() % 100);
00432     localSection.lumiSummary.lumiNoise[1]          = (float)(rand() % 100)/100;
00433     
00434     for(j=0; j < 3564; j++){
00435       localSection.lumiDetail.ETBXNormalization[j]     = 0.25*j/35640.0;
00436       localSection.lumiDetail.OccBXNormalization[0][j] = 0.5*j/35640.0;
00437       localSection.lumiDetail.OccBXNormalization[1][j] = 0.75*j/35640.0;      
00438       localSection.lumiDetail.LHCLumi[j]               = (float)(rand() % 100)/100.0;
00439       localSection.lumiDetail.ETLumi[j]                = (float)(rand() % 100)/100.0;
00440       localSection.lumiDetail.ETLumiErr[j]             = (float)(rand() % 100)/100.0;
00441       localSection.lumiDetail.ETLumiQlty[j]            = (rand() % 100);
00442       localSection.lumiDetail.OccLumi[0][j]            = (float)(rand() % 100)/100.0;
00443       localSection.lumiDetail.OccLumiErr[0][j]         = (float)(rand() % 100)/100.0;
00444       localSection.lumiDetail.OccLumiQlty[0][j]        = (rand() % 100);
00445       localSection.lumiDetail.OccLumi[1][j]            = (float)(rand() % 100)/100.0;
00446       localSection.lumiDetail.OccLumiErr[1][j]         = (float)(rand() % 100)/100.0;
00447       localSection.lumiDetail.OccLumiQlty[1][j]        = (rand() % 100);
00448     }
00449     
00450     for(i=0; i<36; i ++){
00451       localSection.etSum[i].hdr.numNibbles     = (rand() % 100);
00452       localSection.occupancy[i].hdr.numNibbles = 8*(rand() % 100);
00453       localSection.lhc[i].hdr.numNibbles       = 9*(rand() % 100);
00454       
00455       localSection.etSum[i].hdr.bIsComplete     = true;
00456       localSection.occupancy[i].hdr.bIsComplete = true;
00457       localSection.lhc[i].hdr.bIsComplete       = true;
00458       
00459       for(j=0; j < 3564; j ++){
00460         localSection.etSum[i].data[j]          = 6*(rand() % 3564);
00461         for(k=0; k < 6; k++){ 
00462           localSection.occupancy[i].data[k][j] = k*(rand() % 3564);
00463         }
00464         localSection.lhc[i].data[j]            = 7*(rand() % 3564);
00465       }
00466     } 
00467 
00468 #ifdef DEBUG
00469     std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00470 #endif
00471   }
00472 
00473   bool TCPReceiver::VerifyFakeData(HCAL_HLX::LUMI_SECTION & localSection){
00474 
00475     HCAL_HLX::LUMI_SECTION L;
00476     GenerateFakeData(L);
00477     return !(memcmp(&L, &localSection, sizeof(L)));
00478   }
00479 
00480 }