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 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 13 | namespace chronochat { |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 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(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 140 | if (i == m_wire.elements_end() || i->type() != tlv::ChatroomName) |
| 141 | throw Error("Missing Chatroom Name Info"); |
| 142 | m_chatroomName.wireDecode(i->blockFromValue()); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 143 | ++i; |
| 144 | |
| 145 | // Trust Model |
| 146 | if (i == m_wire.elements_end() || i->type() != tlv::TrustModel) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 147 | throw Error("Missing TrustModel"); |
| 148 | m_trustModel = |
| 149 | static_cast<TrustModel>(readNonNegativeInteger(*i)); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 150 | ++i; |
| 151 | |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 152 | // 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 Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 156 | ++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 Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 176 | } |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 177 | if (j != temp.elements_end()) |
| 178 | throw Error("Unexpected element"); |
| 179 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 180 | if (m_participants.empty()) |
| 181 | throw Error("Missing Participant"); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 182 | |
| 183 | ++i; |
| 184 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 185 | if (i != m_wire.elements_end()) { |
| 186 | throw Error("Unexpected element"); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 191 | ChatroomInfo::setName(const Name::Component& name) |
| 192 | { |
| 193 | m_wire.reset(); |
| 194 | m_chatroomName = name; |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | ChatroomInfo::setTrustModel(const TrustModel trustModel) |
| 199 | { |
| 200 | m_wire.reset(); |
| 201 | m_trustModel = trustModel; |
| 202 | } |
| 203 | |
| 204 | void |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 205 | ChatroomInfo::addParticipant(const Name& participant) |
| 206 | { |
| 207 | m_wire.reset(); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame^] | 208 | if (find(m_participants.begin(), m_participants.end(), participant) == |
| 209 | m_participants.end()) { |
| 210 | m_participants.push_back(participant); |
| 211 | } |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 215 | ChatroomInfo::removeParticipant(const Name& participant) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 216 | { |
| 217 | m_wire.reset(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 218 | m_participants.remove(participant); |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 222 | ChatroomInfo::setSyncPrefix(const Name& prefix) |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 223 | { |
| 224 | m_wire.reset(); |
Qiuhan Ding | 5d98cc5 | 2014-10-30 15:17:53 -0700 | [diff] [blame] | 225 | m_syncPrefix = prefix; |
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::setManager(const Name& manager) |
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_manager = manager; |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 235 | } // namespace chronochat |