blob: 8606117baf9440991aa150099b0b5ba7b426293b [file] [log] [blame]
Alexander Afanasyev45b92d42011-08-14 23:11:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ilya Moiseenko42b49242011-08-05 20:23:30 -07002//
3// ndn_stupidinterestgenerator.cpp
4// Abstraction
5//
6// Created by Ilya Moiseenko on 05.08.11.
7// Copyright 2011 UCLA. All rights reserved.
8//
9
Alexander Afanasyev404c0792011-08-09 17:09:59 -070010#include "stupid-interest-generator.h"
11
Ilya Moiseenko42b49242011-08-05 20:23:30 -070012#include "ns3/socket-factory.h"
13#include "ns3/simulator.h"
Ilya Moiseenko42b49242011-08-05 20:23:30 -070014
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070015#include "ns3/ccnx-interest-header.h"
16#include "ns3/ccnx-content-object-header.h"
Alexander Afanasyev43e70252011-08-08 18:48:18 -070017
Ilya Moiseenko42b49242011-08-05 20:23:30 -070018NS_LOG_COMPONENT_DEFINE ("StupidInterestGenerator");
19
20namespace ns3
21{
Ilya Moiseenko42b49242011-08-05 20:23:30 -070022
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070023NS_OBJECT_ENSURE_REGISTERED (StupidInterestGenerator);
Ilya Moiseenko42b49242011-08-05 20:23:30 -070024
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070025TypeId
26StupidInterestGenerator::GetTypeId (void)
27{
28 static TypeId tid = TypeId ("ns3::StupidInterestGenerator")
Ilya Moiseenko42b49242011-08-05 20:23:30 -070029 .SetParent<Application> ()
30 .AddConstructor<StupidInterestGenerator> ()
31 .AddAttribute ("Remote", "The address of the destination",
32 AddressValue (),
33 MakeAddressAccessor (&StupidInterestGenerator::m_peer),
34 MakeAddressChecker ())
35 .AddAttribute ("OffTime", "Time interval between packets",
36 TimeValue (Seconds (0.1)),
37 MakeTimeAccessor (&StupidInterestGenerator::m_offTime),
38 MakeTimeChecker ())
39 .AddAttribute ("Protocol", "The type of protocol to use.",
40 TypeIdValue (UdpSocketFactory::GetTypeId ()),
41 MakeTypeIdAccessor (&StupidInterestGenerator::m_tid),
42 MakeTypeIdChecker ())
43 ;
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070044 return tid;
45}
Ilya Moiseenko42b49242011-08-05 20:23:30 -070046
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070047StupidInterestGenerator::StupidInterestGenerator ()
48{
49 NS_LOG_FUNCTION_NOARGS ();
50 m_socket = 0;
51 //m_connected = false;
52 //m_residualBits = 0;
53 //m_lastStartTime = Seconds (0);
54 //m_totBytes = 0;
55}
Ilya Moiseenko42b49242011-08-05 20:23:30 -070056
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070057StupidInterestGenerator::~StupidInterestGenerator()
58{
59 NS_LOG_FUNCTION_NOARGS ();
60}
Ilya Moiseenko42b49242011-08-05 20:23:30 -070061
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070062void
63StupidInterestGenerator::DoDispose (void)
64{
65 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko42b49242011-08-05 20:23:30 -070066
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070067 m_socket = 0;
68 // chain up
69 Application::DoDispose ();
70}
Ilya Moiseenko42b49242011-08-05 20:23:30 -070071
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070072// Application Methods
73void StupidInterestGenerator::StartApplication () // Called at time specified by Start
74{
75 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko42b49242011-08-05 20:23:30 -070076
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070077 // Create the socket if not already
78 if (!m_socket)
79 {
80 m_socket = Socket::CreateSocket (GetNode (), m_tid);
81 m_socket->Bind ();
82 m_socket->Connect (m_peer);
83 m_socket->SetAllowBroadcast (true);
84 m_socket->ShutdownRecv ();
85 }
86 // Insure no pending event
87 CancelEvents ();
88 // If we are not yet connected, there is nothing to do here
89 // The ConnectionComplete upcall will start timers at that time
90 //if (!m_connected) return;
91 ScheduleStartEvent ();
92}
Ilya Moiseenko42b49242011-08-05 20:23:30 -070093
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070094void StupidInterestGenerator::StopApplication () // Called at time specified by Stop
95{
96 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko42b49242011-08-05 20:23:30 -070097
Alexander Afanasyev45b92d42011-08-14 23:11:38 -070098 CancelEvents ();
99 if(m_socket != 0)
100 {
101 m_socket->Close ();
102 }
103 else
104 {
105 NS_LOG_WARN ("OnOffApplication found null socket to close in StopApplication");
106 }
107}
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700108
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700109void StupidInterestGenerator::CancelEvents ()
110{
111 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700112
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700113 Simulator::Cancel (m_sendEvent);
114 Simulator::Cancel (m_startStopEvent);
115}
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700116
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700117void StupidInterestGenerator::ScheduleStartEvent ()
118{ // Schedules the event to start sending data (switch to the "On" state)
119 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700120
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700121 Time offInterval = Seconds (m_offTime);
122 NS_LOG_LOGIC ("start at " << offInterval);
123 m_startStopEvent = Simulator::Schedule (offInterval, &StupidInterestGenerator::StartSending, this);
124}
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700125
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700126// Event handlers
127void StupidInterestGenerator::StartSending ()
128{
129 NS_LOG_FUNCTION_NOARGS ();
130 //m_lastStartTime = Simulator::Now ();
131 ScheduleNextTx (); // Schedule the send packet event
132 //ScheduleStopEvent ();
133}
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700134
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700135void StupidInterestGenerator::StopSending ()
136{
137 NS_LOG_FUNCTION_NOARGS ();
138 CancelEvents ();
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700139
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700140 ScheduleStartEvent ();
141}
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700142
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700143// Private helpers
144void StupidInterestGenerator::ScheduleNextTx ()
145{
146 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700147
148
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700149 Time nextTime = Seconds(0.); //send now
150 m_sendEvent = Simulator::Schedule (nextTime,
151 &StupidInterestGenerator::SendPacket, this);
152}
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700153
154
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700155void StupidInterestGenerator::SendPacket ()
156{
157 // NS_LOG_FUNCTION_NOARGS ();
158 // NS_LOG_LOGIC ("sending packet at " << Simulator::Now ());
159 // NS_ASSERT (m_sendEvent.IsExpired ());
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700160
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700161 // NameBuilder name;
162 // name("prefix1")("prefix2")("filename");
163 CcnxInterestHeader ();
Alexander Afanasyev8a677dd2011-08-12 13:08:15 -0700164
Alexander Afanasyev45b92d42011-08-14 23:11:38 -0700165 CcnxContentObjectHeader ();
Alexander Afanasyevf583ef22011-08-08 19:05:28 -0700166
Alexander Afanasyev45b92d42011-08-14 23:11:38 -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 Afanasyev45b92d42011-08-14 23:11:38 -0700174 // ScheduleStartEvent();
175}
Ilya Moiseenko42b49242011-08-05 20:23:30 -0700176
Alexander Afanasyev43e70252011-08-08 18:48:18 -0700177}