blob: e8d47eb72c188575c20c9d3b6253d270cb688605 [file] [log] [blame]
Mengjin Yanaec70742014-08-25 10:37:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Mengjin Yanaec70742014-08-25 10:37:45 -07004 * 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)
Varun Patila24bd3e2020-11-24 10:08:33 +0530140 NDN_THROW(Error("Unexpected TLV number when decoding chatroom packet"));
Mengjin Yanaec70742014-08-25 10:37:45 -0700141
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())
Varun Patila24bd3e2020-11-24 10:08:33 +0530145 NDN_THROW(Error("Missing Chatroom Name"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800146 if (i->type() != tlv::ChatroomName)
Varun Patila24bd3e2020-11-24 10:08:33 +0530147 NDN_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())
Varun Patila24bd3e2020-11-24 10:08:33 +0530153 NDN_THROW(Error("Missing Trust Model"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800154 if (i->type() != tlv::TrustModel)
Varun Patila24bd3e2020-11-24 10:08:33 +0530155 NDN_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())
Varun Patila24bd3e2020-11-24 10:08:33 +0530162 NDN_THROW(Error("Missing Chatroom Prefix"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800163 if (i->type() != tlv::ChatroomPrefix)
Varun Patila24bd3e2020-11-24 10:08:33 +0530164 NDN_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())
Varun Patila24bd3e2020-11-24 10:08:33 +0530170 NDN_THROW(Error("Missing Manager Prefix"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800171 if (i->type() != tlv::ManagerPrefix)
Varun Patila24bd3e2020-11-24 10:08:33 +0530172 NDN_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())
Varun Patila24bd3e2020-11-24 10:08:33 +0530178 NDN_THROW(Error("Missing Participant"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800179 if (i->type() != tlv::Participants)
Varun Patila24bd3e2020-11-24 10:08:33 +0530180 NDN_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())
Varun Patila24bd3e2020-11-24 10:08:33 +0530192 NDN_THROW(Error("Unexpected element"));
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700193
Mengjin Yanaec70742014-08-25 10:37:45 -0700194 if (m_participants.empty())
Varun Patila24bd3e2020-11-24 10:08:33 +0530195 NDN_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()) {
Varun Patila24bd3e2020-11-24 10:08:33 +0530200 NDN_THROW(Error("Unexpected element"));
Mengjin Yanaec70742014-08-25 10:37:45 -0700201 }
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