blob: ff219f065d39d2836d07f6f8455128af784b2d5c [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 Afanasyevf9f4eb02011-12-16 01:51:14 -080023#include "../syntax-tree/ccnb-parser-block.h"
24#include "../syntax-tree/ccnb-parser-dtag.h"
25
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070026#include "ns3/ccnx-name-components.h"
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080027
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070028#include "ns3/assert.h"
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -070029#include "ns3/nstime.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070030
31#include "ns3/ccnx-interest-header.h"
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -070032#include "ccnb-parser-name-components-visitor.h"
33#include "ccnb-parser-non-negative-integer-visitor.h"
34#include "ccnb-parser-timestamp-visitor.h"
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070035#include "ccnb-parser-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
41NS_LOG_COMPONENT_DEFINE ("CcnbParserInterestVisitor");
42
Alexander Afanasyev8b379052011-08-21 16:58:20 -070043namespace ns3 {
44namespace CcnbParser {
45
46// We don't care about any other fields
47void
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070048InterestVisitor::visit (Dtag &n, boost::any param/*should be CcnxInterestHeader* */)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070049{
50 // uint32_t n.m_dtag;
51 // std::list<Ptr<Block> > n.m_nestedBlocks;
52
53 static NonNegativeIntegerVisitor nonNegativeIntegerVisitor;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070054 static NameComponentsVisitor nameComponentsVisitor;
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -070055 static TimestampVisitor timestampVisitor;
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070056 static Uint32tBlobVisitor nonceVisitor;
Alexander Afanasyev8b379052011-08-21 16:58:20 -070057
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070058 CcnxInterestHeader &interest = *(boost::any_cast<CcnxInterestHeader*> (param));
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080059
Alexander Afanasyev8b379052011-08-21 16:58:20 -070060 switch (n.m_dtag)
61 {
62 case CCN_DTAG_Interest:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080063 NS_LOG_DEBUG ("Interest");
64
Alexander Afanasyev8b379052011-08-21 16:58:20 -070065 // process nested blocks
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070066 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070067 {
68 block->accept (*this, param);
69 }
70 break;
71 case CCN_DTAG_Name:
72 {
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080073 NS_LOG_DEBUG ("Name");
74
Alexander Afanasyev8b379052011-08-21 16:58:20 -070075 // process name components
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070076 Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070077
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070078 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070079 {
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070080 block->accept (nameComponentsVisitor, &(*name));
Alexander Afanasyev8b379052011-08-21 16:58:20 -070081 }
82 interest.SetName (name);
83 break;
84 }
85 case CCN_DTAG_MinSuffixComponents:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080086 NS_LOG_DEBUG ("MinSuffixComponents");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070087 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
88 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070089 interest.SetMinSuffixComponents (
90 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070091 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -070092 nonNegativeIntegerVisitor
93 )));
94 break;
95 case CCN_DTAG_MaxSuffixComponents:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080096 NS_LOG_DEBUG ("MaxSuffixComponents");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070097 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
98 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070099 interest.SetMaxSuffixComponents (
100 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700101 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700102 nonNegativeIntegerVisitor
103 )));
104 break;
105 case CCN_DTAG_Exclude:
106 {
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800107 NS_LOG_DEBUG ("Exclude");
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700108 // process exclude components
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -0700109 Ptr<CcnxNameComponents> exclude = Create<CcnxNameComponents> ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700110
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700111 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700112 {
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700113 block->accept (nameComponentsVisitor, &(*exclude));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700114 }
115 interest.SetExclude (exclude);
116 break;
117 }
118 case CCN_DTAG_ChildSelector:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800119 NS_LOG_DEBUG ("ChildSelector");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700120 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
121 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700122
123 interest.SetChildSelector (
124 1 == boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700125 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700126 nonNegativeIntegerVisitor
127 )));
128 break;
129 case CCN_DTAG_AnswerOriginKind:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800130 NS_LOG_DEBUG ("AnswerOriginKind");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700131 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
132 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700133 interest.SetAnswerOriginKind (
134 1 == boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700135 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700136 nonNegativeIntegerVisitor
137 )));
138 break;
139 case CCN_DTAG_Scope:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800140 NS_LOG_DEBUG ("Scope");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700141 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
142 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700143 interest.SetScope (
144 boost::any_cast<uint32_t> (
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700145 (*n.m_nestedTags.begin())->accept(
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700146 nonNegativeIntegerVisitor
147 )));
148 break;
149 case CCN_DTAG_InterestLifetime:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800150 NS_LOG_DEBUG ("InterestLifetime");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700151 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
152 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700153
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -0700154 interest.SetInterestLifetime (
155 boost::any_cast<Time> (
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800156 (*n.m_nestedTags.begin())->accept(
157 timestampVisitor
158 )));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700159 break;
160 case CCN_DTAG_Nonce:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800161 NS_LOG_DEBUG ("Nonce");
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700162 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
163 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700164
Alexander Afanasyevf21c5be2011-08-22 00:35:54 -0700165 interest.SetNonce (
166 boost::any_cast<uint32_t> (
167 (*n.m_nestedTags.begin())->accept(
168 nonceVisitor
169 )));
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700170 break;
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800171
172
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800173 case CCN_DTAG_Nack:
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800174 NS_LOG_DEBUG ("Nack");
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800175 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
176 throw CcnbDecodingException ();
177
178 interest.SetNack (
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800179 boost::any_cast<uint32_t> (
180 (*n.m_nestedTags.begin())->accept(nonNegativeIntegerVisitor)));
Ilya Moiseenkod1f19512011-11-16 14:31:19 -0800181 break;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800182 }
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700183}
184
185} // namespace CcnbParser
186} // namespace ns3