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 "ClientSocket.h"
#include "Timer.h"
#include "logger.h"
#include "slsDetector.h"
#include "sls_detector_defs.h"
#include "Timer.h"
#include "sls_detector_exceptions.h"
#include "sls_detector_funcs.h"
#include <iostream>
#include <vector>
#define VERBOSE
using sls::RuntimeError;
using sls::SharedMemoryError;
using sls::SocketError;
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;
}

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(slsDetectorDefs::dimension::X) == 1024);
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::Y) == 256);
CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Z) == 1);
// CHECK(d.getTotalNumberOfChannelsInclGapPixels(slsDetectorDefs::dimension::Z) == 1);
CHECK(d.getNChans() == 256 * 256);
CHECK(d.getNChans(slsDetectorDefs::dimension::X) == 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(slsDetectorDefs::dimension::X) == 4);
CHECK(d.getNChips(slsDetectorDefs::dimension::Y) == 1);
CHECK(d.getNChips(slsDetectorDefs::dimension::Z) == 1);
// CHECK(d.getNChips(slsDetectorDefs::dimension::Z) == 1);
d.freeSharedMemory();
}

View File

@ -314,7 +314,7 @@ void multiSlsDetector::initSharedMemory(bool verify) {
FILE_LOG(logERROR) << "Multi shared memory (" << detId << ") version mismatch "
"(expected 0x"
<< 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 <memory>
using sls::RuntimeError;
inline int dummyCallback(detectorData *d, int p, void *) {
std::cout << "got data " << p << std::endl;
return 0;
@ -73,7 +75,7 @@ class multiSlsDetectorClient {
try {
localDet = sls::make_unique<multiSlsDetector>(parser.multi_id(), verify, update);
detPtr = localDet.get();
} catch (const SlsDetectorPackageExceptions &e) {
} catch (const RuntimeError &e) {
/*std::cout << e.GetMessage() << std::endl;*/
return;
} catch (...) {

View File

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

View File

@ -22,6 +22,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cassert>
#define DEFAULT_HOSTNAME "localhost"
@ -230,7 +231,7 @@ void slsDetector::initSharedMemory(detectorType type,
"version mismatch "
"(expected 0x"
<< 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()) {
FILE_LOG(logERROR) << "Shared memory " << shm.GetName() << " does not exist.\n"
"Corrupted Multi Shared memory. Please free shared memory.";
throw SharedMemoryException();
throw SharedMemoryError("Could not read detector type from shared memory");
}
// 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;
// unmap and throw
detector_shm.UnmapSharedMemory();
throw SharedMemoryException();
throw SharedMemoryError("Shared memory version mismatch");
}
auto type = shm()->myDetectorType;
return type;

View File

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

View File

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

View File

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