/** @file \author Imanol Barba Sabariego \date 13/06/2013 \page server_code Server \brief Ejemplo de aplicaciĆ³n servidor \code{.cpp} #include "server.h" #include Server *serv; void killThread(thread_args *t_arg) { (t_arg->s)->Close(); pthread_mutex_lock(t_arg->mutex); t_arg->serv->setNWorkers(t_arg->serv->getNWorkers()-1); cout << "Worker " << t_arg->id << ": connection terminated" << endl; pthread_mutex_unlock(t_arg->mutex); pthread_cond_signal(t_arg->condition); t_arg->serv->getStartedThreads()->remove(t_arg->thread); t_arg->serv->getStoppedThreads()->push_back(t_arg->thread); if(t_arg->s != 0) { delete t_arg->s; t_arg->s = 0; } if(t_arg != 0) { delete t_arg; t_arg = 0; } pthread_exit(NULL); } void *WorkerThread(void* args) { struct thread_args *t_arg = (struct thread_args*)args; while(true) { string message; *(t_arg->s) >> message; cout << "Worker " << t_arg->id << " received: " << message << endl; string send = "You said: "; send += message; *(t_arg->s) << send; } killThread(t_arg); } void stopServer(int signal) { serv->requestExit(); } void processText(string *str) { for(int i = 0; i < str->length(); i++) { if((*str)[i] == 32 || (*str)[i] == 10 || (*str)[i] == 11) { str->erase(i--,1); } } } bool readConf(string *ip, int *port) { *ip = ""; *port = 0; ifstream confFile; confFile.open(CONFFILE); if(!confFile.is_open()) { cout << "Error opening configuration file" << endl; return false; } string parameter; while(true) { getline(confFile, parameter, '='); processText(¶meter); if(confFile.eof()) { break; } if(parameter == "bind-ip") { confFile >> *ip; } else if(parameter == "port") { confFile >> *port; } } confFile.close(); if(*ip == "" || *port == 0) { return false; } return true; } int main() { string ip; int port; serv = new Server(); signal(SIGINT, stopServer); if(!readConf(&ip,&port)) { cout << "Configuration couldn't be loaded" << endl; return -1; } serv->startServer(ip,port); delete serv; return 0; } \endcode */