blob: 94732f70d02755486e387f32c95cff6096f9448a [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-content-object-visitor.h"
22#include "ccnb-parser-name-components-visitor.h"
Alexander Afanasyev9568f952012-04-05 16:09:14 -070023#include "ccnb-parser-non-negative-integer-visitor.h"
24#include "ccnb-parser-timestamp-visitor.h"
Alexander Afanasyev8b379052011-08-21 16:58:20 -070025
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080026#include "../syntax-tree/ccnb-parser-block.h"
27#include "../syntax-tree/ccnb-parser-dtag.h"
28
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070029#include "ns3/ccnx-name-components.h"
Alexander Afanasyev8b379052011-08-21 16:58:20 -070030#include "ns3/assert.h"
Alexander Afanasyev9568f952012-04-05 16:09:14 -070031#include "ns3/log.h"
Alexander Afanasyev8b379052011-08-21 16:58:20 -070032
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070033#include "ns3/ccnx-content-object-header.h"
34
35#include <boost/foreach.hpp>
36
Alexander Afanasyev9568f952012-04-05 16:09:14 -070037NS_LOG_COMPONENT_DEFINE ("CcnbParserContentObjectVisitor");
38
Alexander Afanasyev8b379052011-08-21 16:58:20 -070039namespace ns3 {
40namespace CcnbParser {
41
42// We don't really care about any other fields
43void
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070044ContentObjectVisitor::visit (Dtag &n, boost::any param/*should be CcnxContentObjectHeader* */)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070045{
46 // uint32_t n.m_dtag;
47 // std::list<Ptr<Block> > n.m_nestedBlocks;
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070048 static NameComponentsVisitor nameComponentsVisitor;
Alexander Afanasyev9568f952012-04-05 16:09:14 -070049 static NonNegativeIntegerVisitor nonNegativeIntegerVisitor;
50 static TimestampVisitor timestampVisitor;
Alexander Afanasyev8b379052011-08-21 16:58:20 -070051
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070052 CcnxContentObjectHeader &contentObject = *(boost::any_cast<CcnxContentObjectHeader*> (param));
Alexander Afanasyev8b379052011-08-21 16:58:20 -070053
54 switch (n.m_dtag)
55 {
56 case CCN_DTAG_ContentObject:
57 // process nested blocks
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070058 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070059 {
60 block->accept (*this, param);
61 }
62 break;
63 case CCN_DTAG_Name:
64 {
65 // process name components
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070066 Ptr<CcnxNameComponents> name = Create<CcnxNameComponents> ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070067
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070068 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070069 {
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070070 block->accept (nameComponentsVisitor, &(*name));
Alexander Afanasyev8b379052011-08-21 16:58:20 -070071 }
72 contentObject.SetName (name);
73 break;
74 }
Alexander Afanasyev9568f952012-04-05 16:09:14 -070075
Alexander Afanasyev8b379052011-08-21 16:58:20 -070076 case CCN_DTAG_Signature: // ignoring
77 break;
Alexander Afanasyev9568f952012-04-05 16:09:14 -070078
79 case CCN_DTAG_SignedInfo:
80 // process nested blocks
81 BOOST_FOREACH (Ptr<Block> block, n.m_nestedTags)
82 {
83 block->accept (*this, param);
84 }
Alexander Afanasyev8b379052011-08-21 16:58:20 -070085 break;
Alexander Afanasyev9568f952012-04-05 16:09:14 -070086
87 case CCN_DTAG_Timestamp:
88 NS_LOG_DEBUG ("Timestamp");
89 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
90 throw CcnbDecodingException ();
91
92 contentObject.SetTimestamp (
93 boost::any_cast<Time> (
94 (*n.m_nestedTags.begin())->accept(
95 timestampVisitor
96 )));
97 break;
98
99 case CCN_DTAG_FreshnessSeconds:
100 NS_LOG_DEBUG ("FreshnessSeconds");
101
102 if (n.m_nestedTags.size()!=1) // should be exactly one UDATA inside this tag
103 throw CcnbDecodingException ();
104 contentObject.SetFreshness (
105 Seconds (
106 boost::any_cast<uint32_t> (
107 (*n.m_nestedTags.begin())->accept(
108 nonNegativeIntegerVisitor
109 ))));
110
111 break;
112
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700113 case CCN_DTAG_Content: // !!! HACK
114 // This hack was necessary for memory optimizations (i.e., content is virtual payload)
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -0700115 NS_ASSERT_MSG (n.m_nestedTags.size() == 0, "Parser should have stopped just after processing <Content> tag");
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700116 break;
Alexander Afanasyev9568f952012-04-05 16:09:14 -0700117
118 default: // ignore all other stuff
119 break;
Alexander Afanasyev8b379052011-08-21 16:58:20 -0700120 }
121}
122
123} // namespace CcnbParser
124} // namespace ns3