Blame view

JChatClient/doc/latex/server_code.tex 6.71 KB
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
Ejemplo de aplicaciĆ³n servidor


\begin{DoxyCode}
\textcolor{preprocessor}{#include "\hyperlink{server_8h}{server.h}"}
\textcolor{preprocessor}{#include <fstream>}

\hyperlink{class_server}{Server} *\hyperlink{main__server_8cpp_ab6b71e6574fcb2d2e0a5c2af08a2799f}{serv};

\textcolor{keywordtype}{void} \hyperlink{server_8h_ab99831172d1d4bca2c79bb37c7c062df}{killThread}(\hyperlink{structthread__args}{thread\_args} *t\_arg)
\{
        (t\_arg->\hyperlink{structthread__args_aede3ef61778d02bdf25c368d1cf9d489}{s})->Close();
        pthread\_mutex\_lock(t\_arg->\hyperlink{structthread__args_ad5d64499f5f6e2df467c96b52a94c520}{mutex});
        t\_arg->\hyperlink{structthread__args_ab29508135b033282f374d0fb47028fbd}{serv}->\hyperlink{class_server_a0090663c66f1f371f8b1aa87c5f49623}{setNWorkers}(t\_arg->\hyperlink{structthread__args_ab29508135b033282f374d0fb47028fbd}{serv}->\hyperlink{class_server_acf13ac1f7fc49f28c1b1fa76eab209ab}{getNWorkers}()-1);
        cout << \textcolor{stringliteral}{"Worker "} << t\_arg->\hyperlink{structthread__args_a6322b3468fd88f8f49977a6439e9f352}{id} << \textcolor{stringliteral}{": connection terminated"} << endl;
        pthread\_mutex\_unlock(t\_arg->\hyperlink{structthread__args_ad5d64499f5f6e2df467c96b52a94c520}{mutex});
        pthread\_cond\_signal(t\_arg->\hyperlink{structthread__args_a96ee28beaed4175257db3d8794245f25}{condition});
        t\_arg->\hyperlink{structthread__args_ab29508135b033282f374d0fb47028fbd}{serv}->\hyperlink{class_server_a0622813f3b675ab661d5f628cec03d30}{getStartedThreads}()->remove(t\_arg->
      \hyperlink{structthread__args_acdcd2db4307e5e75d5110067e196a70e}{thread});
        t\_arg->\hyperlink{structthread__args_ab29508135b033282f374d0fb47028fbd}{serv}->\hyperlink{class_server_a81289d4e58dec18dd01e343c7f397c1e}{getStoppedThreads}()->push\_back(t\_arg->
      \hyperlink{structthread__args_acdcd2db4307e5e75d5110067e196a70e}{thread});
        \textcolor{keywordflow}{if}(t\_arg->\hyperlink{structthread__args_aede3ef61778d02bdf25c368d1cf9d489}{s} != 0)
        \{
                \textcolor{keyword}{delete} t\_arg->\hyperlink{structthread__args_aede3ef61778d02bdf25c368d1cf9d489}{s};
                t\_arg->\hyperlink{structthread__args_aede3ef61778d02bdf25c368d1cf9d489}{s} = 0;
        \}
        \textcolor{keywordflow}{if}(t\_arg != 0)
        \{
                \textcolor{keyword}{delete} t\_arg;
                t\_arg = 0;
        \}
        pthread\_exit(NULL);
\}

\textcolor{keywordtype}{void} *\hyperlink{server_8h_a7d13c650ec7a525ad17cabba4f371745}{WorkerThread}(\textcolor{keywordtype}{void}* args)
\{
        \textcolor{keyword}{struct }\hyperlink{structthread__args}{thread\_args} *t\_arg = (\textcolor{keyword}{struct }\hyperlink{structthread__args}{thread\_args}*)args;
        \textcolor{keywordflow}{while}(\textcolor{keyword}{true})
        \{
                \textcolor{keywordtype}{string} message;
                *(t\_arg->\hyperlink{structthread__args_aede3ef61778d02bdf25c368d1cf9d489}{s}) >> message;
                cout << \textcolor{stringliteral}{"Worker "} << t\_arg->\hyperlink{structthread__args_a6322b3468fd88f8f49977a6439e9f352}{id} << \textcolor{stringliteral}{" received: "} << message << endl;
                \textcolor{keywordtype}{string} send = \textcolor{stringliteral}{"You said: "};
                send += message;
                *(t\_arg->\hyperlink{structthread__args_aede3ef61778d02bdf25c368d1cf9d489}{s}) << send;
        \}
        \hyperlink{server_8h_ab99831172d1d4bca2c79bb37c7c062df}{killThread}(t\_arg);
\}

\textcolor{keywordtype}{void} \hyperlink{main__server_8cpp_a5ee86dec2f1282750e7b1da928d45dbc}{stopServer}(\textcolor{keywordtype}{int} signal)
\{
        serv->\hyperlink{class_server_a74769349252c1ad61717b156e42d61d3}{requestExit}();
\}

\textcolor{keywordtype}{void} \hyperlink{server_8h_aa6328bc358b87d3e11c3f460d6427c5b}{processText}(\textcolor{keywordtype}{string} *str)
\{
        \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int} i = 0; i < str->length(); i++)
        \{
                \textcolor{keywordflow}{if}((*str)[i] == 32 || (*str)[i] == 10 || (*str)[i] == 11)
                \{
                        str->erase(i--,1);
                \}
        \}
\}

\textcolor{keywordtype}{bool} \hyperlink{server_8h_a4f850f08d755a0d0ebe936b5b84975e3}{readConf}(\textcolor{keywordtype}{string} *ip, \textcolor{keywordtype}{int} *port)
\{
        *ip = \textcolor{stringliteral}{""};
        *port = 0;
        ifstream confFile;
        confFile.open(\hyperlink{server_8h_ad22e7f0ef164a8dafb32f779f737a1bd}{CONFFILE});
        \textcolor{keywordflow}{if}(!confFile.is\_open())
        \{
                cout << \textcolor{stringliteral}{"Error opening configuration file"} << endl;
                \textcolor{keywordflow}{return} \textcolor{keyword}{false};
        \}
        \textcolor{keywordtype}{string} parameter;
        \textcolor{keywordflow}{while}(\textcolor{keyword}{true})
        \{
                getline(confFile, parameter, \textcolor{charliteral}{'='});
                \hyperlink{server_8h_aa6328bc358b87d3e11c3f460d6427c5b}{processText}(&parameter);
                \textcolor{keywordflow}{if}(confFile.eof())
                \{
                        \textcolor{keywordflow}{break};
                \}
                \textcolor{keywordflow}{if}(parameter == \textcolor{stringliteral}{"bind-ip"})
                \{
                        confFile >> *ip;
                \}
                \textcolor{keywordflow}{else} \textcolor{keywordflow}{if}(parameter == \textcolor{stringliteral}{"port"})
                \{
                        confFile >> *port;
                \}
        \}
        confFile.close();
        \textcolor{keywordflow}{if}(*ip == \textcolor{stringliteral}{""} || *port == 0)
        \{
                \textcolor{keywordflow}{return} \textcolor{keyword}{false};
        \}
        \textcolor{keywordflow}{return} \textcolor{keyword}{true};
\}

\textcolor{keywordtype}{int} \hyperlink{client_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4}{main}()
\{
        \textcolor{keywordtype}{string} ip;
        \textcolor{keywordtype}{int} port;
        serv = \textcolor{keyword}{new} \hyperlink{class_server}{Server}();
        signal(SIGINT, \hyperlink{main__server_8cpp_a5ee86dec2f1282750e7b1da928d45dbc}{stopServer});
        \textcolor{keywordflow}{if}(!\hyperlink{server_8h_a4f850f08d755a0d0ebe936b5b84975e3}{readConf}(&ip,&port))
        \{
                cout << \textcolor{stringliteral}{"Configuration couldn't be loaded"} << endl;
                \textcolor{keywordflow}{return} -1;
        \}
        serv->\hyperlink{class_server_aef89851f83a9bbe4459c46b6c7906e1f}{startServer}(ip,port);
        \textcolor{keyword}{delete} \hyperlink{main__server_8cpp_ab6b71e6574fcb2d2e0a5c2af08a2799f}{serv};
        \textcolor{keywordflow}{return} 0;
\}
\end{DoxyCode}