Skip to content

Commit

Permalink
Change ChatroomInfo to contain more information
Browse files Browse the repository at this point in the history
Change-Id: Ie91ced5dd9be51c8d59a0c5d881967658a58fd15
  • Loading branch information
qiuhanding authored and bruinfish committed Nov 4, 2014
1 parent 6a61444 commit 5d98cc5
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 433 deletions.
3 changes: 1 addition & 2 deletions src/chatroom-discovery-dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace chronos {

using std::vector;
using ndn::Name;

ChatroomDiscoveryDialog::ChatroomDiscoveryDialog(QWidget* parent)
Expand Down Expand Up @@ -67,7 +66,7 @@ ChatroomDiscoveryDialog::updateChatroomList()

QString content;

for (vector<Name>::const_iterator nameIt = it->second.getParticipants().begin();
for (std::list<Name>::const_iterator nameIt = it->second.getParticipants().begin();
nameIt != it->second.getParticipants().end(); nameIt++) {
content.append(QString::fromStdString(nameIt->toUri())).append(",");
}
Expand Down
11 changes: 6 additions & 5 deletions src/chatroom-discovery-logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,18 @@ ChatroomDiscoveryLogic::onReceiveData(const ndn::Interest& interest,
const ndn::Data& data,
const bool isRefreshing)
{
Name::Component chatroomName = data.getName().get(OFFSET_CHATROOM_NAME);
// Name::Component chatroomName = data.getName().get(OFFSET_CHATROOM_NAME);

ChatroomInfo chatroom;
chatroom.wireDecode(data.getContent().blockFromValue());
chatroom.setName(chatroomName);
// chatroom.setName(chatroomName);

// Tmp Disabled
// if (chatroom.getTrustModel() == ChatroomInfo::TRUST_MODEL_WEBOFTRUST)
// addContacts(chatroom);


m_chatrooms[chatroomName] = chatroom;
m_chatrooms[chatroom.getName()] = chatroom;
m_onUpdate(chatroom, true); //add

time::milliseconds refreshingTime;
Expand All @@ -172,7 +172,8 @@ ChatroomDiscoveryLogic::onReceiveData(const ndn::Interest& interest,
refreshingTime = DEFAULT_REFRESHING_TIMER;

m_scheduler.scheduleEvent(refreshingTime,
bind(&ChatroomDiscoveryLogic::refreshChatroom, this, chatroomName));
bind(&ChatroomDiscoveryLogic::refreshChatroom, this,
chatroom.getName()));

if (!isRefreshing)
sendDiscoveryInterest();
Expand Down Expand Up @@ -218,4 +219,4 @@ ChatroomDiscoveryLogic::addContacts(ChatroomInfo& chatroom)
}


} //namespace chronos
} // namespace chronos
5 changes: 2 additions & 3 deletions src/chatroom-discovery-view-dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace chronos {

using std::vector;
using ndn::Name;

ChatroomDiscoveryViewDialog::ChatroomDiscoveryViewDialog(QWidget* parent)
Expand Down Expand Up @@ -52,10 +51,10 @@ ChatroomDiscoveryViewDialog::setChatroomTrustModel(QString chatroomTrustModel)
}

void
ChatroomDiscoveryViewDialog::setChatroomParticipants(const vector<Name>& participants)
ChatroomDiscoveryViewDialog::setChatroomParticipants(const std::list<Name>& participants)
{
QString content;
for (vector<Name>::const_iterator it = participants.begin();
for (std::list<Name>::const_iterator it = participants.begin();
it != participants.end(); it++) {
content.append(QString::fromStdString(it->toUri())).append("\n");
}
Expand Down
2 changes: 1 addition & 1 deletion src/chatroom-discovery-view-dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ChatroomDiscoveryViewDialog : public QDialog
setChatroomTrustModel(QString chatroomTrustModel);

void
setChatroomParticipants(const std::vector<ndn::Name>& chatroomParticipants);
setChatroomParticipants(const std::list<ndn::Name>& chatroomParticipants);


private slots:
Expand Down
174 changes: 137 additions & 37 deletions src/chatroom-info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,62 @@ ChatroomInfo::wireEncode(ndn::EncodingImpl<T>& block) const
{
size_t totalLength = 0;

//Chatroom := CHATROOM-TYPE TLV-LENGTH
// TrustModel
// Participant+

//Participants
for (std::vector<Name>::const_reverse_iterator it = m_participants.rbegin();
// ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
// ChatroomName
// TrustModel
// ChatroomPrefix
// ManagerPrefix
// Participants
//
// ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
// NameComponent
//
// TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
// nonNegativeInteger
//
// ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
// Name
//
// ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
// Name
//
// Participants := PARTICIPANTS-TYPE TLV-LENGTH
// Name+

// Participants
size_t participantsLength = 0;
for (std::list<Name>::const_reverse_iterator it = m_participants.rbegin();
it != m_participants.rend(); ++it) {
size_t entryLength = 0;

entryLength += it->wireEncode(block);
entryLength += block.prependVarNumber(entryLength);
entryLength += block.prependVarNumber(tlv::PARTICIPANT);
totalLength += entryLength;
participantsLength += it->wireEncode(block);
}

//TrustModel
totalLength += prependNonNegativeIntegerBlock(block, tlv::TRUSTMODEL, m_trustModel);

//type = TYPE_CHATROOM;
participantsLength += block.prependVarNumber(participantsLength);
participantsLength += block.prependVarNumber(tlv::Participants);
totalLength += participantsLength;

// Manager Prefix
size_t managerLength = m_manager.wireEncode(block);
totalLength += managerLength;
totalLength += block.prependVarNumber(managerLength);
totalLength += block.prependVarNumber(tlv::ManagerPrefix);

// Chatroom Sync Prefix
size_t chatroomSyncPrefixLength = m_syncPrefix.wireEncode(block);
totalLength += chatroomSyncPrefixLength;
totalLength += block.prependVarNumber(chatroomSyncPrefixLength);
totalLength += block.prependVarNumber(tlv::ChatroomPrefix);

// Trust Model
totalLength += prependNonNegativeIntegerBlock(block, tlv::TrustModel, m_trustModel);

// Chatroom Name
size_t chatroomNameLength = m_chatroomName.wireEncode(block);
totalLength += chatroomNameLength;
totalLength += block.prependVarNumber(chatroomNameLength);
totalLength += block.prependVarNumber(tlv::ChatroomName);

// Chatroom Info
totalLength += block.prependVarNumber(totalLength);
totalLength += block.prependVarNumber(tlv::CHATROOM);
totalLength += block.prependVarNumber(tlv::ChatroomInfo);

return totalLength;
}
Expand Down Expand Up @@ -75,36 +110,101 @@ ChatroomInfo::wireDecode(const Block& chatroomWire)

m_participants.clear();

//Chatroom := CHATROOM-TYPE TLV-LENGTH
// TrustModel
// Participant+

if (m_wire.type() != tlv::CHATROOM)
// ChatroomInfo := CHATROOM-INFO-TYPE TLV-LENGTH
// ChatroomName
// TrustModel
// ChatroomPrefix
// ManagerPrefix
// Participants
//
// ChatroomName := CHATROOM-NAME-TYPE TLV-LENGTH
// NameComponent
//
// TrustModel := TRUST-MODEL-TYPE TLV-LENGTH
// nonNegativeInteger
//
// ChatroomPrefix := CHATROOM-PREFIX-TYPE TLV-LENGTH
// Name
//
// ManagerPrefix := MANAGER-PREFIX-TYPE TLV-LENGTH
// Name
//
// Participants := PARTICIPANTS-TYPE TLV-LENGTH
// Name+

if (m_wire.type() != tlv::ChatroomInfo)
throw Error("Unexpected TLV number when decoding chatroom packet");

// Chatroom Info
Block::element_const_iterator i = m_wire.elements_begin();

//TrustModel
if (i == m_wire.elements_end() || i->type() != tlv::TRUSTMODEL)
if (i == m_wire.elements_end() || i->type() != tlv::ChatroomName)
throw Error("Missing Chatroom Name Info");
m_chatroomName.wireDecode(i->blockFromValue());

++i;

// Trust Model
if (i == m_wire.elements_end() || i->type() != tlv::TrustModel)
throw Error("Missing TrustModel");
m_trustModel =
static_cast<TrustModel>(readNonNegativeInteger(*i));

++i;

//Participants
for (; i != m_wire.elements_end() && i->type() == tlv::PARTICIPANT; ++i) {
Name name;
name.wireDecode(i->blockFromValue());
m_participants.push_back(name);
// Chatroom Sync Prefix
if (i == m_wire.elements_end() || i->type() != tlv::ChatroomPrefix)
throw Error("Missing Chatroom Prefix");
m_syncPrefix.wireDecode(i->blockFromValue());

++i;

// Manager Prefix
if (i == m_wire.elements_end() || i->type() != tlv::ManagerPrefix)
throw Error("Missing Manager Prefix");
m_manager.wireDecode(i->blockFromValue());
++i;

// Participants
if (i == m_wire.elements_end() || i->type() != tlv::Participants)
throw Error("Missing Participant");

Block temp = *i;
temp.parse();

Block::element_const_iterator j = temp.elements_begin();

while (j != temp.elements_end() && j->type() == tlv::Name) {
m_participants.push_back(Name(*j));
++j;
}
if (j != temp.elements_end())
throw Error("Unexpected element");

if (m_participants.empty())
throw Error("Missing Participant");

++i;

if (i != m_wire.elements_end()) {
throw Error("Unexpected element");
}
}

void
ChatroomInfo::setName(const Name::Component& name)
{
m_wire.reset();
m_chatroomName = name;
}

void
ChatroomInfo::setTrustModel(const TrustModel trustModel)
{
m_wire.reset();
m_trustModel = trustModel;
}

void
ChatroomInfo::addParticipant(const Name& participant)
{
Expand All @@ -113,24 +213,24 @@ ChatroomInfo::addParticipant(const Name& participant)
}

void
ChatroomInfo::addContact(const Name& contact)
ChatroomInfo::removeParticipant(const Name& participant)
{
m_wire.reset();
m_contacts.push_back(contact);
m_participants.remove(participant);
}

void
ChatroomInfo::setName(const Name::Component& name)
ChatroomInfo::setSyncPrefix(const Name& prefix)
{
m_wire.reset();
m_name = name;
m_syncPrefix = prefix;
}

void
ChatroomInfo::setTrustModel(const TrustModel trustModel)
ChatroomInfo::setManager(const Name& manager)
{
m_wire.reset();
m_trustModel = trustModel;
m_manager = manager;
}

} //namespace chronos
} // namespace chronos
Loading

0 comments on commit 5d98cc5

Please sign in to comment.