blob: aa8efcc9d60cd9bfdeb8bbf9b6320d04ef332ef7 [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>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08009 * Yingdi Yu <yingdi@cs.ucla.edu>
10 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Mengjin Yanaec70742014-08-25 10:37:45 -070011 */
12#include "chatroom-info.hpp"
13
Yingdi Yueb692ac2015-02-10 18:46:18 -080014namespace chronochat {
Mengjin Yanaec70742014-08-25 10:37:45 -070015
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080016BOOST_CONCEPT_ASSERT((ndn::WireEncodable<ChatroomInfo>));
17BOOST_CONCEPT_ASSERT((ndn::WireDecodable<ChatroomInfo>));
18
Mengjin Yanaec70742014-08-25 10:37:45 -070019ChatroomInfo::ChatroomInfo()
20{
21}
22
23ChatroomInfo::ChatroomInfo(const Block& chatroomWire)
24{
25 this->wireDecode(chatroomWire);
26}
27
Varun Patil3d850902020-11-23 12:19:14 +053028template<ndn::encoding::Tag T>
Mengjin Yanaec70742014-08-25 10:37:45 -070029size_t
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070030ChatroomInfo::wireEncode(ndn::EncodingImpl<T>& encoder) const
Mengjin Yanaec70742014-08-25 10:37:45 -070031{
32 size_t totalLength = 0;
33
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070034 // ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
35 // ChatroomName
36 // TrustModel
37 // ChatroomPrefix
38 // ManagerPrefix
39 // Participants
40 //
41 // ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
42 // NameComponent
43 //
44 // TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
45 // nonNegativeInteger
46 //
47 // ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
48 // Name
49 //
50 // ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
51 // Name
52 //
53 // Participants := PARTICIPANTS-TYPE TLV-LENGTH
54 // Name+
Mengjin Yanaec70742014-08-25 10:37:45 -070055
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070056 // Participants
57 size_t participantsLength = 0;
58 for (std::list<Name>::const_reverse_iterator it = m_participants.rbegin();
Mengjin Yanaec70742014-08-25 10:37:45 -070059 it != m_participants.rend(); ++it) {
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070060 participantsLength += it->wireEncode(encoder);
Mengjin Yanaec70742014-08-25 10:37:45 -070061 }
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070062 participantsLength += encoder.prependVarNumber(participantsLength);
63 participantsLength += encoder.prependVarNumber(tlv::Participants);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070064 totalLength += participantsLength;
Mengjin Yanaec70742014-08-25 10:37:45 -070065
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070066 // Manager Prefix
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070067 size_t managerLength = m_manager.wireEncode(encoder);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070068 totalLength += managerLength;
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070069 totalLength += encoder.prependVarNumber(managerLength);
70 totalLength += encoder.prependVarNumber(tlv::ManagerPrefix);
Mengjin Yanaec70742014-08-25 10:37:45 -070071
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070072 // Chatroom Sync Prefix
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070073 size_t chatroomSyncPrefixLength = m_syncPrefix.wireEncode(encoder);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070074 totalLength += chatroomSyncPrefixLength;
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070075 totalLength += encoder.prependVarNumber(chatroomSyncPrefixLength);
76 totalLength += encoder.prependVarNumber(tlv::ChatroomPrefix);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070077
78 // Trust Model
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070079 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::TrustModel, m_trustModel);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070080
81 // Chatroom Name
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070082 size_t chatroomNameLength = m_chatroomName.wireEncode(encoder);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070083 totalLength += chatroomNameLength;
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070084 totalLength += encoder.prependVarNumber(chatroomNameLength);
85 totalLength += encoder.prependVarNumber(tlv::ChatroomName);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070086
87 // Chatroom Info
Alexander Afanasyev28eb9092015-06-29 20:15:11 -070088 totalLength += encoder.prependVarNumber(totalLength);
89 totalLength += encoder.prependVarNumber(tlv::ChatroomInfo);
Mengjin Yanaec70742014-08-25 10:37:45 -070090
91 return totalLength;
92}
93
94const Block&
95ChatroomInfo::wireEncode() const
96{
97 ndn::EncodingEstimator estimator;
98 size_t estimatedSize = wireEncode(estimator);
99
100 ndn::EncodingBuffer buffer(estimatedSize, 0);
101 wireEncode(buffer);
102
103 m_wire = buffer.block();
104 m_wire.parse();
105
106 return m_wire;
107}
108
109void
110ChatroomInfo::wireDecode(const Block& chatroomWire)
111{
112 m_wire = chatroomWire;
113 m_wire.parse();
114
115 m_participants.clear();
116
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700117 // ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
118 // ChatroomName
119 // TrustModel
120 // ChatroomPrefix
121 // ManagerPrefix
122 // Participants
123 //
124 // ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
125 // NameComponent
126 //
127 // TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
128 // nonNegativeInteger
129 //
130 // ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
131 // Name
132 //
133 // ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
134 // Name
135 //
136 // Participants := PARTICIPANTS-TYPE TLV-LENGTH
137 // Name+
Mengjin Yanaec70742014-08-25 10:37:45 -0700138
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700139 if (m_wire.type() != tlv::ChatroomInfo)
Mengjin Yanaec70742014-08-25 10:37:45 -0700140 throw Error("Unexpected TLV number when decoding chatroom packet");
141
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700142 // Chatroom Info
Mengjin Yanaec70742014-08-25 10:37:45 -0700143 Block::element_const_iterator i = m_wire.elements_begin();
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800144 if (i == m_wire.elements_end())
145 throw Error("Missing Chatroom Name");
146 if (i->type() != tlv::ChatroomName)
147 throw Error("Expect Chatroom Name but get TLV Type " + std::to_string(i->type()));
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700148 m_chatroomName.wireDecode(i->blockFromValue());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700149 ++i;
150
151 // Trust Model
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800152 if (i == m_wire.elements_end())
153 throw Error("Missing Trust Model");
154 if (i->type() != tlv::TrustModel)
155 throw Error("Expect Trust Model but get TLV Type " + std::to_string(i->type()));
Mengjin Yanaec70742014-08-25 10:37:45 -0700156 m_trustModel =
157 static_cast<TrustModel>(readNonNegativeInteger(*i));
Mengjin Yanaec70742014-08-25 10:37:45 -0700158 ++i;
159
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700160 // Chatroom Sync Prefix
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800161 if (i == m_wire.elements_end())
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700162 throw Error("Missing Chatroom Prefix");
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800163 if (i->type() != tlv::ChatroomPrefix)
164 throw Error("Expect Chatroom Prefix but get TLV Type " + std::to_string(i->type()));
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700165 m_syncPrefix.wireDecode(i->blockFromValue());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700166 ++i;
167
168 // Manager Prefix
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800169 if (i == m_wire.elements_end())
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700170 throw Error("Missing Manager Prefix");
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800171 if (i->type() != tlv::ManagerPrefix)
172 throw Error("Expect Manager Prefix but get TLV Type " + std::to_string(i->type()));
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700173 m_manager.wireDecode(i->blockFromValue());
174 ++i;
175
176 // Participants
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800177 if (i == m_wire.elements_end())
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700178 throw Error("Missing Participant");
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800179 if (i->type() != tlv::Participants)
180 throw Error("Expect Participant but get TLV Type " + std::to_string(i->type()));
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700181
182 Block temp = *i;
183 temp.parse();
184
185 Block::element_const_iterator j = temp.elements_begin();
186
187 while (j != temp.elements_end() && j->type() == tlv::Name) {
188 m_participants.push_back(Name(*j));
189 ++j;
Mengjin Yanaec70742014-08-25 10:37:45 -0700190 }
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700191 if (j != temp.elements_end())
192 throw Error("Unexpected element");
193
Mengjin Yanaec70742014-08-25 10:37:45 -0700194 if (m_participants.empty())
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800195 throw Error("No participant in the chatroom");
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700196
197 ++i;
198
Mengjin Yanaec70742014-08-25 10:37:45 -0700199 if (i != m_wire.elements_end()) {
200 throw Error("Unexpected element");
201 }
202}
203
204void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700205ChatroomInfo::setName(const Name::Component& name)
206{
207 m_wire.reset();
208 m_chatroomName = name;
209}
210
211void
212ChatroomInfo::setTrustModel(const TrustModel trustModel)
213{
214 m_wire.reset();
215 m_trustModel = trustModel;
216}
217
218void
Mengjin Yanaec70742014-08-25 10:37:45 -0700219ChatroomInfo::addParticipant(const Name& participant)
220{
221 m_wire.reset();
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800222 if (find(m_participants.begin(), m_participants.end(), participant) ==
223 m_participants.end()) {
224 m_participants.push_back(participant);
225 }
Mengjin Yanaec70742014-08-25 10:37:45 -0700226}
227
228void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700229ChatroomInfo::removeParticipant(const Name& participant)
Mengjin Yanaec70742014-08-25 10:37:45 -0700230{
231 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700232 m_participants.remove(participant);
Mengjin Yanaec70742014-08-25 10:37:45 -0700233}
234
235void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700236ChatroomInfo::setSyncPrefix(const Name& prefix)
Mengjin Yanaec70742014-08-25 10:37:45 -0700237{
238 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700239 m_syncPrefix = prefix;
Mengjin Yanaec70742014-08-25 10:37:45 -0700240}
241
242void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700243ChatroomInfo::setManager(const Name& manager)
Mengjin Yanaec70742014-08-25 10:37:45 -0700244{
245 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700246 m_manager = manager;
Mengjin Yanaec70742014-08-25 10:37:45 -0700247}
248
Yingdi Yueb692ac2015-02-10 18:46:18 -0800249} // namespace chronochat