argv, argc moved from tcpip interface to slsReceiver

This commit is contained in:
bergamaschi 2014-07-25 10:28:56 +02:00
parent 4f04fc5457
commit 91c0a083b3
3 changed files with 188 additions and 113 deletions

View File

@ -3,18 +3,140 @@
* @short creates the UDP and TCP class objects
***********************************************/
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include "slsReceiver.h"
#include "slsReceiverUDPFunctions.h"
#include "eigerReceiver.h"
using namespace std;
slsReceiver::slsReceiver(int argc, char *argv[], int &success){
//creating base receiver
cout << "SLS Receiver" << endl;
receiverBase = new slsReceiverUDPFunctions();
int tcpip_port_no=-1;
ifstream infile;
string sLine,sargname;
int iline = 0;
success=OK;
string fname = "";
//parse command line for config
for(int iarg=1;iarg<argc;iarg++){
if((!strcasecmp(argv[iarg],"-config"))||(!strcasecmp(argv[iarg],"-f"))){
if(iarg+1==argc){
cout << "no config file name given. Exiting." << endl;
success=FAIL;
}else
fname.assign(argv[iarg+1]);
}
}
if((!fname.empty()) && (success == OK)){
#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 << sLine << 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;
//tcp port
if(sargname=="rx_tcpport"){
if(sstr.good()) {
sstr >> sargname;
if(sscanf(sargname.c_str(),"%d",&tcpip_port_no))
cout<<"dataport:"<<tcpip_port_no<<endl;
else{
cout << "could not decode port in config file. Exiting." << endl;
success=FAIL;
}
}
}
}
}
infile.close();
}else {
cout << "Error opening configuration file " << fname << endl;
success = FAIL;
}
#ifdef VERBOSE
cout << "Read configuration file of " << iline << " lines" << endl;
#endif
}
//parse command line for port etc.. more priority
if(success == OK){
for(int iarg=1;iarg<argc;iarg++){
//tcp port
if(!strcasecmp(argv[iarg],"-rx_tcpport")){
if(iarg+1==argc){
cout << "no port given after -rx_tcpport in command line. Exiting." << endl;
success=FAIL;
}else{
if(sscanf(argv[iarg+1],"%d",&tcpip_port_no)){
cout<<"dataport:"<<tcpip_port_no<<endl;
iarg++;
}else{
cout << "could not decode port in command line. \n\nExiting." << endl;
success=FAIL;
}
}
}
else{
cout << "Unknown argument:" << argv[iarg] << endl;
success=FAIL;
}
}
}
//help
if(success != OK){
cout << "Help Commands " << endl;
cout << "rx_tcpport:\t TCP Communication Port with the client. Default:1954. " << endl << endl;
}
if (success==OK)
tcpipInterface = new slsReceiverTCPIPInterface(success,receiverBase, tcpip_port_no);
//tcp ip interface
//tcp ip interface
tcpipInterface = new slsReceiverTCPIPInterface(argc,argv,success,receiverBase);
}

View File

@ -28,7 +28,7 @@ slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() {
}
slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int argc, char *argv[], int &success, slsReceiverBase* rbase):
slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int &success, slsReceiverBase* rbase, int pn):
myDetectorType(GOTTHARD),
receiverBase(rbase),
ret(OK),
@ -38,116 +38,15 @@ slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int argc, char *argv[], int
dynamicrange(16),
socket(NULL),
killTCPServerThread(0),
tenGigaEnable(0){
tenGigaEnable(0), portNumber(DEFAULT_PORTNO+2){
int port_no = DEFAULT_PORTNO+2;
ifstream infile;
string sLine,sargname;
int iline = 0;
int port_no=portNumber;
success=OK;
string fname = "";
//parse command line for config
for(int iarg=1;iarg<argc;iarg++){
if((!strcasecmp(argv[iarg],"-config"))||(!strcasecmp(argv[iarg],"-f"))){
if(iarg+1==argc){
cout << "no config file name given. Exiting." << endl;
success=FAIL;
}else
fname.assign(argv[iarg+1]);
}
}
if((!fname.empty()) && (success == OK)){
#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 << sLine << 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;
//tcp port
if(sargname=="rx_tcpport"){
if(sstr.good()) {
sstr >> sargname;
if(sscanf(sargname.c_str(),"%d",&port_no))
cout<<"dataport:"<<port_no<<endl;
else{
cout << "could not decode port in config file. Exiting." << endl;
success=FAIL;
}
}
}
}
}
infile.close();
}else {
cout << "Error opening configuration file " << fname << endl;
success = FAIL;
}
#ifdef VERBOSE
cout << "Read configuration file of " << iline << " lines" << endl;
#endif
}
//parse command line for port etc.. more priority
if(success == OK){
for(int iarg=1;iarg<argc;iarg++){
//tcp port
if(!strcasecmp(argv[iarg],"-rx_tcpport")){
if(iarg+1==argc){
cout << "no port given after -rx_tcpport in command line. Exiting." << endl;
success=FAIL;
}else{
if(sscanf(argv[iarg+1],"%d",&port_no)){
cout<<"dataport:"<<port_no<<endl;
iarg++;
}else{
cout << "could not decode port in command line. \n\nExiting." << endl;
success=FAIL;
}
}
}
else{
cout << "Unknown argument:" << argv[iarg] << endl;
success=FAIL;
}
}
}
//help
if(success != OK){
cout << "Help Commands " << endl;
cout << "rx_tcpport:\t TCP Communication Port with the client. Default:1954. " << endl << endl;
}
if (pn>0)
port_no = pn;
success=OK;
//create socket
if(success == OK){
@ -157,6 +56,7 @@ slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int argc, char *argv[], int
delete socket;
socket=NULL;
} else {
portNumber=port_no;
//initialize variables
strcpy(socket->lastClientIP,"none");
strcpy(socket->thisClientIP,"none1");
@ -175,6 +75,47 @@ slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int argc, char *argv[], int
}
int slsReceiverTCPIPInterface::setPortNumber(int pn){
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);
cout << mess << endl;
} else {
oldsocket=socket;
socket = new MySocketTCP(p_number);
if(socket){
sd = socket->getErrorStatus();
if (!sd){
portNumber=p_number;
delete oldsocket;
} else {
cout << "Could not bind port " << p_number << endl;
if (sd==-10) {
cout << "Port "<< p_number << " already set" << endl;
} else {
delete socket;
socket=oldsocket;
}
}
} else {
socket=oldsocket;
}
}
}
return portNumber;
}
int slsReceiverTCPIPInterface::start(){
cout << "Creating TCP Server Thread" << endl;

View File

@ -23,12 +23,19 @@ public:
/**
* Constructor
* reads config file, creates socket, assigns function table
* @param argc from command line
* @param argv from command line
* @param succecc socket creation was successfull
* rbase pointer to the receiver base
* @param rbase pointer to the receiver base
* @param pn port number (defaults to default port number)
*/
slsReceiverTCPIPInterface(int argc, char *argv[], int &success, slsReceiverBase* rbase);
slsReceiverTCPIPInterface(int &success, slsReceiverBase* rbase, int pn=-1);
/**
* Sets the port number to listen to.
Take care that the client must know to whcih port it has to listen to, so normally it is better to use a fixes port from the instatiation or change it from the client.
@param pn port number (-1 only get)
\returns actual port number
*/
int setPortNumber(int pn=-1);
/**
* Starts listening on the TCP port for client comminication
@ -100,6 +107,7 @@ private:
*/
static void* startTCPServerThread(void *this_pointer);
/**
* Thread started which is a TCP server
* Called by start()
@ -261,6 +269,10 @@ private:
/** size of one frame*/
int tenGigaEnable;
/** port number */
int portNumber;
protected:
/** Socket */
MySocketTCP* socket;