StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gl3HistoManager.cxx
1 #include "gl3HistoManager.h"
2 
3 #include <rtsLog.h>
4 
5 #include <errno.h>
6 #include <netinet/in.h>
7 #include <unistd.h>
8 #include <netdb.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <fcntl.h>
12 #include <string.h>
13 
14 #include <iostream>
15 
16 using namespace std;
17 
18 // ###########################################################
19 // # Instantiation
20 // ###########################################################
21 gl3HistoManager *gl3HistoManager::pinstance = 0;
22 
23 gl3HistoManager::gl3HistoManager()
24 {
25  histoList.clear();
26 
27  verbosity = warn;
28  maxBytes = 300000;
29 }
30 
31 
32 gl3HistoManager *gl3HistoManager::instance()
33 {
34  if (pinstance == 0) {
35  pinstance = new gl3HistoManager; // create sole instance
36  }
37  return pinstance; // address of sole instance
38 }
39 
40 
41 // ###########################################################
42 // # Histogram handling
43 // ###########################################################
44 void gl3HistoManager::add(gl3Histo *histo)
45 {
46  histoList.push_back(histo);
47 
48  if (verbosity >= dbg) {
49  cout << "gl3HistoManager: Histogram '"
50  << histo->header.title << "' added." << endl;
51  }
52 }
53 
54 void gl3HistoManager::remove(gl3Histo *histo)
55 {
56  histoList.remove(histo);
57 }
58 
59 
60 int gl3HistoManager::resetHistos ( ) {
61  list<gl3Histo*>::iterator histo;
62  for(histo=histoList.begin(); histo!=histoList.end(); histo++) {
63  (*histo)->Reset();
64  }
65  return 0 ;
66 }
67 
68 
69 int gl3HistoManager::saveHistos(char * filename)
70 {
71  char* buffer = new char[maxBytes];
72 
73  int nBytes = writeHistos (buffer) ;
74 
75  if(verbosity >= dbg)
76  LOG(ERR, "gl3Conductor::saveHistos: writing to %s\n",
77  filename,0,0,0,0) ;
78 
79 
80  if ( nBytes < 0 ) {
81  if(verbosity >= err)
82  LOG(ERR, "gl3Conductor::saveHistos: buffer too small\n",0,0,0,0,0);
83  return 1 ;
84  }
85 
86 
87  int fd = open(filename, O_RDWR|O_CREAT, 00644);
88  if ( fd<0 ) {
89  if(verbosity >= err)
90  LOG(ERR, "gl3Conductor::saveHistos: unable to open file %s \n",
91  filename,0,0,0,0) ;
92  return 1 ;
93  }
94 
95 
96  if (write(fd,buffer,nBytes) != nBytes) {
97  if(verbosity >= err)
98  LOG(ERR, "gl3Conductor::saveHistos: write to file %s failed\n",
99  filename,0,0,0,0) ;
100  return 1 ;
101  }
102 
103  delete[] buffer;
104 
105  close(fd);
106 
107  return 0;
108 }
109 
110 
111 // ###########################################################
112 // # Networking / remote requests
113 // ###########################################################
114 
115 int gl3HistoManager::listenAtPort(int port)
116 {
117  if (is_listening) return 0;
118 
119  struct sockaddr_in gl3Address; /* my address information */
120 
121  int backLog = 5 ; // how many pending connections will hold
122 
123  if ((socketFd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
124  LOG(ERR, "gl3HistoManager::listen: error opening socket: %s\n",
125  strerror(errno) ,0,0,0,0);
126  return -1;
127  }
128  fcntl(socketFd, F_SETFL, O_NONBLOCK);
129  int optval = 1;
130  if(setsockopt(socketFd, SOL_SOCKET,
131  SO_REUSEADDR, (void *)&optval, sizeof(optval)) == -1) {
132  LOG(ERR, "gl3HistoManager::listen: setsockopt: %s\n", strerror(errno),0,0,0,0);
133  return -1;
134  }
135 
136 
137  gl3Address.sin_family = AF_INET; // host byte order
138  gl3Address.sin_port = htons(port); // short, network byte order
139  gl3Address.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
140  bzero(&(gl3Address.sin_zero), 8); // zero the rest of the struct
141 
142 
143  if (bind(socketFd, (struct sockaddr *)&gl3Address,
144  sizeof(struct sockaddr)) == -1) {
145  LOG(ERR, "gl3HistoManager::listen: bind: %s\n",strerror(errno) ,0,0,0,0);
146  return -1;
147  }
148 
149  if (listen(socketFd, backLog) == -1) {
150  LOG(ERR, "gl3HistoManager::listen: listen: %s\n",strerror(errno) ,0,0,0,0);
151  return -1;
152  }
153 
154  is_listening = true;
155  return 0 ;
156 }
157 
158 
159 int gl3HistoManager::checkRequest ( ) {
160 
161  struct sockaddr_in remoteAddress; /* connector's address information */
162  socklen_t sin_size = sizeof(struct sockaddr_in);
163 
164  if ((remoteSocket = accept(socketFd, (struct sockaddr *)&remoteAddress,
165  &sin_size)) == -1) {
166  return 0;
167  }
168 
169  //const int maxBytes = 200000 ;
170  char* buffer = new char[maxBytes];
171 
172  int nBytes = writeHistos(buffer) ;
173 
174  if ( nBytes < 0 ) {
175  LOG(ERR, "gl3Conductor::checkHistoRequest: buffer too small \n ",0,0,0,0,0 );
176  return 1 ;
177  }
178  int nSend = send(remoteSocket, buffer, nBytes, 0 ) ;
179  LOG(ERR, "gl3Conductor: %d out of %d bytes sent\n ", nSend, nBytes ,0,0,0) ;
180  if ( nSend == -1) {
181  perror("send");
182  return 1 ;
183  }
184  delete []buffer ;
185 
186  return 0 ;
187 }
188 
189 
190 
191 // ####################################################################
192 // # write histos to a buffer
193 // ####################################################################
194 int gl3HistoManager::writeHistos (char *buffer){
195 
196  // I don't know how to get the run number, so I'll set it to a
197  // nonsense value;
198  int runNumber = -1;
199 
200  //gl3Histo* hPointer ;
201 
202  gl3HistoContainer* container = (gl3HistoContainer *)buffer ;
203  container->runNumber = runNumber ;
204  container->nHistos = histoList.size();
205 
206  char* pointer = (char *)&(container->buffer) ;
207  char* endBuffer = buffer + maxBytes ;
208  int nBytes ;
209  int nTotalBytes = sizeof(gl3HistoContainer) - sizeof(int);
210 
211  list<gl3Histo*>::iterator histo;
212  for(histo=histoList.begin(); histo!=histoList.end(); histo++) {
213 
214  if (verbosity >= dbg) {
215  cout << "gl3HistoManager: Histogram '"
216  << (*histo)->header.title << "' added." << endl;
217  }
218 
219  nBytes = (*histo)->Write ( endBuffer-pointer, pointer ) ;
220  if ( nBytes <= 0 ) {
221 
222  LOG(ERR, "gl3Container::writeHistos: buffer too short (%d bytes)\n",
223  maxBytes,0,0,0,0) ;
224  return 0 ;
225  }
226  nTotalBytes += nBytes ;
227  if ( nTotalBytes > maxBytes ) {
228  LOG(ERR, "gl3HistoManager::writeHistos: nTotalBytes %d max %d\n",
229  nTotalBytes, maxBytes,0,0,0 ) ;
230  return -1 ;
231  }
232  pointer += nBytes * sizeof(char) ;
233  }
234  container->nBytes = nTotalBytes ;
235  if(verbosity >= note) {
236  LOG(ERR, "gl3HistoManager::writeHistos: histos %d Bytes %d \n",
237  histoList.size(), nTotalBytes ,0,0,0) ;
238  }
239 
240  return nTotalBytes ;
241 }
int Reset()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Definition: gl3Histo.cxx:121