blob: 3346b52005dedca4bf257f5bc8fc643c9a404504 [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);
64
65 m_face->ExpressInterest (interest,
66 MakeCallback (&ApiApp::GotData, this),
67 MakeNullCallback< void, Ptr<const Interest> > ());
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070068}
69
70void
Alexander Afanasyev79606062013-07-11 00:57:28 -070071ApiApp::GotData (Ptr<const Interest> origInterest, Ptr<const ContentObject> data)
72{
73 NS_LOG_FUNCTION (this << origInterest->GetName () << data->GetName ());
74 // do nothing else
75}
76
77void
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070078ApiApp::StartApplication ()
79{
Alexander Afanasyeva4e74282013-07-11 15:23:20 -070080 m_face = CreateObject<ApiFace> (GetNode ());
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070081
Alexander Afanasyev79606062013-07-11 00:57:28 -070082 Simulator::Schedule (Seconds (1), &::ns3::ndn::ApiApp::RequestData, this);
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070083}
84
85void
86ApiApp::StopApplication ()
87{
Alexander Afanasyeva4e74282013-07-11 15:23:20 -070088 NS_LOG_FUNCTION (this);
89 m_face->Shutdown ();
90 m_face = 0;
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -070091}
92
93} // namespace ndn
94} // namespace ns3
95