diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index 798dc127c..e39c49921 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -721,8 +721,8 @@ bool multiSlsDetector::initSharedMemory(bool verify) { cprintf(RED, "Multi shared memory (%d) version mismatch " "(expected 0x%x but got 0x%x)\n", detId, MULTI_SHMVERSION, thisMultiDetector->shmversion); - sharedMemory->UnmapSharedMemory(thisMultiDetector);/** is this unncessary? */ - delete sharedMemory;/** is this unncessary? */ + sharedMemory->UnmapSharedMemory(thisMultiDetector); + delete sharedMemory; sharedMemory = 0; throw SharedMemoryException(); } @@ -732,8 +732,8 @@ bool multiSlsDetector::initSharedMemory(bool verify) { created = true; } catch(...) { sharedMemory->RemoveSharedMemory(); - delete sharedMemory;/** is this unncessary? */ - sharedMemory = 0;/** is this unncessary? */ + delete sharedMemory; + sharedMemory = 0; thisMultiDetector = 0; throw; } @@ -4852,6 +4852,7 @@ int multiSlsDetector::createReceivingDataSockets(const bool destroy) { cprintf(MAGENTA, "Going to destroy data sockets\n"); //close socket for (vector::const_iterator it = zmqSocket.begin(); it != zmqSocket.end(); ++it) { + (*it)->Close(); delete(*it); } zmqSocket.clear(); diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h index 7250d63a3..04b323a2d 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h @@ -113,7 +113,8 @@ public: // create multiSlsDetector class if required if (myDetector==NULL) { \ try { \ - myDetector = new multiSlsDetector(id, verify, update); \ + multiSlsDetector* m = new multiSlsDetector(id, verify, update); \ + myDetector = m; \ } catch (const SharedMemoryException & e) { \ cout << e.GetMessage() << endl; \ return; \ diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index ee8311f45..a92cd11f9 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -1001,7 +1001,7 @@ slsDetectorDefs::sls_detector_module* slsDetector::createModule(detectorType ty nd = detlist.nDacs; na = detlist.nAdcs; } catch(...) { - ;// FIXME do what here? + return NULL; } dacs_t *dacs=new dacs_t[nd]; dacs_t *adcs=new dacs_t[na]; diff --git a/slsReceiverSoftware/include/HDF5FileStatic.h b/slsReceiverSoftware/include/HDF5FileStatic.h index 457f2347c..890ed2db5 100644 --- a/slsReceiverSoftware/include/HDF5FileStatic.h +++ b/slsReceiverSoftware/include/HDF5FileStatic.h @@ -270,13 +270,17 @@ public: dset->extend(dims); delete dspace; - dspace = new DataSpace(dset->getSpace()); + dspace = 0; + DataSpace* d = new DataSpace(dset->getSpace()); + dspace = d; hsize_t dims_para[1] = {dims[0]}; for (unsigned int i = 0; i < dset_para.size(); ++i) dset_para[i]->extend(dims_para); delete dspace_para; - dspace_para = new DataSpace(dset_para[0]->getSpace()); + dspace_para = 0; + DataSpace* ds = new DataSpace(dset_para[0]->getSpace()); + dspace_para = ds; } catch(Exception error){ @@ -318,14 +322,16 @@ public: FileAccPropList flist; flist.setFcloseDegree(H5F_CLOSE_STRONG); + int k = 0; if(!owenable) - fd = new H5File( fname.c_str(), H5F_ACC_EXCL, + k = new H5File( fname.c_str(), H5F_ACC_EXCL, FileCreatPropList::DEFAULT, flist ); else - fd = new H5File( fname.c_str(), H5F_ACC_TRUNC, + k = new H5File( fname.c_str(), H5F_ACC_TRUNC, FileCreatPropList::DEFAULT, flist ); + fd = k; //variables DataSpace dataspace = DataSpace (H5S_SCALAR); @@ -416,6 +422,7 @@ public: } catch(Exception error) { cprintf(RED,"Error in creating master HDF5 handles\n"); error.printErrorStack(); + if (fd) fd->close(); return 1; } return 0; @@ -459,14 +466,16 @@ public: //file FileAccPropList fapl; fapl.setFcloseDegree(H5F_CLOSE_STRONG); + int k = 0; if(!owenable) - fd = new H5File( fname.c_str(), H5F_ACC_EXCL, + k = new H5File( fname.c_str(), H5F_ACC_EXCL, FileCreatPropList::DEFAULT, fapl ); else - fd = new H5File( fname.c_str(), H5F_ACC_TRUNC, + k = new H5File( fname.c_str(), H5F_ACC_TRUNC, FileCreatPropList::DEFAULT, fapl ); + fd = k; //attributes - version double dValue=version; @@ -477,7 +486,8 @@ public: //dataspace hsize_t srcdims[3] = {nDimx, nDimy, nDimz}; hsize_t srcdimsmax[3] = {H5S_UNLIMITED, nDimy, nDimz}; - dspace = new DataSpace (3,srcdims,srcdimsmax); + DataSpace* d = new DataSpace (3,srcdims,srcdimsmax); + dspace = d; //dataset name @@ -494,12 +504,14 @@ public: // always create chunked dataset as unlimited is only supported with chunked layout hsize_t chunk_dims[3] ={maxchunkedimages, nDimy, nDimz}; plist.setChunk(3, chunk_dims); - dset = new DataSet (fd->createDataSet(dsetname.c_str(), dtype, *dspace, plist)); + DataSet* ds = new DataSet (fd->createDataSet(dsetname.c_str(), dtype, *dspace, plist)); + dset = ds; //create parameter datasets hsize_t dims[1] = {nDimx}; hsize_t dimsmax[1] = {H5S_UNLIMITED}; - dspace_para = new DataSpace (1,dims,dimsmax); + DataSpace* dsp = new DataSpace (1,dims,dimsmax); + dspace_para = dsp; // always create chunked dataset as unlimited is only supported with chunked layout DSetCreatPropList paralist; @@ -515,7 +527,7 @@ public: catch(Exception error){ cprintf(RED,"Error in creating HDF5 handles in object %d\n",ind); error.printErrorStack(); - fd->close(); + if (fd) fd->close(); return 1; } return 0; diff --git a/slsReceiverSoftware/include/ZmqSocket.h b/slsReceiverSoftware/include/ZmqSocket.h index ef23ea84a..b00cb40d2 100644 --- a/slsReceiverSoftware/include/ZmqSocket.h +++ b/slsReceiverSoftware/include/ZmqSocket.h @@ -44,10 +44,7 @@ public: * @param portnumber port number */ ZmqSocket (const char* const hostname_or_ip, const uint32_t portnumber): - portno (portnumber), - server (false), - contextDescriptor (NULL), - socketDescriptor (NULL) + portno (portnumber) // headerMessage(0) { char ip[MAX_STR_LENGTH] = ""; @@ -60,19 +57,19 @@ public: throw std::exception(); // construct address - sprintf (serverAddress, "tcp://%s:%d", ip, portno); + sprintf (sockfd.serverAddress, "tcp://%s:%d", ip, portno); #ifdef VERBOSE - cprintf(BLUE,"address:%s\n",serverAddress); + cprintf(BLUE,"address:%s\n",sockfd.serverAddress); #endif // create context - contextDescriptor = zmq_ctx_new(); - if (contextDescriptor == NULL) + sockfd.contextDescriptor = zmq_ctx_new(); + if (sockfd.contextDescriptor == 0) throw std::exception(); // create publisher - socketDescriptor = zmq_socket (contextDescriptor, ZMQ_SUB); - if (socketDescriptor == NULL) { + sockfd.socketDescriptor = zmq_socket (sockfd.contextDescriptor, ZMQ_SUB); + if (sockfd.socketDescriptor == 0) { PrintError (); Close (); throw std::exception(); @@ -80,7 +77,7 @@ public: //Socket Options provided above // an empty string implies receiving any messages - if ( zmq_setsockopt(socketDescriptor, ZMQ_SUBSCRIBE, "", 0)) { + if ( zmq_setsockopt(sockfd.socketDescriptor, ZMQ_SUBSCRIBE, "", 0)) { PrintError (); Close(); throw std::exception(); @@ -89,7 +86,7 @@ public: //ZMQ_SNDHWM default is 0 means no limit. use this to optimize if optimizing required // eg. int value = -1; int value = 0; - if (zmq_setsockopt(socketDescriptor, ZMQ_LINGER, &value,sizeof(value))) { + if (zmq_setsockopt(sockfd.socketDescriptor, ZMQ_LINGER, &value,sizeof(value))) { PrintError (); Close(); throw std::exception(); @@ -104,19 +101,19 @@ public: * @param ethip is the ip of the ethernet interface to stream zmq from */ ZmqSocket (const uint32_t portnumber, const char *ethip): - portno (portnumber), - server (true), - contextDescriptor (NULL), - socketDescriptor (NULL) + + portno (portnumber) // headerMessage(0) { + sockfd.server = true; + // create context - contextDescriptor = zmq_ctx_new(); - if (contextDescriptor == NULL) + sockfd.contextDescriptor = zmq_ctx_new(); + if (sockfd.contextDescriptor == 0) throw std::exception(); // create publisher - socketDescriptor = zmq_socket (contextDescriptor, ZMQ_PUB); - if (socketDescriptor == NULL) { + sockfd.socketDescriptor = zmq_socket (sockfd.contextDescriptor, ZMQ_PUB); + if (sockfd.socketDescriptor == 0) { PrintError (); Close (); throw std::exception(); @@ -125,12 +122,12 @@ public: //Socket Options provided above // construct addresss - sprintf (serverAddress,"tcp://%s:%d", ethip, portno); + sprintf (sockfd.serverAddress,"tcp://%s:%d", ethip, portno); #ifdef VERBOSE - cprintf(BLUE,"address:%s\n",serverAddress); + cprintf(BLUE,"address:%s\n",sockfd.serverAddress); #endif // bind address - if (zmq_bind (socketDescriptor, serverAddress) < 0) { + if (zmq_bind (sockfd.socketDescriptor, sockfd.serverAddress) < 0) { PrintError (); Close (); throw std::exception(); @@ -144,69 +141,49 @@ public: * Destructor */ ~ZmqSocket () { - Disconnect(); - Close(); + //mySocketDescriptor destructor also gets called }; - - /** - * Returns Server Address - * @returns Server Address - */ - char* GetZmqServerAddress () { return serverAddress; }; - /** * Returns Port Number * @returns Port Number */ uint32_t GetPortNumber () { return portno; }; + /** + * Returns Server Address + * @returns Server Address + */ + char* GetZmqServerAddress () { return sockfd.serverAddress; }; + /** * Returns Socket Descriptor * @reutns Socket descriptor */ - void* GetsocketDescriptor () { return socketDescriptor; }; + void* GetsocketDescriptor () { return sockfd.socketDescriptor; }; /** * Connect client socket to server socket * @returns 1 for fail, 0 for success */ int Connect() { - if (zmq_connect(socketDescriptor, serverAddress) < 0) { + if (zmq_connect(sockfd.socketDescriptor, sockfd.serverAddress) < 0) { PrintError (); - Close (); return 1; } return 0; } - /** * Unbinds the Socket */ - void Disconnect () { - if (server) - zmq_unbind (socketDescriptor, serverAddress); - else - zmq_disconnect (socketDescriptor, serverAddress); - }; + void Disconnect () {sockfd.Disconnect();}; /** * Close Socket and destroy Context */ - void Close () { - if (socketDescriptor != NULL) { - zmq_close (socketDescriptor); - socketDescriptor = NULL; - } - - if (contextDescriptor != NULL) { - zmq_ctx_destroy (contextDescriptor); - contextDescriptor = NULL; - } - }; - + void Close () { sockfd.Close(); }; /** * Convert Hostname to Internet address info structure @@ -331,7 +308,7 @@ public: cprintf(BLUE,"%d : Streamer: buf: %s\n", index, buf); #endif - if(zmq_send (socketDescriptor, buf, length, dummy?0:ZMQ_SNDMORE) < 0) { + if(zmq_send (sockfd.socketDescriptor, buf, length, dummy?0:ZMQ_SNDMORE) < 0) { PrintError (); return 0; } @@ -348,7 +325,7 @@ public: * @returns 0 if error, else 1 */ int SendData (char* buf, int length) { - if(zmq_send (socketDescriptor, buf, length, 0) < 0) { + if(zmq_send (sockfd.socketDescriptor, buf, length, 0) < 0) { PrintError (); return 0; } @@ -366,7 +343,7 @@ public: * @returns length of message, -1 if error */ int ReceiveMessage(const int index, zmq_msg_t& message) { - int length = zmq_msg_recv (&message, socketDescriptor, 0); + int length = zmq_msg_recv (&message, sockfd.socketDescriptor, 0); if (length == -1) { PrintError (); cprintf (BG_RED,"Error: Could not read header for socket %d\n",index); @@ -393,7 +370,7 @@ public: // zmq_msg_init (&message); // int len = ReceiveMessage(index, message); std::vectorbuffer(MAX_STR_LENGTH); - int len = zmq_recv(socketDescriptor, buffer.data(), buffer.size(),0); + int len = zmq_recv(sockfd.socketDescriptor, buffer.data(), buffer.size(),0); if ( len > 0 ) { bool dummy = false; #ifdef ZMQ_DETAIL @@ -462,29 +439,6 @@ public: dummy = temp ? false : true; return 1; - /* - int temp = d["data"].GetUint(); - dummy = temp ? false : true; - if (!dummy) { - acqIndex = d["acqIndex"].GetUint64(); - frameIndex = d["fIndex"].GetUint64(); - fileIndex = d["fileIndex"].GetUint64(); - subframeIndex = d["expLength"].GetUint(); - filename = d["fname"].GetString(); - } -#ifdef VERYVERBOSE - cprintf(BLUE,"%d Dummy:%d\n" - "\tAcqIndex:%lu\n" - "\tFrameIndex:%lu\n" - "\tSubIndex:%u\n" - "\tFileIndex:%lu\n" - "\tBitMode:%u\n" - "\tDetType:%u\n", - index, (int)dummy, acqIndex, frameIndex, subframeIndex, fileIndex, - d["bitmode"].GetUint(),d["detType"].GetUint()); -#endif - return 1; - */ }; @@ -586,24 +540,60 @@ public: }; +private: + + /** + * Class to close socket descriptors automatically + * upon encountering exceptions in the ZmqSocket constructor + */ + class mySocketDescriptors { + public: + /** Constructor */ + mySocketDescriptors(): + server(false), + contextDescriptor(0), + socketDescriptor(0) {}; + /** Destructor */ + ~mySocketDescriptors() { + Disconnect(); + Close(); + } + /** Unbinds the Socket */ + void Disconnect () { + if (server) + zmq_unbind (socketDescriptor, serverAddress); + else + zmq_disconnect (socketDescriptor, serverAddress); + }; + /** Close Socket and destroy Context */ + void Close () { + if (socketDescriptor != NULL) { + zmq_close (socketDescriptor); + socketDescriptor = NULL; + } + + if (contextDescriptor != NULL) { + zmq_ctx_destroy (contextDescriptor); + contextDescriptor = NULL; + } + }; + /** true if server, else false */ + bool server; + /** Server Address */ + char serverAddress[1000]; + /** Context Descriptor */ + void* contextDescriptor; + /** Socket Descriptor */ + void* socketDescriptor; + }; private: /** Port Number */ uint32_t portno; - /** true if server, else false */ - bool server; - - /** Context Descriptor */ - void* contextDescriptor; - - /** Socket Descriptor */ - void* socketDescriptor; - - /** Server Address */ - char serverAddress[MAX_STR_LENGTH]; + /** Socket descriptor */ + mySocketDescriptors sockfd; /** Header Message pointer */ - // zmq_msg_t* headerMessage; - + //zmq_msg_t* headerMessage; }; diff --git a/slsReceiverSoftware/include/genericSocket.h b/slsReceiverSoftware/include/genericSocket.h index 6dc972065..f605627ab 100644 --- a/slsReceiverSoftware/include/genericSocket.h +++ b/slsReceiverSoftware/include/genericSocket.h @@ -50,31 +50,6 @@ using namespace std; #define DEFAULT_BACKLOG 5 -/** - * Class to close socket descriptors automatically - * upon encountering exceptions in the constructor - */ -class mySocketDescriptors { -public: - mySocketDescriptors():fd(-1), newfd(-1){}; - ~mySocketDescriptors() { - // close TCP server new socket descriptor from accept - if (newfd >= 0) { - close(newfd); - } - // close socket descriptor - if (fd >= 0) { - close(fd); - } - } - /** socket descriptor */ - int fd; - /** new socket descriptor in TCP server from accept */ - int newfd; -}; - - - class genericSocket{ public: @@ -814,6 +789,33 @@ public: char thisClientIP[INET_ADDRSTRLEN]; int differentClients; +private: + /** + * Class to close socket descriptors automatically + * upon encountering exceptions in the genericSocket constructor + */ + class mySocketDescriptors { + public: + + /** Constructor */ + mySocketDescriptors():fd(-1), newfd(-1){}; + /** Destructor */ + ~mySocketDescriptors() { + // close TCP server new socket descriptor from accept + if (newfd >= 0) { + close(newfd); + } + // close socket descriptor + if (fd >= 0) { + close(fd); + } + } + /** socket descriptor */ + int fd; + /** new socket descriptor in TCP server from accept */ + int newfd; + }; + protected: int portno; communicationProtocol protocol; diff --git a/slsReceiverSoftware/src/DataStreamer.cpp b/slsReceiverSoftware/src/DataStreamer.cpp index aca101686..428875e20 100644 --- a/slsReceiverSoftware/src/DataStreamer.cpp +++ b/slsReceiverSoftware/src/DataStreamer.cpp @@ -132,7 +132,8 @@ void DataStreamer::CreateZmqSockets(int* nunits, uint32_t port, const char* srci uint32_t portnum = port + index; try { - zmqSocket = new ZmqSocket(portnum, (strlen(srcip)?srcip:NULL)); + ZmqSocket* z = new ZmqSocket(portnum, (strlen(srcip)?srcip:NULL)); + zmqSocket = z; } catch (...) { cprintf(RED, "Error: Could not create Zmq socket on port %d for Streamer %d\n", portnum, index); throw;