mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 18:17:59 +02:00
Exceptions: zmq socket class descriptors made into its own class for auto destruction upon construction exception, similarly for other try blocks. slsDetector and multislsdetector left to do
This commit is contained in:
@ -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;
|
||||
|
@ -45,12 +45,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),
|
||||
headerMessage(0)
|
||||
{
|
||||
portno (portnumber) {
|
||||
char ip[MAX_STR_LENGTH] = "";
|
||||
memset(ip, 0, MAX_STR_LENGTH);
|
||||
|
||||
@ -61,19 +56,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();
|
||||
@ -81,7 +76,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();
|
||||
@ -90,7 +85,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();
|
||||
@ -105,19 +100,16 @@ 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),
|
||||
headerMessage(0)
|
||||
{
|
||||
portno (portnumber) {
|
||||
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();
|
||||
@ -126,12 +118,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();
|
||||
@ -145,69 +137,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
|
||||
@ -332,7 +304,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;
|
||||
}
|
||||
@ -349,7 +321,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;
|
||||
}
|
||||
@ -367,7 +339,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);
|
||||
@ -461,29 +433,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;
|
||||
*/
|
||||
};
|
||||
|
||||
|
||||
@ -585,24 +534,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[1000];
|
||||
/** Socket descriptor */
|
||||
mySocketDescriptors sockfd;
|
||||
|
||||
/** Header Message pointer */
|
||||
zmq_msg_t* headerMessage;
|
||||
|
||||
};
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user