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;
}
uint16_t getPortNumber(){
return ntohs(serverAddress.sin_port);
}
/** @short free connection */

View File

@ -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]);

View File

@ -0,0 +1 @@
dataport 1955

View File

@ -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;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;
//assign function table
slsReceiverFuncs *receiver = new slsReceiverFuncs(socket);
#ifdef VERBOSE
cout << "Function table assigned." << endl;
#endif
cout << " Ready..." << endl;
//waits for connection
while(retval!=GOODBYE) {
while(ret!=GOODBYE) {
#ifdef VERBOSE
cout<< endl;
#endif
@ -38,7 +45,7 @@ int main(int argc, char *argv[])
#ifdef VERY_VERBOSE
cout << "Conenction accepted" << endl;
#endif
retval = receiver->decode_function();
ret = receiver->decode_function();
#ifdef VERY_VERBOSE
cout << "function executed" << endl;
#endif

View File

@ -7,23 +7,98 @@
#include "slsReceiverFunctionList.h"
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
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;i<numberOfFunctions;i++)
@ -90,6 +165,7 @@ int slsReceiverFuncs::decode_function(){
if (ret==FAIL)
cout << "Error executing the function = " << fnum << endl;
return ret;
}
@ -598,20 +674,21 @@ int slsReceiverFuncs::lock_receiver() {
int slsReceiverFuncs::set_port() {
ret=OK;
MySocketTCP* mySocket=NULL;
int sd=-1;
enum portType p_type; /** data? control? stop? Unused! */
int p_number; /** new port number */
// receive arguments
if(socket->ReceiveDataOnly(&p_type,sizeof(p_type)) < 0 ){
sprintf(mess,"Error reading from socket\n");
printf("Error reading from socket (ptype)\n");
strcpy(mess,"Error reading from socket\n");
cout << mess << endl;
ret=FAIL;
}
if(socket->ReceiveDataOnly(&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 <<endl;
socket = new MySocketTCP(p_number);
mySocket = new MySocketTCP(p_number);
}
if (!socket->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

View File

@ -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;