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