blob: 6b1efd773dca3f9172625ba299d906c8b4b98cae [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 Afanasyev2ba8f662014-01-05 22:53:18 -080021 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 Afanasyevfadc97d2014-01-03 13:22:10 -080054void
55Data::wireDecode(const Block &wire)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070056{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080057 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 Thompson5cae5e52013-07-10 19:41:20 -070078}
79
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -080080std::ostream&
81operator << (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 Thompson5cae5e52013-07-10 19:41:20 -070093
94}