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
This commit is contained in:
l_maliakal_d 2012-11-20 16:50:36 +00:00
parent 97ed3e8872
commit ffd392c7eb
6 changed files with 153 additions and 99 deletions

View File

@ -351,7 +351,9 @@ protocol(p), is_a_server(0), socketDescriptor(-1),file_des(-1), packet_size(DEFA
return file_des; return file_des;
} }
uint16_t getPortNumber(){
return ntohs(serverAddress.sin_port);
}
/** @short free connection */ /** @short free connection */

View File

@ -3461,8 +3461,7 @@ int slsDetector::setPort(portType index, int num){
// uint64_t ut; // uint64_t ut;
char mess[100]; char mess[100];
int ret=FAIL; int ret=FAIL;
int n=0; bool online=false;
MySocketTCP *s; MySocketTCP *s;
if (num>1024) { if (num>1024) {
@ -3495,13 +3494,17 @@ int slsDetector::setPort(portType index, int num){
setTCPSocket("",retval); setTCPSocket("",retval);
} }
online = (thisDetector->onlineFlag==ONLINE_FLAG);
break; break;
case DATA_PORT: case DATA_PORT:
s=dataSocket; s=dataSocket;
retval=thisDetector->dataPort; retval=thisDetector->dataPort;
if (s==NULL) setReceiverTCPSocket("",DEFAULT_PORTNO+2); if(strcmp(thisDetector->receiverIP,"none")){
if (dataSocket) s=dataSocket; if (s==NULL) setReceiverTCPSocket("",DEFAULT_PORTNO+2);
else setReceiverTCPSocket("",retval); if (dataSocket) s=dataSocket;
else setReceiverTCPSocket("",retval);
online = (thisDetector->receiverOnlineFlag==ONLINE_FLAG);
}
break; break;
case STOP_PORT: case STOP_PORT:
s=stopSocket; s=stopSocket;
@ -3509,17 +3512,18 @@ int slsDetector::setPort(portType index, int num){
if (s==NULL) setTCPSocket("",-1,DEFAULT_PORTNO+1); if (s==NULL) setTCPSocket("",-1,DEFAULT_PORTNO+1);
if (stopSocket) s=stopSocket; if (stopSocket) s=stopSocket;
else setTCPSocket("",-1,retval); else setTCPSocket("",-1,retval);
online = (thisDetector->onlineFlag==ONLINE_FLAG);
break; break;
default: default:
s=NULL; s=NULL;
} }
if (thisDetector->onlineFlag==ONLINE_FLAG) { if (online) {
if (s) { if (s) {
if (s->Connect()>=0) { if (s->Connect()>=0) {
s->SendDataOnly(&fnum,sizeof(fnum)); s->SendDataOnly(&fnum,sizeof(fnum));
s->SendDataOnly(&index,sizeof(index)); s->SendDataOnly(&index,sizeof(index));
n=s->SendDataOnly(&num,sizeof(num)); s->SendDataOnly(&num,sizeof(num));
s->ReceiveDataOnly(&ret,sizeof(ret)); s->ReceiveDataOnly(&ret,sizeof(ret));
if (ret==FAIL) { if (ret==FAIL) {
s->ReceiveDataOnly(mess,sizeof(mess)); s->ReceiveDataOnly(mess,sizeof(mess));
@ -3530,7 +3534,6 @@ int slsDetector::setPort(portType index, int num){
s->Disconnect(); s->Disconnect();
} }
} }
} }
if (ret!=FAIL) { if (ret!=FAIL) {
@ -3559,7 +3562,11 @@ int slsDetector::setPort(portType index, int num){
thisDetector->controlPort=num; thisDetector->controlPort=num;
break; break;
case DATA_PORT: case DATA_PORT:
thisDetector->dataPort=num; if(thisDetector->receiverOnlineFlag==ONLINE_FLAG)
thisDetector->dataPort=retval;
else
thisDetector->dataPort=num;
break; break;
case STOP_PORT: case STOP_PORT:
thisDetector->stopPort=num; thisDetector->stopPort=num;
@ -5027,55 +5034,6 @@ int slsDetector::writeConfigurationFile(ofstream &outfile, int id){
int slsDetector::writeSettingsFile(string fname, int imod){ int slsDetector::writeSettingsFile(string fname, int imod){
return writeSettingsFile(fname,thisDetector->myDetectorType, detectorModules[imod]); return writeSettingsFile(fname,thisDetector->myDetectorType, detectorModules[imod]);

View File

@ -0,0 +1 @@
dataport 1955

View File

@ -12,22 +12,29 @@ using namespace std;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int portno = DEFAULT_PORTNO+2; int ret = slsDetectorDefs::OK;
int retval = slsDetectorDefs::OK; MySocketTCP *socket = NULL;
string fname = "";
MySocketTCP *socket = new MySocketTCP(portno); //parse command line for config
if (socket->getErrorStatus()) for(int iarg=2;iarg<argc;iarg++)
if(!strcasecmp(argv[iarg-1],"-config"))
fname.assign(argv[iarg]);
//reads config file, creates socket, assigns function table
slsReceiverFuncs *receiver = new slsReceiverFuncs(socket,fname,ret);
if(ret==slsDetectorDefs::FAIL)
return -1; return -1;
//assign function table
slsReceiverFuncs *receiver = new slsReceiverFuncs(socket);
#ifdef VERBOSE #ifdef VERBOSE
cout << "Function table assigned." << endl; cout << "Function table assigned." << endl;
#endif #endif
cout << " Ready..." << endl; cout << " Ready..." << endl;
//waits for connection //waits for connection
while(retval!=GOODBYE) { while(ret!=GOODBYE) {
#ifdef VERBOSE #ifdef VERBOSE
cout<< endl; cout<< endl;
#endif #endif
@ -38,7 +45,7 @@ int main(int argc, char *argv[])
#ifdef VERY_VERBOSE #ifdef VERY_VERBOSE
cout << "Conenction accepted" << endl; cout << "Conenction accepted" << endl;
#endif #endif
retval = receiver->decode_function(); ret = receiver->decode_function();
#ifdef VERY_VERBOSE #ifdef VERY_VERBOSE
cout << "function executed" << endl; cout << "function executed" << endl;
#endif #endif

View File

@ -7,23 +7,98 @@
#include "slsReceiverFunctionList.h" #include "slsReceiverFunctionList.h"
#include <iostream> #include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std; using namespace std;
slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *socket): slsReceiverFuncs::slsReceiverFuncs(MySocketTCP *&mySocket,string const fname,int &success):
socket(socket), socket(mySocket),
ret(OK), ret(OK),
lockStatus(0){ lockStatus(0){
//initialize variables
strcpy(mess,"dummy message");
strcpy(socket->lastClientIP,"none");
strcpy(socket->thisClientIP,"none1");
function_table(); int port_no = DEFAULT_PORTNO+2;
slsReceiverList = new slsReceiverFunctionList(); 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(){ int slsReceiverFuncs::function_table(){
for (int i=0;i<numberOfFunctions;i++) for (int i=0;i<numberOfFunctions;i++)
@ -90,6 +165,7 @@ int slsReceiverFuncs::decode_function(){
if (ret==FAIL) if (ret==FAIL)
cout << "Error executing the function = " << fnum << endl; cout << "Error executing the function = " << fnum << endl;
return ret; return ret;
} }
@ -598,20 +674,21 @@ int slsReceiverFuncs::lock_receiver() {
int slsReceiverFuncs::set_port() { int slsReceiverFuncs::set_port() {
ret=OK; ret=OK;
MySocketTCP* mySocket=NULL;
int sd=-1; int sd=-1;
enum portType p_type; /** data? control? stop? Unused! */ enum portType p_type; /** data? control? stop? Unused! */
int p_number; /** new port number */ int p_number; /** new port number */
// receive arguments // receive arguments
if(socket->ReceiveDataOnly(&p_type,sizeof(p_type)) < 0 ){ if(socket->ReceiveDataOnly(&p_type,sizeof(p_type)) < 0 ){
sprintf(mess,"Error reading from socket\n"); strcpy(mess,"Error reading from socket\n");
printf("Error reading from socket (ptype)\n"); cout << mess << endl;
ret=FAIL; ret=FAIL;
} }
if(socket->ReceiveDataOnly(&p_number,sizeof(p_number)) < 0 ){ if(socket->ReceiveDataOnly(&p_number,sizeof(p_number)) < 0 ){
sprintf(mess,"Error reading from socket\n"); strcpy(mess,"Error reading from socket\n");
printf("Error reading from socket (pnum)\n"); cout << mess << endl;
ret=FAIL; ret=FAIL;
} }
@ -620,26 +697,30 @@ int slsReceiverFuncs::set_port() {
if (socket->differentClients==1 && lockStatus==1 ) { if (socket->differentClients==1 && lockStatus==1 ) {
ret=FAIL; ret=FAIL;
sprintf(mess,"Detector locked by %s\n",socket->lastClientIP); sprintf(mess,"Detector locked by %s\n",socket->lastClientIP);
} else { }
else {
if (p_number<1024) { if (p_number<1024) {
sprintf(mess,"Too low port number %d\n", p_number); sprintf(mess,"Too low port number %d\n", p_number);
cout << endl; cout << mess << endl;
ret=FAIL; ret=FAIL;
} }
cout << "set port " << p_type << " to " << p_number <<endl; cout << "set port " << p_type << " to " << p_number <<endl;
socket = new MySocketTCP(p_number); mySocket = new MySocketTCP(p_number);
} }
if (!socket->getErrorStatus()){ if(mySocket){
ret=OK; sd = mySocket->getErrorStatus();
if (socket->differentClients) if (!sd){
ret=FORCE_UPDATE; ret=OK;
} else { if (mySocket->differentClients)
ret=FAIL; ret=FORCE_UPDATE;
sprintf(mess,"Could not bind port %d\n", p_number); } else {
cout << "Could not bind port " << p_number << endl; ret=FAIL;
if (socket->getErrorStatus()==-10) { sprintf(mess,"Could not bind port %d\n", p_number);
sprintf(mess,"Port %d already set\n", p_number); cout << mess << endl;
cout << "Port " << p_number << " already set" << 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)); socket->SendDataOnly(mess,sizeof(mess));
} else { } else {
socket->SendDataOnly(&p_number,sizeof(p_number)); socket->SendDataOnly(&p_number,sizeof(p_number));
socket->Disconnect(); if(sd>=0){
delete socket; socket->Disconnect();
delete socket;
socket = mySocket;
}
} }
//return ok/fail //return ok/fail

View File

@ -21,9 +21,12 @@ class slsReceiverFuncs : private virtual slsDetectorDefs {
public: public:
/** /**
* Constructor * 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 */ /** Destructor */
virtual ~slsReceiverFuncs(){}; virtual ~slsReceiverFuncs(){};
@ -89,11 +92,10 @@ public:
/** Execute command */ /** Execute command */
int exec_command(); int exec_command();
private: private:
/** Socket */ /** Socket */
MySocketTCP *socket; MySocketTCP*& socket;
/** slsReceiverFunctionList object */ /** slsReceiverFunctionList object */
slsReceiverFunctionList *slsReceiverList; slsReceiverFunctionList *slsReceiverList;