tp.cc

Go to the documentation of this file.
00001 #include "tp.h"
00002 #include "rtp.h"
00003 #include "random.h"
00004 #include "address.h"
00005 #include "ip.h"
00006 
00007 
00008 static class TPAgentClass : public TclClass {
00009 public:
00010     TPAgentClass() : TclClass("Agent/TP") {}
00011     TclObject* create(int, const char*const*) {
00012         return (new TPAgent());
00013     }
00014 } class_tp_agent;
00015 
00016 TPAgent::TPAgent() : Agent(PT_MESSAGE), seqno_(-1) {
00017   size_ = 2000;
00018 }
00019 
00020 TPAgent::TPAgent(packet_t type) : Agent(type) {
00021   //bind("packetSize_", &size_);
00022 }
00023 
00024 void TPAgent::sendto(int nbytes, unsigned int saddr, int sport, unsigned int daddr, int dport) {
00025   double local_time = Scheduler::instance().clock();
00026   printf("send %f %u %d %u %d %d\n",
00027     local_time, saddr, sport, daddr, dport, nbytes);
00028 
00029   if (nbytes == -1) {
00030     printf("Error: packet size for TPAgent should not be -1\n");
00031     return;
00032   } 
00033   
00034   // check packet size (we don't fragment packets)
00035   if (nbytes > size_) {
00036     printf("Error: packet greater than maximum TPAgent packet size\n");
00037     return;
00038   }
00039   
00040   Packet *p = allocpkt();
00041   // fill IP header
00042   hdr_ip* iph = hdr_ip::access(p);
00043   iph->saddr() = saddr;
00044   iph->sport() = sport;
00045   iph->daddr() = daddr;
00046   iph->dport() = dport;
00047 
00048   hdr_cmn::access(p)->size() = nbytes;
00049   hdr_rtp* rh = hdr_rtp::access(p);
00050   rh->flags() = 0;
00051   rh->seqno() = ++seqno_;
00052   
00053   hdr_cmn::access(p)->timestamp() = 
00054     (u_int32_t)(SAMPLERATE*local_time);
00055 
00056   target_->recv(p);
00057   idle();
00058 }
00059 
00060 void TPAgent::recv(Packet* pkt, Handler*) {
00061   if (app_ ) {
00062     // If an application is attached, pass the data to the app
00063     hdr_cmn* h = hdr_cmn::access(pkt);
00064     app_->process_data(h->size(), pkt->userdata());
00065   }
00066 
00067   hdr_ip* iph = hdr_ip::access(pkt);
00068   printf("recv %f %d %u %d %u %d %d\n",
00069          Scheduler::instance().clock(), addr(),
00070          iph->saddr(), iph->sport(),
00071          iph->daddr(), iph->dport(),
00072          hdr_cmn::access(pkt)->size());
00073 
00074   // recyle packets received
00075   Packet::free(pkt);
00076 }
00077 
00078 int TPAgent::command(int argc, const char*const* argv) {
00079   if (argc == 7) {
00080     if (strcmp(argv[1], "sendto") == 0) {
00081       sendto(atoi(argv[6]), atoi(argv[2]), atoi(argv[3]),
00082              atoi(argv[4]), atoi(argv[5]));
00083       return TCL_OK;
00084     }
00085   }
00086   
00087   return (Agent::command(argc, argv));
00088 }

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