Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 56ebf73a8cc21535297a85df4640d63b > files > 173

libccrtp0.7-0.7.2-1mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta name="robots" content="noindex">
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Example Documentation</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body bgcolor="#ffffff">
<!-- Generated by Doxygen 1.2.5 on Fri Oct 5 10:28:25 2001 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; <a class="qindex" href="pages.html">Related Pages</a> &nbsp; <a class="qindex" href="examples.html">Examples</a> &nbsp; </center>
<hr><h1>rtpduphello.cpp</h1>
<p>
<div class="fragment"><pre><font class="comment">// rtpduphello. </font>
<font class="comment">// A very simple program for testing and illustrating basic features of ccRTP.</font>
<font class="comment">// Copyright (C) 2001  Federico Montesino &lt;p5087@quintero.fie.us.es&gt;</font>
<font class="comment">//  </font>
<font class="comment">// This program is free software; you can redistribute it and/or modify</font>
<font class="comment">// it under the terms of the GNU General Public License as published by</font>
<font class="comment">// the Free Software Foundation; either version 2 of the License, or</font>
<font class="comment">// (at your option) any later version.</font>
<font class="comment">//  </font>
<font class="comment">// This program is distributed in the hope that it will be useful,</font>
<font class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</font>
<font class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</font>
<font class="comment">// GNU General Public License for more details.</font>
<font class="comment">//  </font>
<font class="comment">// You should have received a copy of the GNU General Public License</font>
<font class="comment">// along with this program; if not, write to the Free Software</font>
<font class="comment">//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</font>


<font class="comment">// This is an introductory example file that illustrates basic usage</font>
<font class="comment">// of ccRTP. You will also see a bit on how to use CommonC++ threads.</font>

<font class="comment">// It is a typical hello world program. It consists of tow duplex</font>
<font class="comment">// connections that talk each other through RTP packets. They do not</font>
<font class="comment">// say more than a typical salutation message. They both send and</font>
<font class="comment">// receive messages, and print the messages they receive.</font>


<font class="comment">// In order to use ccRTP, the RTP stack of CommonC++, you only need to</font>
<font class="comment">// include ...</font>
<font class="preprocessor">#include &lt;cc++/<a class="code" href="rtp_h.html">rtp.h</a>&gt;</font>

<font class="keyword">class </font>ccRTP_dupHello: <font class="keyword">public</font> <a name="_a0"></a><a class="code" href="class_Thread.html">Thread</a>
{
<font class="keyword">private</font>:
        <font class="comment">// There will be two duplex connections. They both will send</font>
        <font class="comment">// and receive packets.</font>
        <a name="_a1"></a><a class="code" href="class_RTPDuplex.html">RTPDuplex</a> *duplexA, *duplexB;
        
<font class="keyword">public</font>:
        <font class="comment">// Destructor.</font>
        ~ccRTP_dupHello()<font class="keyword"></font>{
                Terminate();
                <font class="keyword">delete</font> duplexA;
                <font class="keyword">delete</font> duplexB;  
        }
        
        <font class="comment">// Constructor.</font>
        ccRTP_dupHello()<font class="keyword"></font>{
                duplexA = duplexB = NULL;
        }

        <font class="comment">// This method does almost everything.</font>
        <font class="keywordtype">void</font> Run(<font class="keywordtype">void</font>)<font class="keyword"></font>{    
                <font class="comment">// redefined from Thread.</font>
                
                <font class="comment">// Before using ccRTP you should learn something about other</font>
                <font class="comment">// CommonC++ classes. We need InetHostAddress...</font>

                <font class="comment">// Construct loopback address</font>
                InetHostAddress local_ip;
                local_ip = <font class="stringliteral">"127.0.0.1"</font>;
                
                <font class="comment">// Is that correct?</font>
                <font class="keywordflow">if</font>( ! local_ip ){  
                <font class="comment">// this is equivalent to `! local_ip.isInetAddress()'</font>
                        cerr &lt;&lt; <font class="stringliteral">": IP address is not correct!"</font> &lt;&lt; endl;
                        exit(1);
                }
                
                cout &lt;&lt; local_ip.getHostname() &lt;&lt; 
                        <font class="stringliteral">" is going to talk to perself through "</font> &lt;&lt;
                        local_ip &lt;&lt; <font class="stringliteral">"..."</font> &lt;&lt; endl;
                
                
                <font class="comment">// ____Here comes the real RTP stuff____</font>
                
                <font class="comment">// Construct two RTPSocket. 22222 will be the base</font>
                <font class="comment">// port of A.  33334 will be the base port of B.</font>
                <font class="keyword">const</font> <font class="keywordtype">int</font> A_BASE = 22222;
                <font class="keyword">const</font> <font class="keywordtype">int</font> B_BASE = 33334;

                duplexA = <font class="keyword">new</font> RTPDuplex(local_ip,A_BASE,B_BASE,0);
                
                duplexB = <font class="keyword">new</font> RTPDuplex(local_ip,B_BASE,A_BASE,0);

                <font class="comment">// Set up A's connection</font>
                duplexA-&gt;<a name="a2"></a><a class="code" href="class_RTPQueue.html#a21">setTimeout</a>(90000);
                duplexA-&gt;<a name="a3"></a><a class="code" href="class_RTPQueue.html#a22">setExpired</a>(2500000);
                <font class="keywordflow">if</font>( duplexA-&gt;<a name="a4"></a><a class="code" href="class_RTPDuplex.html#a2">Connect</a>(local_ip,B_BASE) &lt; 0 )
                        cerr &lt;&lt; <font class="stringliteral">"Duplex A could not connect."</font>;
                
                <font class="comment">// Set up B's connection</font>
                duplexB-&gt;<a class="code" href="class_RTPQueue.html#a21">setTimeout</a>(160000);  
                duplexB-&gt;<a class="code" href="class_RTPQueue.html#a22">setExpired</a>(3500000);
                <font class="keywordflow">if</font>( duplexB-&gt;<a class="code" href="class_RTPDuplex.html#a2">Connect</a>(local_ip,A_BASE) &lt; 0 )
                        cerr &lt;&lt; <font class="stringliteral">"Duplex B could not connect."</font>;
                
                <font class="comment">// Let's check the queues  (you should read the documentation</font>
                <font class="comment">// so that you know what the queues are for).</font>
                
                <font class="keywordflow">if</font>( duplexA-&gt;<a name="a5"></a><a class="code" href="class_RTPQueue.html#a20">RTPQueue::isActive</a>() )
                        cout &lt;&lt; <font class="stringliteral">"The queue A is active."</font> &lt;&lt; endl;
                <font class="keywordflow">else</font>
                        cerr &lt;&lt; <font class="stringliteral">"The queue  A is not active."</font> &lt;&lt; endl;
                
                <font class="keywordflow">if</font>( duplexB-&gt;<a class="code" href="class_RTPQueue.html#a20">RTPQueue::isActive</a>() )
                        cout &lt;&lt; <font class="stringliteral">"The queue B is active."</font> &lt;&lt; endl;
                <font class="keywordflow">else</font>
                        cerr &lt;&lt; <font class="stringliteral">"The queue B is not active."</font> &lt;&lt; endl;
                

                cout &lt;&lt; <font class="stringliteral">"Transmitting..."</font> &lt;&lt; endl;
                
                <font class="comment">// This message will be sent on RTP packets, from A to</font>
                <font class="comment">// B and from B to A.</font>
                <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> helloA[] = <font class="stringliteral">"Hello, brave gnu world A!"</font>;
                <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> helloB[] = <font class="stringliteral">"Hello, brave gnu world B!"</font>;

                <font class="comment">// This is not important</font>
                time_t sending_time;
                time_t receiving_time;
                <font class="keywordtype">char</font> tmstring[30];

                <font class="comment">// This is the main loop, where packets are sent and receipt.</font>
                <font class="comment">// A and B both will send and receive packets.</font>
                <font class="keywordflow">for</font>( <font class="keywordtype">int</font> i=0 ; <font class="keyword">true</font> ; i++ ){

                        <font class="comment">// A and B do almost exactly the same things,</font>
                        <font class="comment">// I have kept this here -out of a send/receive</font>
                        <font class="comment">// method- in the interest of clarity.</font>

                        <font class="comment">// A: Send an RTP packet                        </font>
                        sending_time = time(NULL);
                        duplexA-&gt;<a name="a6"></a><a class="code" href="class_RTPQueue.html#a4">putPacket</a>(2*(i)*90000,RTP_PAYLOAD_MP2T,helloA,
                                          strlen((<font class="keywordtype">char</font> *)helloA));
                        <font class="comment">// Tell it</font>
                        strftime(tmstring,30,<font class="stringliteral">"%X"</font>,localtime(&amp;sending_time));
                        cout &lt;&lt; <font class="stringliteral">"A: sending message at "</font> &lt;&lt; tmstring &lt;&lt; <font class="stringliteral">"..."</font> 
                             &lt;&lt; endl;

                        <font class="comment">// A: Receive an RTP packet</font>
                        <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> bufferA[50] = <font class="stringliteral">"empty"</font>;
                        receiving_time = time(NULL);
                        duplexA-&gt;<a name="a7"></a><a class="code" href="class_RTPQueue.html#a8">getPacket</a>(duplexA-&gt;<a name="a8"></a><a class="code" href="class_RTPQueue.html#a6">getFirstTimestamp</a>(),
                                           bufferA,50);
                        <font class="comment">// Tell it</font>
                        strftime(tmstring,30,<font class="stringliteral">"%X"</font>,localtime(&amp;receiving_time));
                        cout &lt;&lt; <font class="stringliteral">"A:[receiving at "</font> &lt;&lt; tmstring &lt;&lt; <font class="stringliteral">"]: "</font> &lt;&lt; 
                                bufferA &lt;&lt; endl;

                        <font class="comment">// Wait for 0.1 seconds</font>
                        ccxx_sleep(100);

                        <font class="comment">// B: Send an RTP packet                        </font>
                        sending_time = time(NULL);
                        duplexB-&gt;<a class="code" href="class_RTPQueue.html#a4">putPacket</a>(2*(i)*90000,RTP_PAYLOAD_MP2T,helloB,
                                          strlen((<font class="keywordtype">char</font> *)helloB));
                        <font class="comment">// Tell it</font>
                        strftime(tmstring,30,<font class="stringliteral">"%X"</font>,localtime(&amp;sending_time));
                        cout &lt;&lt; <font class="stringliteral">"B: sending message at "</font> &lt;&lt; tmstring &lt;&lt; <font class="stringliteral">"..."</font> 
                             &lt;&lt; endl;

                        <font class="comment">// B: Receive an RTP packet</font>
                        <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> bufferB[50] = <font class="stringliteral">"empty"</font>;
                        receiving_time = time(NULL);
                        duplexB-&gt;<a class="code" href="class_RTPQueue.html#a8">getPacket</a>(duplexB-&gt;<a class="code" href="class_RTPQueue.html#a6">getFirstTimestamp</a>(),
                                           bufferB,50);
                        <font class="comment">// Tell it</font>
                        strftime(tmstring,30,<font class="stringliteral">"%X"</font>,localtime(&amp;receiving_time));
                        cout &lt;&lt; <font class="stringliteral">"B:[receiving at "</font> &lt;&lt; tmstring &lt;&lt; <font class="stringliteral">"]: "</font> &lt;&lt; 
                                bufferB &lt;&lt; endl;

                        ccxx_sleep(1900);
                }

        }
};

<font class="keywordtype">void</font> main(<font class="keywordtype">int</font> argc, <font class="keywordtype">char</font> *argv[])<font class="keyword"></font>{

        <font class="comment">// Construct the main thread. It will not run yet.</font>
        ccRTP_dupHello *hello = <font class="keyword">new</font> ccRTP_dupHello;
        
        cout &lt;&lt; <font class="stringliteral">"This is rtpduphello, a very simple test program for ccRTP."</font> 
             &lt;&lt; endl &lt;&lt; <font class="stringliteral">"Strike [Enter] when you are fed up."</font> &lt;&lt; endl;
        
        <font class="comment">// Start execution of hello.</font>
        hello-&gt;Start();
        
        cin.get();

        cout &lt;&lt; endl &lt;&lt; <font class="stringliteral">"That's all"</font> &lt;&lt; endl;

        <font class="keyword">delete</font> hello;

        exit(0);
}

</div></pre><hr><address><small>Generated at Fri Oct 5 10:28:25 2001 for ccRTP by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.gif" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.5 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>,
 &copy;&nbsp;1997-2001</small></address>
</body>
</html>