Alexander Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 23 | NS_LOG_COMPONENT_DEFINE ("ndn.ApiApp"); |
| 24 | |
| 25 | namespace ns3 { |
| 26 | namespace ndn { |
| 27 | |
| 28 | // Necessary if you are planning to use ndn::AppHelper |
| 29 | NS_OBJECT_ENSURE_REGISTERED (ApiApp); |
| 30 | |
| 31 | TypeId |
| 32 | ApiApp::GetTypeId () |
| 33 | { |
| 34 | static TypeId tid = TypeId ("ns3::ndn::ApiApp") |
| 35 | .SetParent<Application> () |
| 36 | .AddConstructor<ApiApp> () |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 37 | |
| 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 Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 46 | ; |
| 47 | |
| 48 | return tid; |
| 49 | } |
| 50 | |
| 51 | ApiApp::ApiApp () |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 52 | : m_face (0) |
Alexander Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 53 | { |
Alexander Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void |
| 57 | ApiApp::RequestData () |
| 58 | { |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 59 | NS_LOG_FUNCTION (this); |
| 60 | |
| 61 | Ptr<Interest> interest = Create<Interest> (); |
| 62 | interest->SetName (m_name); |
| 63 | interest->SetInterestLifetime (m_interestLifetime); |
Alexander Afanasyev | eec89ba | 2013-07-19 16:34:30 -0700 | [diff] [blame] | 64 | |
| 65 | Ptr<Exclude> exclude = Create<Exclude> (); |
| 66 | exclude->excludeOne (name::Component ("unique")); |
| 67 | interest->SetExclude (exclude); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 68 | |
| 69 | m_face->ExpressInterest (interest, |
| 70 | MakeCallback (&ApiApp::GotData, this), |
| 71 | MakeNullCallback< void, Ptr<const Interest> > ()); |
Alexander Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 75 | ApiApp::GotData (Ptr<const Interest> origInterest, Ptr<const Data> data) |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 76 | { |
| 77 | NS_LOG_FUNCTION (this << origInterest->GetName () << data->GetName ()); |
| 78 | // do nothing else |
| 79 | } |
| 80 | |
| 81 | void |
Alexander Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 82 | ApiApp::StartApplication () |
| 83 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 84 | m_face = CreateObject<ApiFace> (GetNode ()); |
Alexander Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 86 | Simulator::Schedule (Seconds (1), &::ns3::ndn::ApiApp::RequestData, this); |
Alexander Afanasyev | eec89ba | 2013-07-19 16:34:30 -0700 | [diff] [blame] | 87 | Simulator::Schedule (Seconds (10), &::ns3::ndn::ApiApp::RequestData, this); |
Alexander Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void |
| 91 | ApiApp::StopApplication () |
| 92 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 93 | NS_LOG_FUNCTION (this); |
| 94 | m_face->Shutdown (); |
| 95 | m_face = 0; |
Alexander Afanasyev | 79a5bd6 | 2013-06-23 22:12:39 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace ndn |
| 99 | } // namespace ns3 |
| 100 | |