blob: f6b81e302c5533ea445566f8b1718601620347e9 [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
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
75 totalLength += prependNonNegativeIntegerBlock(block, tlv::TrustModel, m_trustModel);
76
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();
140
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700141 if (i == m_wire.elements_end() || i->type() != tlv::ChatroomName)
142 throw Error("Missing Chatroom Name Info");
143 m_chatroomName.wireDecode(i->blockFromValue());
144
145 ++i;
146
147 // Trust Model
148 if (i == m_wire.elements_end() || i->type() != tlv::TrustModel)
Mengjin Yanaec70742014-08-25 10:37:45 -0700149 throw Error("Missing TrustModel");
150 m_trustModel =
151 static_cast<TrustModel>(readNonNegativeInteger(*i));
152
153 ++i;
154
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700155 // Chatroom Sync Prefix
156 if (i == m_wire.elements_end() || i->type() != tlv::ChatroomPrefix)
157 throw Error("Missing Chatroom Prefix");
158 m_syncPrefix.wireDecode(i->blockFromValue());
159
160 ++i;
161
162 // Manager Prefix
163 if (i == m_wire.elements_end() || i->type() != tlv::ManagerPrefix)
164 throw Error("Missing Manager Prefix");
165 m_manager.wireDecode(i->blockFromValue());
166 ++i;
167
168 // Participants
169 if (i == m_wire.elements_end() || i->type() != tlv::Participants)
170 throw Error("Missing Participant");
171
172 Block temp = *i;
173 temp.parse();
174
175 Block::element_const_iterator j = temp.elements_begin();
176
177 while (j != temp.elements_end() && j->type() == tlv::Name) {
178 m_participants.push_back(Name(*j));
179 ++j;
Mengjin Yanaec70742014-08-25 10:37:45 -0700180 }
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700181 if (j != temp.elements_end())
182 throw Error("Unexpected element");
183
Mengjin Yanaec70742014-08-25 10:37:45 -0700184 if (m_participants.empty())
185 throw Error("Missing Participant");
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700186
187 ++i;
188
Mengjin Yanaec70742014-08-25 10:37:45 -0700189 if (i != m_wire.elements_end()) {
190 throw Error("Unexpected element");
191 }
192}
193
194void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700195ChatroomInfo::setName(const Name::Component& name)
196{
197 m_wire.reset();
198 m_chatroomName = name;
199}
200
201void
202ChatroomInfo::setTrustModel(const TrustModel trustModel)
203{
204 m_wire.reset();
205 m_trustModel = trustModel;
206}
207
208void
Mengjin Yanaec70742014-08-25 10:37:45 -0700209ChatroomInfo::addParticipant(const Name& participant)
210{
211 m_wire.reset();
212 m_participants.push_back(participant);
213}
214
215void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700216ChatroomInfo::removeParticipant(const Name& participant)
Mengjin Yanaec70742014-08-25 10:37:45 -0700217{
218 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700219 m_participants.remove(participant);
Mengjin Yanaec70742014-08-25 10:37:45 -0700220}
221
222void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700223ChatroomInfo::setSyncPrefix(const Name& prefix)
Mengjin Yanaec70742014-08-25 10:37:45 -0700224{
225 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700226 m_syncPrefix = prefix;
Mengjin Yanaec70742014-08-25 10:37:45 -0700227}
228
229void
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700230ChatroomInfo::setManager(const Name& manager)
Mengjin Yanaec70742014-08-25 10:37:45 -0700231{
232 m_wire.reset();
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700233 m_manager = manager;
Mengjin Yanaec70742014-08-25 10:37:45 -0700234}
235
Qiuhan Ding5d98cc52014-10-30 15:17:53 -0700236} // namespace chronos