Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 8 | #include <ndn-cpp/common.hpp> |
| 9 | #include <ndn-cpp/data.hpp> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 10 | |
| 11 | using namespace std; |
| 12 | |
| 13 | namespace ndn { |
| 14 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 15 | const Block& |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 16 | Data::wireEncode() const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 17 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 18 | if (wire_.hasWire()) |
| 19 | return wire_; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 20 | |
Alexander Afanasyev | 277f469 | 2014-01-03 15:29:55 -0800 | [diff] [blame] | 21 | // Data ::= DATA-TLV TLV-LENGTH |
| 22 | // Name |
| 23 | // MetaInfo |
| 24 | // Content |
| 25 | // Signature |
| 26 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 27 | wire_ = Block(Tlv::Data); |
| 28 | |
| 29 | // Name |
| 30 | wire_.push_back(getName().wireEncode()); |
| 31 | |
| 32 | // MetaInfo |
| 33 | wire_.push_back(getMetaInfo().wireEncode()); |
| 34 | |
| 35 | // Content |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame^] | 36 | wire_.push_back(getContent()); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 37 | |
| 38 | if (!signature_) { |
| 39 | throw Error("Requested wire format, but data packet has not been signed yet"); |
| 40 | } |
| 41 | |
| 42 | /////////////// |
| 43 | // Signature // |
| 44 | /////////////// |
| 45 | |
| 46 | // SignatureInfo |
| 47 | wire_.push_back(signature_.getInfo()); |
| 48 | |
| 49 | // SignatureValue |
| 50 | wire_.push_back(signature_.getValue()); |
| 51 | |
| 52 | wire_.encode(); |
| 53 | return wire_; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Decode the input using a particular wire format and update this Data. |
| 58 | * @param input The input byte array to be decoded. |
| 59 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 60 | void |
| 61 | Data::wireDecode(const Block &wire) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 62 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 63 | wire_ = wire; |
| 64 | wire_.parse(); |
| 65 | |
Alexander Afanasyev | 277f469 | 2014-01-03 15:29:55 -0800 | [diff] [blame] | 66 | // Data ::= DATA-TLV TLV-LENGTH |
| 67 | // Name |
| 68 | // MetaInfo |
| 69 | // Content |
| 70 | // Signature |
| 71 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 72 | // Name |
| 73 | name_.wireDecode(wire_.get(Tlv::Name)); |
| 74 | |
| 75 | // MetaInfo |
| 76 | metaInfo_.wireDecode(wire_.get(Tlv::MetaInfo)); |
| 77 | |
| 78 | // Content |
| 79 | content_ = wire_.get(Tlv::Content); |
| 80 | |
| 81 | /////////////// |
| 82 | // Signature // |
| 83 | /////////////// |
| 84 | |
| 85 | // SignatureInfo |
| 86 | signature_.setInfo(wire_.get(Tlv::SignatureInfo)); |
| 87 | |
| 88 | // SignatureValue |
| 89 | signature_.setValue(wire_.get(Tlv::SignatureValue)); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 92 | std::ostream& |
| 93 | operator << (std::ostream &os, const Data &data) |
| 94 | { |
| 95 | os << "Name: " << data.getName() << "\n"; |
| 96 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 97 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
| 98 | os << "Signature: (type: " << data.getSignature().getType() << |
| 99 | ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
| 100 | os << std::endl; |
| 101 | |
| 102 | return os; |
| 103 | } |
| 104 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 105 | |
| 106 | } |