blob: 1964ac71a87651f64a3e03fe2adaa047df465120 [file] [log] [blame]
Alexander Afanasyev8b379052011-08-21 16:58:20 -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#include "ccnb-parser-interest-visitor.h"
22
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070023#include "ns3/ccnb-parser-block.h"
24#include "ns3/ccnb-parser-dtag.h"
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070025#include "ns3/ccnx-name-components.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070026#include "ns3/assert.h"
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -070027#include "ns3/nstime.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070028
29#include "ns3/ccnx-interest-header.h"
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -070030#include "ccnb-parser-name-components-visitor.h"
31#include "ccnb-parser-non-negative-integer-visitor.h"
32#include "ccnb-parser-timestamp-visitor.h"
33#include "ccnb-parser-nonce-visitor.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070034
35#include <boost/foreach.hpp>
36
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080037#include "ns3/log.h"
38
39NS_LOG_COMPONENT_DEFINE ("CcnbParserInterestVisitor");
40
Alexander Afanasyev8b379052011-08-21 16:58:20 -070041namespace ns3 {
42namespace CcnbParser {
43
44// We don't care about any other fields
45void
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070046InterestVisitor::visit (Dtag &n, boost::any param/*should be CcnxInterestHeader* */)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070047{
48 // uint32_t n.m_dtag;
49 // std::list<Ptr<Block> > n.m_nestedBlocks;
50
51 static NonNegativeIntegerVisitor nonNegativeIntegerVisitor;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070052 static NameComponentsVisitor nameComponentsVisitor;
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -070053 static TimestampVisitor timestampVisitor;
54 static NonceVisitor nonceVisitor;
Alexander Afanasyev8b379052011-08-21 16:58:20 -070055
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070056 CcnxInterestHeader &interest = *(boost::any_cast<CcnxInterestHeader*> (param));
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080057
Alexander Afanasyev8b379052011-08-21 16:58:20 -070058 switch (n.m_dtag)
59 {
60 case CCN_DTAG_Interest:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080061 NS_LOG_DEBUG ("Interest");
62
Alexander Afanasyev8b379052011-08-21 16:58:20 -070063 // process nested blocks
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070064 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070065 {
66 block->accept (*this, param);
67 }
68 break;
69 case CCN_DTAG_Name:
70 {
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080071 NS_LOG_DEBUG ("Name");
72
Alexander Afanasyev8b379052011-08-21 16:58:20 -070073 // process name components
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070074 Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070075
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070076 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070077 {
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070078 block->accept (nameComponentsVisitor, &(*name));
Alexander Afanasyev8b379052011-08-21 16:58:20 -070079 }
80 interest.SetName (name);
81 break;
82 }
83 case CCN_DTAG_MinSuffixComponents:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080084 NS_LOG_DEBUG ("MinSuffixComponents");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070085 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
86 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070087 interest.SetMinSuffixComponents (
88 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070089 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -070090 nonNegativeIntegerVisitor
91 )));
92 break;
93 case CCN_DTAG_MaxSuffixComponents:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080094 NS_LOG_DEBUG ("MaxSuffixComponents");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070095 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
96 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070097 interest.SetMaxSuffixComponents (
98 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070099 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700100 nonNegativeIntegerVisitor
101 )));
102 break;
103 case CCN_DTAG_Exclude:
104 {
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800105 NS_LOG_DEBUG ("Exclude");
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700106 // process exclude components
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -0700107 Ptr<CcnxNameComponents> exclude = Create<CcnxNameComponents> ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700108
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700109 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700110 {
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700111 block->accept (nameComponentsVisitor, &(*exclude));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700112 }
113 interest.SetExclude (exclude);
114 break;
115 }
116 case CCN_DTAG_ChildSelector:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800117 NS_LOG_DEBUG ("ChildSelector");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700118 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
119 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700120
121 interest.SetChildSelector (
122 1 == boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700123 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700124 nonNegativeIntegerVisitor
125 )));
126 break;
127 case CCN_DTAG_AnswerOriginKind:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800128 NS_LOG_DEBUG ("AnswerOriginKind");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700129 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
130 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700131 interest.SetAnswerOriginKind (
132 1 == boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700133 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700134 nonNegativeIntegerVisitor
135 )));
136 break;
137 case CCN_DTAG_Scope:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800138 NS_LOG_DEBUG ("Scope");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700139 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
140 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700141 interest.SetScope (
142 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700143 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700144 nonNegativeIntegerVisitor
145 )));
146 break;
147 case CCN_DTAG_InterestLifetime:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800148 NS_LOG_DEBUG ("InterestLifetime");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700149 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
150 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700151
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -0700152 interest.SetInterestLifetime (
153 boost::any_cast<Time> (
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800154 (*n.m_nestedTags.begin())->accept(
155 timestampVisitor
156 )));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700157 break;
158 case CCN_DTAG_Nonce:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800159 NS_LOG_DEBUG ("Nonce");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700160 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
161 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700162
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -0700163 interest.SetNonce (
164 boost::any_cast<uint32_t> (
165 (*n.m_nestedTags.begin())->accept(
166 nonceVisitor
167 )));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700168 break;
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800169
170
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800171 case CCN_DTAG_Nack:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800172 NS_LOG_DEBUG ("Nack");
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800173 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
174 throw CcnbDecodingException ();
175
176 interest.SetNack (
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800177 boost::any_cast<uint32_t> (
178 (*n.m_nestedTags.begin())->accept(nonNegativeIntegerVisitor)));
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800179 break;
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700180}
181
182} // namespace CcnbParser
183} // namespace ns3