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> |
| 9 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 10 | */ |
| 11 | #include "chatroom-info.hpp" |
| 12 | |
| 13 | namespace chronos { |
| 14 | |
| 15 | ChatroomInfo::ChatroomInfo() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | ChatroomInfo::ChatroomInfo(const Block& chatroomWire) |
| 20 | { |
| 21 | this->wireDecode(chatroomWire); |
| 22 | } |
| 23 | |
| 24 | template<bool T> |
| 25 | size_t |
| 26 | ChatroomInfo::wireEncode(ndn::EncodingImpl<T>& block) const |
| 27 | { |
| 28 | size_t totalLength = 0; |
| 29 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 30 | // 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 51 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 52 | // Participants |
| 53 | size_t participantsLength = 0; |
| 54 | for (std::list<Name>::const_reverse_iterator it = m_participants.rbegin(); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 55 | it != m_participants.rend(); ++it) { |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 56 | participantsLength += it->wireEncode(block); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 57 | } |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 58 | participantsLength += block.prependVarNumber(participantsLength); |
| 59 | participantsLength += block.prependVarNumber(tlv::Participants); |
| 60 | totalLength += participantsLength; |
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 | // 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 67 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 68 | // 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 Yu | 1cc45d9 | 2015-02-09 14:19:54 -0800 | [diff] [blame^] | 75 | totalLength += ndn::prependNonNegativeIntegerBlock(block, tlv::TrustModel, m_trustModel); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 84 | totalLength += block.prependVarNumber(totalLength); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 85 | totalLength += block.prependVarNumber(tlv::ChatroomInfo); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 86 | |
| 87 | return totalLength; |
| 88 | } |
| 89 | |
| 90 | const Block& |
| 91 | ChatroomInfo::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 | |
| 105 | void |
| 106 | ChatroomInfo::wireDecode(const Block& chatroomWire) |
| 107 | { |
| 108 | m_wire = chatroomWire; |
| 109 | m_wire.parse(); |
| 110 | |
| 111 | m_participants.clear(); |
| 112 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 113 | // 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 134 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 135 | if (m_wire.type() != tlv::ChatroomInfo) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 136 | throw Error("Unexpected TLV number when decoding chatroom packet"); |
| 137 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 138 | // Chatroom Info |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 139 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 140 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 141 | 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 149 | throw Error("Missing TrustModel"); |
| 150 | m_trustModel = |
| 151 | static_cast<TrustModel>(readNonNegativeInteger(*i)); |
| 152 | |
| 153 | ++i; |
| 154 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 155 | // 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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 180 | } |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 181 | if (j != temp.elements_end()) |
| 182 | throw Error("Unexpected element"); |
| 183 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 184 | if (m_participants.empty()) |
| 185 | throw Error("Missing Participant"); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 186 | |
| 187 | ++i; |
| 188 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 189 | if (i != m_wire.elements_end()) { |
| 190 | throw Error("Unexpected element"); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 195 | ChatroomInfo::setName(const Name::Component& name) |
| 196 | { |
| 197 | m_wire.reset(); |
| 198 | m_chatroomName = name; |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | ChatroomInfo::setTrustModel(const TrustModel trustModel) |
| 203 | { |
| 204 | m_wire.reset(); |
| 205 | m_trustModel = trustModel; |
| 206 | } |
| 207 | |
| 208 | void |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 209 | ChatroomInfo::addParticipant(const Name& participant) |
| 210 | { |
| 211 | m_wire.reset(); |
| 212 | m_participants.push_back(participant); |
| 213 | } |
| 214 | |
| 215 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 216 | ChatroomInfo::removeParticipant(const Name& participant) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 217 | { |
| 218 | m_wire.reset(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 219 | m_participants.remove(participant); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 223 | ChatroomInfo::setSyncPrefix(const Name& prefix) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 224 | { |
| 225 | m_wire.reset(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 226 | m_syncPrefix = prefix; |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 230 | ChatroomInfo::setManager(const Name& manager) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 231 | { |
| 232 | m_wire.reset(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 233 | m_manager = manager; |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 236 | } // namespace chronos |