Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [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 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu> |
| 8 | */ |
| 9 | |
| 10 | #include "chat-message.hpp" |
| 11 | |
| 12 | namespace chronochat { |
| 13 | |
| 14 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<ChatMessage>)); |
| 15 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<ChatMessage>)); |
| 16 | |
| 17 | ChatMessage::ChatMessage() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | ChatMessage::ChatMessage(const Block& chatMsgWire) |
| 22 | { |
| 23 | this->wireDecode(chatMsgWire); |
| 24 | } |
| 25 | |
| 26 | template<bool T> |
| 27 | size_t |
| 28 | ChatMessage::wireEncode(ndn::EncodingImpl<T>& block) const |
| 29 | { |
| 30 | // ChatMessage := CHAT-MESSAGE-TYPE TLV-LENGTH |
| 31 | // Nick |
| 32 | // ChatroomName |
| 33 | // ChatMessageType |
| 34 | // ChatData |
| 35 | // Timestamp |
| 36 | // |
| 37 | // Nick := NICK-NAME-TYPE TLV-LENGTH |
| 38 | // String |
| 39 | // |
| 40 | // ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH |
| 41 | // String |
| 42 | // |
| 43 | // ChatMessageType := CHAT-MESSAGE-TYPE TLV-LENGTH |
| 44 | // nonNegativeInteger |
| 45 | // |
| 46 | // ChatData := CHAT-DATA-TYPE TLV-LENGTH |
| 47 | // String |
| 48 | // |
| 49 | // Timestamp := TIMESTAMP-TYPE TLV-LENGTH |
| 50 | // VarNumber |
| 51 | // |
| 52 | size_t totalLength = 0; |
| 53 | |
| 54 | // Timestamp |
| 55 | totalLength += ndn::prependNonNegativeIntegerBlock(block, tlv::Timestamp, m_timestamp); |
| 56 | |
| 57 | // ChatData |
| 58 | if (m_msgType == CHAT) { |
| 59 | const uint8_t* dataWire = reinterpret_cast<const uint8_t*>(m_data.c_str()); |
| 60 | totalLength += block.prependByteArrayBlock(tlv::ChatData, dataWire, m_data.length()); |
| 61 | } |
| 62 | |
| 63 | // ChatMessageType |
| 64 | totalLength += ndn::prependNonNegativeIntegerBlock(block, tlv::ChatMessageType, m_msgType); |
| 65 | |
| 66 | // ChatroomName |
| 67 | const uint8_t* chatroomWire = reinterpret_cast<const uint8_t*>(m_chatroomName.c_str()); |
| 68 | totalLength += block.prependByteArrayBlock(tlv::ChatroomName, chatroomWire, |
| 69 | m_chatroomName.length()); |
| 70 | |
| 71 | // Nick |
| 72 | const uint8_t* nickWire = reinterpret_cast<const uint8_t*>(m_nick.c_str()); |
| 73 | totalLength += block.prependByteArrayBlock(tlv::Nick, nickWire, m_nick.length()); |
| 74 | |
| 75 | // Chat Message |
| 76 | totalLength += block.prependVarNumber(totalLength); |
| 77 | totalLength += block.prependVarNumber(tlv::ChatMessage); |
| 78 | |
| 79 | return totalLength; |
| 80 | } |
| 81 | |
| 82 | const Block& |
| 83 | ChatMessage::wireEncode() const |
| 84 | { |
| 85 | ndn::EncodingEstimator estimator; |
| 86 | size_t estimatedSize = wireEncode(estimator); |
| 87 | |
| 88 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 89 | wireEncode(buffer); |
| 90 | |
| 91 | m_wire = buffer.block(); |
| 92 | m_wire.parse(); |
| 93 | |
| 94 | return m_wire; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | ChatMessage::wireDecode(const Block& chatMsgWire) |
| 99 | { |
| 100 | m_wire = chatMsgWire; |
| 101 | m_wire.parse(); |
| 102 | |
| 103 | if (m_wire.type() != tlv::ChatMessage) |
| 104 | throw Error("Unexpected TLV number when decoding chat message packet"); |
| 105 | |
| 106 | Block::element_const_iterator i = m_wire.elements_begin(); |
| 107 | if (i == m_wire.elements_end() || i->type() != tlv::Nick) |
| 108 | throw Error("Expect Nick but get ..."); |
| 109 | m_nick = std::string(reinterpret_cast<const char* >(i->value()), |
| 110 | i->value_size());; |
| 111 | i++; |
| 112 | |
| 113 | if (i == m_wire.elements_end() || i->type() != tlv::ChatroomName) |
| 114 | throw Error("Expect Chatroom Name but get ..."); |
| 115 | m_chatroomName = std::string(reinterpret_cast<const char* >(i->value()), |
| 116 | i->value_size());; |
| 117 | i++; |
| 118 | |
| 119 | if (i == m_wire.elements_end() || i->type() != tlv::ChatMessageType) |
| 120 | throw Error("Expect Chat Message Type but get ..."); |
| 121 | m_msgType = static_cast<ChatMessageType>(readNonNegativeInteger(*i)); |
| 122 | i++; |
| 123 | |
| 124 | if (m_msgType != CHAT) |
| 125 | m_data = ""; |
| 126 | else { |
| 127 | if (i == m_wire.elements_end() || i->type() != tlv::ChatData) |
| 128 | throw Error("Expect Chat Data but get ..."); |
| 129 | m_data = std::string(reinterpret_cast<const char* >(i->value()), |
| 130 | i->value_size());; |
| 131 | i++; |
| 132 | } |
| 133 | |
| 134 | if (i == m_wire.elements_end() || i->type() != tlv::Timestamp) |
| 135 | throw Error("Expect Timestamp but get ..."); |
| 136 | m_timestamp = static_cast<time_t>(readNonNegativeInteger(*i)); |
| 137 | i++; |
| 138 | |
| 139 | if (i != m_wire.elements_end()) { |
| 140 | throw Error("Unexpected element"); |
| 141 | } |
| 142 | |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | ChatMessage::setNick(const std::string& nick) |
| 147 | { |
| 148 | m_wire.reset(); |
| 149 | m_nick = nick; |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | ChatMessage::setChatroomName(const std::string& chatroomName) |
| 154 | { |
| 155 | m_wire.reset(); |
| 156 | m_chatroomName = chatroomName; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | ChatMessage::setMsgType(const ChatMessageType msgType) |
| 161 | { |
| 162 | m_wire.reset(); |
| 163 | m_msgType = msgType; |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | ChatMessage::setData(const std::string& data) |
| 168 | { |
| 169 | m_wire.reset(); |
| 170 | m_data = data; |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | ChatMessage::setTimestamp(const time_t timestamp) |
| 175 | { |
| 176 | m_wire.reset(); |
| 177 | m_timestamp = timestamp; |
| 178 | } |
| 179 | |
| 180 | }// namespace chronochat |