Skip to content

Commit

Permalink
Storing and reusing the message index information when saving and loa…
Browse files Browse the repository at this point in the history
…ding files.
  • Loading branch information
arobenko committed Jun 20, 2024
1 parent 5c7b1e0 commit 7534194
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
9 changes: 1 addition & 8 deletions lib/include/cc_tools_qt/ProtocolBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,7 @@ class ProtocolBase : public Protocol
HasMsgFactoryTag
>::type;

auto allMsgs = createAllMessagesInTupleInternal<TMsgsTuple>(Tag());
for (auto& msgPtr : allMsgs) {
setNameToMessageProperties(*msgPtr);
setForceExtraInfoExistenceToMessageProperties(*msgPtr);
updateMessage(*msgPtr);
}

return allMsgs;
return createAllMessagesInTupleInternal<TMsgsTuple>(Tag());
}

private:
Expand Down
12 changes: 12 additions & 0 deletions lib/include/cc_tools_qt/property/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ class CC_API Type : public PropBase<unsigned>
static const QByteArray PropName;
};

class CC_API MsgIdx : public PropBase<unsigned>
{
typedef PropBase<unsigned> Base;
public:
MsgIdx() : Base(Name, PropName) {}

private:
static const QString Name;
static const QByteArray PropName;
};


class CC_API Timestamp : public PropBase<unsigned long long>
{
typedef PropBase<unsigned long long> Base;
Expand Down
34 changes: 34 additions & 0 deletions lib/src/MsgFileMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ class IdProp : public property::message::PropBase<QString>
const QString IdProp::Name("id");
const QByteArray IdProp::PropName = IdProp::Name.toUtf8();

class MsgIdxProp : public property::message::PropBase<unsigned>
{
typedef property::message::PropBase<unsigned> Base;
public:
MsgIdxProp() : Base(Name, PropName) {}
private:
static const QString Name;
static const QByteArray PropName;
};

const QString MsgIdxProp::Name("msg_idx");
const QByteArray MsgIdxProp::PropName = MsgIdxProp::Name.toUtf8();

class DataProp : public property::message::PropBase<QString>
{
typedef property::message::PropBase<QString> Base;
Expand Down Expand Up @@ -220,6 +233,7 @@ MessagePtr createMsgObjectFrom(

auto msgMap = msgMapVar.value<QVariantMap>();
auto msgId = IdProp().getFrom(msgMap);
auto msgIdx = MsgIdxProp().getFrom(msgMap);
auto dataStr = DataProp().getFrom(msgMap);

if (msgId.isEmpty() && dataStr.isEmpty()) {
Expand Down Expand Up @@ -275,6 +289,23 @@ MessagePtr createMsgObjectFrom(
return msg;
}

do {
if (msgIdx == 0U) {
break;
}

msg = protocol.createMessage(msgId, msgIdx);
if (!msg) {
break;
}

if (!msg->decodeData(data)) {
msg.reset();
break;
}

} while (false);

unsigned idx = 0;
while (!msg) {
msg = protocol.createMessage(msgId, idx);
Expand Down Expand Up @@ -312,7 +343,9 @@ QVariantMap convertRecvMsg(const Message& msg)

if (!idStr.isEmpty()) {
IdProp().setTo(std::move(idStr), msgInfoMap);
MsgIdxProp().setTo(property::message::MsgIdx().getFrom(msg), msgInfoMap);
}

DataProp().setTo(std::move(dataStr), msgInfoMap);
TimestampProp().setTo(property::message::Timestamp().getFrom(msg), msgInfoMap);
TypeProp().setTo(static_cast<unsigned>(property::message::Type().getFrom(msg)), msgInfoMap);
Expand Down Expand Up @@ -397,6 +430,7 @@ QVariantList convertSendMsgList(

QVariantMap msgInfoMap;
IdProp().setTo(msg->idAsString(), msgInfoMap);
MsgIdxProp().setTo(property::message::MsgIdx().getFrom(*msg), msgInfoMap);
DataProp().setTo(encodeMsgData(*msg), msgInfoMap);
DelayProp().setTo(property::message::Delay().getFrom(*msg), msgInfoMap);
DelayUnitsProp().setTo(property::message::DelayUnits().getFrom(*msg), msgInfoMap);
Expand Down
24 changes: 22 additions & 2 deletions lib/src/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,32 @@ DataInfoPtr Protocol::write(Message& msg)

Protocol::MessagesList Protocol::createAllMessages()
{
return createAllMessagesImpl();
auto allMsgs = createAllMessagesImpl();
QString prevId;
unsigned prevIdx = 0U;
for (auto& msgPtr : allMsgs) {
unsigned idx = 0U;
if (prevId == msgPtr->idAsString()) {
idx = prevIdx + 1U;
}

prevId = msgPtr->idAsString();
prevIdx = idx;
property::message::MsgIdx().setTo(idx, *msgPtr);

setNameToMessageProperties(*msgPtr);
setForceExtraInfoExistenceToMessageProperties(*msgPtr);
updateMessage(*msgPtr);
}

return allMsgs;
}

MessagePtr Protocol::createMessage(const QString& idAsString, unsigned idx)
{
return createMessageImpl(idAsString, idx);
auto msgPtr = createMessageImpl(idAsString, idx);
property::message::MsgIdx().setTo(idx, *msgPtr);
return msgPtr;
}

Protocol::UpdateStatus Protocol::updateMessage(Message& msg)
Expand Down
3 changes: 3 additions & 0 deletions lib/src/property/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ namespace message
const QString Type::Name("cc.msg_type");
const QByteArray Type::PropName = Type::Name.toUtf8();

const QString MsgIdx::Name("cc.msg_idx");
const QByteArray MsgIdx::PropName = MsgIdx::Name.toUtf8();

const QString Timestamp::Name("cc.msg_timestamp");
const QByteArray Timestamp::PropName = Timestamp::Name.toUtf8();

Expand Down

0 comments on commit 7534194

Please sign in to comment.