File: tp.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,864 kB
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 818; awk: 525; csh: 355
file content (88 lines) | stat: -rw-r--r-- 2,168 bytes parent folder | download | duplicates (8)
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
#include "tp.h"
#include "rtp.h"
#include "random.h"
#include "address.h"
#include "ip.h"


static class TPAgentClass : public TclClass {
public:
	TPAgentClass() : TclClass("Agent/TP") {}
	TclObject* create(int, const char*const*) {
		return (new TPAgent());
	}
} class_tp_agent;

TPAgent::TPAgent() : Agent(PT_MESSAGE), seqno_(-1) {
  size_ = 2000;
}

TPAgent::TPAgent(packet_t type) : Agent(type) {
  //bind("packetSize_", &size_);
}

void TPAgent::sendto(int nbytes, unsigned int saddr, int sport, unsigned int daddr, int dport) {
  double local_time = Scheduler::instance().clock();
  printf("send %f %u %d %u %d %d\n",
  	local_time, saddr, sport, daddr, dport, nbytes);

  if (nbytes == -1) {
    printf("Error: packet size for TPAgent should not be -1\n");
    return;
  }	
  
  // check packet size (we don't fragment packets)
  if (nbytes > size_) {
    printf("Error: packet greater than maximum TPAgent packet size\n");
    return;
  }
  
  Packet *p = allocpkt();
  // fill IP header
  hdr_ip* iph = hdr_ip::access(p);
  iph->saddr() = saddr;
  iph->sport() = sport;
  iph->daddr() = daddr;
  iph->dport() = dport;

  hdr_cmn::access(p)->size() = nbytes;
  hdr_rtp* rh = hdr_rtp::access(p);
  rh->flags() = 0;
  rh->seqno() = ++seqno_;
  
  hdr_cmn::access(p)->timestamp() = 
    (u_int32_t)(SAMPLERATE*local_time);

  target_->recv(p);
  idle();
}

void TPAgent::recv(Packet* pkt, Handler*) {
  if (app_ ) {
    // If an application is attached, pass the data to the app
    hdr_cmn* h = hdr_cmn::access(pkt);
    app_->process_data(h->size(), pkt->userdata());
  }

  hdr_ip* iph = hdr_ip::access(pkt);
  printf("recv %f %d %u %d %u %d %d\n",
         Scheduler::instance().clock(), addr(),
         iph->saddr(), iph->sport(),
         iph->daddr(), iph->dport(),
         hdr_cmn::access(pkt)->size());

  // recyle packets received
  Packet::free(pkt);
}

int TPAgent::command(int argc, const char*const* argv) {
  if (argc == 7) {
    if (strcmp(argv[1], "sendto") == 0) {
      sendto(atoi(argv[6]), atoi(argv[2]), atoi(argv[3]),
             atoi(argv[4]), atoi(argv[5]));
      return TCL_OK;
    }
  }
  
  return (Agent::command(argc, argv));
}