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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// rtpduphello.
// A very simple program for testing and illustrating basic features of ccRTP.
// Copyright (C) 2001,2002 Federico Montesino <fedemp@altern.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// This is an introductory example file that illustrates basic usage
// of ccRTP. You will also see a bit on how to use CommonC++ threads.
// It is a typical hello world program. It consists of tow duplex
// connections that talk each other through RTP packets. They do not
// say more than a typical salutation message. They both send and
// receive messages, and print the messages they receive.
#include <cstdio>
#include <cstdlib>
// In order to use ccRTP, the RTP stack of CommonC++, you only need to
// include ...
#include <ccrtp/ext.h>
#ifdef CCXX_NAMESPACES
using namespace ost;
using namespace std;
#endif
/**
* @class ccRTP_dupHello
* This is the class that will do almost everything.
*/
class ccRTP_dupHello: public Thread
{
private:
// There will be two duplex connections. They both will send
// and receive packets.
RTPDuplex *duplexA, *duplexB;
public:
// Destructor.
~ccRTP_dupHello()
{
terminate();
delete duplexA;
delete duplexB;
}
// Constructor.
ccRTP_dupHello() : duplexA(NULL), duplexB(NULL)
{}
// This method does almost everything.
void run(void)
{
// redefined from Thread.
// Before using ccRTP you should learn something about other
// CommonC++ classes. We need InetHostAddress...
// Construct loopback address
InetHostAddress local_ip;
local_ip = "127.0.0.1";
// Is that correct?
if( ! local_ip ) {
// this is equivalent to `! local_ip.isInetAddress()'
cerr << ": IP address is not correct!" << endl;
exit();
}
cout << local_ip.getHostname() <<
" is going to talk to perself through " <<
local_ip << "..." << endl;
// ____Here comes the real RTP stuff____
// Construct two RTPSocket. 22222 will be the base
// port of A. 33334 will be the base port of B.
const int A_BASE = 22222;
const int B_BASE = 33334;
duplexA = new RTPDuplex(local_ip,A_BASE,B_BASE);
duplexB = new RTPDuplex(local_ip,B_BASE,A_BASE);
// Set up A's connection
duplexA->setSchedulingTimeout(90000);
duplexA->setExpireTimeout(2500000);
if( duplexA->connect(local_ip,B_BASE) < 0 )
cerr << "Duplex A could not connect.";
// Set up B's connection
duplexB->setSchedulingTimeout(160000);
duplexB->setExpireTimeout(3500000);
if( duplexB->connect(local_ip,A_BASE) < 0 )
cerr << "Duplex B could not connect.";
// Let's check the queues (you should read the documentation
// so that you know what the queues are for).
if( duplexA->RTPDataQueue::isActive() )
cout << "The queue A is active." << endl;
else
cerr << "The queue A is not active." << endl;
if( duplexB->RTPDataQueue::isActive() )
cout << "The queue B is active." << endl;
else
cerr << "The queue B is not active." << endl;
cout << "Transmitting..." << endl;
// This message will be sent on RTP packets, from A to
// B and from B to A.
unsigned char helloA[] = "Hello, brave gnu world from A!";
unsigned char helloB[] = "Hello, brave gnu world from B!";
// This is not important
time_t sending_time;
time_t receiving_time;
char tmstring[30];
StaticPayloadFormat pf = sptMP2T;
duplexA->setPayloadFormat(pf);
duplexB->setPayloadFormat(pf);
// This is the main loop, where packets are sent and receipt.
// A and B both will send and receive packets.
for( int i = 0 ; true ; i++ ) {
// A and B do almost exactly the same things,
// I have kept this here -out of a send/receive
// method- in the interest of clarity.
// A: Send an RTP packet
sending_time = time(NULL);
duplexA->putData(2*(i)*90000,helloA,
strlen((char *)helloA));
// Tell it
strftime(tmstring,30,"%X",localtime(&sending_time));
cout << "A: sending message at " << tmstring << "..."
<< endl;
// A: Receive an RTP packet
receiving_time = time(NULL);
const AppDataUnit* aduA =
duplexA->getData(duplexA->getFirstTimestamp());
if ( aduA ) {
// Tell it
strftime(tmstring,30,"%X",localtime(&receiving_time));
cout << "A:[receiving at " << tmstring << "]: " <<
aduA->getData() << endl;
}
// Wait for 0.1 seconds
Thread::sleep(100);
// B: Send an RTP packet
sending_time = time(NULL);
duplexB->putData(2*(i)*90000,helloB,
strlen((char *)helloB));
// Tell it
strftime(tmstring,30,"%X",localtime(&sending_time));
cout << "B: sending message at " << tmstring << "..."
<< endl;
// B: Receive an RTP packet
receiving_time = time(NULL);
const AppDataUnit* aduB =
duplexB->getData(duplexB->getFirstTimestamp());
if ( aduB ) {
// Tell it
strftime(tmstring,30,"%X",localtime(&receiving_time));
cout << "B:[receiving at " << tmstring << "]: " <<
aduB->getData() << endl;
}
Thread::sleep(1900);
}
}
};
int main(int argc, char *argv[])
{
// Construct the main thread. It will not run yet.
ccRTP_dupHello *hello = new ccRTP_dupHello;
cout << "This is rtpduphello, a very simple test program for ccRTP."
<< endl << "Strike [Enter] when you are fed up." << endl;
// Start execution of hello.
hello->start();
cin.get();
cout << endl << "That's all" << endl;
delete hello;
exit(0);
}
/** EMACS **
* Local variables:
* mode: c++
* c-basic-offset: 4
* End:
*/
|