00001
00002
00003
00004
00005
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
00018 #include <cstdlib>
00019
00020
00021 #include <cstdio>
00022
00023
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) {
00070 FD_ZERO(&ReadFDs);
00071 FD_ZERO(&WriteFDs);
00072 FD_ZERO(&ExceptFDs);
00073
00074
00075
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){
00096 if(Connected == false){
00097 errorCode = 701;
00098 } else {
00099 unsigned int bytesRcvd, bytesToReceive, totalBytesRcvd;
00100 const unsigned int Buffer_Size = 8192;
00101 char *Buffer;
00102 char *BigBuffer;
00103
00104
00105
00106 fd_set fdsRead, fdsWrite, fdsExcept;
00107
00108
00109
00110 time_t tempTime, curTime;
00111
00112
00113 time(&curTime);
00114
00115 bytesToReceive = sizeof(localSection);
00116 Buffer = new char[Buffer_Size];
00117 BigBuffer = new char[bytesToReceive];
00118 totalBytesRcvd = 0;
00119
00120 memset(reinterpret_cast<char *>(&localSection), 0, Buffer_Size);
00121 memset(Buffer, 0, Buffer_Size);
00122 memset(BigBuffer, 0, bytesToReceive);
00123
00124 usleep(10000);
00125
00126 while((totalBytesRcvd < bytesToReceive) && (errorCode == 0)){
00127
00128 SetupFDSets(fdsRead, fdsWrite, fdsExcept, -1, tcpSocket);
00129
00130 if(select(tcpSocket+1, &fdsRead, 0, &fdsExcept, 0)> 0){
00131
00132 if (FD_ISSET(tcpSocket, &fdsRead)) {
00133
00134 if((bytesRcvd = recv(tcpSocket, Buffer, Buffer_Size, 0))<=0){
00135 perror("Recv Error");
00136 errorCode = 501;
00137 }else{
00138 if((totalBytesRcvd + bytesRcvd)<= bytesToReceive){
00139 memcpy(&BigBuffer[totalBytesRcvd], Buffer, bytesRcvd);
00140 }else{
00141 std::cout << "***** MEM OVER FLOW: Did someone forget to update LumiStructures.hh? *****" << std::endl;
00142 errorCode = 502;
00143 }
00144 totalBytesRcvd += bytesRcvd;
00145 time(&tempTime);
00146 }
00147 }
00148 }
00149 }
00150
00151 if(errorCode == 0){
00152 memcpy(reinterpret_cast<char *>(&localSection), BigBuffer, sizeof(localSection));
00153 errorCode = 1;
00154 }
00155 delete [] Buffer;
00156 delete [] BigBuffer;
00157 }
00158 } else if(acquireMode == 1){
00159 GenerateFakeData(localSection);
00160 errorCode = 1;
00161 } else if(acquireMode == 2){
00162 GenerateRandomData(localSection);
00163 errorCode = 1;
00164 } else {
00165 errorCode = 201;
00166 }
00167
00168 #ifdef DEBUG
00169 std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00170 #endif
00171
00172 return errorCode;
00173 }
00174
00175 bool TCPReceiver::IsConnected(){
00176 #ifdef DEBUG
00177 std::cout << "Begin and End " << __PRETTY_FUNCTION__ << " " << Connected << std::endl;
00178 #endif
00179 return Connected;
00180 }
00181
00182 int TCPReceiver::SetPort(unsigned short int port){
00183 #ifdef DEBUG
00184 std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00185 #endif
00186
00187 int errorCode;
00188
00189 if(port < 1024){
00190 errorCode = 101;
00191 }else{
00192 servPort = port;
00193 errorCode = 1;
00194 }
00195
00196 #ifdef DEBUG
00197 std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00198 #endif
00199 return errorCode;
00200 }
00201
00202 int TCPReceiver::SetMode(unsigned char mode){
00203 #ifdef DEBUG
00204 std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00205 #endif
00206
00207 int errorCode;
00208
00209 if(mode > 2){
00210 errorCode = 201;
00211 } else {
00212 acquireMode = mode;
00213 errorCode = 1;
00214 }
00215
00216 #ifdef DEBUG
00217 std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00218 #endif
00219 return errorCode;
00220 }
00221
00222 void TCPReceiver::SetIP(std::string IP){
00223 #ifdef DEBUG
00224 std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00225 #endif
00226 servIP = IP;
00227 #ifdef DEBUG
00228 std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00229 #endif
00230 }
00231
00232 int TCPReceiver::Connect(){
00233 #ifdef DEBUG
00234 std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00235 #endif
00236
00237 int errorCode;
00238
00239 if(acquireMode == 0){
00240 struct hostent * hostInfo = gethostbyname(servIP.c_str());
00241
00242 if(servPort < 1024){
00243 errorCode = 101;
00244 } else {
00245 #ifdef DEBUG
00246 std::cout << "Requesting connection" << std::endl;
00247 #endif
00248 if((tcpSocket = socket(AF_INET, SOCK_STREAM, 0))<0){
00249 perror("Socket Error");
00250 errorCode = 301;
00251 }else{
00252 memset(&servAddr, 0, sizeof(servAddr));
00253 servAddr.sin_family = hostInfo->h_addrtype;
00254 memcpy((char *) &servAddr.sin_addr.s_addr,
00255 hostInfo->h_addr_list[0],
00256 hostInfo->h_length);
00257
00258 servAddr.sin_port = htons(servPort);
00259 #ifdef DEBUG
00260 std::cout << "Connecting" << std::endl;
00261 #endif
00262 if(connect(tcpSocket, (struct sockaddr *) &servAddr, sizeof(servAddr))<0){
00263 perror("Connect Error");
00264 errorCode = 302;
00265 close(tcpSocket);
00266 } else{
00267 Connected = true;
00268 errorCode = 1;
00269 }
00270 }
00271 }
00272 } else if(acquireMode == 1) {
00273 Connected = true;
00274 errorCode = 1;
00275 } else if(acquireMode == 2) {
00276 Connected = true;
00277 errorCode = 1;
00278 } else {
00279 errorCode = 201;
00280 }
00281
00282 #ifdef DEBUG
00283 std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00284 #endif
00285 return errorCode;
00286 }
00287
00288 int TCPReceiver::Disconnect(){
00289 #ifdef DEBUG
00290 std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00291 #endif
00292
00293 int errorCode = 0;
00294
00295 if(Connected){
00296 if( acquireMode == 0 ){
00297 if(shutdown(tcpSocket,SHUT_RDWR)<0){
00298 perror("Shutdown Error");
00299 errorCode = 601;
00300 } else {
00301
00302 errorCode = 1;
00303 }
00304 }
00305 Connected = false;
00306 } else {
00307 errorCode = 401;
00308 }
00309
00310 #ifdef DEBUG
00311 std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
00312 #endif
00313 return errorCode;
00314 }
00315
00316 void TCPReceiver::GenerateFakeData(HCAL_HLX::LUMI_SECTION & localSection){
00317 #ifdef DEBUG
00318 std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00319 #endif
00320 int i, j, k;
00321
00322 localSection.hdr.runNumber = 1;
00323 localSection.hdr.startOrbit = 2;
00324 localSection.hdr.numOrbits = 3;
00325 localSection.hdr.numBunches = 4;
00326 localSection.hdr.numHLXs = 5;
00327 localSection.hdr.bCMSLive = true;
00328 localSection.hdr.sectionNumber = 120;
00329
00330 timeval tvTemp;
00331 gettimeofday(&tvTemp, NULL);
00332 localSection.hdr.timestamp = tvTemp.tv_sec;
00333 localSection.hdr.timestamp_micros = tvTemp.tv_usec;
00334
00335 localSection.lumiSummary.DeadtimeNormalization = 0.7;
00336 localSection.lumiSummary.LHCNormalization = 0.75;
00337 localSection.lumiSummary.OccNormalization[0] = 0.8;
00338 localSection.lumiSummary.OccNormalization[1] = 0.85;
00339 localSection.lumiSummary.ETNormalization = 0.8;
00340 localSection.lumiSummary.InstantLumi = 0.9;
00341 localSection.lumiSummary.InstantLumiErr = 0.10;
00342 localSection.lumiSummary.InstantLumiQlty = 11;
00343 localSection.lumiSummary.InstantETLumi = 0.12;
00344 localSection.lumiSummary.InstantETLumiErr = 0.13;
00345 localSection.lumiSummary.InstantETLumiQlty = 14;
00346 localSection.lumiSummary.InstantOccLumi[0] = 0.15;
00347 localSection.lumiSummary.InstantOccLumiErr[0] = 0.16;
00348 localSection.lumiSummary.InstantOccLumiQlty[0] = 17;
00349 localSection.lumiSummary.lumiNoise[0] = 0.18;
00350 localSection.lumiSummary.InstantOccLumi[1] = 0.19;
00351 localSection.lumiSummary.InstantOccLumiErr[1] = 0.20;
00352 localSection.lumiSummary.InstantOccLumiQlty[1] = 21;
00353 localSection.lumiSummary.lumiNoise[1] = 0.22;
00354
00355 for(j=0; j < 3564; j++){
00356 localSection.lumiDetail.ETBXNormalization[j] = 0.25*j/35640.0;
00357 localSection.lumiDetail.OccBXNormalization[0][j] = 0.5*j/35640.0;
00358 localSection.lumiDetail.OccBXNormalization[1][j] = 0.75*j/35640.0;
00359 localSection.lumiDetail.LHCLumi[j] = 1*j/35640.0;
00360 localSection.lumiDetail.ETLumi[j] = 2*j/35640.0;
00361 localSection.lumiDetail.ETLumiErr[j] = 3*j/35640.0;
00362 localSection.lumiDetail.ETLumiQlty[j] = 4*j;
00363 localSection.lumiDetail.OccLumi[0][j] = 5*j/35640.0;
00364 localSection.lumiDetail.OccLumiErr[0][j] = 6*j/35640.0;
00365 localSection.lumiDetail.OccLumiQlty[0][j] = 7*j;
00366 localSection.lumiDetail.OccLumi[1][j] = 8*j/35640.0;
00367 localSection.lumiDetail.OccLumiErr[1][j] = 9*j/35640.0;
00368 localSection.lumiDetail.OccLumiQlty[1][j] = 10*j;
00369 }
00370
00371 for(i=0; i<36; i ++){
00372 localSection.etSum[i].hdr.numNibbles = 7;
00373 localSection.occupancy[i].hdr.numNibbles = 8;
00374 localSection.lhc[i].hdr.numNibbles = 9;
00375
00376 localSection.etSum[i].hdr.bIsComplete = true;
00377 localSection.occupancy[i].hdr.bIsComplete = true;
00378 localSection.lhc[i].hdr.bIsComplete = true;
00379
00380 for(j=0; j < 3564; j ++){
00381 localSection.etSum[i].data[j] = 6*j+ 10*i;
00382 for(k=0; k < 6; k++){
00383 localSection.occupancy[i].data[k][j] = k*j + 11*i;
00384 }
00385 localSection.lhc[i].data[j] = 7*j + 12*i;
00386 }
00387 }
00388
00389 #ifdef DEBUG
00390 std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00391 #endif
00392
00393 }
00394
00395 void TCPReceiver::GenerateRandomData(HCAL_HLX::LUMI_SECTION & localSection){
00396 #ifdef DEBUG
00397 std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
00398 #endif
00399 int i, j, k;
00400
00401 srand(time(NULL));
00402 localSection.hdr.runNumber = 55;
00403 localSection.hdr.startOrbit = (rand() % 100);
00404 localSection.hdr.numOrbits = (rand() % 100);
00405 localSection.hdr.numBunches = (rand() % 100);
00406 localSection.hdr.numHLXs = (rand() % 100);
00407 localSection.hdr.bCMSLive = true;
00408 localSection.hdr.sectionNumber = (rand() %100);
00409
00410 localSection.lumiSummary.DeadtimeNormalization = (float)(rand() % 100)/100;
00411 localSection.lumiSummary.LHCNormalization = (float)(rand() % 100)/100;
00412 localSection.lumiSummary.OccNormalization[0] = (float)(rand() % 100)/100;
00413 localSection.lumiSummary.OccNormalization[1] = (float)(rand() % 100)/100;
00414 localSection.lumiSummary.ETNormalization = (float)(rand() % 100)/100;
00415 localSection.lumiSummary.InstantLumi = (float)(rand() % 100)/100;
00416 localSection.lumiSummary.InstantLumiErr = (float)(rand() % 100)/100;
00417 localSection.lumiSummary.InstantLumiQlty = (rand() % 100);
00418 localSection.lumiSummary.InstantETLumi = (float)(rand() % 100)/100;
00419 localSection.lumiSummary.InstantETLumiErr = (float)(rand() % 100)/100;
00420 localSection.lumiSummary.InstantETLumiQlty = (rand() % 100);
00421 localSection.lumiSummary.InstantOccLumi[0] = (float)(rand() % 100)/100;
00422 localSection.lumiSummary.InstantOccLumiErr[0] = (float)(rand() % 100)/100;
00423 localSection.lumiSummary.InstantOccLumiQlty[0] = (rand() % 100);
00424 localSection.lumiSummary.lumiNoise[0] = (float)(rand() % 100)/100;
00425 localSection.lumiSummary.InstantOccLumi[1] = (float)(rand() % 100)/100;
00426 localSection.lumiSummary.InstantOccLumiErr[1] = (float)(rand() % 100)/100;
00427 localSection.lumiSummary.InstantOccLumiQlty[1] = (rand() % 100);
00428 localSection.lumiSummary.lumiNoise[1] = (float)(rand() % 100)/100;
00429
00430 for(j=0; j < 3564; j++){
00431 localSection.lumiDetail.ETBXNormalization[j] = 0.25*j/35640.0;
00432 localSection.lumiDetail.OccBXNormalization[0][j] = 0.5*j/35640.0;
00433 localSection.lumiDetail.OccBXNormalization[1][j] = 0.75*j/35640.0;
00434 localSection.lumiDetail.LHCLumi[j] = (float)(rand() % 100)/100.0;
00435 localSection.lumiDetail.ETLumi[j] = (float)(rand() % 100)/100.0;
00436 localSection.lumiDetail.ETLumiErr[j] = (float)(rand() % 100)/100.0;
00437 localSection.lumiDetail.ETLumiQlty[j] = (rand() % 100);
00438 localSection.lumiDetail.OccLumi[0][j] = (float)(rand() % 100)/100.0;
00439 localSection.lumiDetail.OccLumiErr[0][j] = (float)(rand() % 100)/100.0;
00440 localSection.lumiDetail.OccLumiQlty[0][j] = (rand() % 100);
00441 localSection.lumiDetail.OccLumi[1][j] = (float)(rand() % 100)/100.0;
00442 localSection.lumiDetail.OccLumiErr[1][j] = (float)(rand() % 100)/100.0;
00443 localSection.lumiDetail.OccLumiQlty[1][j] = (rand() % 100);
00444 }
00445
00446 for(i=0; i<36; i ++){
00447 localSection.etSum[i].hdr.numNibbles = (rand() % 100);
00448 localSection.occupancy[i].hdr.numNibbles = 8*(rand() % 100);
00449 localSection.lhc[i].hdr.numNibbles = 9*(rand() % 100);
00450
00451 localSection.etSum[i].hdr.bIsComplete = true;
00452 localSection.occupancy[i].hdr.bIsComplete = true;
00453 localSection.lhc[i].hdr.bIsComplete = true;
00454
00455 for(j=0; j < 3564; j ++){
00456 localSection.etSum[i].data[j] = 6*(rand() % 3564);
00457 for(k=0; k < 6; k++){
00458 localSection.occupancy[i].data[k][j] = k*(rand() % 3564);
00459 }
00460 localSection.lhc[i].data[j] = 7*(rand() % 3564);
00461 }
00462 }
00463
00464 #ifdef DEBUG
00465 std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
00466 #endif
00467 }
00468
00469 bool TCPReceiver::VerifyFakeData(HCAL_HLX::LUMI_SECTION & localSection){
00470
00471 HCAL_HLX::LUMI_SECTION L;
00472 GenerateFakeData(L);
00473 return !(memcmp(&L, &localSection, sizeof(L)));
00474 }
00475
00476 }