blob: d320a2d63b96c70fe16861dca0009e31d6c9d398 [file] [log] [blame]
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 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: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21#include "ccnx-producer.h"
22
23
24NS_LOG_COMPONENT_DEFINE ("CcnxProducer");
25
26namespace ns3
27{
28
29NS_OBJECT_ENSURE_REGISTERED (CcnxProducer);
30
31TypeId
32CcnxProducer::GetTypeId (void)
33{
34 static TypeId tid = TypeId ("ns3::CcnxProducer")
35 .SetParent<Application> ()
36 .AddConstructor<CcnxProducer> ()
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070037 /*.AddAttribute ("Capacity", "Capacity of the ContentStore",
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070038 UintegerValue(100),
39 MakeUintegerAccessor(&CcnxProducer::m_storeCapacity),
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070040 MakeUintegerChecker<uint32_t>())*/
41 .AddAttribute ("Prefix","Prefix, for which producer has the data",
42 CcnxNameComponentsValue (),
43 MakeCcnxNameComponentsAccessor (&CcnxProducer::m_prefix),
44 MakeCcnxNameComponentsChecker ())
45 .AddAttribute ("PayloadSize", "Virtual payload size for Content packets",
46 UintegerValue(100),
47 MakeUintegerAccessor(&CcnxProducer::m_virtualPayloadSize),
48 MakeUintegerChecker<uint32_t>())
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070049 .AddTraceSource ("InterestTrace", "Interests that were received",
50 MakeTraceSourceAccessor (&CcnxProducer::m_interestsTrace))
51 .AddTraceSource ("ContentObjectTrace", "ContentObjects that were sent",
52 MakeTraceSourceAccessor (&CcnxProducer::m_contentObjectsTrace))
53 ;
54
55 return tid;
56}
57
58CcnxProducer::CcnxProducer ()
59{
60 NS_LOG_FUNCTION_NOARGS ();
61}
62
63CcnxProducer::~CcnxProducer()
64{
65 NS_LOG_FUNCTION_NOARGS ();
66}
67
68void
69CcnxProducer::DoDispose (void)
70{
71 NS_LOG_FUNCTION_NOARGS ();
72
73 Application::DoDispose ();
74}
75
76 // Application Methods
77void
78CcnxProducer::StartApplication () // Called at time specified by Start
79{
80 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenkob62c7402011-10-28 13:02:18 -070081
82 NS_ASSERT_MSG (m_face == 0, "Face should not exist");
83 m_face = Create<CcnxLocalFace> ();
84
85 // step 1. Set up forwarding from face to application
86 m_face->SetNode (GetNode ());
87 m_face->SetInterestHandler (MakeCallback (&CcnxProducer::OnInterest, this));
88
89 // step 2. Set up forwarding to and from ccnx
90 NS_ASSERT_MSG (GetNode ()->GetObject<Ccnx> () !=0,
91 "Ccnx stack should be installed on the node " << GetNode ());
92 GetNode ()->GetObject<Ccnx> ()->AddFace (m_face);
93 //Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric);
94 GetNode ()->GetObject<Ccnx> ()->GetObject<CcnxFib> ()->Add(m_prefix, m_face, 0);
95 // step 3. Enable face
96 m_face->SetUp ();
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070097}
98
99void
100CcnxProducer::StopApplication () // Called at time specified by Stop
101{
102 NS_LOG_FUNCTION_NOARGS ();
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700103}
104
105void
106CcnxProducer::OnInterest(const Ptr<const CcnxInterestHeader> &interest)
107{
108 NS_LOG_FUNCTION (this);
109
110
111
112 //Ptr<Packet> data = Lookup (interest);
113
114
115
116 Ptr<Packet> incomingPacket = Create<Packet>(m_virtualPayloadSize);
117 incomingPacket->AddHeader (*interest);
118 m_interestsTrace(m_face,incomingPacket);
119
120
121
122 static CcnxContentObjectTail tail; ///< \internal for optimization purposes
123 Ptr<Packet> outgoingPacket = Create<Packet> (m_virtualPayloadSize);
124 Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader>();
125 header->SetName(Create<CcnxNameComponents>(interest->GetName()));
126 outgoingPacket->AddHeader(*header);
127 outgoingPacket->AddTrailer (tail);
128
129 m_contentObjectsTrace(m_face,outgoingPacket);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700130
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700131 m_face->ReceiveFromApplication(outgoingPacket);
132
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700133}
134
135void
136CcnxProducer::CancelEvents ()
137{
138 NS_LOG_FUNCTION_NOARGS ();
139
Alexander Afanasyev152cf112011-08-29 17:58:32 -0700140 // Simulator::Cancel (m_sendEvent);
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700141}
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700142
143CcnxNameComponents
144CcnxProducer::GetPrefix() const
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700145{
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700146 return m_prefix;
147}
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700148
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700149/*uint32_t
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700150CcnxProducer::GetStoreCapacity()
151{
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700152 return m_storeCapacity;
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700153}
154
155void
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700156CcnxProducer::SetStoreCapacity(uint32_t capacity)
157{
158 m_storeCapacity = capacity;
159}
160 */
161/*void
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700162CcnxProducer::HandlePacket(const Ptr<CcnxFace> &face, const Ptr<const Packet> &packet)
163{
164 uint8_t type[2];
165 uint32_t read = packet->CopyData (type,2);
166 if (read!=2)
167 {
168 NS_LOG_INFO ("Unknown CcnxPacket");
169 return;
170 }
171
172 if (type[0] == INTEREST_BYTE0 && type[1] == INTEREST_BYTE1)
173 {
174 m_interestsTrace(face,packet);
175 }
176 else if (type[0] == CONTENT_OBJECT_BYTE0 && type[1] == CONTENT_OBJECT_BYTE1)
177 {
178 m_contentObjectsTrace(face,packet);
179 }
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700180}*/
181
182/*Ptr<Packet>
183CcnxProducer::Lookup (Ptr<const CcnxInterestHeader> interest)
184{
185 NS_LOG_FUNCTION_NOARGS ();
186 DataStoreContainer::type::iterator it = m_availableData.get<i_prefix> ().find (interest->GetName ());
187
188 if (it != m_availableData.end ())
189 {
190 // return fully formed CCNx packet
191 return it->GetFullyFormedCcnxPacket ();
192 }
193
194 return 0;
195}
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700196
197void
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700198CcnxProducer::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700199{
Ilya Moiseenkob62c7402011-10-28 13:02:18 -0700200 NS_LOG_FUNCTION_NOARGS ();
201 DataStoreContainer::type::iterator it = m_availableData.get<i_prefix> ().find (header->GetName ());
202
203 if (it == m_availableData.end ())
204 { // add entry to the top
205 m_availableData.get<i_mru> ().push_front (DataStoreEntry (header, packet));
206
207 if (m_availableData.size () > m_storeCapacity)
208 m_availableData.get<i_mru> ().pop_back ();
209 }
210 else
211 {
212 // promote entry to the top
213 //m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
214 // m_contentStore.project<i_mru> (it));
215 }
216}*/
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700217}