Changeset View
Changeset View
Standalone View
Standalone View
src/private/protocol.cpp
Show First 20 Lines • Show All 188 Lines • ▼ Show 20 Line(s) | 187 | { | |||
---|---|---|---|---|---|
189 | return *this; | 189 | return *this; | ||
190 | } | 190 | } | ||
191 | 191 | | |||
192 | bool Command::operator==(const Command &other) const | 192 | bool Command::operator==(const Command &other) const | ||
193 | { | 193 | { | ||
194 | return mType == other.mType; | 194 | return mType == other.mType; | ||
195 | } | 195 | } | ||
196 | 196 | | |||
197 | QTextStream &Command::toJson(QTextStream &stream) const | ||||
198 | { | ||||
199 | stream << "{\"type\": \""; | ||||
200 | | ||||
201 | #define case_label(x) case Command::x: stream << #x; break | ||||
202 | switch (mType) | ||||
203 | { | ||||
204 | case_label(Invalid); | ||||
205 | | ||||
anthonyfieroni: You can make a helper macro like this
```
#define case_label(x) case Command::x: stream <<… | |||||
206 | case_label(Hello); | ||||
207 | case_label(Login); | ||||
208 | case_label(Logout); | ||||
209 | | ||||
210 | case_label(Transaction); | ||||
211 | | ||||
212 | case_label(CreateItem); | ||||
213 | case_label(CopyItems); | ||||
214 | case_label(DeleteItems); | ||||
215 | case_label(FetchItems); | ||||
216 | case_label(LinkItems); | ||||
217 | case_label(ModifyItems); | ||||
218 | case_label(MoveItems); | ||||
219 | | ||||
220 | case_label(CreateCollection); | ||||
221 | case_label(CopyCollection); | ||||
222 | case_label(DeleteCollection); | ||||
223 | case_label(FetchCollections); | ||||
224 | case_label(FetchCollectionStats); | ||||
225 | case_label(ModifyCollection); | ||||
226 | case_label(MoveCollection); | ||||
227 | | ||||
228 | case_label(Search); | ||||
229 | case_label(SearchResult); | ||||
230 | case_label(StoreSearch); | ||||
231 | | ||||
232 | case_label(CreateTag); | ||||
233 | case_label(DeleteTag); | ||||
234 | case_label(FetchTags); | ||||
235 | case_label(ModifyTag); | ||||
236 | | ||||
237 | case_label(FetchRelations); | ||||
238 | case_label(ModifyRelation); | ||||
239 | case_label(RemoveRelations); | ||||
240 | | ||||
241 | case_label(SelectResource); | ||||
242 | | ||||
243 | case_label(StreamPayload); | ||||
244 | case_label(ItemChangeNotification); | ||||
245 | case_label(CollectionChangeNotification); | ||||
246 | case_label(TagChangeNotification); | ||||
247 | case_label(RelationChangeNotification); | ||||
248 | case_label(SubscriptionChangeNotification); | ||||
249 | case_label(DebugChangeNotification); | ||||
250 | case_label(CreateSubscription); | ||||
251 | case_label(ModifySubscription); | ||||
252 | } | ||||
253 | #undef case_label | ||||
254 | stream << "\"reponse\": " << ((mType & Command::_ResponseBit) ? "true" : "false"); | ||||
255 | stream << "\"}"; | ||||
256 | return stream; | ||||
257 | } | ||||
258 | | ||||
197 | DataStream &operator<<(DataStream &stream, const Command &cmd) | 259 | DataStream &operator<<(DataStream &stream, const Command &cmd) | ||
198 | { | 260 | { | ||
199 | return stream << cmd.mType; | 261 | return stream << cmd.mType; | ||
200 | } | 262 | } | ||
201 | 263 | | |||
202 | DataStream &operator>>(DataStream &stream, Command &cmd) | 264 | DataStream &operator>>(DataStream &stream, Command &cmd) | ||
203 | { | 265 | { | ||
204 | return stream >> cmd.mType; | 266 | return stream >> cmd.mType; | ||
Show All 35 Lines | |||||
240 | 302 | | |||
241 | bool Response::operator==(const Response &other) const | 303 | bool Response::operator==(const Response &other) const | ||
242 | { | 304 | { | ||
243 | return *static_cast<const Command*>(this) == static_cast<const Command&>(other) | 305 | return *static_cast<const Command*>(this) == static_cast<const Command&>(other) | ||
244 | && mErrorCode == other.mErrorCode | 306 | && mErrorCode == other.mErrorCode | ||
245 | && mErrorMsg == other.mErrorMsg; | 307 | && mErrorMsg == other.mErrorMsg; | ||
246 | } | 308 | } | ||
247 | 309 | | |||
310 | QTextStream &Response::toJson(QTextStream &stream) const | ||||
311 | { | ||||
312 | stream << "{" | ||||
313 | << "\"parent\": "; | ||||
314 | static_cast<const Command *>(this)->toJson(stream); | ||||
315 | stream << "," | ||||
316 | << "\"errcode\": " << mErrorCode << "," | ||||
317 | << "\"errstr\": \"" << mErrorMsg << "\"" | ||||
318 | << "}"; | ||||
319 | return stream; | ||||
320 | } | ||||
321 | | ||||
248 | DataStream &operator<<(DataStream &stream, const Response &cmd) | 322 | DataStream &operator<<(DataStream &stream, const Response &cmd) | ||
249 | { | 323 | { | ||
250 | return stream << static_cast<const Command &>(cmd) | 324 | return stream << static_cast<const Command &>(cmd) | ||
251 | << cmd.mErrorCode | 325 | << cmd.mErrorCode | ||
252 | << cmd.mErrorMsg; | 326 | << cmd.mErrorMsg; | ||
253 | } | 327 | } | ||
254 | 328 | | |||
255 | DataStream &operator>>(DataStream &stream, Response &cmd) | 329 | DataStream &operator>>(DataStream &stream, Response &cmd) | ||
▲ Show 20 Lines • Show All 195 Lines • ▼ Show 20 Line(s) | |||||
451 | { | 525 | { | ||
452 | if (flags == None) { | 526 | if (flags == None) { | ||
453 | return mFlags == None; | 527 | return mFlags == None; | ||
454 | } else { | 528 | } else { | ||
455 | return mFlags & flags; | 529 | return mFlags & flags; | ||
456 | } | 530 | } | ||
457 | } | 531 | } | ||
458 | 532 | | |||
533 | QTextStream &ItemFetchScope::toJson(QTextStream &stream) const | ||||
534 | { | ||||
535 | stream << "{ \"type\": \"fetchscope\"," | ||||
536 | << "\"flags\": " << mFlags << "," | ||||
537 | << "\"TagFetchScope\": ["; | ||||
538 | for (const auto &tag : qAsConst(mTagFetchScope)) { | ||||
539 | stream << "\"" << tag << "\","; | ||||
540 | } | ||||
541 | if (!mTagFetchScope.isEmpty()) { | ||||
542 | stream.seek(-1); | ||||
543 | } | ||||
544 | stream << "]," | ||||
545 | << "\"ChangedSince\": \"" << mChangedSince.toString() << "\"," | ||||
546 | << "\"AncestorDepth\":" << mAncestorDepth << "," | ||||
547 | << "\"RequestedParts\": ["; | ||||
548 | | ||||
549 | for (const auto &part : qAsConst(mRequestedParts)) { | ||||
550 | stream << "\"" << part << "\","; | ||||
551 | } | ||||
552 | if (!mRequestedParts.isEmpty()) { | ||||
553 | stream.seek(-1); | ||||
554 | } | ||||
555 | stream << "]" | ||||
556 | << "}"; | ||||
557 | return stream; | ||||
558 | } | ||||
559 | | ||||
459 | QDebug operator<<(QDebug dbg, ItemFetchScope::AncestorDepth depth) | 560 | QDebug operator<<(QDebug dbg, ItemFetchScope::AncestorDepth depth) | ||
460 | { | 561 | { | ||
461 | switch (depth) { | 562 | switch (depth) { | ||
462 | case ItemFetchScope::NoAncestor: | 563 | case ItemFetchScope::NoAncestor: | ||
463 | return dbg << "No ancestor"; | 564 | return dbg << "No ancestor"; | ||
464 | case ItemFetchScope::ParentAncestor: | 565 | case ItemFetchScope::ParentAncestor: | ||
465 | return dbg << "Parent ancestor"; | 566 | return dbg << "Parent ancestor"; | ||
466 | case ItemFetchScope::AllAncestors: | 567 | case ItemFetchScope::AllAncestors: | ||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Line(s) | 638 | { | |||
540 | return *this; | 641 | return *this; | ||
541 | } | 642 | } | ||
542 | 643 | | |||
543 | bool ScopeContext::operator==(const ScopeContext &other) const | 644 | bool ScopeContext::operator==(const ScopeContext &other) const | ||
544 | { | 645 | { | ||
545 | return mColCtx == other.mColCtx && mTagCtx == other.mTagCtx; | 646 | return mColCtx == other.mColCtx && mTagCtx == other.mTagCtx; | ||
546 | } | 647 | } | ||
547 | 648 | | |||
649 | QTextStream &ScopeContext::toJson(QTextStream &stream) const | ||||
650 | { | ||||
651 | stream << "{\"scopeContext\":"; | ||||
652 | if (isEmpty()) { | ||||
653 | stream << "\"empty\""; | ||||
654 | } else if (hasContextId(ScopeContext::Tag)) { | ||||
655 | stream << "\"tag\","; | ||||
656 | stream << "\"TagID\": " << contextId(ScopeContext::Tag); | ||||
657 | } else if (hasContextId(ScopeContext::Collection)) { | ||||
658 | stream << "\"collection\""; | ||||
659 | stream << "\"ColID\":" << contextId(ScopeContext::Collection); | ||||
660 | } else if (hasContextRID(ScopeContext::Tag)) { | ||||
661 | stream << "\"tagrid\""; | ||||
662 | stream << "\"TagRID\": \"" << contextRID(ScopeContext::Tag) << "\""; | ||||
663 | } else if (hasContextRID(ScopeContext::Collection)) { | ||||
664 | stream << "\"colrid\","; | ||||
665 | stream << "\"ColRID\": \"" << contextRID(ScopeContext::Collection) << "\""; | ||||
666 | } | ||||
667 | stream << "}"; | ||||
668 | | ||||
669 | return stream; | ||||
670 | } | ||||
671 | | ||||
548 | DataStream &operator<<(DataStream &stream, const ScopeContext &context) | 672 | DataStream &operator<<(DataStream &stream, const ScopeContext &context) | ||
549 | { | 673 | { | ||
550 | // We don't have a custom generic DataStream streaming operator for QVariant | 674 | // We don't have a custom generic DataStream streaming operator for QVariant | ||
551 | // because it's very hard, esp. without access to QVariant private | 675 | // because it's very hard, esp. without access to QVariant private | ||
552 | // stuff, so we have have to decompose it manually here. | 676 | // stuff, so we have have to decompose it manually here. | ||
553 | QVariant::Type vType = context.mColCtx.type(); | 677 | QVariant::Type vType = context.mColCtx.type(); | ||
554 | stream << vType; | 678 | stream << vType; | ||
555 | if (vType == QVariant::LongLong) { | 679 | if (vType == QVariant::LongLong) { | ||
▲ Show 20 Lines • Show All 190 Lines • ▼ Show 20 Line(s) | 826 | if (msg->type() == Command::CollectionChangeNotification) { | |||
746 | } | 870 | } | ||
747 | } | 871 | } | ||
748 | 872 | | |||
749 | // All other cases are just append, as the compression becomes too expensive in large batches | 873 | // All other cases are just append, as the compression becomes too expensive in large batches | ||
750 | list.append(msg); | 874 | list.append(msg); | ||
751 | return true; | 875 | return true; | ||
752 | } | 876 | } | ||
753 | 877 | | |||
878 | QTextStream &ChangeNotification::toJson(QTextStream &stream) const | ||||
879 | { | ||||
880 | stream << "{" | ||||
881 | << "\"parent\": "; | ||||
882 | static_cast<const Command *>(this)->toJson(stream); | ||||
883 | stream << "," | ||||
884 | << "\"session\": \"" << mSessionId << "\"," | ||||
885 | << "\"metadata\": ["; | ||||
886 | for (const auto &m : qAsConst(mMetaData)) { | ||||
887 | stream << "\"" << m << "\","; | ||||
888 | } | ||||
889 | if (!mMetaData.isEmpty()) { | ||||
890 | stream.seek(-1); | ||||
891 | } | ||||
892 | stream << "]" | ||||
893 | << "}"; | ||||
894 | return stream; | ||||
895 | } | ||||
896 | | ||||
754 | DataStream &operator<<(DataStream &stream, const ChangeNotification &ntf) | 897 | DataStream &operator<<(DataStream &stream, const ChangeNotification &ntf) | ||
755 | { | 898 | { | ||
756 | return stream << static_cast<const Command &>(ntf) | 899 | return stream << static_cast<const Command &>(ntf) | ||
757 | << ntf.mSessionId; | 900 | << ntf.mSessionId; | ||
758 | } | 901 | } | ||
759 | 902 | | |||
760 | DataStream &operator>>(DataStream &stream, ChangeNotification &ntf) | 903 | DataStream &operator>>(DataStream &stream, ChangeNotification &ntf) | ||
761 | { | 904 | { | ||
▲ Show 20 Lines • Show All 72 Lines • Show Last 20 Lines |
You can make a helper macro like this