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 | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 21 | wire_ = Block(Tlv::Data); |
| 22 | |
| 23 | // Name |
| 24 | wire_.push_back(getName().wireEncode()); |
| 25 | |
| 26 | // MetaInfo |
| 27 | wire_.push_back(getMetaInfo().wireEncode()); |
| 28 | |
| 29 | // Content |
| 30 | wire_.push_back(content_); |
| 31 | |
| 32 | if (!signature_) { |
| 33 | throw Error("Requested wire format, but data packet has not been signed yet"); |
| 34 | } |
| 35 | |
| 36 | /////////////// |
| 37 | // Signature // |
| 38 | /////////////// |
| 39 | |
| 40 | // SignatureInfo |
| 41 | wire_.push_back(signature_.getInfo()); |
| 42 | |
| 43 | // SignatureValue |
| 44 | wire_.push_back(signature_.getValue()); |
| 45 | |
| 46 | wire_.encode(); |
| 47 | return wire_; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Decode the input using a particular wire format and update this Data. |
| 52 | * @param input The input byte array to be decoded. |
| 53 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 54 | void |
| 55 | Data::wireDecode(const Block &wire) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 56 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 57 | wire_ = wire; |
| 58 | wire_.parse(); |
| 59 | |
| 60 | // Name |
| 61 | name_.wireDecode(wire_.get(Tlv::Name)); |
| 62 | |
| 63 | // MetaInfo |
| 64 | metaInfo_.wireDecode(wire_.get(Tlv::MetaInfo)); |
| 65 | |
| 66 | // Content |
| 67 | content_ = wire_.get(Tlv::Content); |
| 68 | |
| 69 | /////////////// |
| 70 | // Signature // |
| 71 | /////////////// |
| 72 | |
| 73 | // SignatureInfo |
| 74 | signature_.setInfo(wire_.get(Tlv::SignatureInfo)); |
| 75 | |
| 76 | // SignatureValue |
| 77 | signature_.setValue(wire_.get(Tlv::SignatureValue)); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 80 | std::ostream& |
| 81 | operator << (std::ostream &os, const Data &data) |
| 82 | { |
| 83 | os << "Name: " << data.getName() << "\n"; |
| 84 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 85 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
| 86 | os << "Signature: (type: " << data.getSignature().getType() << |
| 87 | ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
| 88 | os << std::endl; |
| 89 | |
| 90 | return os; |
| 91 | } |
| 92 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 93 | |
| 94 | } |