Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 1 | /* -*- 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: |
| 19 | */ |
| 20 | |
| 21 | #ifndef _CCNX_DECODING_HELPER_H_ |
| 22 | #define _CCNX_DECODING_HELPER_H_ |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <boost/any.hpp> |
| 26 | #include <list> |
| 27 | |
| 28 | #include "ns3/ptr.h" |
| 29 | #include "ns3/nstime.h" |
| 30 | #include "ns3/buffer.h" |
| 31 | #include "ns3/simple-ref-count.h" |
| 32 | |
| 33 | |
| 34 | namespace ns3 { |
| 35 | |
| 36 | namespace Name{ class Components; } |
| 37 | |
| 38 | class CcnxInterestHeader; |
| 39 | class CcnxContentObjectHeader; |
| 40 | |
| 41 | namespace CcnxParser { |
| 42 | class InterestVisitor; |
| 43 | class ContentObjectVisitor; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Helper to encode/decode ccnb formatted CCNx message |
| 48 | * |
| 49 | */ |
| 50 | class CcnxDecodingHelper |
| 51 | { |
| 52 | public: |
| 53 | static size_t |
| 54 | Deserialize (Buffer::Iterator start, const CcnxInterestHeader &interest); |
| 55 | |
| 56 | static size_t |
| 57 | Deserialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject); |
| 58 | |
| 59 | private: |
| 60 | static CcnxParser::InterestVisitor m_interestVisitor; |
| 61 | static CcnxParser::ContentObjectVisitor m_contentObjectVisitor; |
| 62 | }; |
| 63 | |
| 64 | namespace CcnxParser { |
| 65 | |
| 66 | class Block; |
| 67 | class Blob; |
| 68 | class Udata; |
| 69 | class Tag; |
| 70 | class Attr; |
| 71 | class Dtag; |
| 72 | class Dattr; |
| 73 | class Ext; |
| 74 | |
| 75 | class Visitor |
| 76 | { |
| 77 | public: |
| 78 | virtual void visit (Blob& )=0; |
| 79 | virtual void visit (Udata&)=0; |
| 80 | virtual void visit (Tag& )=0; |
| 81 | virtual void visit (Attr& )=0; |
| 82 | virtual void visit (Dtag& )=0; |
| 83 | virtual void visit (Dattr&)=0; |
| 84 | virtual void visit (Ext& )=0; |
| 85 | }; |
| 86 | |
| 87 | class GJVisitor |
| 88 | { |
| 89 | public: |
| 90 | virtual boost::any visit (Blob&, boost::any)=0; |
| 91 | virtual boost::any visit (Udata&, boost::any)=0; |
| 92 | virtual boost::any visit (Tag&, boost::any)=0; |
| 93 | virtual boost::any visit (Attr&, boost::any)=0; |
| 94 | virtual boost::any visit (Dtag&, boost::any)=0; |
| 95 | virtual boost::any visit (Dattr&, boost::any)=0; |
| 96 | virtual boost::any visit (Ext&, boost::any)=0; |
| 97 | }; |
| 98 | |
| 99 | class GJNoArguVisitor |
| 100 | { |
| 101 | public: |
| 102 | virtual boost::any visit (Blob& )=0; |
| 103 | virtual boost::any visit (Udata&)=0; |
| 104 | virtual boost::any visit (Tag& )=0; |
| 105 | virtual boost::any visit (Attr& )=0; |
| 106 | virtual boost::any visit (Dtag& )=0; |
| 107 | virtual boost::any visit (Dattr&)=0; |
| 108 | virtual boost::any visit (Ext& )=0; |
| 109 | }; |
| 110 | |
| 111 | class GJVoidVisitor |
| 112 | { |
| 113 | public: |
| 114 | virtual void visit (Blob&, boost::any)=0; |
| 115 | virtual void visit (Udata&, boost::any)=0; |
| 116 | virtual void visit (Tag&, boost::any)=0; |
| 117 | virtual void visit (Attr&, boost::any)=0; |
| 118 | virtual void visit (Dtag&, boost::any)=0; |
| 119 | virtual void visit (Dattr&, boost::any)=0; |
| 120 | virtual void visit (Ext&, boost::any)=0; |
| 121 | }; |
| 122 | |
| 123 | class Block : public SimpleRefCount<Block> |
| 124 | { |
| 125 | public: |
| 126 | /** |
| 127 | * Parsing block header and creating an appropriate object |
| 128 | */ |
| 129 | static Ptr<Block> |
| 130 | ParseBlock (Buffer::Iterator &start); |
| 131 | |
| 132 | virtual void accept( Visitor &v ) =0; |
| 133 | virtual void accept (GJVoidVisitor &v, boost::any param) =0; |
| 134 | virtual boost::any accept( GJNoArguVisitor &v ) =0; |
| 135 | virtual boost::any accept( GJVisitor &v, boost::any param ) =0; |
| 136 | }; |
| 137 | |
| 138 | class Blob : public Block |
| 139 | { |
| 140 | public: |
| 141 | Blob (Buffer::Iterator &start, uint32_t length); |
| 142 | |
| 143 | virtual void accept( Visitor &v ) { v.visit( *this ); } |
| 144 | virtual void accept( GJVoidVisitor &v, boost::any param ) { v.visit( *this, param ); } |
| 145 | virtual boost::any accept( GJNoArguVisitor &v ) { return v.visit( *this ); } |
| 146 | virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); } |
| 147 | |
| 148 | Buffer m_blob; |
| 149 | }; |
| 150 | |
| 151 | class Udata : public Block |
| 152 | { |
| 153 | public: |
| 154 | Udata (Buffer::Iterator &start, uint32_t length); |
| 155 | |
| 156 | virtual void accept( Visitor &v ) { v.visit( *this ); } |
| 157 | virtual void accept( GJVoidVisitor &v, boost::any param ) { v.visit( *this, param ); } |
| 158 | virtual boost::any accept( GJNoArguVisitor &v ) { return v.visit( *this ); } |
| 159 | virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); } |
| 160 | |
| 161 | std::string m_udata; |
| 162 | }; |
| 163 | |
| 164 | class Tag : public Block |
| 165 | { |
| 166 | public: |
| 167 | Tag (Buffer::Iterator &start, uint32_t length); |
| 168 | |
| 169 | virtual void accept( Visitor &v ) { v.visit( *this ); } |
| 170 | virtual void accept( GJVoidVisitor &v, boost::any param ) { v.visit( *this, param ); } |
| 171 | virtual boost::any accept( GJNoArguVisitor &v ) { return v.visit( *this ); } |
| 172 | virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); } |
| 173 | |
| 174 | std::string m_tag; |
| 175 | std::list<Ptr<Block> > m_nestedBlocks; |
| 176 | }; |
| 177 | |
| 178 | class Attr : public Block |
| 179 | { |
| 180 | public: |
| 181 | Attr (Buffer::Iterator &start, uint32_t length); |
| 182 | |
| 183 | virtual void accept( Visitor &v ) { v.visit( *this ); } |
| 184 | virtual void accept( GJVoidVisitor &v, boost::any param ) { v.visit( *this, param ); } |
| 185 | virtual boost::any accept( GJNoArguVisitor &v ) { return v.visit( *this ); } |
| 186 | virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); } |
| 187 | |
| 188 | std::string m_attr; |
| 189 | Ptr<Udata> m_value; |
| 190 | }; |
| 191 | |
| 192 | class Dtag : public Block |
| 193 | { |
| 194 | public: |
| 195 | Dtag (Buffer::Iterator &start, uint32_t dtag); |
| 196 | |
| 197 | virtual void accept( Visitor &v ) { v.visit( *this ); } |
| 198 | virtual void accept( GJVoidVisitor &v, boost::any param ) { v.visit( *this, param ); } |
| 199 | virtual boost::any accept( GJNoArguVisitor &v ) { return v.visit( *this ); } |
| 200 | virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); } |
| 201 | |
| 202 | uint32_t m_dtag; |
| 203 | std::list<Ptr<Block> > m_nestedBlocks; |
| 204 | }; |
| 205 | |
| 206 | class Dattr : public Block |
| 207 | { |
| 208 | public: |
| 209 | Dattr (Buffer::Iterator &start, uint32_t dattr); |
| 210 | |
| 211 | virtual void accept( Visitor &v ) { v.visit( *this ); } |
| 212 | virtual void accept( GJVoidVisitor &v, boost::any param ) { v.visit( *this, param ); } |
| 213 | virtual boost::any accept( GJNoArguVisitor &v ) { return v.visit( *this ); } |
| 214 | virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); } |
| 215 | |
| 216 | uint32_t m_dattr; |
| 217 | Ptr<Udata> m_value; |
| 218 | }; |
| 219 | |
| 220 | class Ext : public Block |
| 221 | { |
| 222 | public: |
| 223 | Ext (Buffer::Iterator &start, uint32_t extSubtype); |
| 224 | |
| 225 | virtual void accept( Visitor &v ) { v.visit( *this ); } |
| 226 | virtual void accept( GJVoidVisitor &v, boost::any param ) { v.visit( *this, param ); } |
| 227 | virtual boost::any accept( GJNoArguVisitor &v ) { return v.visit( *this ); } |
| 228 | virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); } |
| 229 | |
| 230 | uint64_t m_extSubtype; |
| 231 | }; |
| 232 | |
| 233 | class DepthFirstVisitor : public Visitor |
| 234 | { |
| 235 | public: |
| 236 | virtual void visit (Blob& ); |
| 237 | virtual void visit (Udata&); |
| 238 | virtual void visit (Tag& ); |
| 239 | virtual void visit (Attr& ); |
| 240 | virtual void visit (Dtag& ); |
| 241 | virtual void visit (Dattr&); |
| 242 | virtual void visit (Ext& ); |
| 243 | }; |
| 244 | |
| 245 | class GJDepthFirstVisitor : public GJVisitor |
| 246 | { |
| 247 | public: |
| 248 | virtual boost::any visit (Blob&, boost::any); |
| 249 | virtual boost::any visit (Udata&, boost::any); |
| 250 | virtual boost::any visit (Tag&, boost::any); |
| 251 | virtual boost::any visit (Attr&, boost::any); |
| 252 | virtual boost::any visit (Dtag&, boost::any); |
| 253 | virtual boost::any visit (Dattr&, boost::any); |
| 254 | virtual boost::any visit (Ext&, boost::any); |
| 255 | }; |
| 256 | |
| 257 | class GJNoArguDepthFirstVisitor : public GJNoArguVisitor |
| 258 | { |
| 259 | public: |
| 260 | virtual boost::any visit (Blob& ); |
| 261 | virtual boost::any visit (Udata&); |
| 262 | virtual boost::any visit (Tag& ); |
| 263 | virtual boost::any visit (Attr& ); |
| 264 | virtual boost::any visit (Dtag& ); |
| 265 | virtual boost::any visit (Dattr&); |
| 266 | virtual boost::any visit (Ext& ); |
| 267 | }; |
| 268 | |
| 269 | class GJVoidDepthFirstVisitor : public GJVoidVisitor |
| 270 | { |
| 271 | public: |
| 272 | virtual void visit (Blob&, boost::any); |
| 273 | virtual void visit (Udata&, boost::any); |
| 274 | virtual void visit (Tag&, boost::any); |
| 275 | virtual void visit (Attr&, boost::any); |
| 276 | virtual void visit (Dtag&, boost::any); |
| 277 | virtual void visit (Dattr&, boost::any); |
| 278 | virtual void visit (Ext&, boost::any); |
| 279 | }; |
| 280 | |
| 281 | // class NameComponentsVisitor : public |
| 282 | |
| 283 | class NonNegativeIntegerVisitor : public GJNoArguDepthFirstVisitor |
| 284 | { |
| 285 | public: |
| 286 | virtual boost::any visit (Blob &n); //to throw parsing error |
| 287 | virtual boost::any visit (Udata &n); |
| 288 | }; |
| 289 | |
| 290 | class StringVisitor : public GJNoArguDepthFirstVisitor |
| 291 | { |
| 292 | public: |
| 293 | virtual boost::any visit (Blob &n); //to throw parsing error |
| 294 | virtual boost::any visit (Udata &n); |
| 295 | }; |
| 296 | |
| 297 | class NameComponentsVisitor : public GJVoidDepthFirstVisitor |
| 298 | { |
| 299 | public: |
| 300 | virtual void visit (Dtag &n, boost::any param/*should be Name::Components*/); |
| 301 | private: |
| 302 | static StringVisitor m_stringVisitor; |
| 303 | }; |
| 304 | |
| 305 | class InterestVisitor : public GJVoidDepthFirstVisitor |
| 306 | { |
| 307 | public: |
| 308 | virtual void visit (Dtag &n, boost::any param/*should be CcnxInterestHeader&*/); |
| 309 | |
| 310 | private: |
| 311 | static NonNegativeIntegerVisitor m_nonNegativeIntegerVisitor; |
| 312 | static NameComponentsVisitor m_nameComponentsVisitor; |
| 313 | }; |
| 314 | |
| 315 | class ContentObjectVisitor : public GJVoidDepthFirstVisitor |
| 316 | { |
| 317 | public: |
| 318 | virtual void visit (Dtag &n, boost::any param/*should be CcnxContentObjectHeader&*/); |
| 319 | |
| 320 | private: |
| 321 | static NameComponentsVisitor m_nameComponentsVisitor; |
| 322 | }; |
| 323 | |
| 324 | |
| 325 | class CcnxDecodingException {}; |
| 326 | |
| 327 | } // namespace CcnxParser |
| 328 | |
| 329 | } // namespace ns3 |
| 330 | |
| 331 | #endif // _CCNX_DECODING_HELPER_H_ |
| 332 | |