blob: b3275da2091adba4ad8ab9187dcec4d179ea1af0 [file] [log] [blame]
Alexander Afanasyev9a989702012-06-29 17:44:00 -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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#ifndef CCNX_CONTENT_STORE_IMPL_H_
22#define CCNX_CONTENT_STORE_IMPL_H_
23
24#include "ccnx-content-store.h"
25#include "ns3/packet.h"
26#include "ns3/ccnx-interest-header.h"
27#include "ns3/ccnx-content-object-header.h"
28#include <boost/foreach.hpp>
29
30namespace ns3
31{
32
33template<class Container>
34class CcnxContentStoreImpl : public CcnxContentStore,
35 protected Container
36{
37public:
38 // from CcnxContentStore
39
40 virtual inline boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
41 Lookup (Ptr<const CcnxInterestHeader> interest);
42
43 virtual inline bool
Alexander Afanasyev996b4872012-07-17 17:07:56 -070044 Add (Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> packet);
Alexander Afanasyev9a989702012-06-29 17:44:00 -070045
46 // virtual bool
47 // Remove (Ptr<CcnxInterestHeader> header);
48
49 virtual inline void
50 Print (std::ostream &os) const;
51};
52
53
54template<class Container>
55boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
56CcnxContentStoreImpl<Container>::Lookup (Ptr<const CcnxInterestHeader> interest)
57{
58 // NS_LOG_FUNCTION (this << interest->GetName ());
59
60 /// @todo Change to search with predicate
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070061 typename Container::const_iterator node = this->deepest_prefix_match (interest->GetName ());
Alexander Afanasyev9a989702012-06-29 17:44:00 -070062
63 if (node != this->end ())
64 {
65 this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
66
67 // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
68 return boost::make_tuple (node->payload ()->GetFullyFormedCcnxPacket (),
69 node->payload ()->GetHeader (),
70 node->payload ()->GetPacket ());
71 }
72 else
73 {
74 // NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
75 this->m_cacheMissesTrace (interest);
76 return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
77 }
78}
79
80template<class Container>
81bool
Alexander Afanasyev996b4872012-07-17 17:07:56 -070082CcnxContentStoreImpl<Container>::Add (Ptr<const CcnxContentObjectHeader> header, Ptr<const Packet> packet)
Alexander Afanasyev9a989702012-06-29 17:44:00 -070083{
84 // NS_LOG_FUNCTION (this << header->GetName ());
85
86 return
87 this->insert (header->GetName (), Create<CcnxContentStoreEntry> (header, packet))
88 .second;
89}
90
91template<class Container>
92void
93CcnxContentStoreImpl<Container>::Print (std::ostream &os) const
94{
Alexander Afanasyev051d3782012-07-03 10:10:15 -070095 for (typename Container::policy_container::const_iterator item = this->getPolicy ().begin ();
96 item != this->getPolicy ().end ();
97 item++)
98 // BOOST_FOREACH (const typename Container::parent_trie &item, this->getPolicy ())
Alexander Afanasyev9a989702012-06-29 17:44:00 -070099 {
Alexander Afanasyev051d3782012-07-03 10:10:15 -0700100 os << item->payload ()->GetName () << std::endl;
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700101 }
102}
103
104
105} // namespace ns3
106
107#endif // CCNX_CONTENT_STORE_IMPL_H_