mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-20 08:38:00 +02:00
Exceptions handling in constructor for genericSocket, created object to handle both socket descriptors upon throwing exception in constructor (as raw pointers wont get destructed automatically)
This commit is contained in:
@ -193,16 +193,17 @@ int Listener::CreateUDPSockets() {
|
||||
|
||||
ShutDownUDPSocket();
|
||||
|
||||
udpSocket = new genericSocket(*udpPortNumber, genericSocket::UDP,
|
||||
generalData->packetSize, (strlen(eth)?eth:NULL), generalData->headerPacketSize,
|
||||
*udpSocketBufferSize);
|
||||
int iret = udpSocket->getErrorStatus();
|
||||
if(!iret){
|
||||
try{
|
||||
genericSocket* g = new genericSocket(*udpPortNumber, genericSocket::UDP,
|
||||
generalData->packetSize, (strlen(eth)?eth:NULL), generalData->headerPacketSize,
|
||||
*udpSocketBufferSize);
|
||||
udpSocket = g;
|
||||
FILE_LOG(logINFO) << index << ": UDP port opened at port " << *udpPortNumber;
|
||||
}else{
|
||||
FILE_LOG(logERROR) << "Could not create UDP socket on port " << *udpPortNumber << " error: " << iret;
|
||||
} catch (...) {
|
||||
FILE_LOG(logERROR) << "Could not create UDP socket on port " << *udpPortNumber;
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
udpSocketAlive = true;
|
||||
sem_init(&semaphore_socket,1,0);
|
||||
|
||||
@ -248,17 +249,21 @@ int Listener::CreateDummySocketForUDPSocketBufferSize(uint32_t s) {
|
||||
if(udpSocket){
|
||||
udpSocket->ShutDownSocket();
|
||||
delete udpSocket;
|
||||
udpSocket = 0;
|
||||
}
|
||||
|
||||
//create dummy socket
|
||||
udpSocket = new genericSocket(*udpPortNumber, genericSocket::UDP,
|
||||
try {
|
||||
genericSocket* g = new genericSocket(*udpPortNumber, genericSocket::UDP,
|
||||
generalData->packetSize, (strlen(eth)?eth:NULL), generalData->headerPacketSize,
|
||||
*udpSocketBufferSize);
|
||||
int iret = udpSocket->getErrorStatus();
|
||||
if (iret){
|
||||
FILE_LOG(logERROR) << "Could not create a test UDP socket on port " << *udpPortNumber << " error: " << iret;
|
||||
udpSocket = g;
|
||||
} catch (...) {
|
||||
FILE_LOG(logERROR) << "Could not create a test UDP socket on port " << *udpPortNumber;
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
|
||||
// doubled due to kernel bookkeeping (could also be less due to permissions)
|
||||
*actualUDPSocketBufferSize = udpSocket->getActualUDPSocketBufferSize();
|
||||
if (*actualUDPSocketBufferSize != (s*2)) {
|
||||
|
@ -5,8 +5,6 @@
|
||||
***********************************************/
|
||||
|
||||
#include "UDPBaseImplementation.h"
|
||||
#include "genericSocket.h"
|
||||
#include "ZmqSocket.h"
|
||||
|
||||
#include <sys/stat.h> // stat
|
||||
#include <iostream>
|
||||
|
@ -18,11 +18,8 @@ using namespace std;
|
||||
|
||||
|
||||
|
||||
slsReceiver::slsReceiver(int argc, char *argv[], int &success):
|
||||
tcpipInterface (NULL),
|
||||
udp_interface (NULL)
|
||||
{
|
||||
success=OK;
|
||||
slsReceiver::slsReceiver(int argc, char *argv[]):
|
||||
tcpipInterface (0) {
|
||||
|
||||
// options
|
||||
map<string, string> configuration_map;
|
||||
@ -72,8 +69,7 @@ slsReceiver::slsReceiver(int argc, char *argv[], int &success):
|
||||
tempval = GITREV;
|
||||
tempval = (tempval <<32) | GITDATE;
|
||||
cout << "SLS Receiver " << GITBRANCH << " (0x" << hex << tempval << ")" << endl;
|
||||
success = FAIL; // to exit
|
||||
break;
|
||||
throw exception();
|
||||
|
||||
case 'h':
|
||||
default:
|
||||
@ -87,26 +83,18 @@ slsReceiver::slsReceiver(int argc, char *argv[], int &success):
|
||||
+ "\t receivers\n\n";
|
||||
|
||||
FILE_LOG(logINFO) << help_message << endl;
|
||||
break;
|
||||
throw exception();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if( !fname.empty() ){
|
||||
try{
|
||||
FILE_LOG(logINFO) << "config file name " << fname;
|
||||
success = read_config_file(fname, &tcpip_port_no, &configuration_map);
|
||||
//VERBOSE_PRINT("Read configuration file of " + iline + " lines");
|
||||
}
|
||||
catch(...){
|
||||
FILE_LOG(logERROR) << "Coult not open configuration file " << fname ;
|
||||
success = FAIL;
|
||||
}
|
||||
if( !fname.empty() && read_config_file(fname, &tcpip_port_no, &configuration_map) == FAIL) {
|
||||
throw exception();
|
||||
}
|
||||
|
||||
if (success==OK){
|
||||
tcpipInterface = new slsReceiverTCPIPInterface(success, udp_interface, tcpip_port_no);
|
||||
}
|
||||
// might throw an exception
|
||||
tcpipInterface = new slsReceiverTCPIPInterface(tcpip_port_no);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -131,40 +119,26 @@ int64_t slsReceiver::getReceiverVersion(){
|
||||
}
|
||||
|
||||
|
||||
void slsReceiver::registerCallBackStartAcquisition(int (*func)(char*, char*, uint64_t, uint32_t, void*),void *arg){
|
||||
//tcpipInterface
|
||||
if(udp_interface)
|
||||
udp_interface->registerCallBackStartAcquisition(func,arg);
|
||||
else
|
||||
tcpipInterface->registerCallBackStartAcquisition(func,arg);
|
||||
void slsReceiver::registerCallBackStartAcquisition(int (*func)(
|
||||
char*, char*, uint64_t, uint32_t, void*),void *arg){
|
||||
tcpipInterface->registerCallBackStartAcquisition(func,arg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void slsReceiver::registerCallBackAcquisitionFinished(void (*func)(uint64_t, void*),void *arg){
|
||||
//tcpipInterface
|
||||
if(udp_interface)
|
||||
udp_interface->registerCallBackAcquisitionFinished(func,arg);
|
||||
else
|
||||
tcpipInterface->registerCallBackAcquisitionFinished(func,arg);
|
||||
void slsReceiver::registerCallBackAcquisitionFinished(
|
||||
void (*func)(uint64_t, void*),void *arg){
|
||||
tcpipInterface->registerCallBackAcquisitionFinished(func,arg);
|
||||
}
|
||||
|
||||
|
||||
void slsReceiver::registerCallBackRawDataReady(void (*func)(char*,
|
||||
char*, uint32_t, void*),void *arg){
|
||||
//tcpipInterface
|
||||
if(udp_interface)
|
||||
udp_interface->registerCallBackRawDataReady(func,arg);
|
||||
else
|
||||
tcpipInterface->registerCallBackRawDataReady(func,arg);
|
||||
tcpipInterface->registerCallBackRawDataReady(func,arg);
|
||||
}
|
||||
|
||||
|
||||
void slsReceiver::registerCallBackRawDataModifyReady(void (*func)(char*,
|
||||
char*, uint32_t &, void*),void *arg){
|
||||
//tcpipInterface
|
||||
if(udp_interface)
|
||||
udp_interface->registerCallBackRawDataModifyReady(func,arg);
|
||||
else
|
||||
tcpipInterface->registerCallBackRawDataModifyReady(func,arg);
|
||||
tcpipInterface->registerCallBackRawDataModifyReady(func,arg);
|
||||
}
|
||||
|
@ -31,16 +31,16 @@ slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() {
|
||||
delete receiverBase;
|
||||
}
|
||||
|
||||
slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int &success, UDPInterface* rbase, int pn):
|
||||
slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int pn):
|
||||
myDetectorType(GOTTHARD),
|
||||
receiverBase(rbase),
|
||||
receiverBase(0),
|
||||
ret(OK),
|
||||
fnum(-1),
|
||||
lockStatus(0),
|
||||
killTCPServerThread(0),
|
||||
tcpThreadCreated(false),
|
||||
portNumber(DEFAULT_PORTNO+2),
|
||||
mySock(NULL)
|
||||
mySock(0)
|
||||
{
|
||||
//***callback parameters***
|
||||
startAcquisitionCallBack = NULL;
|
||||
@ -51,83 +51,25 @@ slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int &success, UDPInterface*
|
||||
rawDataModifyReadyCallBack = NULL;
|
||||
pRawDataReady = NULL;
|
||||
|
||||
unsigned short int port_no=portNumber;
|
||||
if(receiverBase == NULL)
|
||||
receiverBase = 0;
|
||||
// create socket
|
||||
portNumber = (pn > 0 ? pn : DEFAULT_PORTNO + 2);
|
||||
MySocketTCP* m = new MySocketTCP(portNumber);
|
||||
mySock = m;
|
||||
|
||||
if (pn>0)
|
||||
port_no = pn;
|
||||
//initialize variables
|
||||
strcpy(mySock->lastClientIP,"none");
|
||||
strcpy(mySock->thisClientIP,"none1");
|
||||
memset(mess,0,sizeof(mess));
|
||||
strcpy(mess,"dummy message");
|
||||
|
||||
success=OK;
|
||||
|
||||
//create socket
|
||||
if(success == OK){
|
||||
mySock = new MySocketTCP(port_no);
|
||||
if (mySock->getErrorStatus()) {
|
||||
success = FAIL;
|
||||
delete mySock;
|
||||
mySock=NULL;
|
||||
} else {
|
||||
portNumber=port_no;
|
||||
//initialize variables
|
||||
strcpy(mySock->lastClientIP,"none");
|
||||
strcpy(mySock->thisClientIP,"none1");
|
||||
memset(mess,0,sizeof(mess));
|
||||
strcpy(mess,"dummy message");
|
||||
function_table();
|
||||
function_table();
|
||||
#ifdef VERYVERBOSE
|
||||
FILE_LOG(logINFO) << "Function table assigned.";
|
||||
FILE_LOG(logINFO) << "Function table assigned.";
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
int slsReceiverTCPIPInterface::setPortNumber(int pn){
|
||||
memset(mess, 0, sizeof(mess));
|
||||
int p_number;
|
||||
|
||||
MySocketTCP *oldsocket = NULL;;
|
||||
int sd = 0;
|
||||
|
||||
if (pn > 0) {
|
||||
p_number = pn;
|
||||
|
||||
if (p_number < 1024) {
|
||||
sprintf(mess,"Too low port number %d\n", p_number);
|
||||
FILE_LOG(logERROR) << mess;
|
||||
} else {
|
||||
|
||||
oldsocket=mySock;
|
||||
mySock = new MySocketTCP(p_number);
|
||||
if(mySock){
|
||||
sd = mySock->getErrorStatus();
|
||||
if (!sd){
|
||||
portNumber=p_number;
|
||||
strcpy(mySock->lastClientIP,oldsocket->lastClientIP);
|
||||
delete oldsocket;
|
||||
} else {
|
||||
FILE_LOG(logERROR) << "Could not bind port " << p_number;
|
||||
if (sd == -10) {
|
||||
FILE_LOG(logINFO) << "Port "<< p_number << " already set";
|
||||
} else {
|
||||
delete mySock;
|
||||
mySock=oldsocket;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
mySock=oldsocket;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return portNumber;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int slsReceiverTCPIPInterface::start(){
|
||||
FILE_LOG(logDEBUG) << "Creating TCP Server Thread";
|
||||
killTCPServerThread = 0;
|
||||
@ -566,15 +508,14 @@ int slsReceiverTCPIPInterface::get_last_client_ip() {
|
||||
int slsReceiverTCPIPInterface::set_port() {
|
||||
ret = OK;
|
||||
memset(mess, 0, sizeof(mess));
|
||||
int unused = 0;
|
||||
int p_type = 0;
|
||||
int p_number = -1;
|
||||
MySocketTCP* mySocket = NULL;
|
||||
MySocketTCP* mySocket = 0;
|
||||
char oldLastClientIP[INET_ADDRSTRLEN];
|
||||
memset(oldLastClientIP, 0, sizeof(oldLastClientIP));
|
||||
int sd = -1;
|
||||
|
||||
// receive arguments
|
||||
if (mySock->ReceiveDataOnly(&unused,sizeof(unused)) < 0 )
|
||||
if (mySock->ReceiveDataOnly(&p_type,sizeof(p_type)) < 0 )
|
||||
return printSocketReadError();
|
||||
if (mySock->ReceiveDataOnly(&p_number,sizeof(p_number)) < 0 )
|
||||
return printSocketReadError();
|
||||
@ -586,29 +527,26 @@ int slsReceiverTCPIPInterface::set_port() {
|
||||
FILE_LOG(logERROR) << mess;
|
||||
}
|
||||
else {
|
||||
if (p_number<1024) {
|
||||
if (p_number < 1024) {
|
||||
ret = FAIL;
|
||||
sprintf(mess,"Port Number (%d) too low\n", p_number);
|
||||
FILE_LOG(logERROR) << mess;
|
||||
}
|
||||
FILE_LOG(logINFO) << "set port to " << p_number <<endl;
|
||||
strcpy(oldLastClientIP, mySock->lastClientIP);
|
||||
mySocket = new MySocketTCP(p_number);
|
||||
} else {
|
||||
FILE_LOG(logINFO) << "set port to " << p_number <<endl;
|
||||
strcpy(oldLastClientIP, mySock->lastClientIP);
|
||||
|
||||
if(mySocket){
|
||||
sd = mySocket->getErrorStatus();
|
||||
if (sd < 0) {
|
||||
ret = FAIL;
|
||||
sprintf(mess,"Could not bind port %d\n", p_number);
|
||||
FILE_LOG(logERROR) << mess;
|
||||
if (sd == -10) {
|
||||
ret = FAIL;
|
||||
sprintf(mess,"Port %d already set\n", p_number);
|
||||
FILE_LOG(logERROR) << mess;
|
||||
}
|
||||
}
|
||||
else
|
||||
try {
|
||||
mySocket = new MySocketTCP(p_number);
|
||||
strcpy(mySock->lastClientIP,oldLastClientIP);
|
||||
} catch(SamePortSocketException e) {
|
||||
ret = FAIL;
|
||||
sprintf(mess, "Could not bind port %d. It is already set\n", p_number);
|
||||
FILE_LOG(logERROR) << mess;
|
||||
} catch (...) {
|
||||
ret = FAIL;
|
||||
sprintf(mess, "Could not bind port %d.\n", p_number);
|
||||
FILE_LOG(logERROR) << mess;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -621,7 +559,7 @@ int slsReceiverTCPIPInterface::set_port() {
|
||||
mySock->SendDataOnly(mess,sizeof(mess));
|
||||
else {
|
||||
mySock->SendDataOnly(&p_number,sizeof(p_number));
|
||||
if(sd>=0){
|
||||
if(ret != FAIL){
|
||||
mySock->Disconnect();
|
||||
delete mySock;
|
||||
mySock = mySocket;
|
||||
|
@ -2,11 +2,18 @@
|
||||
#include "slsReceiver.h"
|
||||
|
||||
slsReceiverUsers::slsReceiverUsers(int argc, char *argv[], int &success) {
|
||||
receiver=new slsReceiver(argc, argv, success);
|
||||
// catch the exception here to limit it to within the library (for current version)
|
||||
try {
|
||||
slsReceiver* r = new slsReceiver(argc, argv);
|
||||
receiver = r;
|
||||
success = slsReceiverDefs::OK;
|
||||
} catch (...) {
|
||||
success = slsReceiverDefs::FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
slsReceiverUsers::~slsReceiverUsers() {
|
||||
delete receiver;
|
||||
delete receiver;
|
||||
}
|
||||
|
||||
int slsReceiverUsers::start() {
|
||||
@ -28,13 +35,13 @@ void slsReceiverUsers::registerCallBackStartAcquisition(int (*func)(char*, char*
|
||||
void slsReceiverUsers::registerCallBackAcquisitionFinished(void (*func)(uint64_t, void*),void *arg){
|
||||
receiver->registerCallBackAcquisitionFinished(func,arg);
|
||||
}
|
||||
|
||||
|
||||
void slsReceiverUsers::registerCallBackRawDataReady(void (*func)(char* header,
|
||||
char* datapointer, uint32_t datasize, void*), void *arg){
|
||||
receiver->registerCallBackRawDataReady(func,arg);
|
||||
}
|
||||
|
||||
void slsReceiverUsers::registerCallBackRawDataModifyReady(void (*func)(char* header,
|
||||
char* datapointer, uint32_t& revDatasize, void*), void *arg){
|
||||
receiver->registerCallBackRawDataModifyReady(func,arg);
|
||||
char* datapointer, uint32_t& revDatasize, void*), void *arg){
|
||||
receiver->registerCallBackRawDataModifyReady(func,arg);
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <map>
|
||||
|
||||
#include "utilities.h"
|
||||
#include "logger.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
@ -21,9 +22,15 @@ int read_config_file(string fname, int *tcpip_port_no, map<string, string> * con
|
||||
int success = slsReceiverDefs::OK;
|
||||
|
||||
|
||||
FILE_LOG(logINFO) << "config file name " << fname;
|
||||
try {
|
||||
infile.open(fname.c_str(), ios_base::in);
|
||||
} catch(...) {
|
||||
FILE_LOG(logERROR) << "Could not open configuration file " << fname ;
|
||||
success = slsReceiverDefs::FAIL;
|
||||
}
|
||||
|
||||
infile.open(fname.c_str(), ios_base::in);
|
||||
if (infile.is_open()) {
|
||||
if (success == slsReceiverDefs::OK && infile.is_open()) {
|
||||
while(infile.good()){
|
||||
getline(infile,sLine);
|
||||
iline++;
|
||||
|
Reference in New Issue
Block a user