Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 1 | /* -*- 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 Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 9 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 10 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 11 | */ |
| 12 | #include "chatroom-info.hpp" |
| 13 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 14 | namespace chronochat { |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 15 | |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 16 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<ChatroomInfo>)); |
| 17 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<ChatroomInfo>)); |
| 18 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 19 | ChatroomInfo::ChatroomInfo() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | ChatroomInfo::ChatroomInfo(const Block& chatroomWire) |
| 24 | { |
| 25 | this->wireDecode(chatroomWire); |
| 26 | } |
| 27 | |
| 28 | template<bool T> |
| 29 | size_t |
| 30 | ChatroomInfo::wireEncode(ndn::EncodingImpl<T>& block) const |
| 31 | { |
| 32 | size_t totalLength = 0; |
| 33 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 34 | // 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 55 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 56 | // Participants |
| 57 | size_t participantsLength = 0; |
| 58 | for (std::list<Name>::const_reverse_iterator it = m_participants.rbegin(); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 59 | it != m_participants.rend(); ++it) { |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 60 | participantsLength += it->wireEncode(block); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 61 | } |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 62 | participantsLength += block.prependVarNumber(participantsLength); |
| 63 | participantsLength += block.prependVarNumber(tlv::Participants); |
| 64 | totalLength += participantsLength; |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 65 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 66 | // Manager Prefix |
| 67 | size_t managerLength = m_manager.wireEncode(block); |
| 68 | totalLength += managerLength; |
| 69 | totalLength += block.prependVarNumber(managerLength); |
| 70 | totalLength += block.prependVarNumber(tlv::ManagerPrefix); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 71 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 72 | // Chatroom Sync Prefix |
| 73 | size_t chatroomSyncPrefixLength = m_syncPrefix.wireEncode(block); |
| 74 | totalLength += chatroomSyncPrefixLength; |
| 75 | totalLength += block.prependVarNumber(chatroomSyncPrefixLength); |
| 76 | totalLength += block.prependVarNumber(tlv::ChatroomPrefix); |
| 77 | |
| 78 | // Trust Model |
Yingdi Yu | 1cc45d9 | 2015-02-09 14:19:54 -0800 | [diff] [blame] | 79 | totalLength += ndn::prependNonNegativeIntegerBlock(block, tlv::TrustModel, m_trustModel); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 80 | |
| 81 | // Chatroom Name |
| 82 | size_t chatroomNameLength = m_chatroomName.wireEncode(block); |
| 83 | totalLength += chatroomNameLength; |
| 84 | totalLength += block.prependVarNumber(chatroomNameLength); |
| 85 | totalLength += block.prependVarNumber(tlv::ChatroomName); |
| 86 | |
| 87 | // Chatroom Info |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 88 | totalLength += block.prependVarNumber(totalLength); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 89 | totalLength += block.prependVarNumber(tlv::ChatroomInfo); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 90 | |
| 91 | return totalLength; |
| 92 | } |
| 93 | |
| 94 | const Block& |
| 95 | ChatroomInfo::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 | |
| 109 | void |
| 110 | ChatroomInfo::wireDecode(const Block& chatroomWire) |
| 111 | { |
| 112 | m_wire = chatroomWire; |
| 113 | m_wire.parse(); |
| 114 | |
| 115 | m_participants.clear(); |
| 116 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 117 | // 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 138 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 139 | if (m_wire.type() != tlv::ChatroomInfo) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 140 | throw Error("Unexpected TLV number when decoding chatroom packet"); |
| 141 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 142 | // Chatroom Info |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 143 | Block::element_const_iterator i = m_wire.elements_begin(); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 144 | 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 Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 148 | m_chatroomName.wireDecode(i->blockFromValue()); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 149 | ++i; |
| 150 | |
| 151 | // Trust Model |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 152 | 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 156 | m_trustModel = |
| 157 | static_cast<TrustModel>(readNonNegativeInteger(*i)); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 158 | ++i; |
| 159 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 160 | // Chatroom Sync Prefix |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 161 | if (i == m_wire.elements_end()) |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 162 | throw Error("Missing Chatroom Prefix"); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 163 | if (i->type() != tlv::ChatroomPrefix) |
| 164 | throw Error("Expect Chatroom Prefix but get TLV Type " + std::to_string(i->type())); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 165 | m_syncPrefix.wireDecode(i->blockFromValue()); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 166 | ++i; |
| 167 | |
| 168 | // Manager Prefix |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 169 | if (i == m_wire.elements_end()) |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 170 | throw Error("Missing Manager Prefix"); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 171 | if (i->type() != tlv::ManagerPrefix) |
| 172 | throw Error("Expect Manager Prefix but get TLV Type " + std::to_string(i->type())); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 173 | m_manager.wireDecode(i->blockFromValue()); |
| 174 | ++i; |
| 175 | |
| 176 | // Participants |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 177 | if (i == m_wire.elements_end()) |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 178 | throw Error("Missing Participant"); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 179 | if (i->type() != tlv::Participants) |
| 180 | throw Error("Expect Participant but get TLV Type " + std::to_string(i->type())); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 181 | |
| 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 190 | } |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 191 | if (j != temp.elements_end()) |
| 192 | throw Error("Unexpected element"); |
| 193 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 194 | if (m_participants.empty()) |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 195 | throw Error("No participant in the chatroom"); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 196 | |
| 197 | ++i; |
| 198 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 199 | if (i != m_wire.elements_end()) { |
| 200 | throw Error("Unexpected element"); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 205 | ChatroomInfo::setName(const Name::Component& name) |
| 206 | { |
| 207 | m_wire.reset(); |
| 208 | m_chatroomName = name; |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | ChatroomInfo::setTrustModel(const TrustModel trustModel) |
| 213 | { |
| 214 | m_wire.reset(); |
| 215 | m_trustModel = trustModel; |
| 216 | } |
| 217 | |
| 218 | void |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 219 | ChatroomInfo::addParticipant(const Name& participant) |
| 220 | { |
| 221 | m_wire.reset(); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 222 | if (find(m_participants.begin(), m_participants.end(), participant) == |
| 223 | m_participants.end()) { |
| 224 | m_participants.push_back(participant); |
| 225 | } |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 229 | ChatroomInfo::removeParticipant(const Name& participant) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 230 | { |
| 231 | m_wire.reset(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 232 | m_participants.remove(participant); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 236 | ChatroomInfo::setSyncPrefix(const Name& prefix) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 237 | { |
| 238 | m_wire.reset(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 239 | m_syncPrefix = prefix; |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 243 | ChatroomInfo::setManager(const Name& manager) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 244 | { |
| 245 | m_wire.reset(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 246 | m_manager = manager; |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 249 | } // namespace chronochat |