blob: bd705dad69061494a9234570c4521c86764bd16d [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
Yingdi Yueb692ac2015-02-10 18:46:18 -080013namespace chronochat {
Mengjin Yanaec70742014-08-25 10:37:45 -070014
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
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070030 // ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
31 // ChatroomName
32 // TrustModel
33 // ChatroomPrefix
34 // ManagerPrefix
35 // Participants
36 //
37 // ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
38 // NameComponent
39 //
40 // TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
41 // nonNegativeInteger
42 //
43 // ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
44 // Name
45 //
46 // ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
47 // Name
48 //
49 // Participants := PARTICIPANTS-TYPE TLV-LENGTH
50 // Name+
Mengjin Yanaec70742014-08-25 10:37:45 -070051
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070052 // Participants
53 size_t participantsLength = 0;
54 for (std::list<Name>::const_reverse_iterator it = m_participants.rbegin();
Mengjin Yanaec70742014-08-25 10:37:45 -070055 it != m_participants.rend(); ++it) {
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070056 participantsLength += it->wireEncode(block);
Mengjin Yanaec70742014-08-25 10:37:45 -070057 }
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070058 participantsLength += block.prependVarNumber(participantsLength);
59 participantsLength += block.prependVarNumber(tlv::Participants);
60 totalLength += participantsLength;
Mengjin Yanaec70742014-08-25 10:37:45 -070061
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070062 // Manager Prefix
63 size_t managerLength = m_manager.wireEncode(block);
64 totalLength += managerLength;
65 totalLength += block.prependVarNumber(managerLength);
66 totalLength += block.prependVarNumber(tlv::ManagerPrefix);
Mengjin Yanaec70742014-08-25 10:37:45 -070067
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070068 // Chatroom Sync Prefix
69 size_t chatroomSyncPrefixLength = m_syncPrefix.wireEncode(block);
70 totalLength += chatroomSyncPrefixLength;
71 totalLength += block.prependVarNumber(chatroomSyncPrefixLength);
72 totalLength += block.prependVarNumber(tlv::ChatroomPrefix);
73
74 // Trust Model
Yingdi Yu1cc45d92015-02-09 14:19:54 -080075 totalLength += ndn::prependNonNegativeIntegerBlock(block, tlv::TrustModel, m_trustModel);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070076
77 // Chatroom Name
78 size_t chatroomNameLength = m_chatroomName.wireEncode(block);
79 totalLength += chatroomNameLength;
80 totalLength += block.prependVarNumber(chatroomNameLength);
81 totalLength += block.prependVarNumber(tlv::ChatroomName);
82
83 // Chatroom Info
Mengjin Yanaec70742014-08-25 10:37:45 -070084 totalLength += block.prependVarNumber(totalLength);
Qiuhan Ding5d98cc52014-10-30 15:17:53 -070085 totalLength += block.prependVarNumber(tlv::ChatroomInfo);
Mengjin Yanaec70742014-08-25 10:37:45 -070086
87 return totalLength;
88}
89
90const Block&
91ChatroomInfo::wireEncode() const
92{
93 ndn::EncodingEstimator estimator;
94 size_t estimatedSize = wireEncode(estimator);
95
96 ndn::EncodingBuffer buffer(estimatedSize, 0);
97 wireEncode(buffer);
98
99 m_wire = buffer.block();
100 m_wire.parse();
101
102 return m_wire;
103}
104
105void
106ChatroomInfo::wireDecode(const Block& chatroomWire)
107{
108 m_wire = chatroomWire;
109 m_wire.parse();
110
111 m_participants.clear();
112
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700113 // ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
114 // ChatroomName
115 // TrustModel
116 // ChatroomPrefix
117 // ManagerPrefix
118 // Participants
119 //
120 // ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
121 // NameComponent
122 //
123 // TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
124 // nonNegativeInteger
125 //
126 // ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
127 // Name
128 //
129 // ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
130 // Name
131 //
132 // Participants := PARTICIPANTS-TYPE TLV-LENGTH
133 // Name+
Mengjin Yanaec70742014-08-25 10:37:45 -0700134
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700135 if (m_wire.type() != tlv::ChatroomInfo)
Mengjin Yanaec70742014-08-25 10:37:45 -0700136 throw Error("Unexpected TLV number when decoding chatroom packet");
137
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700138 // Chatroom Info
Mengjin Yanaec70742014-08-25 10:37:45 -0700139 Block::element_const_iterator i = m_wire.elements_begin();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700140 if (i == m_wire.elements_end() || i->type() != tlv::ChatroomName)
141 throw Error("Missing Chatroom Name Info");
142 m_chatroomName.wireDecode(i->blockFromValue());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700143 ++i;
144
145 // Trust Model
146 if (i == m_wire.elements_end() || i->type() != tlv::TrustModel)
Mengjin Yanaec70742014-08-25 10:37:45 -0700147 throw Error("Missing TrustModel");
148 m_trustModel =
149 static_cast<TrustModel>(readNonNegativeInteger(*i));
Mengjin Yanaec70742014-08-25 10:37:45 -0700150 ++i;
151
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700152 // Chatroom Sync Prefix
153 if (i == m_wire.elements_end() || i->type() != tlv::ChatroomPrefix)
154 throw Error("Missing Chatroom Prefix");
155 m_syncPrefix.wireDecode(i->blockFromValue());
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700156 ++i;
157
158 // Manager Prefix
159 if (i == m_wire.elements_end() || i->type() != tlv::ManagerPrefix)
160 throw Error("Missing Manager Prefix");
161 m_manager.wireDecode(i->blockFromValue());
162 ++i;
163
164 // Participants
165 if (i == m_wire.elements_end() || i->type() != tlv::Participants)
166 throw Error("Missing Participant");
167
168 Block temp = *i;
169 temp.parse();
170
171 Block::element_const_iterator j = temp.elements_begin();
172
173 while (j != temp.elements_end() && j->type() == tlv::Name) {
174 m_participants.push_back(Name(*j));
175 ++j;
Mengjin Yanaec70742014-08-25 10:37:45 -0700176 }
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700177 if (j != temp.elements_end())
178 throw Error("Unexpected element");
179
Mengjin Yanaec70742014-08-25 10:37:45 -0700180 if (m_participants.empty())
181 throw Error("Missing Participant");
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700182
183 ++i;
184
Mengjin Yanaec70742014-08-25 10:37:45 -0700185 if (i != m_wire.elements_end()) {
186 throw Error("Unexpected element");
187 }
188}
189
190void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700191ChatroomInfo::setName(const Name::Component& name)
192{
193 m_wire.reset();
194 m_chatroomName = name;
195}
196
197void
198ChatroomInfo::setTrustModel(const TrustModel trustModel)
199{
200 m_wire.reset();
201 m_trustModel = trustModel;
202}
203
204void
Mengjin Yanaec70742014-08-25 10:37:45 -0700205ChatroomInfo::addParticipant(const Name& participant)
206{
207 m_wire.reset();
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800208 if (find(m_participants.begin(), m_participants.end(), participant) ==
209 m_participants.end()) {
210 m_participants.push_back(participant);
211 }
Mengjin Yanaec70742014-08-25 10:37:45 -0700212}
213
214void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700215ChatroomInfo::removeParticipant(const Name& participant)
Mengjin Yanaec70742014-08-25 10:37:45 -0700216{
217 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700218 m_participants.remove(participant);
Mengjin Yanaec70742014-08-25 10:37:45 -0700219}
220
221void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700222ChatroomInfo::setSyncPrefix(const Name& prefix)
Mengjin Yanaec70742014-08-25 10:37:45 -0700223{
224 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700225 m_syncPrefix = prefix;
Mengjin Yanaec70742014-08-25 10:37:45 -0700226}
227
228void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700229ChatroomInfo::setManager(const Name& manager)
Mengjin Yanaec70742014-08-25 10:37:45 -0700230{
231 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700232 m_manager = manager;
Mengjin Yanaec70742014-08-25 10:37:45 -0700233}
234
Yingdi Yueb692ac2015-02-10 18:46:18 -0800235} // namespace chronochat