blob: 0bf5296aa06c673f77f8fc53637fbdb5f2a7b270 [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 "dtag.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070022
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070023#include "base-attr.h"
24#include "base-tag.h"
Alexander Afanasyev8b379052011-08-21 16:58:20 -070025
Alexander Afanasyev1043c702013-07-15 16:21:09 -070026NDN_NAMESPACE_BEGIN
27
28namespace wire {
Alexander Afanasyev8b379052011-08-21 16:58:20 -070029namespace CcnbParser {
30
31Dtag::Dtag (Buffer::Iterator &start, uint32_t dtag)
32{
33 m_dtag = dtag;
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070034 // std::cout << m_dtag << ", position: " << Block::counter << "\n";
Alexander Afanasyev8b379052011-08-21 16:58:20 -070035 /**
36 * Hack
37 *
Alexander Afanasyev6315ef72012-06-01 20:56:31 -070038 * Stop processing after encountering "Content" dtag. Actual
Alexander Afanasyev8b379052011-08-21 16:58:20 -070039 * content (including virtual payload) will be stored in Packet
40 * buffer
41 */
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070042 if (dtag == CCN_DTAG_Content)
Alexander Afanasyevb4bf3ef2013-07-30 10:40:55 -070043 {
44 Block::ParseBlock (start, true); // process length field and ignore it
45 return; // hack #1. Do not process nesting block for <Content>
46 }
Alexander Afanasyev8b379052011-08-21 16:58:20 -070047
48 // parse attributes until first nested block reached
Alexander Afanasyevc1e33eb2012-05-31 12:13:17 -070049 while (!start.IsEnd () && BufferIteratorPeekU8 (start)!=CCN_CLOSE)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070050 {
51 Ptr<Block> block = Block::ParseBlock (start);
52 if (DynamicCast<BaseAttr> (block)!=0)
53 m_attrs.push_back (block);
54 else
55 {
56 m_nestedTags.push_back (block);
57 break;
58 }
59 }
60
61 // parse the rest of nested blocks
Alexander Afanasyevc1e33eb2012-05-31 12:13:17 -070062 while (!start.IsEnd () && BufferIteratorPeekU8 (start)!=CCN_CLOSE)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070063 {
64 // hack #2. Stop processing nested blocks if last block was <Content>
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070065 if (m_dtag == CCN_DTAG_Data && // we are in <Data>
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070066 DynamicCast<Dtag> (m_nestedTags.back())!=0 && // last block is DTAG
67 DynamicCast<Dtag> (m_nestedTags.back())->m_dtag == CCN_DTAG_Content)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070068 {
69 return;
70 }
71
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070072 m_nestedTags.push_back (Block::ParseBlock (start));
Alexander Afanasyev8b379052011-08-21 16:58:20 -070073 }
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070074
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070075 // hack #3. Stop processing when last tag was <Data>
76 if (m_dtag == CCN_DTAG_Data && // we are in <Data>
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070077 DynamicCast<Dtag> (m_nestedTags.back())!=0 && // last block is DTAG
78 DynamicCast<Dtag> (m_nestedTags.back())->m_dtag == CCN_DTAG_Content)
79 {
80 return;
81 }
82
Alexander Afanasyev8b379052011-08-21 16:58:20 -070083 if (start.IsEnd ())
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070084 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070085
86 start.ReadU8 (); // read CCN_CLOSE
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070087 // std::cout << "closer, position = " << Block::counter << "\n";
88 // Block::counter ++;
Alexander Afanasyev8b379052011-08-21 16:58:20 -070089}
90
Alexander Afanasyev1043c702013-07-15 16:21:09 -070091} // namespace CcnbParser
92} // namespace wire
93
94NDN_NAMESPACE_END