Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:34

0001 #include "eventReceiverClient.h"
0002 
0003 #include <stdlib.h> 
0004 #include <unistd.h> 
0005 #include <string.h> 
0006 #include <sys/types.h> 
0007 #include <sys/socket.h> 
0008 #include <arpa/inet.h> 
0009 #include <netinet/in.h> 
0010 #include <netdb.h>
0011 #include <stdio.h> 
0012 #include <iostream>
0013 #include <iomanip>
0014 
0015 #include "oncsEvent.h"
0016 
0017 using namespace std;
0018 
0019 #define PORT     8080 
0020 #define MAXSIZE 1024*1024
0021 
0022 eventReceiverClient::eventReceiverClient( const std::string hostname, const int flags, const int port)
0023 {
0024 
0025   _hostname = hostname;
0026   _port = port;
0027   _flags = flags;
0028   _broken = 0;
0029   _had_timeout = 0;
0030   _verbosity = 0;
0031 
0032   _timeout = 300000;  // 300ms default timeout
0033   _user_timeout = 0;  // 300ms default timeout
0034  
0035   //int  buffer[MAXSIZE];
0036 
0037   struct addrinfo hints;
0038   memset(&hints, 0, sizeof(struct addrinfo));
0039   
0040   hints.ai_family = AF_INET;
0041   struct addrinfo *result;
0042 
0043   char port_str[512];
0044   sprintf(port_str, "%d", _port);
0045 
0046   int status = getaddrinfo(_hostname.c_str(), port_str,
0047                &hints,
0048                &result);
0049 
0050 
0051   if ( status < 0)
0052     {
0053       cerr  << _hostname << ": " << gai_strerror(status) << endl;
0054       _broken =1;
0055       return;
0056     }
0057 
0058   struct sockaddr_in* ipv4;
0059   ipv4 = ( struct sockaddr_in*)result->ai_addr;
0060       
0061 
0062   // for (rp = result; rp != NULL; rp = rp->ai_next)
0063   //   {
0064   //     //      char ip_str[INET_ADDRSTRLEN];
0065   //     //inet_ntop(AF_INET, &ipv4->sin_addr, ip_str, INET_ADDRSTRLEN);
0066   //     //cout << __FILE__<< " " << __LINE__ << " " << _hostname << " " << ip_str << endl;
0067       
0068   //   }
0069 
0070   freeaddrinfo(result);
0071   
0072   // Creating socket file descriptor 
0073   if ( (_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
0074     { 
0075       perror("socket creation failed"); 
0076       _broken = 2;
0077     } 
0078 
0079   memcpy(&_serveraddr, ipv4, sizeof(*ipv4)); 
0080 
0081   // if (inet_aton(_hostname.c_str(), &_serveraddr.sin_addr)==0)
0082   //   {
0083   //     cerr << "invalid IP address " << _hostname << endl;
0084   //     _broken =3;
0085   //   }
0086 
0087   _serveraddr.sin_family = AF_INET; 
0088   _serveraddr.sin_port = htons(_port); 
0089 
0090 }
0091 
0092 eventReceiverClient::~eventReceiverClient()
0093 {
0094   if ( _sockfd > 0) close(_sockfd);
0095 }
0096 
0097 Event *eventReceiverClient::getEvent(const int eventnumber, const int flag )
0098 {
0099   
0100   int sendbuffer[2] = {0};
0101   int buffer[MAXSIZE];
0102   
0103   if (_verbosity) cout << "requesting event " << eventnumber << endl;
0104 
0105   int f= flag;
0106   if (!f) f = _flags;
0107   sendbuffer[0] = eventnumber;
0108   sendbuffer[1] = f;
0109   
0110   sendto(_sockfd, (const char *)sendbuffer, 2*sizeof(int), 
0111      0, (const struct sockaddr *) &_serveraddr, 
0112      sizeof(_serveraddr)); 
0113 
0114 
0115   struct timeval tv;
0116   fd_set myset;
0117 
0118   if ( _user_timeout )
0119     {
0120       tv.tv_sec = _user_timeout;
0121       tv.tv_usec = 0;
0122     }
0123   else
0124     {
0125       tv.tv_sec = 0;
0126       tv.tv_usec = _timeout;
0127     }
0128 
0129   FD_ZERO(&myset);
0130   FD_SET(_sockfd, &myset);
0131 
0132   
0133   //coutfl << "waiting for data, highest_fd =  " << _sockfd << endl;
0134   int retval = select(_sockfd+1, &myset, 0, 0, &tv);
0135 
0136   if (retval <=0)
0137     {
0138       if (_verbosity) cout << "timeout receiving event " << eventnumber << endl;
0139       _had_timeout = 1;
0140       return 0;
0141     }
0142   else
0143     {
0144 
0145       socklen_t len; 
0146       int n = recvfrom(_sockfd, (char *)buffer, MAXSIZE*sizeof(int), 
0147                MSG_WAITALL, (struct sockaddr *) &_serveraddr, 
0148                &len);
0149       _had_timeout = 0;
0150   
0151       if (_verbosity) cout << " received " << n << " bytes" << std::endl;
0152       if ( buffer[0] == 0)
0153     {
0154       if (_verbosity) std::cout << "Event " << sendbuffer[0] << " not found"  << endl;
0155       return NULL;
0156     }
0157       
0158       if ( n < 4*buffer[0] ) // idiot check - we have received fewer data than the event says it has
0159     {
0160       if (_verbosity) std::cout << " corrupt Event, need " << sendbuffer[0]*4  << " bytes but got " << n << endl;
0161       return NULL;
0162     }     
0163 
0164       Event *e = new oncsEvent(buffer);
0165       e->convert();
0166       if (_verbosity) e->identify();
0167       return e;
0168     }
0169   return 0;
0170 }