From ffd392c7ebe2c243ffce3b3e1c26704a1a45ef72 Mon Sep 17 00:00:00 2001 From: l_maliakal_d Date: Tue, 20 Nov 2012 16:50:36 +0000 Subject: [PATCH] fixed changing ports inreceiver, included receiver config file git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@351 951219d9-93cf-4727-9268-0efd64621fa3 --- .../MySocketTCP/genericSocket.h | 4 +- .../slsDetector/slsDetector.cpp | 74 ++------- .../slsReceiver/receiver.config | 1 + .../slsReceiver/slsReceiver.cpp | 23 ++- .../slsReceiver/slsReceiver_funcs.cpp | 140 ++++++++++++++---- .../slsReceiver/slsReceiver_funcs.h | 10 +- 6 files changed, 153 insertions(+), 99 deletions(-) create mode 100644 slsDetectorSoftware/slsReceiver/receiver.config diff --git a/slsDetectorSoftware/MySocketTCP/genericSocket.h b/slsDetectorSoftware/MySocketTCP/genericSocket.h index a06f1d3b4..8390c6322 100644 --- a/slsDetectorSoftware/MySocketTCP/genericSocket.h +++ b/slsDetectorSoftware/MySocketTCP/genericSocket.h @@ -351,7 +351,9 @@ protocol(p), is_a_server(0), socketDescriptor(-1),file_des(-1), packet_size(DEFA return file_des; } - + uint16_t getPortNumber(){ + return ntohs(serverAddress.sin_port); + } /** @short free connection */ diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 243e330f9..a96ada8db 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -3461,8 +3461,7 @@ int slsDetector::setPort(portType index, int num){ // uint64_t ut; char mess[100]; int ret=FAIL; - int n=0; - + bool online=false; MySocketTCP *s; if (num>1024) { @@ -3495,13 +3494,17 @@ int slsDetector::setPort(portType index, int num){ setTCPSocket("",retval); } + online = (thisDetector->onlineFlag==ONLINE_FLAG); break; case DATA_PORT: s=dataSocket; retval=thisDetector->dataPort; - if (s==NULL) setReceiverTCPSocket("",DEFAULT_PORTNO+2); - if (dataSocket) s=dataSocket; - else setReceiverTCPSocket("",retval); + if(strcmp(thisDetector->receiverIP,"none")){ + if (s==NULL) setReceiverTCPSocket("",DEFAULT_PORTNO+2); + if (dataSocket) s=dataSocket; + else setReceiverTCPSocket("",retval); + online = (thisDetector->receiverOnlineFlag==ONLINE_FLAG); + } break; case STOP_PORT: s=stopSocket; @@ -3509,17 +3512,18 @@ int slsDetector::setPort(portType index, int num){ if (s==NULL) setTCPSocket("",-1,DEFAULT_PORTNO+1); if (stopSocket) s=stopSocket; else setTCPSocket("",-1,retval); + online = (thisDetector->onlineFlag==ONLINE_FLAG); break; default: s=NULL; } - if (thisDetector->onlineFlag==ONLINE_FLAG) { + if (online) { if (s) { if (s->Connect()>=0) { s->SendDataOnly(&fnum,sizeof(fnum)); s->SendDataOnly(&index,sizeof(index)); - n=s->SendDataOnly(&num,sizeof(num)); + s->SendDataOnly(&num,sizeof(num)); s->ReceiveDataOnly(&ret,sizeof(ret)); if (ret==FAIL) { s->ReceiveDataOnly(mess,sizeof(mess)); @@ -3530,7 +3534,6 @@ int slsDetector::setPort(portType index, int num){ s->Disconnect(); } } - } if (ret!=FAIL) { @@ -3559,7 +3562,11 @@ int slsDetector::setPort(portType index, int num){ thisDetector->controlPort=num; break; case DATA_PORT: - thisDetector->dataPort=num; + if(thisDetector->receiverOnlineFlag==ONLINE_FLAG) + thisDetector->dataPort=retval; + else + thisDetector->dataPort=num; + break; case STOP_PORT: thisDetector->stopPort=num; @@ -5027,55 +5034,6 @@ int slsDetector::writeConfigurationFile(ofstream &outfile, int id){ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - int slsDetector::writeSettingsFile(string fname, int imod){ return writeSettingsFile(fname,thisDetector->myDetectorType, detectorModules[imod]); diff --git a/slsDetectorSoftware/slsReceiver/receiver.config b/slsDetectorSoftware/slsReceiver/receiver.config new file mode 100644 index 000000000..fa4586e14 --- /dev/null +++ b/slsDetectorSoftware/slsReceiver/receiver.config @@ -0,0 +1 @@ +dataport 1955 diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver.cpp index 7539f72a3..297935915 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiver.cpp @@ -12,22 +12,29 @@ using namespace std; int main(int argc, char *argv[]) { - int portno = DEFAULT_PORTNO+2; - int retval = slsDetectorDefs::OK; + int ret = slsDetectorDefs::OK; + MySocketTCP *socket = NULL; + string fname = ""; - MySocketTCP *socket = new MySocketTCP(portno); - if (socket->getErrorStatus()) + //parse command line for config + for(int iarg=2;iargdecode_function(); + ret = receiver->decode_function(); #ifdef VERY_VERBOSE cout << "function executed" << endl; #endif diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp index fd61726b4..280e94ef0 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp @@ -7,23 +7,98 @@ #include "slsReceiverFunctionList.h" #include +#include +#include +#include using namespace std; -slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *socket): - socket(socket), +slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *&mySocket,string const fname,int &success): + socket(mySocket), ret(OK), lockStatus(0){ - //initialize variables - strcpy(mess,"dummy message"); - strcpy(socket->lastClientIP,"none"); - strcpy(socket->thisClientIP,"none1"); - function_table(); - slsReceiverList = new slsReceiverFunctionList(); + int port_no = DEFAULT_PORTNO+2; + ifstream infile; + string sLine,sargname; + int iline = 0; + + + if(!fname.empty()){ +#ifdef VERBOSE + std::cout<< "config file name "<< fname << std::endl; +#endif + infile.open(fname.c_str(), ios_base::in); + if (infile.is_open()) { + while(infile.good()){ + getline(infile,sLine); + iline++; +#ifdef VERBOSE + cout << str << endl; +#endif + if(sLine.find('#')!=string::npos){ +#ifdef VERBOSE + cout << "Line is a comment " << endl; +#endif + continue; + }else if(sLine.length()<2){ +#ifdef VERBOSE + cout << "Empty line " << endl; +#endif + continue; + }else{ + istringstream sstr(sLine); + + //parameter name + if(sstr.good()) + sstr >> sargname; + + //value + if(sargname=="dataport"){ + if(sstr.good()) { + sstr >> sargname; + sscanf(sargname.c_str(),"%d",&port_no); + } + } + } + } + infile.close(); + }else { + cout << "Error opening configuration file " << fname << endl; + success = FAIL; + } +//#ifdef VERBOSE + cout << "Read configuration file of " << iline << " lines" << endl; +//#endif + + } + + if(success == OK){ + //create socket + MySocketTCP* mySocket = new MySocketTCP(port_no); + if (mySocket->getErrorStatus()) + success = FAIL; + else{ + + delete socket; + socket = mySocket; + + //initialize variables + strcpy(socket->lastClientIP,"none"); + strcpy(socket->thisClientIP,"none1"); + strcpy(mess,"dummy message"); + + function_table(); + slsReceiverList = new slsReceiverFunctionList(); + + success = OK; + } + } } + + int slsReceiverFuncs::function_table(){ for (int i=0;iReceiveDataOnly(&p_number,sizeof(p_number)) < 0 ){ - sprintf(mess,"Error reading from socket\n"); - printf("Error reading from socket (pnum)\n"); + strcpy(mess,"Error reading from socket\n"); + cout << mess << endl; ret=FAIL; } @@ -620,26 +697,30 @@ int slsReceiverFuncs::set_port() { if (socket->differentClients==1 && lockStatus==1 ) { ret=FAIL; sprintf(mess,"Detector locked by %s\n",socket->lastClientIP); - } else { + } + else { if (p_number<1024) { sprintf(mess,"Too low port number %d\n", p_number); - cout << endl; + cout << mess << endl; ret=FAIL; } cout << "set port " << p_type << " to " << p_number <getErrorStatus()){ - ret=OK; - if (socket->differentClients) - ret=FORCE_UPDATE; - } else { - ret=FAIL; - sprintf(mess,"Could not bind port %d\n", p_number); - cout << "Could not bind port " << p_number << endl; - if (socket->getErrorStatus()==-10) { - sprintf(mess,"Port %d already set\n", p_number); - cout << "Port " << p_number << " already set" << endl; + if(mySocket){ + sd = mySocket->getErrorStatus(); + if (!sd){ + ret=OK; + if (mySocket->differentClients) + ret=FORCE_UPDATE; + } else { + ret=FAIL; + sprintf(mess,"Could not bind port %d\n", p_number); + cout << mess << endl; + if (sd==-10) { + sprintf(mess,"Port %d already set\n", p_number); + cout << mess << endl; + } } } } @@ -650,8 +731,11 @@ int slsReceiverFuncs::set_port() { socket->SendDataOnly(mess,sizeof(mess)); } else { socket->SendDataOnly(&p_number,sizeof(p_number)); - socket->Disconnect(); - delete socket; + if(sd>=0){ + socket->Disconnect(); + delete socket; + socket = mySocket; + } } //return ok/fail diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h index 85152d935..b41583938 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h +++ b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h @@ -21,9 +21,12 @@ class slsReceiverFuncs : private virtual slsDetectorDefs { public: /** * Constructor - * @param socket tcp socket connecting receiver and client + * reads config file, creates socket, assigns function table + * @param mySocket tcp socket connecting receiver and client + * @param fname name of config file + * @param success if socket creation was successfull */ - slsReceiverFuncs(MySocketTCP *socket); + slsReceiverFuncs(MySocketTCP *&mySocket,string const fname,int &success); /** Destructor */ virtual ~slsReceiverFuncs(){}; @@ -89,11 +92,10 @@ public: /** Execute command */ int exec_command(); - private: /** Socket */ - MySocketTCP *socket; + MySocketTCP*& socket; /** slsReceiverFunctionList object */ slsReceiverFunctionList *slsReceiverList;