blob: 2b237198203130b30b42843c4d2ef8d0e7a2d116 [file] [log] [blame]
Mengjin Yanaec70742014-08-25 10:37:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Mengjin Yan <jane.yan0129@gmail.com>
9 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
10 */
11#include "chatroom-info.hpp"
12
13namespace chronos {
14
15ChatroomInfo::ChatroomInfo()
16{
17}
18
19ChatroomInfo::ChatroomInfo(const Block& chatroomWire)
20{
21 this->wireDecode(chatroomWire);
22}
23
24template<bool T>
25size_t
26ChatroomInfo::wireEncode(ndn::EncodingImpl<T>& block) const
27{
28 size_t totalLength = 0;
29
30 //Chatroom := CHATROOM-TYPE TLV-LENGTH
31 // TrustModel
32 // Participant+
33
34 //Participants
35 for (std::vector<Name>::const_reverse_iterator it = m_participants.rbegin();
36 it != m_participants.rend(); ++it) {
37 size_t entryLength = 0;
38
39 entryLength += it->wireEncode(block);
40 entryLength += block.prependVarNumber(entryLength);
41 entryLength += block.prependVarNumber(tlv::PARTICIPANT);
42 totalLength += entryLength;
43 }
44
45 //TrustModel
46 totalLength += prependNonNegativeIntegerBlock(block, tlv::TRUSTMODEL, m_trustModel);
47
48 //type = TYPE_CHATROOM;
49 totalLength += block.prependVarNumber(totalLength);
50 totalLength += block.prependVarNumber(tlv::CHATROOM);
51
52 return totalLength;
53}
54
55const Block&
56ChatroomInfo::wireEncode() const
57{
58 ndn::EncodingEstimator estimator;
59 size_t estimatedSize = wireEncode(estimator);
60
61 ndn::EncodingBuffer buffer(estimatedSize, 0);
62 wireEncode(buffer);
63
64 m_wire = buffer.block();
65 m_wire.parse();
66
67 return m_wire;
68}
69
70void
71ChatroomInfo::wireDecode(const Block& chatroomWire)
72{
73 m_wire = chatroomWire;
74 m_wire.parse();
75
76 m_participants.clear();
77
78 //Chatroom := CHATROOM-TYPE TLV-LENGTH
79 // TrustModel
80 // Participant+
81
82 if (m_wire.type() != tlv::CHATROOM)
83 throw Error("Unexpected TLV number when decoding chatroom packet");
84
85 Block::element_const_iterator i = m_wire.elements_begin();
86
87 //TrustModel
88 if (i == m_wire.elements_end() || i->type() != tlv::TRUSTMODEL)
89 throw Error("Missing TrustModel");
90 m_trustModel =
91 static_cast<TrustModel>(readNonNegativeInteger(*i));
92
93 ++i;
94
95 //Participants
96 for (; i != m_wire.elements_end() && i->type() == tlv::PARTICIPANT; ++i) {
97 Name name;
98 name.wireDecode(i->blockFromValue());
99 m_participants.push_back(name);
100 }
101 if (m_participants.empty())
102 throw Error("Missing Participant");
103 if (i != m_wire.elements_end()) {
104 throw Error("Unexpected element");
105 }
106}
107
108void
109ChatroomInfo::addParticipant(const Name& participant)
110{
111 m_wire.reset();
112 m_participants.push_back(participant);
113}
114
115void
116ChatroomInfo::addContact(const Name& contact)
117{
118 m_wire.reset();
119 m_contacts.push_back(contact);
120}
121
122void
123ChatroomInfo::setName(const Name::Component& name)
124{
125 m_wire.reset();
126 m_name = name;
127}
128
129void
130ChatroomInfo::setTrustModel(const TrustModel trustModel)
131{
132 m_wire.reset();
133 m_trustModel = trustModel;
134}
135
136} //namespace chronos