blob: db55bb63a3fe295ccaabead9b934ee5842c1cf7b [file] [log] [blame]
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ndn-api-app.h"
22
23NS_LOG_COMPONENT_DEFINE ("ndn.ApiApp");
24
25namespace ns3 {
26namespace ndn {
27
28// Necessary if you are planning to use ndn::AppHelper
29NS_OBJECT_ENSURE_REGISTERED (ApiApp);
30
31TypeId
32ApiApp::GetTypeId ()
33{
34 static TypeId tid = TypeId ("ns3::ndn::ApiApp")
35 .SetParent<Application> ()
36 .AddConstructor<ApiApp> ()
Alexander Afanasyev79606062013-07-11 00:57:28 -070037
38 .AddAttribute ("Prefix","Name of the Interest",
39 StringValue ("/"),
40 MakeNameAccessor (&ApiApp::m_name),
41 MakeNameChecker ())
42 .AddAttribute ("LifeTime", "LifeTime for interest packet",
43 StringValue ("2s"),
44 MakeTimeAccessor (&ApiApp::m_interestLifetime),
45 MakeTimeChecker ())
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070046 ;
47
48 return tid;
49}
50
51ApiApp::ApiApp ()
Alexander Afanasyev79606062013-07-11 00:57:28 -070052 : m_face (0)
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070053{
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070054}
55
56void
57ApiApp::RequestData ()
58{
Alexander Afanasyev79606062013-07-11 00:57:28 -070059 NS_LOG_FUNCTION (this);
60
61 Ptr<Interest> interest = Create<Interest> ();
62 interest->SetName (m_name);
63 interest->SetInterestLifetime (m_interestLifetime);
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -070064
65 Ptr<Exclude> exclude = Create<Exclude> ();
66 exclude->excludeOne (name::Component ("unique"));
67 interest->SetExclude (exclude);
Alexander Afanasyev79606062013-07-11 00:57:28 -070068
69 m_face->ExpressInterest (interest,
70 MakeCallback (&ApiApp::GotData, this),
71 MakeNullCallback< void, Ptr<const Interest> > ());
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070072}
73
74void
Alexander Afanasyev79606062013-07-11 00:57:28 -070075ApiApp::GotData (Ptr<const Interest> origInterest, Ptr<const ContentObject> data)
76{
77 NS_LOG_FUNCTION (this << origInterest->GetName () << data->GetName ());
78 // do nothing else
79}
80
81void
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070082ApiApp::StartApplication ()
83{
Alexander Afanasyeva4e74282013-07-11 15:23:20 -070084 m_face = CreateObject<ApiFace> (GetNode ());
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070085
Alexander Afanasyev79606062013-07-11 00:57:28 -070086 Simulator::Schedule (Seconds (1), &::ns3::ndn::ApiApp::RequestData, this);
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -070087 Simulator::Schedule (Seconds (10), &::ns3::ndn::ApiApp::RequestData, this);
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070088}
89
90void
91ApiApp::StopApplication ()
92{
Alexander Afanasyeva4e74282013-07-11 15:23:20 -070093 NS_LOG_FUNCTION (this);
94 m_face->Shutdown ();
95 m_face = 0;
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070096}
97
98} // namespace ndn
99} // namespace ns3
100