blob: 5bc18612c639cabc520c5f77254fe2e909c72ce7 [file] [log] [blame]
Ilya Moiseenko42b49242011-08-05 20:23:30 -07001//
2// ndn_stupidinterestgenerator.cpp
3// Abstraction
4//
5// Created by Ilya Moiseenko on 05.08.11.
6// Copyright 2011 UCLA. All rights reserved.
7//
8
9#include "ndn_stupidinterestgenerator.h"
10#include "ns3/socket.h"
11#include "ns3/socket-factory.h"
12#include "ns3/simulator.h"
13#include "ndn_interestpacket.h"
14#include "ndn_namebuilder.h"
15
Alexander Afanasyev43e70252011-08-08 18:48:18 -070016#include "ccn/ccn_charbuf.h"
17
Ilya Moiseenko42b49242011-08-05 20:23:30 -070018NS_LOG_COMPONENT_DEFINE ("StupidInterestGenerator");
19
20namespace ns3
21{
22//namespace NDNabstraction
23//{
24 using namespace NDNabstraction;
25
26 NS_OBJECT_ENSURE_REGISTERED (StupidInterestGenerator);
27
28 TypeId
29 StupidInterestGenerator::GetTypeId (void)
30 {
31 static TypeId tid = TypeId ("ns3::StupidInterestGenerator")
32 .SetParent<Application> ()
33 .AddConstructor<StupidInterestGenerator> ()
34 .AddAttribute ("Remote", "The address of the destination",
35 AddressValue (),
36 MakeAddressAccessor (&StupidInterestGenerator::m_peer),
37 MakeAddressChecker ())
38 .AddAttribute ("OffTime", "Time interval between packets",
39 TimeValue (Seconds (0.1)),
40 MakeTimeAccessor (&StupidInterestGenerator::m_offTime),
41 MakeTimeChecker ())
42 .AddAttribute ("Protocol", "The type of protocol to use.",
43 TypeIdValue (UdpSocketFactory::GetTypeId ()),
44 MakeTypeIdAccessor (&StupidInterestGenerator::m_tid),
45 MakeTypeIdChecker ())
46 ;
47 return tid;
48 }
49
50 StupidInterestGenerator::StupidInterestGenerator ()
51 {
52 NS_LOG_FUNCTION_NOARGS ();
53 m_socket = 0;
54 //m_connected = false;
55 //m_residualBits = 0;
56 //m_lastStartTime = Seconds (0);
57 //m_totBytes = 0;
58 }
59
60 StupidInterestGenerator::~StupidInterestGenerator()
61 {
62 NS_LOG_FUNCTION_NOARGS ();
63 }
64
65 void
66 StupidInterestGenerator::DoDispose (void)
67 {
68 NS_LOG_FUNCTION_NOARGS ();
69
70 m_socket = 0;
71 // chain up
72 Application::DoDispose ();
73 }
74
75 // Application Methods
76 void StupidInterestGenerator::StartApplication () // Called at time specified by Start
77 {
78 NS_LOG_FUNCTION_NOARGS ();
79
80 // Create the socket if not already
81 if (!m_socket)
82 {
83 m_socket = Socket::CreateSocket (GetNode (), m_tid);
84 m_socket->Bind ();
85 m_socket->Connect (m_peer);
86 m_socket->SetAllowBroadcast (true);
87 m_socket->ShutdownRecv ();
88 }
89 // Insure no pending event
90 CancelEvents ();
91 // If we are not yet connected, there is nothing to do here
92 // The ConnectionComplete upcall will start timers at that time
93 //if (!m_connected) return;
94 ScheduleStartEvent ();
95 }
96
97 void StupidInterestGenerator::StopApplication () // Called at time specified by Stop
98 {
99 NS_LOG_FUNCTION_NOARGS ();
100
101 CancelEvents ();
102 if(m_socket != 0)
103 {
104 m_socket->Close ();
105 }
106 else
107 {
108 NS_LOG_WARN ("OnOffApplication found null socket to close in StopApplication");
109 }
110 }
111
112 void StupidInterestGenerator::CancelEvents ()
113 {
114 NS_LOG_FUNCTION_NOARGS ();
115
116 Simulator::Cancel (m_sendEvent);
117 Simulator::Cancel (m_startStopEvent);
118 }
119
120 void StupidInterestGenerator::ScheduleStartEvent ()
121 { // Schedules the event to start sending data (switch to the "On" state)
122 NS_LOG_FUNCTION_NOARGS ();
123
124 Time offInterval = Seconds (m_offTime);
125 NS_LOG_LOGIC ("start at " << offInterval);
126 m_startStopEvent = Simulator::Schedule (offInterval, &StupidInterestGenerator::StartSending, this);
127 }
128
129 // Event handlers
130 void StupidInterestGenerator::StartSending ()
131 {
132 NS_LOG_FUNCTION_NOARGS ();
133 //m_lastStartTime = Simulator::Now ();
134 ScheduleNextTx (); // Schedule the send packet event
135 //ScheduleStopEvent ();
136 }
137
138 void StupidInterestGenerator::StopSending ()
139 {
140 NS_LOG_FUNCTION_NOARGS ();
141 CancelEvents ();
142
143 ScheduleStartEvent ();
144 }
145
146 // Private helpers
147 void StupidInterestGenerator::ScheduleNextTx ()
148 {
149 NS_LOG_FUNCTION_NOARGS ();
150
151
152 Time nextTime = Seconds(0.); //send now
153 m_sendEvent = Simulator::Schedule (nextTime,
154 &StupidInterestGenerator::SendPacket, this);
155 }
156
157
158 void StupidInterestGenerator::SendPacket ()
159 {
160 NS_LOG_FUNCTION_NOARGS ();
161 NS_LOG_LOGIC ("sending packet at " << Simulator::Now ());
162 NS_ASSERT (m_sendEvent.IsExpired ());
163
Alexander Afanasyev43e70252011-08-08 18:48:18 -0700164 NameBuilder name;
165 name("prefix1")("prefix2")("filename");
166 const ccn_charbuf *output = name.GetName();
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700167 Ptr<InterestPacket> packet = Create<InterestPacket>(output->buf,(uint32_t)output->length);
168 packet->AddTimeout(4000);
169 UniformVariable var;
170 packet->AddNonce(var.GetInteger(1,10000));
171 m_socket->Send(packet);
172
173 ScheduleStartEvent();
174 }
175
176//}
Alexander Afanasyev43e70252011-08-08 18:48:18 -0700177}