new exceptions

This commit is contained in:
Erik Frojdh
2019-03-14 16:37:36 +01:00
parent 5235a87e93
commit 9639e480d9
9 changed files with 113 additions and 58 deletions

View File

@ -2,20 +2,60 @@
#include "catch.hpp" #include "catch.hpp"
#include "ClientSocket.h" #include "ClientSocket.h"
#include "Timer.h"
#include "logger.h" #include "logger.h"
#include "slsDetector.h" #include "slsDetector.h"
#include "sls_detector_defs.h" #include "sls_detector_defs.h"
#include "sls_detector_exceptions.h"
#include "Timer.h"
#include "sls_detector_funcs.h" #include "sls_detector_funcs.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#define VERBOSE #define VERBOSE
using sls::RuntimeError;
using sls::SharedMemoryError;
using sls::SocketError;
int main() { int main() {
//Catch exception
try {
throw RuntimeError("something went wrong");
} catch (RuntimeError &e) {
std::cout << "Caught RuntimeError with message : " << e.what() << '\n';
}
//Catch base class
try {
throw SharedMemoryError("Could not create shared memory");
} catch (RuntimeError &e) {
std::cout << "Caught: " << e.what() << '\n';
}
//Catch base class after looking for something else
try {
throw SharedMemoryError("Could not create shared memory");
} catch (SocketError &e) {
std::cout << "Caught Socket error: " << e.what() << '\n';
} catch (RuntimeError &e) {
std::cout << "Caught base class: " << e.what() << '\n';
}
//Catch any after looking for something else
try {
throw SharedMemoryError("Could not create shared memory");
} catch (SocketError &e) {
std::cout << "Caught Socket error: " << e.what() << '\n';
} catch (...) {
std::cout << "Caught Something else probably should have let me crash\n";
}
throw RuntimeError("This one we missed");
return 0; return 0;
} }

View File

@ -46,20 +46,20 @@ TEST_CASE("single EIGER detector no receiver basic set and get") {
CHECK(d.getTotalNumberOfChannels() == 256 * 256 * 4); CHECK(d.getTotalNumberOfChannels() == 256 * 256 * 4);
CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::X) == 1024); CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::X) == 1024);
CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::Y) == 256); CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::Y) == 256);
CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::Z) == 1); // CHECK(d.getTotalNumberOfChannels(slsDetectorDefs::dimension::Z) == 1);
CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::X) == 1024); CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::X) == 1024);
CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Y) == 256); CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Y) == 256);
CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Z) == 1); // CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Z) == 1);
CHECK(d.getNChans() == 256 * 256); CHECK(d.getNChans() == 256 * 256);
CHECK(d.getNChans(slsDetectorDefs::dimension::X) == 256); CHECK(d.getNChans(slsDetectorDefs::dimension::X) == 256);
CHECK(d.getNChans(slsDetectorDefs::dimension::Y) == 256); CHECK(d.getNChans(slsDetectorDefs::dimension::Y) == 256);
CHECK(d.getNChans(slsDetectorDefs::dimension::Z) == 1); // CHECK(d.getNChans(slsDetectorDefs::dimension::Z) == 1);
CHECK(d.getNChips() == 4); CHECK(d.getNChips() == 4);
CHECK(d.getNChips(slsDetectorDefs::dimension::X) == 4); CHECK(d.getNChips(slsDetectorDefs::dimension::X) == 4);
CHECK(d.getNChips(slsDetectorDefs::dimension::Y) == 1); CHECK(d.getNChips(slsDetectorDefs::dimension::Y) == 1);
CHECK(d.getNChips(slsDetectorDefs::dimension::Z) == 1); // CHECK(d.getNChips(slsDetectorDefs::dimension::Z) == 1);
d.freeSharedMemory(); d.freeSharedMemory();
} }

View File

@ -314,7 +314,7 @@ void multiSlsDetector::initSharedMemory(bool verify) {
FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch " FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch "
"(expected 0x" "(expected 0x"
<< std::hex << MULTI_SHMVERSION << " but got 0x" << multi_shm()->shmversion << std::dec; << std::hex << MULTI_SHMVERSION << " but got 0x" << multi_shm()->shmversion << std::dec;
throw SharedMemoryException(); throw SharedMemoryError("Shared memory version mismatch!");
} }
} }
} }

View File

@ -12,6 +12,8 @@
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
using sls::RuntimeError;
inline int dummyCallback(detectorData *d, int p, void *) { inline int dummyCallback(detectorData *d, int p, void *) {
std::cout << "got data " << p << std::endl; std::cout << "got data " << p << std::endl;
return 0; return 0;
@ -73,7 +75,7 @@ class multiSlsDetectorClient {
try { try {
localDet = sls::make_unique<multiSlsDetector>(parser.multi_id(), verify, update); localDet = sls::make_unique<multiSlsDetector>(parser.multi_id(), verify, update);
detPtr = localDet.get(); detPtr = localDet.get();
} catch (const SlsDetectorPackageExceptions &e) { } catch (const RuntimeError &e) {
/*std::cout << e.GetMessage() << std::endl;*/ /*std::cout << e.GetMessage() << std::endl;*/
return; return;
} catch (...) { } catch (...) {

View File

@ -30,6 +30,8 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
using sls::SharedMemoryError;
template <typename T> template <typename T>
class SharedMemory { class SharedMemory {
public: public:
@ -118,21 +120,23 @@ class SharedMemory {
/** /**
* Create Shared memory and call MapSharedMemory to map it to an address * Create Shared memory and call MapSharedMemory to map it to an address
* throws a SharedMemoryException exception on failure to create, ftruncate or map * throws a SharedMemoryError exception on failure to create, ftruncate or map
* @param sz of shared memory * @param sz of shared memory
*/ */
void CreateSharedMemory() { void CreateSharedMemory() {
fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) { if (fd < 0) {
FILE_LOG(logERROR) << "Create shared memory " << name << " failed: " << strerror(errno); std::string msg = "Create shared memory " + name + " failed: " + strerror(errno);
throw SharedMemoryException(); FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
} }
if (ftruncate(fd, sizeof(T)) < 0) { if (ftruncate(fd, sizeof(T)) < 0) {
FILE_LOG(logERROR) << "Create shared memory " << name << " failed at ftruncate: " << strerror(errno); std::string msg = "Create shared memory " + name + " failed at ftruncate: " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd); close(fd);
RemoveSharedMemory(); RemoveSharedMemory();
throw SharedMemoryException(); throw SharedMemoryError(msg);
} }
shared_struct = MapSharedMemory(); shared_struct = MapSharedMemory();
@ -141,14 +145,15 @@ class SharedMemory {
/** /**
* Open existing Shared memory and call MapSharedMemory to map it to an address * Open existing Shared memory and call MapSharedMemory to map it to an address
* throws a SharedMemoryException exception on failure to open or map * throws a SharedMemoryError exception on failure to open or map
* @param sz of shared memory * @param sz of shared memory
*/ */
void OpenSharedMemory() { void OpenSharedMemory() {
fd = shm_open(name.c_str(), O_RDWR, 0); fd = shm_open(name.c_str(), O_RDWR, 0);
if (fd < 0) { if (fd < 0) {
FILE_LOG(logERROR) << "Open existing shared memory " << name << " failed: " << strerror(errno); std::string msg = "Open existing shared memory " + name + " failed: " + strerror(errno);
throw SharedMemoryException(); FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
} }
shared_struct = MapSharedMemory(); shared_struct = MapSharedMemory();
@ -156,14 +161,15 @@ class SharedMemory {
/** /**
* Unmap shared memory from an address * Unmap shared memory from an address
* throws a SharedMemoryException exception on failure * throws a SharedMemoryError exception on failure
*/ */
void UnmapSharedMemory() { void UnmapSharedMemory() {
if (shared_struct != nullptr) { if (shared_struct != nullptr) {
if (munmap(shared_struct, shmSize) < 0) { if (munmap(shared_struct, shmSize) < 0) {
FILE_LOG(logERROR) << "Unmapping shared memory " << name << " failed: " << strerror(errno); std::string msg = "Unmapping shared memory " + name +" failed: " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd); close(fd);
throw SharedMemoryException(); throw SharedMemoryError(msg);
} }
shared_struct = nullptr; shared_struct = nullptr;
} }
@ -178,8 +184,9 @@ class SharedMemory {
// silent exit if shm did not exist anyway // silent exit if shm did not exist anyway
if (errno == ENOENT) if (errno == ENOENT)
return; return;
FILE_LOG(logERROR) << "Free Shared Memory " << name << " Failed: " << strerror(errno); std::string msg = "Free Shared Memory " + name + " Failed: " + strerror(errno);
throw SharedMemoryException(); FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
} }
FILE_LOG(logINFO) << "Shared memory deleted " << name; FILE_LOG(logINFO) << "Shared memory deleted " << name;
} }
@ -229,10 +236,10 @@ class SharedMemory {
std::string temp = ss.str(); std::string temp = ss.str();
if (temp.length() > NAME_MAX) { if (temp.length() > NAME_MAX) {
FILE_LOG(logERROR) << "Shared memory initialization failed. " << temp << " has " << temp.length() << " characters. \n" std::string msg = "Shared memory initialization failed. " + temp + " has " + std::to_string(temp.length()) + " characters. \n"
"Maximum is " + "Maximum is " + std::to_string(NAME_MAX) + ". Change the environment variable " + SHM_ENV_NAME;
<< NAME_MAX << ". Change the environment variable " << SHM_ENV_NAME; FILE_LOG(logERROR) << msg;
throw SharedMemoryException(); throw SharedMemoryError(msg);
} }
return temp; return temp;
} }
@ -246,9 +253,10 @@ class SharedMemory {
T *MapSharedMemory() { T *MapSharedMemory() {
void *addr = mmap(nullptr, sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); void *addr = mmap(nullptr, sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) { if (addr == MAP_FAILED) {
FILE_LOG(logERROR) << "Mapping shared memory " << name << " failed: " << strerror(errno); std::string msg = "Mapping shared memory " + name + " failed: " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd); close(fd);
throw SharedMemoryException(); throw SharedMemoryError(msg);
} }
shmSize = sizeof(T); shmSize = sizeof(T);
close(fd); close(fd);
@ -264,19 +272,20 @@ class SharedMemory {
struct stat sb; struct stat sb;
// could not fstat // could not fstat
if (fstat(fd, &sb) < 0) { if (fstat(fd, &sb) < 0) {
FILE_LOG(logERROR) << "Could not verify existing shared memory " << name << " size match " std::string msg = "Could not verify existing shared memory " + name + " size match "
"(could not fstat): " + "(could not fstat): " + strerror(errno);
<< strerror(errno); FILE_LOG(logERROR) << msg;
close(fd); close(fd);
throw SharedMemoryException(); throw SharedMemoryError(msg);
} }
//size does not match //size does not match
long unsigned int sz = (long unsigned int)sb.st_size; long unsigned int sz = (long unsigned int)sb.st_size;
if (sz != expectedSize) { if (sz != expectedSize) {
FILE_LOG(logERROR) << "Existing shared memory " << name << " size does not match"; std::string msg = "Existing shared memory " + name + " size does not match"
FILE_LOG(logDEBUG1) << "Expected " << expectedSize << ", found " << sz; + "Expected " + std::to_string(expectedSize) + ", found " + std::to_string(sz);
throw SharedMemoryException(); FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
return 1; return 1;
} }
return 0; return 0;

View File

@ -22,6 +22,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <cassert>
#define DEFAULT_HOSTNAME "localhost" #define DEFAULT_HOSTNAME "localhost"
@ -230,7 +231,7 @@ void slsDetector::initSharedMemory(detectorType type,
"version mismatch " "version mismatch "
"(expected 0x" "(expected 0x"
<< std::hex << SLS_SHMVERSION << " but got 0x" << detector_shm()->shmversion << ")" << std::dec; << std::hex << SLS_SHMVERSION << " but got 0x" << detector_shm()->shmversion << ")" << std::dec;
throw SharedMemoryException(); throw SharedMemoryError("Shared memory version mismatch (det)");
} }
} }
} }
@ -537,7 +538,7 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
if (!shm.IsExisting()) { if (!shm.IsExisting()) {
FILE_LOG(logERROR) << "Shared memory " << shm.GetName() << " does not exist.\n" FILE_LOG(logERROR) << "Shared memory " << shm.GetName() << " does not exist.\n"
"Corrupted Multi Shared memory. Please free shared memory."; "Corrupted Multi Shared memory. Please free shared memory.";
throw SharedMemoryException(); throw SharedMemoryError("Could not read detector type from shared memory");
} }
// open, map, verify version // open, map, verify version
@ -551,7 +552,7 @@ slsDetectorDefs::detectorType slsDetector::getDetectorTypeFromShm(int multiId, b
<< std::hex << SLS_SHMVERSION << " but got 0x" << shm()->shmversion << ")" << std::dec; << std::hex << SLS_SHMVERSION << " but got 0x" << shm()->shmversion << ")" << std::dec;
// unmap and throw // unmap and throw
detector_shm.UnmapSharedMemory(); detector_shm.UnmapSharedMemory();
throw SharedMemoryException(); throw SharedMemoryError("Shared memory version mismatch");
} }
auto type = shm()->myDetectorType; auto type = shm()->myDetectorType;
return type; return type;

View File

@ -423,7 +423,7 @@ int slsReceiverTCPIPInterface::set_port() {
try { try {
mySocket = new MySocketTCP(p_number); mySocket = new MySocketTCP(p_number);
strcpy(mySock->lastClientIP,oldLastClientIP); strcpy(mySock->lastClientIP,oldLastClientIP);
} catch(SamePortSocketException &e) { } catch(SocketError &e) {
ret = FAIL; ret = FAIL;
sprintf(mess, "Could not bind port %d. It is already set\n", p_number); sprintf(mess, "Could not bind port %d. It is already set\n", p_number);
FILE_LOG(logERROR) << mess; FILE_LOG(logERROR) << mess;

View File

@ -49,6 +49,7 @@ class sockaddr_in;
#define SOCKET_BUFFER_SIZE (100*1024*1024) //100 MB #define SOCKET_BUFFER_SIZE (100*1024*1024) //100 MB
#define DEFAULT_BACKLOG 5 #define DEFAULT_BACKLOG 5
using sls::SocketError;
class genericSocket{ class genericSocket{
@ -90,7 +91,7 @@ public:
struct addrinfo *result; struct addrinfo *result;
if (ConvertHostnameToInternetAddress(host_ip_or_name, &result)) { if (ConvertHostnameToInternetAddress(host_ip_or_name, &result)) {
sockfd.fd = -1; sockfd.fd = -1;
throw SocketException(); throw SocketError("Could convert hostname to address");
} }
sockfd.fd = 0; sockfd.fd = 0;
@ -134,7 +135,7 @@ public:
// same port // same port
if(serverAddress.sin_port == htons(port_number)){ if(serverAddress.sin_port == htons(port_number)){
sockfd.fd = -10; sockfd.fd = -10;
throw SamePortSocketException(); throw SocketError("Cannot create socket on same port");
} }
char ip[20]; char ip[20];
@ -152,7 +153,7 @@ public:
if (sockfd.fd < 0) { if (sockfd.fd < 0) {
FILE_LOG(logERROR) << "Can not create socket"; FILE_LOG(logERROR) << "Can not create socket";
sockfd.fd =-1; sockfd.fd =-1;
throw SocketException(); throw SocketError("Can not create socket");
} }
// Set some fields in the serverAddress structure. // Set some fields in the serverAddress structure.
@ -175,7 +176,7 @@ public:
&val,sizeof(int)) == -1) { &val,sizeof(int)) == -1) {
FILE_LOG(logERROR) << "setsockopt REUSEADDR failed"; FILE_LOG(logERROR) << "setsockopt REUSEADDR failed";
sockfd.fd =-1; sockfd.fd =-1;
throw SocketException(); throw SocketError("setsockopt REUSEADDR failed");
} }
} }
@ -244,7 +245,7 @@ public:
if(bind(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){ if(bind(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
FILE_LOG(logERROR) << "Can not bind socket"; FILE_LOG(logERROR) << "Can not bind socket";
sockfd.fd =-1; sockfd.fd =-1;
throw SocketException(); throw SocketError("Can not bind socket");
} }

View File

@ -8,29 +8,31 @@
*/ */
#include <iostream> #include <iostream>
#include <exception> #include <stdexcept>
struct SlsDetectorPackageExceptions : public std::exception { namespace sls{
struct RuntimeError : public std::runtime_error {
public: public:
SlsDetectorPackageExceptions() {} RuntimeError(): runtime_error("SLS Detector Package Failed") {}
std::string GetMessage() const { return "SLS Detector Package Failed";}; RuntimeError(std::string msg): runtime_error(msg) {}
}; };
struct SharedMemoryException : public SlsDetectorPackageExceptions { struct SharedMemoryError : public RuntimeError {
public: public:
SharedMemoryException() {} SharedMemoryError(std::string msg):RuntimeError(msg) {}
std::string GetMessage() const { return "Shared Memory Failed";};
}; };
struct SocketException : public SlsDetectorPackageExceptions { struct SocketError : public RuntimeError {
public: public:
SocketException() {} SocketError(std::string msg):RuntimeError(msg) {}
std::string GetMessage() const { return "Socket Failed";};
}; };
struct SamePortSocketException : public SocketException {
public:
SamePortSocketException() {} }
std::string GetMessage() const { return "Socket Failed";};
};