<!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>GNU CommonC++: tcpservice.cpp</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.4.6 --> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="classes.html"><span>Classes</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="examples.html"><span>Examples</span></a></li> </ul></div> <h1>tcpservice.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">//</span> <a name="l00002"></a>00002 <span class="comment">// tcpservice.cpp</span> <a name="l00003"></a>00003 <span class="comment">//</span> <a name="l00004"></a>00004 <span class="comment">// Copyright 2000 - Gianni Mariani <gianni@mariani.ws></span> <a name="l00005"></a>00005 <span class="comment">//</span> <a name="l00006"></a>00006 <span class="comment">// An example of a simple chatty server using CommonC++.</span> <a name="l00007"></a>00007 <span class="comment">//</span> <a name="l00008"></a>00008 <span class="comment">// This simple application basically operates as a</span> <a name="l00009"></a>00009 <span class="comment">// very simple chat system. From a telnet session</span> <a name="l00010"></a>00010 <span class="comment">// on localhost:3999 , any messages typed from a telnet</span> <a name="l00011"></a>00011 <span class="comment">// client are written to all participating sessions.</span> <a name="l00012"></a>00012 <span class="comment">//</span> <a name="l00013"></a>00013 <span class="comment">// This is free software licensed under the terms of the GNU</span> <a name="l00014"></a>00014 <span class="comment">// Public License</span> <a name="l00015"></a>00015 <span class="comment">//</span> <a name="l00016"></a>00016 <span class="comment">// This example:</span> <a name="l00017"></a>00017 <span class="comment">//</span> <a name="l00018"></a>00018 <span class="comment">// This demostrates a simple threaded server, actually,</span> <a name="l00019"></a>00019 <span class="comment">// the sessions are not all threaded though they could be</span> <a name="l00020"></a>00020 <span class="comment">// if that's what you wanted. Basically it demonstrates the</span> <a name="l00021"></a>00021 <span class="comment">// use of SocketService, SocketPorts and Threads.</span> <a name="l00022"></a>00022 <span class="comment">//</span> <a name="l00023"></a>00023 <span class="comment">// For those familiar with Unix network programming, SocketService</span> <a name="l00024"></a>00024 <span class="comment">// basically encapsulates all the work to communicate with</span> <a name="l00025"></a>00025 <span class="comment">// the select() or poll() system calls. SocketPorts are</span> <a name="l00026"></a>00026 <span class="comment">// basically encapsulations of sessions or open file descriptors.</span> <a name="l00027"></a>00027 <span class="comment">//</span> <a name="l00028"></a>00028 <span class="comment">// Anyhow, this example is a very simple echo server but</span> <a name="l00029"></a>00029 <span class="comment">// it echos to all connected clients. So it's a poor man's</span> <a name="l00030"></a>00030 <span class="comment">// IRC ! You connect via telnet to localhost port 3999 and</span> <a name="l00031"></a>00031 <span class="comment">// it will echo to all other connected clients what you type in !</span> <a name="l00032"></a>00032 <span class="comment">//</span> <a name="l00033"></a>00033 <a name="l00034"></a>00034 <span class="preprocessor">#include <<a class="code" href="socketport_8h.html">cc++/socketport.h</a>></span> <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="preprocessor">#include <iostream></span> <a name="l00037"></a>00037 <a name="l00038"></a>00038 <span class="comment">// For starters, we need a thread safe list, we'll make one</span> <a name="l00039"></a>00039 <span class="comment">// out of the STL list<> template -</span> <a name="l00040"></a>00040 <span class="comment">// http://www.sgi.com/Technology/STL/index.html</span> <a name="l00041"></a>00041 <span class="comment">//</span> <a name="l00042"></a>00042 <span class="comment">// Thread safe list class</span> <a name="l00043"></a>00043 <span class="comment">//</span> <a name="l00044"></a>00044 <span class="preprocessor">#include <list></span> <a name="l00045"></a>00045 <a name="l00046"></a>00046 <span class="preprocessor">#ifdef CCXX_NAMESPACES</span> <a name="l00047"></a>00047 <span class="preprocessor"></span><span class="keyword">using namespace </span>std; <a name="l00048"></a>00048 <span class="keyword">using namespace </span>ost; <a name="l00049"></a>00049 <span class="preprocessor">#endif</span> <a name="l00050"></a>00050 <span class="preprocessor"></span> <a name="l00051"></a>00051 <span class="keyword">class </span>ts_list_item; <a name="l00052"></a>00052 <span class="keyword">typedef</span> list<ts_list_item *> ts_list; <a name="l00053"></a>00053 <a name="l00054"></a>00054 <span class="comment">// a list head - containing a list and a Mutex.</span> <a name="l00055"></a>00055 <span class="comment">// It would be really nice to teach stl to do this.</span> <a name="l00056"></a>00056 <a name="l00057"></a>00057 <span class="keyword">class </span>ts_list_head { <a name="l00058"></a>00058 <span class="keyword">public</span>: <a name="l00059"></a>00059 <a name="l00060"></a>00060 <span class="comment">// No point inheriting, I'd have to implement</span> <a name="l00061"></a>00061 <span class="comment">// alot of code. We'll hold off on that exercise.</span> <a name="l00062"></a>00062 <a name="l00063"></a>00063 <span class="comment">// Using the CommonC++ Mutex class.</span> <a name="l00064"></a>00064 <a name="_a45"></a><a class="code" href="classost_1_1_mutex.html">Mutex</a> linkmutex; <a name="l00065"></a>00065 <span class="comment">// And the STL template.</span> <a name="l00066"></a>00066 ts_list list_o_items; <a name="l00067"></a>00067 <a name="l00068"></a>00068 <span class="comment">// Not nessasary, but nice to be explicit.</span> <a name="l00069"></a>00069 ts_list_head() <a name="l00070"></a>00070 : linkmutex(), list_o_items() <a name="l00071"></a>00071 { <a name="l00072"></a>00072 } <a name="l00073"></a>00073 <a name="l00074"></a>00074 <span class="comment">// This thing knows how to remove and insert items.</span> <a name="l00075"></a>00075 <span class="keywordtype">void</span> RemoveListItem( ts_list_item * li ); <a name="l00076"></a>00076 <span class="keywordtype">void</span> InsertListItem( ts_list_item * li ); <a name="l00077"></a>00077 <a name="l00078"></a>00078 <span class="comment">// And it knows how to notify that it became empty</span> <a name="l00079"></a>00079 <span class="comment">// or an element was deleted and it was the last one.</span> <a name="l00080"></a>00080 <span class="keyword">virtual</span> <span class="keywordtype">void</span> ListDepleted() <a name="l00081"></a>00081 { <a name="l00082"></a>00082 } <a name="l00083"></a>00083 <a name="l00084"></a>00084 <span class="keyword">virtual</span> ~ts_list_head() <a name="l00085"></a>00085 { <a name="l00086"></a>00086 } <a name="l00087"></a>00087 }; <a name="l00088"></a>00088 <a name="l00089"></a>00089 <a name="l00090"></a>00090 <span class="comment">// This item knows how to remove itself from the</span> <a name="l00091"></a>00091 <span class="comment">// list it belongs to.</span> <a name="l00092"></a>00092 <span class="keyword">class </span>ts_list_item { <a name="l00093"></a>00093 <span class="keyword">public</span>: <a name="l00094"></a>00094 ts_list::iterator linkpoint; <a name="l00095"></a>00095 ts_list_head * listhead; <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="keyword">virtual</span> ~ts_list_item() <a name="l00098"></a>00098 { <a name="l00099"></a>00099 listhead->RemoveListItem( <span class="keyword">this</span> ); <a name="l00100"></a>00100 } <a name="l00101"></a>00101 <a name="l00102"></a>00102 ts_list_item( ts_list_head * head ) <a name="l00103"></a>00103 { <a name="l00104"></a>00104 listhead = head; <a name="l00105"></a>00105 head->InsertListItem( <span class="keyword">this</span> ); <a name="l00106"></a>00106 } <a name="l00107"></a>00107 }; <a name="l00108"></a>00108 <a name="l00109"></a>00109 <span class="keywordtype">void</span> ts_list_head::RemoveListItem( ts_list_item * li ) <a name="l00110"></a>00110 { <a name="l00111"></a>00111 <span class="keywordtype">bool</span> is_empty; <a name="l00112"></a>00112 linkmutex.enterMutex(); <a name="l00113"></a>00113 list_o_items.erase( li->linkpoint ); <a name="l00114"></a>00114 is_empty = list_o_items.empty(); <a name="l00115"></a>00115 linkmutex.leaveMutex(); <a name="l00116"></a>00116 <a name="l00117"></a>00117 <span class="comment">// There is a slim possibility that at this time</span> <a name="l00118"></a>00118 <span class="comment">// we recieve a connection.</span> <a name="l00119"></a>00119 <span class="keywordflow">if</span> ( is_empty ) { <a name="l00120"></a>00120 ListDepleted(); <a name="l00121"></a>00121 } <a name="l00122"></a>00122 } <a name="l00123"></a>00123 <a name="l00124"></a>00124 <span class="keywordtype">void</span> ts_list_head::InsertListItem( ts_list_item * li ) <a name="l00125"></a>00125 { <a name="l00126"></a>00126 linkmutex.enterMutex(); <a name="l00127"></a>00127 list_o_items.push_front( li ); <a name="l00128"></a>00128 li->linkpoint = list_o_items.begin(); <a name="l00129"></a>00129 linkmutex.leaveMutex(); <a name="l00130"></a>00130 } <a name="l00131"></a>00131 <a name="l00132"></a>00132 <span class="comment">// ChatterSession operates on the individual connections</span> <a name="l00133"></a>00133 <span class="comment">// from clients as are managed by the SocketService</span> <a name="l00134"></a>00134 <span class="comment">// contained in CCExec. ChatterThread simply waits in</span> <a name="l00135"></a>00135 <span class="comment">// a loop to create these, listening forever.</span> <a name="l00136"></a>00136 <span class="comment">//</span> <a name="l00137"></a>00137 <span class="comment">// Even though the SocketService contains a list of</span> <a name="l00138"></a>00138 <span class="comment">// clients it serves, it may actually serve more than</span> <a name="l00139"></a>00139 <span class="comment">// one type of session so we create our own list by</span> <a name="l00140"></a>00140 <span class="comment">// inheriting the ts_list_item.</span> <a name="l00141"></a>00141 <span class="comment">//</span> <a name="l00142"></a>00142 <a name="l00143"></a>00143 <span class="keyword">class </span>ChatterSession : <a name="l00144"></a>00144 <span class="keyword">public</span> <span class="keyword">virtual</span> <a name="_a46"></a><a class="code" href="classost_1_1_socket_port.html">SocketPort</a>, <a name="l00145"></a>00145 <span class="keyword">public</span> <span class="keyword">virtual</span> ts_list_item <a name="l00146"></a>00146 { <a name="l00147"></a>00147 <span class="keyword">public</span>: <a name="l00148"></a>00148 <a name="l00149"></a>00149 <span class="keyword">enum</span> { size_o_buf = 2048 }; <a name="l00150"></a>00150 <a name="l00151"></a>00151 <span class="comment">// Nothing special to do here, it's all handled</span> <a name="l00152"></a>00152 <span class="comment">// by SocketPort and ts_list_item</span> <a name="l00153"></a>00153 <a name="l00154"></a>00154 <span class="keyword">virtual</span> ~ChatterSession() <a name="l00155"></a>00155 { <a name="l00156"></a>00156 cerr << <span class="stringliteral">"ChatterSession deleted !\n"</span>; <a name="l00157"></a>00157 } <a name="l00158"></a>00158 <a name="l00159"></a>00159 <span class="comment">// When you create a ChatterSession it waits to accept a</span> <a name="l00160"></a>00160 <span class="comment">// connection. This is done by it's own</span> <a name="l00161"></a>00161 ChatterSession( <a name="l00162"></a>00162 <a name="_a47"></a><a class="code" href="classost_1_1_t_c_p_socket.html">TCPSocket</a> & server, <a name="l00163"></a>00163 <a name="_a48"></a><a class="code" href="classost_1_1_socket_service.html">SocketService</a> * svc, <a name="l00164"></a>00164 ts_list_head * head <a name="l00165"></a>00165 ) : <a name="l00166"></a>00166 <a class="code" href="classost_1_1_socket_port.html">SocketPort</a>( NULL, server ), <a name="l00167"></a>00167 ts_list_item( head ) <a name="l00168"></a>00168 { <a name="l00169"></a>00169 cerr << <span class="stringliteral">"ChatterSession Created\n"</span>; <a name="l00170"></a>00170 <a name="l00171"></a>00171 <a name="a49"></a><a class="code" href="namespaceost.html#3c74a1a40c359fd349f3e3e1b96ebfc7">tpport_t</a> port; <a name="l00172"></a>00172 <a name="a50"></a><a class="code" href="address_8h.html#5eba36aa908d5a479c8ba7d0bc4512b1">InetHostAddress</a> ia = <a name="a51"></a><a class="code" href="classost_1_1_socket.html#6fd1dffdfe9606e56fba838559ff6d67">getPeer</a>( & port ); <a name="l00173"></a>00173 <a name="l00174"></a>00174 cerr << <span class="stringliteral">"connecting from "</span> << ia.getHostname() << <a name="l00175"></a>00175 <span class="stringliteral">":"</span> << port << endl; <a name="l00176"></a>00176 <a name="l00177"></a>00177 <span class="comment">// Set up non-blocking reads</span> <a name="l00178"></a>00178 <a name="a52"></a><a class="code" href="classost_1_1_socket.html#16eb72794ce956b2af6b935eb59b2e24">setCompletion</a>( <span class="keyword">false</span> ); <a name="l00179"></a>00179 <a name="l00180"></a>00180 <span class="comment">// Set yerself to time out in 10 seconds</span> <a name="l00181"></a>00181 <a name="a53"></a><a class="code" href="classost_1_1_socket_port.html#c70f9eae6b6b3625c980fa84180e8151">setTimer</a>( 100000 ); <a name="l00182"></a>00182 <a name="a54"></a><a class="code" href="classost_1_1_socket_port.html#ed5fbff0b874da8e24add7f6cc50b65e">attach</a>(svc); <a name="l00183"></a>00183 } <a name="l00184"></a>00184 <a name="l00185"></a>00185 <span class="comment">//</span> <a name="l00186"></a>00186 <span class="comment">// This is called by the SocketService thread when it the</span> <a name="l00187"></a>00187 <span class="comment">// object has expired.</span> <a name="l00188"></a>00188 <span class="comment">//</span> <a name="l00189"></a>00189 <a name="l00190"></a>00190 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a55"></a><a class="code" href="classost_1_1_socket_port.html#4d09daed3fe5cb81259985db87add34d">expired</a>() <a name="l00191"></a>00191 { <a name="l00192"></a>00192 <span class="comment">// Get outa here - this guy is a LOOSER - type or terminate</span> <a name="l00193"></a>00193 cerr << <span class="stringliteral">"ChatterSession Expired\n"</span>; <a name="l00194"></a>00194 <span class="keyword">delete</span> <span class="keyword">this</span>; <a name="l00195"></a>00195 } <a name="l00196"></a>00196 <a name="l00197"></a>00197 <span class="comment">//</span> <a name="l00198"></a>00198 <span class="comment">// This is called by the SocketService thread when it detects</span> <a name="l00199"></a>00199 <span class="comment">// that there is somthing to read on this connection.</span> <a name="l00200"></a>00200 <span class="comment">//</span> <a name="l00201"></a>00201 <a name="l00202"></a>00202 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a name="a56"></a><a class="code" href="classost_1_1_socket_port.html#5e0ec242cfb737db369cbc63abcc06b6">pending</a>() <a name="l00203"></a>00203 { <a name="l00204"></a>00204 <span class="comment">// Implement the echo</span> <a name="l00205"></a>00205 <a name="l00206"></a>00206 cerr << <span class="stringliteral">"Pending called\n"</span>; <a name="l00207"></a>00207 <a name="l00208"></a>00208 <span class="comment">// reset the timer</span> <a name="l00209"></a>00209 <a class="code" href="classost_1_1_socket_port.html#c70f9eae6b6b3625c980fa84180e8151">setTimer</a>( 100000 ); <a name="l00210"></a>00210 <span class="keywordflow">try</span> { <a name="l00211"></a>00211 <span class="keywordtype">int</span> len; <a name="l00212"></a>00212 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> total = 0; <a name="l00213"></a>00213 <span class="keywordtype">char</span> buf[ size_o_buf ]; <a name="l00214"></a>00214 <a name="l00215"></a>00215 <span class="keywordflow">while</span> ( (len = <a name="a57"></a><a class="code" href="classost_1_1_socket_port.html#872c38a65b17113a580af345946146d7">receive</a>(buf, <span class="keyword">sizeof</span>(buf) )) > 0 ) { <a name="l00216"></a>00216 total += len; <a name="l00217"></a>00217 cerr << <span class="stringliteral">"Read '"</span>; <a name="l00218"></a>00218 cerr.write( buf, len ); <a name="l00219"></a>00219 cerr << <span class="stringliteral">"'\n"</span>; <a name="l00220"></a>00220 <a name="l00221"></a>00221 <span class="comment">// Send it to all the sessions.</span> <a name="l00222"></a>00222 <span class="comment">// We probably don't really want to lock the</span> <a name="l00223"></a>00223 <span class="comment">// entire list for the entire time.</span> <a name="l00224"></a>00224 <span class="comment">// The best way to do this would be to place the</span> <a name="l00225"></a>00225 <span class="comment">// message somewhere and use the service function.</span> <a name="l00226"></a>00226 <span class="comment">// But what are examples for ?</span> <a name="l00227"></a>00227 <a name="l00228"></a>00228 <span class="keywordtype">bool</span> sent = <span class="keyword">false</span>; <a name="l00229"></a>00229 listhead->linkmutex.enterMutex(); <a name="l00230"></a>00230 <span class="keywordflow">for</span> ( <a name="l00231"></a>00231 ts_list::iterator iter = listhead->list_o_items.begin(); <a name="l00232"></a>00232 iter != listhead->list_o_items.end(); <a name="l00233"></a>00233 iter ++ <a name="l00234"></a>00234 ) { <a name="l00235"></a>00235 ChatterSession * sess = <a name="l00236"></a>00236 dynamic_cast< ChatterSession * >( * iter ); <a name="l00237"></a>00237 <span class="keywordflow">if</span> ( sess != <span class="keyword">this</span> ) { <a name="l00238"></a>00238 sess->send( buf, len ); <a name="l00239"></a>00239 sent = <span class="keyword">true</span>; <a name="l00240"></a>00240 } <a name="l00241"></a>00241 } <a name="l00242"></a>00242 listhead->linkmutex.leaveMutex(); <a name="l00243"></a>00243 <a name="l00244"></a>00244 <span class="keywordflow">if</span> ( ! sent ) { <a name="l00245"></a>00245 <a name="a58"></a><a class="code" href="classost_1_1_socket_port.html#9218e4528f5bdf4215200e2040eefe5b">send</a>( <a name="l00246"></a>00246 ( <span class="keywordtype">void</span> * ) <span class="stringliteral">"No one else listening\n"</span>, <a name="l00247"></a>00247 <span class="keyword">sizeof</span>( <span class="stringliteral">"No one else listening\n"</span> ) - 1 <a name="l00248"></a>00248 ); <a name="l00249"></a>00249 <a name="l00250"></a>00250 <a class="code" href="classost_1_1_socket_port.html#9218e4528f5bdf4215200e2040eefe5b">send</a>( buf, len ); <a name="l00251"></a>00251 } <a name="l00252"></a>00252 } <a name="l00253"></a>00253 <span class="keywordflow">if</span> (total == 0) <a name="l00254"></a>00254 { <a name="l00255"></a>00255 cerr << <span class="stringliteral">"Broken connection!\n"</span> << endl; <a name="l00256"></a>00256 <span class="keyword">delete</span> <span class="keyword">this</span>; <a name="l00257"></a>00257 } <a name="l00258"></a>00258 } <a name="l00259"></a>00259 <span class="keywordflow">catch</span> ( ... ) <a name="l00260"></a>00260 { <a name="l00261"></a>00261 <span class="comment">// somthing wrong happened here !</span> <a name="l00262"></a>00262 cerr << <span class="stringliteral">"Socket port write sent an exception !\n"</span>; <a name="l00263"></a>00263 } <a name="l00264"></a>00264 <a name="l00265"></a>00265 } <a name="l00266"></a>00266 <a name="l00267"></a>00267 <span class="keyword">virtual</span> <span class="keywordtype">void</span> disconnect() <a name="l00268"></a>00268 { <a name="l00269"></a>00269 <span class="comment">// Called by the SocketService thread when the client</span> <a name="l00270"></a>00270 <span class="comment">// hangs up.</span> <a name="l00271"></a>00271 cerr << <span class="stringliteral">"ChatterSession disconnected!\n"</span>; <a name="l00272"></a>00272 <a name="l00273"></a>00273 <span class="keyword">delete</span> <span class="keyword">this</span>; <a name="l00274"></a>00274 } <a name="l00275"></a>00275 <a name="l00276"></a>00276 }; <a name="l00277"></a>00277 <a name="l00278"></a>00278 <span class="keyword">class </span>ChatterThread; <a name="l00279"></a>00279 <a name="l00280"></a>00280 <span class="comment">//</span> <a name="l00281"></a>00281 <span class="comment">// This is the main application object containing all the</span> <a name="l00282"></a>00282 <span class="comment">// state for the application. It uses a SocketService object</span> <a name="l00283"></a>00283 <span class="comment">// (and thread) to do all the work, however, that object could</span> <a name="l00284"></a>00284 <span class="comment">// theoretically be use by more than one main application.</span> <a name="l00285"></a>00285 <span class="comment">//</span> <a name="l00286"></a>00286 <span class="comment">// It creates a ChatterThread to sit and wait for connections</span> <a name="l00287"></a>00287 <span class="comment">// from clients.</span> <a name="l00288"></a>00288 <a name="l00289"></a>00289 <span class="keyword">class </span>CCExec : <span class="keyword">public</span> <span class="keyword">virtual</span> ts_list_head { <a name="l00290"></a>00290 <span class="keyword">public</span>: <a name="l00291"></a>00291 <a name="l00292"></a>00292 <a class="code" href="classost_1_1_socket_service.html">SocketService</a> * service; <a name="l00293"></a>00293 ChatterThread * my_Chatter; <a name="l00294"></a>00294 <a name="_a59"></a><a class="code" href="classost_1_1_semaphore.html">Semaphore</a> mainsem[1]; <a name="l00295"></a>00295 <a name="l00296"></a>00296 CCExec():my_Chatter(NULL) <a name="l00297"></a>00297 { <a name="l00298"></a>00298 service = <span class="keyword">new</span> <a name="a60"></a><a class="code" href="namespaceost.html#5b999accae7692fd2abbc9654a00eb8c">SocketService</a>( 0 ); <a name="l00299"></a>00299 } <a name="l00300"></a>00300 <a name="l00301"></a>00301 <span class="keyword">virtual</span> <span class="keywordtype">void</span> ListDepleted(); <a name="l00302"></a>00302 <a name="l00303"></a>00303 <span class="comment">// These methods defined later.</span> <a name="l00304"></a>00304 <span class="keyword">virtual</span> ~CCExec(); <a name="l00305"></a>00305 <span class="keywordtype">int</span> RunApp( <span class="keywordtype">char</span> * hn = <span class="stringliteral">"localhost"</span> ); <a name="l00306"></a>00306 <a name="l00307"></a>00307 }; <a name="l00308"></a>00308 <a name="l00309"></a>00309 <span class="comment">//</span> <a name="l00310"></a>00310 <span class="comment">// ChatterThread simply creates ChatterSession all the time until</span> <a name="l00311"></a>00311 <span class="comment">// it has an error. I suspect you could create as many of these</span> <a name="l00312"></a>00312 <span class="comment">// as the OS could take.</span> <a name="l00313"></a>00313 <span class="comment">//</span> <a name="l00314"></a>00314 <a name="l00315"></a>00315 <span class="keyword">class </span>ChatterThread : <span class="keyword">public</span> <span class="keyword">virtual</span> <a class="code" href="classost_1_1_t_c_p_socket.html">TCPSocket</a>, <span class="keyword">public</span> <span class="keyword">virtual</span> <a name="_a61"></a><a class="code" href="classost_1_1_thread.html">Thread</a> { <a name="l00316"></a>00316 <span class="keyword">public</span>: <a name="l00317"></a>00317 <a name="l00318"></a>00318 CCExec * exec; <a name="l00319"></a>00319 <a name="l00320"></a>00320 <span class="keywordtype">void</span> <a name="a62"></a><a class="code" href="classost_1_1_thread.html#dd7d339d94b8a1ed2b2b0324a95b7e74">run</a> () <a name="l00321"></a>00321 { <a name="l00322"></a>00322 <span class="keywordflow">while</span> ( 1 ) { <a name="l00323"></a>00323 <span class="keywordflow">try</span> { <a name="l00324"></a>00324 <span class="comment">// new does all the work to accept a new connection</span> <a name="l00325"></a>00325 <span class="comment">// attach itself to the SocketService AND include</span> <a name="l00326"></a>00326 <span class="comment">// itself in the CCExec list of sessions.</span> <a name="l00327"></a>00327 <span class="keyword">new</span> ChatterSession( <a name="l00328"></a>00328 * ( <a class="code" href="classost_1_1_t_c_p_socket.html">TCPSocket</a> * ) <span class="keyword">this</span>, <a name="l00329"></a>00329 exec->service, <a name="l00330"></a>00330 exec <a name="l00331"></a>00331 ); <a name="l00332"></a>00332 } <a name="l00333"></a>00333 <span class="keywordflow">catch</span> ( ... ) <a name="l00334"></a>00334 { <a name="l00335"></a>00335 <span class="comment">// Bummer - there was an error.</span> <a name="l00336"></a>00336 cerr << <span class="stringliteral">"ChatterSession create failed\n"</span>; <a name="l00337"></a>00337 <a name="a63"></a><a class="code" href="classost_1_1_thread.html#cd315d7c877b346970680d739da3e994">exit</a>(); <a name="l00338"></a>00338 } <a name="l00339"></a>00339 } <a name="l00340"></a>00340 } <a name="l00341"></a>00341 <a name="l00342"></a>00342 ChatterThread( <a name="l00343"></a>00343 <a class="code" href="address_8h.html#5eba36aa908d5a479c8ba7d0bc4512b1">InetHostAddress</a> & machine, <a name="l00344"></a>00344 <span class="keywordtype">int</span> port, <a name="l00345"></a>00345 CCExec * inexec <a name="l00346"></a>00346 <a name="l00347"></a>00347 ) : <a class="code" href="classost_1_1_t_c_p_socket.html">TCPSocket</a>( machine, port ), <a name="l00348"></a>00348 <a class="code" href="classost_1_1_thread.html">Thread</a>(), <a name="l00349"></a>00349 exec( inexec ) <a name="l00350"></a>00350 { <a name="l00351"></a>00351 <a name="a64"></a><a class="code" href="classost_1_1_thread.html#2ef198d7aefb93cf9de4fcc6b66c27a2">start</a>(); <a name="l00352"></a>00352 } <a name="l00353"></a>00353 <a name="l00354"></a>00354 <a name="l00355"></a>00355 }; <a name="l00356"></a>00356 <a name="l00357"></a>00357 <span class="comment">//</span> <a name="l00358"></a>00358 <span class="comment">// Bug here, this should go ahead and shut down all sessions</span> <a name="l00359"></a>00359 <span class="comment">// for application. An exercise left to the reader.</span> <a name="l00360"></a>00360 <a name="l00361"></a>00361 CCExec::~CCExec() <a name="l00362"></a>00362 { <a name="l00363"></a>00363 <span class="comment">// MUST delete my_Chatter first or it may end up using</span> <a name="l00364"></a>00364 <span class="comment">// a deleted service.</span> <a name="l00365"></a>00365 <span class="keywordflow">if</span> ( my_Chatter ) <span class="keyword">delete</span> my_Chatter; <a name="l00366"></a>00366 <a name="l00367"></a>00367 <span class="comment">// Delete whatever is left.</span> <a name="l00368"></a>00368 <span class="keyword">delete</span> service; <a name="l00369"></a>00369 } <a name="l00370"></a>00370 <a name="l00371"></a>00371 <span class="comment">//</span> <a name="l00372"></a>00372 <span class="comment">// Run App would normally read some config file or take some</span> <a name="l00373"></a>00373 <span class="comment">// parameters about which port to connect to and then</span> <a name="l00374"></a>00374 <span class="comment">// do that !</span> <a name="l00375"></a>00375 <span class="keywordtype">int</span> CCExec::RunApp( <span class="keywordtype">char</span> * hn ) <a name="l00376"></a>00376 { <a name="l00377"></a>00377 <span class="comment">// which port ?</span> <a name="l00378"></a>00378 <a name="l00379"></a>00379 <a class="code" href="address_8h.html#5eba36aa908d5a479c8ba7d0bc4512b1">InetHostAddress</a> machine( hn ); <a name="l00380"></a>00380 <a name="l00381"></a>00381 <span class="keywordflow">if</span> ( machine.isInetAddress() == false ) { <a name="l00382"></a>00382 cerr << <span class="stringliteral">"machine is not address"</span> << endl; <a name="l00383"></a>00383 } <a name="l00384"></a>00384 <a name="l00385"></a>00385 cerr << <span class="stringliteral">"machine is "</span> << machine.getHostname() << endl; <a name="l00386"></a>00386 <a name="l00387"></a>00387 <span class="comment">// Start accepting connections - this will bind to the</span> <a name="l00388"></a>00388 <span class="comment">// port as well.</span> <a name="l00389"></a>00389 <span class="keywordflow">try</span> { <a name="l00390"></a>00390 my_Chatter = <span class="keyword">new</span> ChatterThread( <a name="l00391"></a>00391 machine, <a name="l00392"></a>00392 3999, <a name="l00393"></a>00393 <span class="keyword">this</span> <a name="l00394"></a>00394 ); <a name="l00395"></a>00395 } <a name="l00396"></a>00396 <span class="keywordflow">catch</span> ( ... ) <a name="l00397"></a>00397 { <a name="l00398"></a>00398 cerr << <span class="stringliteral">"Failed to bind\n"</span>; <a name="l00399"></a>00399 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00400"></a>00400 } <a name="l00401"></a>00401 <a name="l00402"></a>00402 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00403"></a>00403 } <a name="l00404"></a>00404 <a name="l00405"></a>00405 <span class="comment">// When there is no one else connected - terminate !</span> <a name="l00406"></a>00406 <span class="keywordtype">void</span> CCExec::ListDepleted() <a name="l00407"></a>00407 { <a name="l00408"></a>00408 mainsem->post(); <a name="l00409"></a>00409 } <a name="l00410"></a>00410 <a name="l00411"></a>00411 <a name="l00412"></a>00412 <span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> ** argv ) <a name="l00413"></a>00413 { <a name="l00414"></a>00414 CCExec * server; <a name="l00415"></a>00415 <a name="l00416"></a>00416 server = <span class="keyword">new</span> CCExec(); <a name="l00417"></a>00417 <a name="l00418"></a>00418 <span class="comment">// take the first command line option as a hostname</span> <a name="l00419"></a>00419 <span class="comment">// to listen to.</span> <a name="l00420"></a>00420 <span class="keywordflow">if</span> ( argc > 1 ) { <a name="l00421"></a>00421 server->RunApp( argv[ 1 ] ); <a name="l00422"></a>00422 } <span class="keywordflow">else</span> { <a name="l00423"></a>00423 server->RunApp(); <a name="l00424"></a>00424 } <a name="l00425"></a>00425 <a name="l00426"></a>00426 server->mainsem->wait(); <a name="l00427"></a>00427 <a name="l00428"></a>00428 <span class="keyword">delete</span> server; <a name="l00429"></a>00429 <a name="l00430"></a>00430 <span class="keywordflow">return</span> 0; <a name="l00431"></a>00431 } </pre></div> <hr size="1"><address style="align: right;"><small>Generated on Mon Jun 19 10:50:55 2006 for GNU CommonC++ by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address> </body> </html>