sharedmem: config, sethostname call freesharedmemory and call setupmulti, freesharedmemoy(nonstatic) also cleans up member variables to make it consistent with shm, sharedmemory pointer set to 0 in non static free shared memory to be able to continue within application, sethostname frees memory only if detectors found in shm, add calls addmultipledetectors, replace replaces a detector in sls detector level, should also take lines in config file that has comments in between while ignoring the comments, sethostname in slsdetector also updates client, adding object in sls when creating new shm complains if shm existed and must delete again, removed remove commands

This commit is contained in:
2018-06-27 15:38:03 +02:00
parent a0016cb005
commit 95254619c6
8 changed files with 204 additions and 171 deletions

View File

@@ -36,12 +36,7 @@ multiSlsDetector::multiSlsDetector(int id, bool verify, bool update)
thisMultiDetector(0),
client_downstream(false),
threadpool(0) {
if (initSharedMemory(verify))
// shared memory just created, so initialize the structure
initializeDetectorStructure();
initializeMembers(verify);
if (update)
updateUserdetails();
setupMultiDetector(verify, update);
}
@@ -70,6 +65,15 @@ bool multiSlsDetector::isMultiSlsDetectorClass() {
return true;
}
void multiSlsDetector::setupMultiDetector(bool verify, bool update) {
if (initSharedMemory(verify))
// shared memory just created, so initialize the structure
initializeDetectorStructure();
initializeMembers(verify);
if (update)
updateUserdetails();
}
std::string multiSlsDetector::concatResultOrPos(std::string (slsDetector::*somefunc)(int), int pos) {
if (pos >= 0 && pos < detectors.size()) {
return (detectors[pos]->*somefunc)(pos);
@@ -637,21 +641,34 @@ void multiSlsDetector::freeSharedMemory(int multiId) {
void multiSlsDetector::freeSharedMemory() {
// should be done before the detector list is deleted
clearAllErrorMask();
// single detector vector
// clear sls detector vector shm
for (vector<slsDetector*>::const_iterator it = detectors.begin(); it != detectors.end(); ++it) {
(*it)->freeSharedMemory();
delete (*it);
}
detectors.clear();
// multi detector
// clear multi detector shm
if (sharedMemory) {
sharedMemory->UnmapSharedMemory(thisMultiDetector);
sharedMemory->RemoveSharedMemory();
delete sharedMemory;
sharedMemory = 0;
}
thisMultiDetector = 0;
// clear zmq vector
for (vector<ZmqSocket*>::const_iterator it = zmqSocket.begin(); it != zmqSocket.end(); ++it) {
delete(*it);
}
zmqSocket.clear();
// zmq
destroyThreadPool();
client_downstream = false;
}
@@ -683,8 +700,9 @@ std::string multiSlsDetector::getUserDetails() {
bool multiSlsDetector::initSharedMemory(bool verify) {
// clear
if (sharedMemory)
if (sharedMemory) {
delete sharedMemory;
}
thisMultiDetector = 0;
for (vector<slsDetector*>::const_iterator it = detectors.begin(); it != detectors.end(); ++it) {
@@ -705,6 +723,7 @@ bool multiSlsDetector::initSharedMemory(bool verify) {
MULTI_SHMVERSION, thisMultiDetector->shmversion);
sharedMemory->UnmapSharedMemory(thisMultiDetector);/** is this unncessary? */
delete sharedMemory;/** is this unncessary? */
sharedMemory = 0;
throw SharedMemoryException();
}
} else {
@@ -713,6 +732,8 @@ bool multiSlsDetector::initSharedMemory(bool verify) {
created = true;
} catch(...) {
sharedMemory->RemoveSharedMemory();
delete sharedMemory;/** is this unncessary? */
sharedMemory = 0;/** is this unncessary? */
thisMultiDetector = 0;
throw;
}
@@ -911,6 +932,22 @@ std::string multiSlsDetector::exec(const char* cmd) {
void multiSlsDetector::setHostname(const char* name) {
// this check is there only to allow the previous detsizechan command
if (thisMultiDetector->numberOfDetectors) {
cprintf(RED, "Warning: There are already detector(s) in shared memory."
"Freeing Shared memory now.\n");
freeSharedMemory();
setupMultiDetector();
}
addMultipleDetectors(name);
}
string multiSlsDetector::getHostname(int pos) {
return concatResultOrPos(&slsDetector::getHostname, pos);
}
void multiSlsDetector::addMultipleDetectors(const char* name) {
size_t p1 = 0;
string temp = string(name);
size_t p2 = temp.find('+', p1);
@@ -931,13 +968,6 @@ void multiSlsDetector::setHostname(const char* name) {
setOnline();
}
string multiSlsDetector::getHostname(int pos) {
return concatResultOrPos(&slsDetector::getHostname, pos);
}
void multiSlsDetector::addSlsDetector (std::string s) {
#ifdef VERBOSE
cout << "Adding detector " << s << endl;
@@ -956,8 +986,7 @@ void multiSlsDetector::addSlsDetector (std::string s) {
// get type by connecting
detectorType type = slsDetector::getDetectorType(s.c_str(), DEFAULT_PORTNO);
if (type == GENERIC) {
cout << "Detector " << s << "does not exist in shared memory "
"and could not connect to it to determine the type!" << endl;
cout << "Could not connect to Detector " << s << " to determine the type!" << endl;
setErrorMask(getErrorMask() | MULTI_DETECTORS_NOT_ADDED);
appendNotAddedList(s.c_str());
return;
@@ -970,9 +999,7 @@ void multiSlsDetector::addSlsDetector (std::string s) {
detectors.push_back(sdet);
thisMultiDetector->numberOfDetectors = detectors.size();
detectors[pos]->setHostname(s.c_str());
if (detectors[pos]->setOnline() ==ONLINE_FLAG)
detectors[pos]->updateDetector();
detectors[pos]->setHostname(s.c_str()); // also updates client
thisMultiDetector->dataBytes += detectors[pos]->getDataBytes();
thisMultiDetector->dataBytesInclGapPixels += detectors[pos]->getDataBytesInclGapPixels();
@@ -1386,16 +1413,9 @@ int multiSlsDetector::exitServer() {
}
int multiSlsDetector::readConfigurationFile(string const fname) {
{
clearAllErrorMask();
freeSharedMemory();
if (initSharedMemory())
// shared memory just created, so initialize the structure
initializeDetectorStructure();
initializeMembers(true); // also deletes zmq objects and destroys threadpool
updateUserdetails();
}
freeSharedMemory();
setupMultiDetector();
multiSlsDetectorClient* cmd;
@@ -1419,9 +1439,12 @@ int multiSlsDetector::readConfigurationFile(string const fname) {
sargval = "0";
getline(infile, str);
++iline;
str.erase(str.find('#'), string::npos);
// remove comments that come after
if (str.find('#') != string::npos)
str.erase(str.find('#'));
#ifdef VERBOSE
std::cout << str << std::endl;
std::cout << "string:" << str << std::endl;
#endif
if (str.length() < 2) {
#ifdef VERBOSE

View File

@@ -21,7 +21,7 @@ class ZmqSocket;
#define MULTI_SHMVERSION 0x180625
#define SHORT_STRING_LENGTH 50
#define DATE_LENGTH 29
#define DATE_LENGTH 30
class multiSlsDetector : public slsDetectorUtils {
@@ -244,6 +244,14 @@ public:
*/
bool isMultiSlsDetectorClass();
/**
* Creates/open shared memory, initializes detector structure and members
* Called by constructor/ set hostname / read config file
* @param verify true to verify if shared memory version matches existing one
* @param update true to update last user pid, date etc
*/
void setupMultiDetector(bool verify = true, bool update = true);
/**
* If specific position, then provide result with that detector at position pos
* else concatenate the result of all detectors
@@ -501,6 +509,8 @@ public:
/**
* Free shared memory and delete shared memory structure
* occupied by the sharedMultiSlsDetector structure
* Clears all the vectors and destroys threadpool to bring
* object back to state before object creation amap
*/
void freeSharedMemory();
@@ -525,6 +535,13 @@ public:
*/
std::string getHostname(int pos = -1);
/**
* Appends detectors to the end of the list in shared memory
* Connects to them to set up online flag
* @param name concatenated hostname of the sls detectors to be appended to the list
*/
void addMultipleDetectors(const char* name);
using slsDetectorBase::getDetectorType;
/**

View File

@@ -55,7 +55,7 @@ public:
// sls pos scanned
iv=sscanf(argv[0],"%d:%s", &pos, cmd); \
if (iv != 2 ) \
pos = -1; \
pos = -1; \
if (iv == 2 && pos >= 0) { \
argv[0] = cmd; \
cout << pos << ":" ; \
@@ -79,19 +79,8 @@ public:
multiSlsDetector::freeSharedMemory(id); \
return; \
} \
// (sls level): give error message
// (multi level): free before calling multiSlsDetector constructor
else if (scmd == "hostname") { \
if (pos != -1) { \
cout << "pos " << pos << "not allowed for hostname. " \
"Only from multi detector level." << endl; \
return; \
} \
else \
multiSlsDetector::freeSharedMemory(id); \
} \
// get user details without verify sharedMultiSlsDetector version
else if ((scmd == "userdetails") && (action==slsDetectorDefs::GET_ACTION)) {\
else if ((scmd == "user") && (action==slsDetectorDefs::GET_ACTION)) { \
verify = false; \
update = false; \
myDetector=NULL; \
@@ -123,7 +112,7 @@ public:
return; \
}; \
cout<<"id:"<<id<<" pos:"<<pos<<endl;
//cout<<"id:"<<id<<" pos:"<<pos<<endl;
// call multi detector command line
myCmd=new multiSlsDetectorCommand(myDetector); \
answer=myCmd->executeLine(argc, argv, action, pos); \