Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eventReceiverClient.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file eventReceiverClient.cc
1 #include "eventReceiverClient.h"
2 
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <arpa/inet.h>
9 #include <netinet/in.h>
10 #include <netdb.h>
11 #include <stdio.h>
12 #include <iostream>
13 #include <iomanip>
14 
15 #include "oncsEvent.h"
16 
17 using namespace std;
18 
19 #define PORT 8080
20 #define MAXSIZE 1024*1024
21 
22 eventReceiverClient::eventReceiverClient( const std::string hostname, const int flags)
23 {
24 
25  _hostname = hostname;
26  _flags = flags;
27  _broken = 0;
28 
29  //int buffer[MAXSIZE];
30 
31  struct addrinfo hints;
32  memset(&hints, 0, sizeof(struct addrinfo));
33 
34  hints.ai_family = AF_INET;
35  struct addrinfo *result;
36 
37  char port_str[512];
38  sprintf(port_str, "%d", PORT);
39 
40  int status = getaddrinfo(_hostname.c_str(), port_str,
41  &hints,
42  &result);
43 
44 
45  if ( status < 0)
46  {
47  cerr << _hostname << ": " << gai_strerror(status) << endl;
48  _broken =1;
49  return;
50  }
51 
52  struct sockaddr_in* ipv4;
53  ipv4 = ( struct sockaddr_in*)result->ai_addr;
54 
55 
56  // for (rp = result; rp != NULL; rp = rp->ai_next)
57  // {
58  // // char ip_str[INET_ADDRSTRLEN];
59  // //inet_ntop(AF_INET, &ipv4->sin_addr, ip_str, INET_ADDRSTRLEN);
60  // //cout << __FILE__<< " " << __LINE__ << " " << _hostname << " " << ip_str << endl;
61 
62  // }
63 
64  freeaddrinfo(result);
65 
66  // Creating socket file descriptor
67  if ( (_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
68  {
69  perror("socket creation failed");
70  _broken = 2;
71  }
72 
73  memcpy(&_serveraddr, ipv4, sizeof(*ipv4));
74 
75  // if (inet_aton(_hostname.c_str(), &_serveraddr.sin_addr)==0)
76  // {
77  // cerr << "invalid IP address " << _hostname << endl;
78  // _broken =3;
79  // }
80 
81  _serveraddr.sin_family = AF_INET;
82  _serveraddr.sin_port = htons(PORT);
83 
84 }
85 
87 {
88  if ( _sockfd > 0) close(_sockfd);
89 }
90 
91 Event *eventReceiverClient::getEvent(const int eventnumber, const int flag )
92 {
93 
94  int sendbuffer[2] = {0};
95  int buffer[MAXSIZE];
96 
97  if (_verbosity) cout << "requesting event " << eventnumber << endl;
98 
99  int f= flag;
100  if (!f) f = _flags;
101  sendbuffer[0] = eventnumber;
102  sendbuffer[1] = f;
103 
104  sendto(_sockfd, (const char *)sendbuffer, 2*sizeof(int),
105  0, (const struct sockaddr *) &_serveraddr,
106  sizeof(_serveraddr));
107 
108  socklen_t len;
109  int n = recvfrom(_sockfd, (char *)buffer, MAXSIZE*sizeof(int),
110  MSG_WAITALL, (struct sockaddr *) &_serveraddr,
111  &len);
112 
113  if (_verbosity) cout << " received " << n << " bytes" << std::endl;
114  if ( buffer[0] == 0)
115  {
116  if (_verbosity) std::cout << "Event " << sendbuffer[0] << " not found" << endl;
117  return NULL;
118  }
119 
120  Event *e = new oncsEvent(buffer);
121  e->convert();
122  if (_verbosity) e->identify();
123  return e;
124 }
125