1 #include "gl3HistoManager.h"
6 #include <netinet/in.h>
10 #include <sys/socket.h>
23 gl3HistoManager::gl3HistoManager()
44 void gl3HistoManager::add(
gl3Histo *histo)
46 histoList.push_back(histo);
48 if (verbosity >= dbg) {
49 cout <<
"gl3HistoManager: Histogram '"
50 << histo->header.title <<
"' added." << endl;
54 void gl3HistoManager::remove(
gl3Histo *histo)
56 histoList.remove(histo);
60 int gl3HistoManager::resetHistos ( ) {
61 list<gl3Histo*>::iterator histo;
62 for(histo=histoList.begin(); histo!=histoList.end(); histo++) {
69 int gl3HistoManager::saveHistos(
char * filename)
71 char* buffer =
new char[maxBytes];
73 int nBytes = writeHistos (buffer) ;
76 LOG(ERR,
"gl3Conductor::saveHistos: writing to %s\n",
82 LOG(ERR,
"gl3Conductor::saveHistos: buffer too small\n",0,0,0,0,0);
87 int fd = open(filename, O_RDWR|O_CREAT, 00644);
90 LOG(ERR,
"gl3Conductor::saveHistos: unable to open file %s \n",
96 if (write(fd,buffer,nBytes) != nBytes) {
98 LOG(ERR,
"gl3Conductor::saveHistos: write to file %s failed\n",
115 int gl3HistoManager::listenAtPort(
int port)
117 if (is_listening)
return 0;
119 struct sockaddr_in gl3Address;
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);
128 fcntl(socketFd, F_SETFL, O_NONBLOCK);
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);
137 gl3Address.sin_family = AF_INET;
138 gl3Address.sin_port = htons(port);
139 gl3Address.sin_addr.s_addr = INADDR_ANY;
140 bzero(&(gl3Address.sin_zero), 8);
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);
149 if (listen(socketFd, backLog) == -1) {
150 LOG(ERR,
"gl3HistoManager::listen: listen: %s\n",strerror(errno) ,0,0,0,0);
159 int gl3HistoManager::checkRequest ( ) {
161 struct sockaddr_in remoteAddress;
162 socklen_t sin_size =
sizeof(
struct sockaddr_in);
164 if ((remoteSocket = accept(socketFd, (
struct sockaddr *)&remoteAddress,
170 char* buffer =
new char[maxBytes];
172 int nBytes = writeHistos(buffer) ;
175 LOG(ERR,
"gl3Conductor::checkHistoRequest: buffer too small \n ",0,0,0,0,0 );
178 int nSend = send(remoteSocket, buffer, nBytes, 0 ) ;
179 LOG(ERR,
"gl3Conductor: %d out of %d bytes sent\n ", nSend, nBytes ,0,0,0) ;
194 int gl3HistoManager::writeHistos (
char *buffer){
203 container->runNumber = runNumber ;
204 container->nHistos = histoList.size();
206 char* pointer = (
char *)&(container->buffer) ;
207 char* endBuffer = buffer + maxBytes ;
211 list<gl3Histo*>::iterator histo;
212 for(histo=histoList.begin(); histo!=histoList.end(); histo++) {
214 if (verbosity >= dbg) {
215 cout <<
"gl3HistoManager: Histogram '"
216 << (*histo)->header.title <<
"' added." << endl;
219 nBytes = (*histo)->Write ( endBuffer-pointer, pointer ) ;
222 LOG(ERR,
"gl3Container::writeHistos: buffer too short (%d bytes)\n",
226 nTotalBytes += nBytes ;
227 if ( nTotalBytes > maxBytes ) {
228 LOG(ERR,
"gl3HistoManager::writeHistos: nTotalBytes %d max %d\n",
229 nTotalBytes, maxBytes,0,0,0 ) ;
232 pointer += nBytes *
sizeof(char) ;
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) ;
int Reset()
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!