<!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>ccRTP: rtpduphello.cpp</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.4.4 --> <div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a> | <a class="qindex" href="pages.html">Related Pages</a> | <a class="qindex" href="examples.html">Examples</a></div> <h1>rtpduphello.cpp</h1>A basic example of how to use <a class="el" href="class_r_t_p_duplex.html">RTPDuplex</a><p> <div class="fragment"><pre class="fragment"><span class="comment">// rtpduphello. </span> <span class="comment">// A very simple program for testing and illustrating basic features of ccRTP.</span> <span class="comment">// Copyright (C) 2001,2002 Federico Montesino <fedemp@altern.org></span> <span class="comment">// </span> <span class="comment">// This program is free software; you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU General Public License as published by</span> <span class="comment">// the Free Software Foundation; either version 2 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">// </span> <span class="comment">// This program is distributed in the hope that it will be useful,</span> <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment">// GNU General Public License for more details.</span> <span class="comment">// </span> <span class="comment">// You should have received a copy of the GNU General Public License</span> <span class="comment">// along with this program; if not, write to the Free Software</span> <span class="comment">// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span> <span class="comment">// This is an introductory example file that illustrates basic usage</span> <span class="comment">// of ccRTP. You will also see a bit on how to use CommonC++ threads.</span> <span class="comment">// It is a typical hello world program. It consists of tow duplex</span> <span class="comment">// connections that talk each other through RTP packets. They do not</span> <span class="comment">// say more than a typical salutation message. They both send and</span> <span class="comment">// receive messages, and print the messages they receive.</span> <span class="preprocessor">#include <cstdio></span> <span class="preprocessor">#include <cstdlib></span> <span class="comment">// In order to use ccRTP, the RTP stack of CommonC++, you only need to</span> <span class="comment">// include ...</span> <span class="preprocessor">#include <<a class="code" href="ext_8h.html">ccrtp/ext.h</a>></span> <span class="preprocessor">#ifdef CCXX_NAMESPACES</span> <span class="preprocessor"></span><span class="keyword">using</span> <span class="keyword">namespace </span>ost; <span class="keyword">using</span> <span class="keyword">namespace </span>std; <span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="keyword">class </span>ccRTP_dupHello: <span class="keyword">public</span> Thread { <span class="keyword">private</span>: <span class="comment">// There will be two duplex connections. They both will send</span> <span class="comment">// and receive packets.</span> <a name="_a33"></a><a class="code" href="class_r_t_p_duplex.html">RTPDuplex</a> *duplexA, *duplexB; <span class="keyword">public</span>: <span class="comment">// Destructor.</span> ~ccRTP_dupHello(){ terminate(); <span class="keyword">delete</span> duplexA; <span class="keyword">delete</span> duplexB; } <span class="comment">// Constructor.</span> ccRTP_dupHello() : duplexA(NULL), duplexB(NULL) { } <span class="comment">// This method does almost everything.</span> <span class="keywordtype">void</span> run(<span class="keywordtype">void</span>){ <span class="comment">// redefined from Thread.</span> <span class="comment">// Before using ccRTP you should learn something about other</span> <span class="comment">// CommonC++ classes. We need InetHostAddress...</span> <span class="comment">// Construct loopback address</span> InetHostAddress local_ip; local_ip = <span class="stringliteral">"127.0.0.1"</span>; <span class="comment">// Is that correct?</span> <span class="keywordflow">if</span>( ! local_ip ){ <span class="comment">// this is equivalent to `! local_ip.isInetAddress()'</span> cerr << <span class="stringliteral">": IP address is not correct!"</span> << endl; exit(); } cout << local_ip.getHostname() << <span class="stringliteral">" is going to talk to perself through "</span> << local_ip << <span class="stringliteral">"..."</span> << endl; <span class="comment">// ____Here comes the real RTP stuff____</span> <span class="comment">// Construct two RTPSocket. 22222 will be the base</span> <span class="comment">// port of A. 33334 will be the base port of B.</span> <span class="keyword">const</span> <span class="keywordtype">int</span> A_BASE = 22222; <span class="keyword">const</span> <span class="keywordtype">int</span> B_BASE = 33334; duplexA = <span class="keyword">new</span> <a class="code" href="class_r_t_p_duplex.html">RTPDuplex</a>(local_ip,A_BASE,B_BASE); duplexB = <span class="keyword">new</span> <a class="code" href="class_r_t_p_duplex.html">RTPDuplex</a>(local_ip,B_BASE,A_BASE); <span class="comment">// Set up A's connection</span> duplexA->setSchedulingTimeout(90000); duplexA->setExpireTimeout(2500000); <span class="keywordflow">if</span>( duplexA->connect(local_ip,B_BASE) < 0 ) cerr << <span class="stringliteral">"Duplex A could not connect."</span>; <span class="comment">// Set up B's connection</span> duplexB->setSchedulingTimeout(160000); duplexB->setExpireTimeout(3500000); <span class="keywordflow">if</span>( duplexB->connect(local_ip,A_BASE) < 0 ) cerr << <span class="stringliteral">"Duplex B could not connect."</span>; <span class="comment">// Let's check the queues (you should read the documentation</span> <span class="comment">// so that you know what the queues are for).</span> <span class="keywordflow">if</span>( duplexA->RTPDataQueue::isActive() ) cout << <span class="stringliteral">"The queue A is active."</span> << endl; <span class="keywordflow">else</span> cerr << <span class="stringliteral">"The queue A is not active."</span> << endl; <span class="keywordflow">if</span>( duplexB->RTPDataQueue::isActive() ) cout << <span class="stringliteral">"The queue B is active."</span> << endl; <span class="keywordflow">else</span> cerr << <span class="stringliteral">"The queue B is not active."</span> << endl; cout << <span class="stringliteral">"Transmitting..."</span> << endl; <span class="comment">// This message will be sent on RTP packets, from A to</span> <span class="comment">// B and from B to A.</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> helloA[] = <span class="stringliteral">"Hello, brave gnu world from A!"</span>; <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> helloB[] = <span class="stringliteral">"Hello, brave gnu world from B!"</span>; <span class="comment">// This is not important</span> time_t sending_time; time_t receiving_time; <span class="keywordtype">char</span> tmstring[30]; <a name="_a34"></a><a class="code" href="class_static_payload_format.html">StaticPayloadFormat</a> pf = <a name="a35"></a><a class="code" href="group__payload.html#gga2a26">sptMP2T</a>; duplexA->setPayloadFormat(pf); duplexB->setPayloadFormat(pf); <span class="comment">// This is the main loop, where packets are sent and receipt.</span> <span class="comment">// A and B both will send and receive packets.</span> <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0 ; true ; i++ ){ <span class="comment">// A and B do almost exactly the same things,</span> <span class="comment">// I have kept this here -out of a send/receive</span> <span class="comment">// method- in the interest of clarity.</span> <span class="comment">// A: Send an RTP packet </span> sending_time = time(NULL); duplexA->putData(2*(i)*90000,helloA, strlen((<span class="keywordtype">char</span> *)helloA)); <span class="comment">// Tell it</span> strftime(tmstring,30,<span class="stringliteral">"%X"</span>,localtime(&sending_time)); cout << <span class="stringliteral">"A: sending message at "</span> << tmstring << <span class="stringliteral">"..."</span> << endl; <span class="comment">// A: Receive an RTP packet</span> receiving_time = time(NULL); <span class="keyword">const</span> <a name="_a36"></a><a class="code" href="class_app_data_unit.html">AppDataUnit</a>* aduA = duplexA->getData(duplexA->getFirstTimestamp()); <span class="keywordflow">if</span> ( aduA ) { <span class="comment">// Tell it</span> strftime(tmstring,30,<span class="stringliteral">"%X"</span>,localtime(&receiving_time)); cout << <span class="stringliteral">"A:[receiving at "</span> << tmstring << <span class="stringliteral">"]: "</span> << aduA-><a name="a37"></a><a class="code" href="class_app_data_unit.html#a5">getData</a>() << endl; } <span class="comment">// Wait for 0.1 seconds</span> Thread::sleep(100); <span class="comment">// B: Send an RTP packet </span> sending_time = time(NULL); duplexB->putData(2*(i)*90000,helloB, strlen((<span class="keywordtype">char</span> *)helloB)); <span class="comment">// Tell it</span> strftime(tmstring,30,<span class="stringliteral">"%X"</span>,localtime(&sending_time)); cout << <span class="stringliteral">"B: sending message at "</span> << tmstring << <span class="stringliteral">"..."</span> << endl; <span class="comment">// B: Receive an RTP packet</span> receiving_time = time(NULL); <span class="keyword">const</span> <a class="code" href="class_app_data_unit.html">AppDataUnit</a>* aduB = duplexB->getData(duplexB->getFirstTimestamp()); <span class="keywordflow">if</span> ( aduB ) { <span class="comment">// Tell it</span> strftime(tmstring,30,<span class="stringliteral">"%X"</span>,localtime(&receiving_time)); cout << <span class="stringliteral">"B:[receiving at "</span> << tmstring << <span class="stringliteral">"]: "</span> << aduB-><a class="code" href="class_app_data_unit.html#a5">getData</a>() << endl; } Thread::sleep(1900); } } }; <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) { <span class="comment">// Construct the main thread. It will not run yet.</span> ccRTP_dupHello *hello = <span class="keyword">new</span> ccRTP_dupHello; cout << <span class="stringliteral">"This is rtpduphello, a very simple test program for ccRTP."</span> << endl << <span class="stringliteral">"Strike [Enter] when you are fed up."</span> << endl; <span class="comment">// Start execution of hello.</span> hello->start(); cin.get(); cout << endl << <span class="stringliteral">"That's all"</span> << endl; <span class="keyword">delete</span> hello; exit(0); } </pre></div> <hr size="1"><address style="align: right;"><small>Generated on Fri Dec 9 22:32:03 2005 for ccRTP by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.4 </small></address> </body> </html>