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}
 