blob: e7fe598f7e0258e13cbf90a658476fa52e8021bb [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
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070021#include "interest-visitor.h"
Alexander Afanasyev8b379052011-08-21 16:58:20 -070022
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070023#include "../syntax-tree/block.h"
24#include "../syntax-tree/dtag.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080025
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070026#include "ns3/ndn-name.h"
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070027#include "ns3/ndn-interest-header.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080028
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070029#include "ns3/assert.h"
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -070030#include "ns3/nstime.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070031
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070032#include "name-components-visitor.h"
33#include "non-negative-integer-visitor.h"
34#include "timestamp-visitor.h"
35#include "uint32t-blob-visitor.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070036
37#include <boost/foreach.hpp>
38
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080039#include "ns3/log.h"
40
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041NS_LOG_COMPONENT_DEFINE ("ndn.CcnbParser.InterestVisitor");
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080042
Alexander Afanasyev8b379052011-08-21 16:58:20 -070043namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070044namespace ndn {
Alexander Afanasyev8b379052011-08-21 16:58:20 -070045namespace CcnbParser {
46
47// We don't care about any other fields
48void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049InterestVisitor::visit (Dtag &n, boost::any param/*should be InterestHeader* */)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070050{
51 // uint32_t n.m_dtag;
52 // std::list<Ptr<Block> > n.m_nestedBlocks;
53
54 static NonNegativeIntegerVisitor nonNegativeIntegerVisitor;
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070055 static NameVisitor nameComponentsVisitor;
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -070056 static TimestampVisitor timestampVisitor;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070057 static Uint32tBlobVisitor nonceVisitor;
Alexander Afanasyev8b379052011-08-21 16:58:20 -070058
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 InterestHeader &interest = *(boost::any_cast<InterestHeader*> (param));
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080060
Alexander Afanasyev8b379052011-08-21 16:58:20 -070061 switch (n.m_dtag)
62 {
63 case CCN_DTAG_Interest:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080064 NS_LOG_DEBUG ("Interest");
65
Alexander Afanasyev8b379052011-08-21 16:58:20 -070066 // process nested blocks
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070067 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070068 {
69 block->accept (*this, param);
70 }
71 break;
72 case CCN_DTAG_Name:
73 {
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080074 NS_LOG_DEBUG ("Name");
75
Alexander Afanasyev8b379052011-08-21 16:58:20 -070076 // process name components
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070077 Ptr<Name> name = Create<Name> ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070078
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070079 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070080 {
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070081 block->accept (nameComponentsVisitor, &(*name));
Alexander Afanasyev8b379052011-08-21 16:58:20 -070082 }
83 interest.SetName (name);
84 break;
85 }
86 case CCN_DTAG_MinSuffixComponents:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080087 NS_LOG_DEBUG ("MinSuffixComponents");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070088 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
89 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070090 interest.SetMinSuffixComponents (
91 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070092 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -070093 nonNegativeIntegerVisitor
94 )));
95 break;
96 case CCN_DTAG_MaxSuffixComponents:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080097 NS_LOG_DEBUG ("MaxSuffixComponents");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070098 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
99 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700100 interest.SetMaxSuffixComponents (
101 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700102 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700103 nonNegativeIntegerVisitor
104 )));
105 break;
106 case CCN_DTAG_Exclude:
107 {
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800108 NS_LOG_DEBUG ("Exclude");
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700109 // process exclude components
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700110 Ptr<Name> exclude = Create<Name> ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700111
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700112 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700113 {
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700114 block->accept (nameComponentsVisitor, &(*exclude));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700115 }
116 interest.SetExclude (exclude);
117 break;
118 }
119 case CCN_DTAG_ChildSelector:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800120 NS_LOG_DEBUG ("ChildSelector");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700121 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
122 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700123
124 interest.SetChildSelector (
125 1 == boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700126 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700127 nonNegativeIntegerVisitor
128 )));
129 break;
130 case CCN_DTAG_AnswerOriginKind:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800131 NS_LOG_DEBUG ("AnswerOriginKind");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700132 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
133 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700134 interest.SetAnswerOriginKind (
135 1 == boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700136 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700137 nonNegativeIntegerVisitor
138 )));
139 break;
140 case CCN_DTAG_Scope:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800141 NS_LOG_DEBUG ("Scope");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700142 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
143 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700144 interest.SetScope (
145 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700146 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700147 nonNegativeIntegerVisitor
148 )));
149 break;
150 case CCN_DTAG_InterestLifetime:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800151 NS_LOG_DEBUG ("InterestLifetime");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700152 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
153 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700154
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -0700155 interest.SetInterestLifetime (
156 boost::any_cast<Time> (
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800157 (*n.m_nestedTags.begin())->accept(
158 timestampVisitor
159 )));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700160 break;
161 case CCN_DTAG_Nonce:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800162 NS_LOG_DEBUG ("Nonce");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700163 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
164 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700165
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -0700166 interest.SetNonce (
167 boost::any_cast<uint32_t> (
168 (*n.m_nestedTags.begin())->accept(
169 nonceVisitor
170 )));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700171 break;
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800172
173
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800174 case CCN_DTAG_Nack:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800175 NS_LOG_DEBUG ("Nack");
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800176 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
177 throw CcnbDecodingException ();
178
179 interest.SetNack (
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800180 boost::any_cast<uint32_t> (
181 (*n.m_nestedTags.begin())->accept(nonNegativeIntegerVisitor)));
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800182 break;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800183 }
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700184}
185
186} // namespace CcnbParser
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187} // namespace ndn
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700188} // namespace ns3