started receiver start on a separate thread and returns, whereas stop receiver will end udp, tcp socket and exit thread

This commit is contained in:
Maliakal Dhanya 2014-05-23 18:28:42 +02:00
parent 567501c6f7
commit 90f5fb39db
5 changed files with 137 additions and 58 deletions

View File

@ -5,6 +5,7 @@
#include "slsReceiverUsers.h" #include "slsReceiverUsers.h"
#include <iostream> #include <iostream>
#include <string.h>
using namespace std; using namespace std;
@ -72,10 +73,18 @@ int main(int argc, char *argv[]) {
//start tcp server thread
if(user->start() == slsReceiverDefs::OK){
string str;
cin>>str;
//wait and look for an exit keyword
while(str.find("exit") == string::npos)
cin>>str;
//stop tcp server thread, stop udp socket
user->stop();
}
user->start(); cout << "Goodbye!" << endl;
return 0; return 0;
} }

View File

@ -19,8 +19,6 @@
using namespace std; using namespace std;
int slsReceiverTCPIPInterface::file_des(-1);
int slsReceiverTCPIPInterface::socketDescriptor(-1);
slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() { slsReceiverTCPIPInterface::~slsReceiverTCPIPInterface() {
@ -36,7 +34,8 @@ slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int argc, char *argv[], int
lockStatus(0), lockStatus(0),
shortFrame(-1), shortFrame(-1),
packetsPerFrame(GOTTHARD_PACKETS_PER_FRAME), packetsPerFrame(GOTTHARD_PACKETS_PER_FRAME),
socket(NULL){ socket(NULL),
killTCPServerThread(0){
int port_no = DEFAULT_PORTNO+2; int port_no = DEFAULT_PORTNO+2;
ifstream infile; ifstream infile;
@ -169,21 +168,64 @@ slsReceiverTCPIPInterface::slsReceiverTCPIPInterface(int argc, char *argv[], int
//Catch signal SIGINT to close files properly //Catch signal SIGINT to close files properly
signal(SIGINT,staticCloseFile); signal(SIGINT,staticCloseFile);
file_des=socket->getFileDes();
socketDescriptor=socket->getsocketDescriptor();
//success = OK;
} }
} }
} }
void slsReceiverTCPIPInterface::start(){ int slsReceiverTCPIPInterface::start(){
cout << "Creating TCP Server Thread" << endl;
killTCPServerThread = 0;
if(pthread_create(&TCPServer_thread, NULL,startTCPServerThread, (void*) this)){
cout << "Could not create TCP Server thread" << endl;
return FAIL;
}
#ifdef VERBOSE
cout << "TCP Server thread created successfully." << endl;
#endif
return OK;
}
void slsReceiverTCPIPInterface::stop(){
cout << "Shutting down UDP Socket" << endl;
if(slsReceiverFunctions)
slsReceiverFunctions->shutDownUDPSocket();
cout << "Closing Files... " << endl;
slsReceiverFunctions->closeFile();
cout<<"Shutting down TCP Socket and TCP thread"<<endl;
killTCPServerThread = 1;
socket->ShutDownSocket();
void* status;
pthread_join(TCPServer_thread, &status);
killTCPServerThread = 0;
}
void* slsReceiverTCPIPInterface::startTCPServerThread(void *this_pointer){
((slsReceiverTCPIPInterface*)this_pointer)->startTCPServer();
return this_pointer;
}
void slsReceiverTCPIPInterface::startTCPServer(){
#ifdef VERYVERBOSE
cout << "Starting Receiver TCP Server" << endl;
#endif
int v=slsReceiverDefs::OK; int v=slsReceiverDefs::OK;
while(v!=GOODBYE) { while(1) {
#ifdef VERBOSE #ifdef VERBOSE
cout<< endl; cout<< endl;
#endif #endif
@ -203,20 +245,28 @@ void slsReceiverTCPIPInterface::start(){
cout << "connection closed" << endl; cout << "connection closed" << endl;
#endif #endif
} }
//if tcp command was to exit server
if(v==GOODBYE){
cout << "Shutting down UDP Socket" << endl;
if(slsReceiverFunctions)
slsReceiverFunctions->shutDownUDPSocket();
cout << "Closing Files... " << endl;
slsReceiverFunctions->closeFile();
pthread_exit(NULL);
}
//if user entered exit
if(killTCPServerThread)
pthread_exit(NULL);
} }
} }
void slsReceiverTCPIPInterface::stop(){
//shut down udp socket
if(slsReceiverFunctions)
slsReceiverFunctions->shutDownUDPSocket();
//disconnect and delete socket
socket->Disconnect();
delete socket;
//close file and exit
closeFile(0);
}
int slsReceiverTCPIPInterface::function_table(){ int slsReceiverTCPIPInterface::function_table(){
@ -330,8 +380,7 @@ int slsReceiverTCPIPInterface::M_nofunc(){
void slsReceiverTCPIPInterface::closeFile(int p){ void slsReceiverTCPIPInterface::closeFile(int p){
cout<<"Closing Files... "<<endl; stop();
slsReceiverFunctions->closeFile();
cout << "Goodbye!" << endl; cout << "Goodbye!" << endl;
exit(-1); exit(-1);
} }
@ -1850,7 +1899,6 @@ int slsReceiverTCPIPInterface::set_port() {
socket->Disconnect(); socket->Disconnect();
delete socket; delete socket;
socket = mySocket; socket = mySocket;
file_des=socket->getFileDes();
} }
} }

View File

@ -29,10 +29,13 @@ public:
*/ */
slsReceiverTCPIPInterface(int argc, char *argv[], int &success); slsReceiverTCPIPInterface(int argc, char *argv[], int &success);
/** starts listening on the TCP port for client comminication */ /**
void start(); * Starts listening on the TCP port for client comminication
\returns OK or FAIL
*/
int start();
/** stop listening on the TCP & UDP port for client comminication and exit receiver */ /** stop listening on the TCP & UDP port for client comminication */
void stop(); void stop();
/** Destructor */ /** Destructor */
@ -88,6 +91,20 @@ public:
private: private:
/**
* Static function - Thread started which is a TCP server
* Called by start()
* @param this_pointer pointer to this object
*/
static void* startTCPServerThread(void *this_pointer);
/**
* Thread started which is a TCP server
* Called by start()
*/
void startTCPServer();
/** assigns functions to the fnum enum */ /** assigns functions to the fnum enum */
int function_table(); int function_table();
@ -226,10 +243,12 @@ private:
/** Packets per frame */ /** Packets per frame */
int packetsPerFrame; int packetsPerFrame;
static int file_des; /** kill tcp server thread */
static int socketDescriptor; int killTCPServerThread;
/** thread for TCP server */
pthread_t TCPServer_thread;
//private:
protected: protected:
/** Socket */ /** Socket */
MySocketTCP* socket; MySocketTCP* socket;

View File

@ -11,8 +11,8 @@ slsReceiverUsers::~slsReceiverUsers() {
delete slsReceiverUsers::receiver; delete slsReceiverUsers::receiver;
} }
void slsReceiverUsers::start() { int slsReceiverUsers::start() {
slsReceiverUsers::receiver->start(); return slsReceiverUsers::receiver->start();
} }
void slsReceiverUsers::stop() { void slsReceiverUsers::stop() {

View File

@ -37,8 +37,11 @@ public:
/** Close File and exits receiver server */ /** Close File and exits receiver server */
void closeFile(int p); void closeFile(int p);
/** starts listening on the TCP port for client comminication */ /**
void start(); * starts listening on the TCP port for client comminication
\return 0 for success or 1 for FAIL in creating TCP server
*/
int start();
/** stops listening to the TCP & UDP port and exit receiver program*/ /** stops listening to the TCP & UDP port and exit receiver program*/
void stop(); void stop();