rtcp.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*-
00002  *
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the MASH Research
00017  *  Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #ifndef lint
00036 static const char rcsid[] =
00037     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/tcp/rtcp.cc,v 1.17 2000/09/01 03:04:06 haoboy Exp $";
00038 #endif
00039 
00040 #include <stdlib.h>
00041 
00042 #include "config.h"
00043 #include "agent.h"
00044 #include "random.h"
00045 #include "rtp.h"
00046 
00047 class RTCPAgent;
00048 
00049 class RTCP_Timer : public TimerHandler {
00050 public: 
00051     RTCP_Timer(RTCPAgent *a) : TimerHandler() { a_ = a; }
00052 protected:
00053     virtual void expire(Event *e);
00054     RTCPAgent *a_;
00055 };
00056 
00057 class RTCPAgent : public Agent {
00058 public:
00059     RTCPAgent();
00060     virtual void timeout(int);
00061     virtual void recv(Packet* p, Handler* h);
00062     int command(int argc, const char*const* argv);
00063 protected:
00064     void start();
00065     void stop();
00066     void sendpkt();
00067 
00068     int running_;
00069     int random_;
00070     int seqno_;
00071     double interval_;
00072     RTPSession* session_;
00073 
00074     RTCP_Timer rtcp_timer_;
00075 };
00076 
00077 static class RTCPAgentClass : public TclClass {
00078 public:
00079     RTCPAgentClass() : TclClass("Agent/RTCP") {}
00080     TclObject* create(int, const char*const*) {
00081         return (new RTCPAgent());
00082     }
00083 } class_rtcp_agent;
00084 
00085 /* XXX Could perhaps derive this from CBR.  If so, use cbr_timer_ */
00086 RTCPAgent::RTCPAgent()
00087     : Agent(PT_RTCP), session_(0), rtcp_timer_(this)
00088 {
00089     size_ = 128;
00090     bind_time("interval_", &interval_);
00091     bind("random_", &random_);
00092     bind("seqno_", &seqno_);
00093     running_ = 0;
00094 }
00095 
00096 void RTCPAgent::start()
00097 {
00098     running_ = 1;
00099     rtcp_timer_.resched(interval_);
00100 }
00101 
00102 void RTCPAgent::stop()
00103 {
00104     rtcp_timer_.cancel();
00105     running_ = 0;
00106 }
00107 
00108 void RTCPAgent::recv(Packet* p, Handler*)
00109 {
00110     session_->recv_ctrl(p);
00111 }
00112 
00113 void RTCPAgent::sendpkt()
00114 {
00115     Packet* p = allocpkt();
00116     hdr_rtp* rh = hdr_rtp::access(p);
00117 
00118     /* Fill in srcid_ and seqno */
00119     rh->seqno() = seqno_++;
00120     rh->srcid() = session_->srcid();
00121     target_->recv(p);
00122 }
00123 
00124 void RTCPAgent::timeout(int)
00125 {
00126     if (running_) {
00127         size_ = session_->build_report(0);
00128         sendpkt();
00129         double t = interval_;
00130         if (random_)
00131             /* add some zero-mean white noise */
00132             t += interval_ * Random::uniform(-0.5, 0.5);    
00133         rtcp_timer_.resched(t);
00134         /* XXX */
00135         Tcl::instance().evalf("%s rtcp_timeout", session_->name());
00136     }
00137 }
00138 
00139 int RTCPAgent::command(int argc, const char*const* argv)
00140 {
00141     if (argc == 2) {
00142         if (strcmp(argv[1], "start") == 0) {
00143             start();
00144             return (TCL_OK);
00145         }
00146         if (strcmp(argv[1], "stop") == 0) {
00147             stop();
00148             return (TCL_OK);
00149         }
00150         if (strcmp(argv[1], "bye") == 0) {
00151             size_ = session_->build_report(1);
00152             sendpkt();
00153             stop();
00154             return (TCL_OK);
00155         }
00156     } else if (argc == 3) {
00157         if (strcmp(argv[1], "session") == 0) {
00158             session_ = (RTPSession*)TclObject::lookup(argv[2]);
00159             return (TCL_OK);
00160         }
00161     }
00162 
00163     return (Agent::command(argc, argv));
00164 }
00165 
00166 void RTCP_Timer::expire(Event* /*e*/) {
00167     a_->timeout(0);
00168 }

Generated on Tue Mar 6 16:47:50 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6