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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Wt examples: /home/koen/project/wt/public-git/wt/examples/simplechat/SimpleChatServer.C Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.6 -->
<div class="navigation" id="top">
<div class="tabs">
<ul>
<li><a href="index.html"><span>Main Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<h1>/home/koen/project/wt/public-git/wt/examples/simplechat/SimpleChatServer.C</h1><a href="SimpleChatServer_8C.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * See the LICENSE file for terms of use.</span>
<a name="l00005"></a>00005 <span class="comment"> */</span>
<a name="l00006"></a>00006
<a name="l00007"></a>00007 <span class="preprocessor">#include "<a class="code" href="SimpleChatServer_8h.html">SimpleChatServer.h</a>"</span>
<a name="l00008"></a>00008
<a name="l00009"></a>00009 <span class="preprocessor">#include <iostream></span>
<a name="l00010"></a>00010 <span class="preprocessor">#include <boost/lexical_cast.hpp></span>
<a name="l00011"></a>00011
<a name="l00012"></a>00012 <span class="keyword">using namespace </span>Wt;
<a name="l00013"></a>00013
<a name="l00014"></a><a class="code" href="classChatEvent.html#6df1eda449e68e7f29644e68919b2f3f">00014</a> <span class="keyword">const</span> <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a> <a class="code" href="classChatEvent.html#6df1eda449e68e7f29644e68919b2f3f" title="Get the message formatted as HTML, rendered for the given user.">ChatEvent::formattedHTML</a>(<span class="keyword">const</span> <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a>& user)<span class="keyword"> const</span>
<a name="l00015"></a>00015 <span class="keyword"></span>{
<a name="l00016"></a>00016 <span class="keywordflow">switch</span> (<a class="code" href="classChatEvent.html#8a55c20da17f812694469924b04bfd4c">type_</a>) {
<a name="l00017"></a>00017 <span class="keywordflow">case</span> <a class="code" href="classChatEvent.html#fa84446dae17c08fe15cf932fc0b7d615173cb4baa818f95af4604d7a01e3ae6">Login</a>:
<a name="l00018"></a>00018 <span class="keywordflow">return</span> <span class="stringliteral">"<span class='chat-info'>"</span>
<a name="l00019"></a>00019 + <a class="code" href="classChatEvent.html#3e2d08dca6b95343ad494e796a320eec">user_</a> + <span class="stringliteral">" joined the conversation.</span>"</span>;
<a name="l00020"></a>00020 <span class="keywordflow">case</span> <a class="code" href="classChatEvent.html#fa84446dae17c08fe15cf932fc0b7d61a8b11c1512738c200b2cfe1facd1c171">Logout</a>:
<a name="l00021"></a>00021 <span class="keywordflow">return</span> <span class="stringliteral">"<span class='chat-info'>"</span>
<a name="l00022"></a>00022 + ((user == <a class="code" href="classChatEvent.html#3e2d08dca6b95343ad494e796a320eec">user_</a>) ? <span class="stringliteral">"You"</span> : <a class="code" href="classChatEvent.html#3e2d08dca6b95343ad494e796a320eec">user_</a>)
<a name="l00023"></a>00023 + <span class="stringliteral">" logged out.</span>"</span>;
<a name="l00024"></a>00024 <span class="keywordflow">case</span> <a class="code" href="classChatEvent.html#fa84446dae17c08fe15cf932fc0b7d61b8894351cd11a316e25495f677eb9ae1">Message</a>:{
<a name="l00025"></a>00025 <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a> result;
<a name="l00026"></a>00026
<a name="l00027"></a>00027 result = <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a>(<span class="stringliteral">"<span class='"</span>)
<a name="l00028"></a>00028 + ((user == <a class="code" href="classChatEvent.html#3e2d08dca6b95343ad494e796a320eec">user_</a>) ? <span class="stringliteral">"chat-self"</span> : <span class="stringliteral">"chat-user"</span>)
<a name="l00029"></a>00029 + <span class="stringliteral">"'>"</span> + <a class="code" href="classChatEvent.html#3e2d08dca6b95343ad494e796a320eec">user_</a> + <span class="stringliteral">":</span>"</span>;
<a name="l00030"></a>00030
<a name="l00031"></a>00031 <span class="keywordflow">if</span> (<a class="code" href="classChatEvent.html#d51677c773550286593db4793dae62eb">message_</a>.<a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html#159d088da6e8f2be1035b2341889254c">toUTF8</a>().find(user.<a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html#159d088da6e8f2be1035b2341889254c">toUTF8</a>()) != std::string::npos)
<a name="l00032"></a>00032 <span class="keywordflow">return</span> result + <span class="stringliteral">"<span class='chat-highlight'>"</span> + <a class="code" href="classChatEvent.html#d51677c773550286593db4793dae62eb">message_</a> + <span class="stringliteral">"</span>"</span>;
<a name="l00033"></a>00033 <span class="keywordflow">else</span>
<a name="l00034"></a>00034 <span class="keywordflow">return</span> result + message_;
<a name="l00035"></a>00035 }
<a name="l00036"></a>00036 <span class="keywordflow">default</span>:
<a name="l00037"></a>00037 <span class="keywordflow">return</span> <span class="stringliteral">""</span>;
<a name="l00038"></a>00038 }
<a name="l00039"></a>00039 }
<a name="l00040"></a>00040
<a name="l00041"></a>00041
<a name="l00042"></a><a class="code" href="classSimpleChatServer.html#b333561090906f3cb1bf79acd4e9b9b4">00042</a> <a class="code" href="classSimpleChatServer.html#b333561090906f3cb1bf79acd4e9b9b4" title="Create a new chat server.">SimpleChatServer::SimpleChatServer</a>()
<a name="l00043"></a>00043 : chatEvent_(this)
<a name="l00044"></a>00044 { }
<a name="l00045"></a>00045
<a name="l00046"></a><a class="code" href="classSimpleChatServer.html#da0ab17464e1711ca15f30b381ede57c">00046</a> <span class="keywordtype">bool</span> <a class="code" href="classSimpleChatServer.html#da0ab17464e1711ca15f30b381ede57c" title="Try to login with given user name.">SimpleChatServer::login</a>(<span class="keyword">const</span> <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a>& user)
<a name="l00047"></a>00047 {
<a name="l00048"></a>00048 boost::mutex::scoped_lock lock(<a class="code" href="classSimpleChatServer.html#949e434d4255597989d74f17faca674b">mutex_</a>);
<a name="l00049"></a>00049
<a name="l00050"></a>00050 <span class="keywordflow">if</span> (<a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>.find(user) == <a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>.end()) {
<a name="l00051"></a>00051 <a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>.insert(user);
<a name="l00052"></a>00052
<a name="l00053"></a>00053 <a class="code" href="classSimpleChatServer.html#c6640dc1ee3ea640de38c1896452b91a">chatEvent_</a>.<a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1Signal.html#b9ec97260c79e7b546b7235ad2ffd627">emit</a>(<a class="code" href="classChatEvent.html" title="Encapsulate a chat event.">ChatEvent</a>(<a class="code" href="classChatEvent.html#fa84446dae17c08fe15cf932fc0b7d615173cb4baa818f95af4604d7a01e3ae6">ChatEvent::Login</a>, user));
<a name="l00054"></a>00054
<a name="l00055"></a>00055 <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00056"></a>00056 } <span class="keywordflow">else</span>
<a name="l00057"></a>00057 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00058"></a>00058 }
<a name="l00059"></a>00059
<a name="l00060"></a><a class="code" href="classSimpleChatServer.html#5d376e3ff441f24843be648dd420e027">00060</a> <span class="keywordtype">void</span> <a class="code" href="classSimpleChatServer.html#5d376e3ff441f24843be648dd420e027" title="Logout from the server.">SimpleChatServer::logout</a>(<span class="keyword">const</span> <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a>& user)
<a name="l00061"></a>00061 {
<a name="l00062"></a>00062 boost::mutex::scoped_lock lock(<a class="code" href="classSimpleChatServer.html#949e434d4255597989d74f17faca674b">mutex_</a>);
<a name="l00063"></a>00063
<a name="l00064"></a>00064 UserSet::iterator i = <a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>.find(user);
<a name="l00065"></a>00065
<a name="l00066"></a>00066 <span class="keywordflow">if</span> (i != <a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>.end()) {
<a name="l00067"></a>00067 <a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>.erase(i);
<a name="l00068"></a>00068
<a name="l00069"></a>00069 <a class="code" href="classSimpleChatServer.html#c6640dc1ee3ea640de38c1896452b91a">chatEvent_</a>.<a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1Signal.html#b9ec97260c79e7b546b7235ad2ffd627">emit</a>(<a class="code" href="classChatEvent.html" title="Encapsulate a chat event.">ChatEvent</a>(<a class="code" href="classChatEvent.html#fa84446dae17c08fe15cf932fc0b7d61a8b11c1512738c200b2cfe1facd1c171">ChatEvent::Logout</a>, user));
<a name="l00070"></a>00070 }
<a name="l00071"></a>00071 }
<a name="l00072"></a>00072
<a name="l00073"></a><a class="code" href="classSimpleChatServer.html#bf692567706118193111c2b2589c8d3f">00073</a> <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a> <a class="code" href="classSimpleChatServer.html#bf692567706118193111c2b2589c8d3f" title="Get a suggestion for a guest user name.">SimpleChatServer::suggestGuest</a>()
<a name="l00074"></a>00074 {
<a name="l00075"></a>00075 boost::mutex::scoped_lock lock(<a class="code" href="classSimpleChatServer.html#949e434d4255597989d74f17faca674b">mutex_</a>);
<a name="l00076"></a>00076
<a name="l00077"></a>00077 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1;; ++i) {
<a name="l00078"></a>00078 std::string s = <span class="stringliteral">"guest "</span> + boost::lexical_cast<std::string>(i);
<a name="l00079"></a>00079 <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a> ss = s;
<a name="l00080"></a>00080
<a name="l00081"></a>00081 <span class="keywordflow">if</span> (<a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>.find(ss) == <a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>.end())
<a name="l00082"></a>00082 <span class="keywordflow">return</span> ss;
<a name="l00083"></a>00083 }
<a name="l00084"></a>00084 }
<a name="l00085"></a>00085
<a name="l00086"></a><a class="code" href="classSimpleChatServer.html#ab42074f7904397b3547a6df89c16d9d">00086</a> <span class="keywordtype">void</span> <a class="code" href="classSimpleChatServer.html#ab42074f7904397b3547a6df89c16d9d" title="Send a message on behalve of a user.">SimpleChatServer::sendMessage</a>(<span class="keyword">const</span> <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a>& user, <span class="keyword">const</span> <a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1WString.html">WString</a>& message)
<a name="l00087"></a>00087 {
<a name="l00088"></a>00088 boost::mutex::scoped_lock lock(<a class="code" href="classSimpleChatServer.html#949e434d4255597989d74f17faca674b">mutex_</a>);
<a name="l00089"></a>00089
<a name="l00090"></a>00090 <a class="code" href="classSimpleChatServer.html#c6640dc1ee3ea640de38c1896452b91a">chatEvent_</a>.<a class="codeRef" doxygen="wt.tags:../../reference/html/" href="../../reference/html/classWt_1_1Signal.html#b9ec97260c79e7b546b7235ad2ffd627">emit</a>(<a class="code" href="classChatEvent.html" title="Encapsulate a chat event.">ChatEvent</a>(user, message));
<a name="l00091"></a>00091 }
<a name="l00092"></a>00092
<a name="l00093"></a><a class="code" href="classSimpleChatServer.html#40f5c6465b1edb44278be93c97a29a39">00093</a> <a class="code" href="classSimpleChatServer.html#911329f24472ca7ed245ab18611488d4" title="Typedef for a collection of user names.">SimpleChatServer::UserSet</a> <a class="code" href="classSimpleChatServer.html#40f5c6465b1edb44278be93c97a29a39" title="Get the users currently logged in.">SimpleChatServer::users</a>()
<a name="l00094"></a>00094 {
<a name="l00095"></a>00095 <span class="keywordflow">return</span> <a class="code" href="classSimpleChatServer.html#e6712308cbb1f6b1ff4024630ea1d20c">users_</a>;
<a name="l00096"></a>00096 }
</pre></div></div>
<hr size="1"><address style="align: right;"><small>
Generated on Fri Mar 26 17:12:12 2010 for <a href="http://www.webtoolkit.eu/wt/">Wt</a> by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6</small></address>
</body>
</html>
|