|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/** @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 <fstream>
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
*/
|