blob: 2bbe9a16f8681edc19b142976585ef45ee470331 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson25b4e612013-10-10 16:03:24 -07008#include <ndn-cpp/common.hpp>
9#include <ndn-cpp/data.hpp>
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010
11using namespace std;
12
13namespace ndn {
14
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080015const Block&
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080016Data::wireEncode() const
Jeff Thompson5cae5e52013-07-10 19:41:20 -070017{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080018 if (wire_.hasWire())
19 return wire_;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080020
Alexander Afanasyev277f4692014-01-03 15:29:55 -080021 // Data ::= DATA-TLV TLV-LENGTH
22 // Name
23 // MetaInfo
24 // Content
25 // Signature
26
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080027 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
36 wire_.push_back(content_);
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 Afanasyevfadc97d2014-01-03 13:22:10 -080060void
61Data::wireDecode(const Block &wire)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070062{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080063 wire_ = wire;
64 wire_.parse();
65
Alexander Afanasyev277f4692014-01-03 15:29:55 -080066 // Data ::= DATA-TLV TLV-LENGTH
67 // Name
68 // MetaInfo
69 // Content
70 // Signature
71
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080072 // 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 Thompson5cae5e52013-07-10 19:41:20 -070090}
91
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -080092std::ostream&
93operator << (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 Thompson5cae5e52013-07-10 19:41:20 -0700105
106}