-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathp3wallservice.cc
1563 lines (1440 loc) · 53.9 KB
/
p3wallservice.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "p3wallservice.h"
#include <retroshare/rsidentity.h>
#include <algorithm>
/*
idea: cache requests
this would reduce load if many request which return the same data
are made in a short time
a task has another member time_t expiresOn;
if this member is zero the request can not be cached
the task sets this member on completion
and then the task will be kept for this time
*/
// ********************* helper classes **************************
/*
rules for task handling
- the service creates and sets the public token, and updates its status
- doWork() is called when (time() > task.mNextTickTS)
and all tokens in task.mPendingTokens have state RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE
- task.mPendingTokens is cleared by the service before task.doWork() is called
- if any of the pending tokens failed, the task is considered failed an will get deleted
- the task has to set mResult to FAIL or COMPLETE if all work is done
*/
namespace RsWall{
class WallServiceTask
{
public:
WallServiceTask():
mResult(NO_RESULT_YET),
mWallService(NULL),
mPublicToken(0)/*, mNextTickTS(0)*//*, mStopped(false)*/{}
// overload this to do work
virtual void doWork() = 0;
enum Result { NO_RESULT_YET, FAIL, COMPLETE};
// modified by task
Result mResult;
// set by service
p3WallService* mWallService;
// set by service
uint32_t mPublicToken; // the public token of this task
//time_t mNextTickTS; // set to 0 if not waiting for time
// filled by task and cleared by service
std::vector<uint32_t> mPendingTokens; // clear if not waiting for tokens
//bool mStopped; // set to true if this task is done
};
class PostMsgTask: public WallServiceTask
{
public:
PostMsgTask(const PostMsg &pm):
WallServiceTask(), mState(BEGIN),
mGroupToken(0), mMsgToken(0), mReferenceMsgToken(0), mPostMsg(pm)
{}
enum State { BEGIN, WAITING_GRP, WAITING_MSG, WAITING_REFERENCE, COMPLETED};
State mState;
// these are own tokens we got from the core
uint32_t mGroupToken;// create group
uint32_t mMsgToken;// create msg in group
uint32_t mReferenceMsgToken; // create reference on own wall
PostMsg mPostMsg;
RsGxsGroupId mPostGrpId;
virtual void doWork()
{
std::cerr << "PostMsgTask::doWork() token=" << mPublicToken << std::endl;
switch(mState){
case BEGIN:
{
// create the group where the post gets stored
PostGroupItem* grpItem = new PostGroupItem();
grpItem->meta.mAuthorId = mPostMsg.mMeta.mAuthorId;
mWallService->publishGroup(mGroupToken, grpItem);
mState = WAITING_GRP;
mPendingTokens.push_back(mGroupToken);
}
break;
case WAITING_GRP:
{
// create the msg in the new group
mWallService->acknowledgeTokenGrp(mGroupToken, mPostGrpId); // todo: safe grp id for reference creation
PostMsgItem* msgItem = new PostMsgItem();
msgItem->mPostMsg = mPostMsg;
msgItem->meta.mAuthorId = mPostMsg.mMeta.mAuthorId;
msgItem->meta.mGroupId = mPostGrpId;
mWallService->publishMsg(mMsgToken, msgItem);
mState = WAITING_MSG;
mPendingTokens.push_back(mMsgToken);
}
break;
case WAITING_MSG:
{
// ackn msg token to get msg id (is the msg-id needed? i think not)
// create reference on target wall group
// can outsource the job of creating a reference msg to ReferenceMsgTask
ReferenceMsg refMsg;
refMsg.mType = 0; // todo: define this
refMsg.mMeta.mAuthorId = mPostMsg.mMeta.mAuthorId;
refMsg.mMeta.mGroupId = mPostMsg.mMeta.mGroupId;
refMsg.mReferencedGroup = mPostGrpId;
mWallService->createPostReferenceMsg(mReferenceMsgToken, refMsg);
mState = WAITING_REFERENCE;
mPendingTokens.push_back(mReferenceMsgToken);
}
break;
case WAITING_REFERENCE:
mState = COMPLETED;
mResult = COMPLETE;
break;
case COMPLETED:
break;
}
}
};
// this is a 1:1 copy of PostMsgTask
// except one detail: the target wall is determined by a PostReferenceParams object
// this task uses ReferenceMsgTask2 to create the reference-msg
// not good to have two classes with the same code
// maybe can remove older class later
class PostMsgTask2: public WallServiceTask
{
public:
PostMsgTask2(const PostReferenceParams& params, const std::string& postText):
WallServiceTask(), mState(BEGIN), mPostReferenceParams(params), mPostText(postText),
mGroupToken(0), mMsgToken(0), mReferenceMsgToken(0)
{}
enum State { BEGIN, WAITING_GRP, WAITING_MSG, WAITING_REFERENCE, COMPLETED};
State mState;
PostReferenceParams mPostReferenceParams;
std::string mPostText;
// these are own tokens we got from the core
uint32_t mGroupToken;// create group
uint32_t mMsgToken;// create msg in group
uint32_t mReferenceMsgToken; // create reference on own wall
RsGxsGroupId mPostGrpId;
virtual void doWork()
{
std::cerr << "PostMsgTask::doWork() token=" << mPublicToken << std::endl;
switch(mState){
case BEGIN:
{
// create the group where the post gets stored
PostGroupItem* grpItem = new PostGroupItem();
grpItem->meta.mAuthorId = mPostReferenceParams.mAuthor;
mWallService->publishGroup(mGroupToken, grpItem);
mState = WAITING_GRP;
mPendingTokens.push_back(mGroupToken);
}
break;
case WAITING_GRP:
{
// create the msg in the new group
mWallService->acknowledgeTokenGrp(mGroupToken, mPostGrpId);
PostMsgItem* msgItem = new PostMsgItem();
msgItem->mPostMsg.mPostText = mPostText;
msgItem->meta.mAuthorId = mPostReferenceParams.mAuthor;
msgItem->meta.mGroupId = mPostGrpId;
mWallService->publishMsg(mMsgToken, msgItem);
mState = WAITING_MSG;
mPendingTokens.push_back(mMsgToken);
}
break;
case WAITING_MSG:
{
// ackn msg token to get msg id (is the msg-id needed? i think not)
// create reference on target wall group
// can outsource the job of creating a reference msg to ReferenceMsgTask
mPostReferenceParams.mReferencedGroupId = mPostGrpId;
mWallService->createPostReferenceMsg(mReferenceMsgToken, mPostReferenceParams);
mState = WAITING_REFERENCE;
mPendingTokens.push_back(mReferenceMsgToken);
}
break;
case WAITING_REFERENCE:
mState = COMPLETED;
mResult = COMPLETE;
break;
case COMPLETED:
break;
}
}
};
class ReferenceMsgTask: public WallServiceTask
{
public:
ReferenceMsgTask(const ReferenceMsg& msg):
WallServiceTask(), mReferenceMsg(msg), mState(BEGIN), mMsgToken(0){}
ReferenceMsg mReferenceMsg;
enum State {BEGIN, WAITING_MSG};
State mState;
uint32_t mMsgToken;
virtual void doWork()
{
switch(mState)
{
case BEGIN:
{
ReferenceMsgItem* item = new ReferenceMsgItem();
item->meta = mReferenceMsg.mMeta;
item->mReferenceMsg = mReferenceMsg;
mWallService->publishMsg(mMsgToken, item);
mState = WAITING_MSG;
mPendingTokens.push_back(mMsgToken);
}
break;
case WAITING_MSG:
{
// could acknwledge msg to get the id
// maybe later
mResult = COMPLETE;
}
break;
}
}
};
// this task takes a PostreferenceParams object to find the desired wall
// this task can create wall-grps if target wall author is self
class ReferenceMsgTask2: public WallServiceTask
{
public:
ReferenceMsgTask2(const PostReferenceParams& params):
WallServiceTask(), mState(BEGIN), mParams(params)
{}
enum State { BEGIN, WAITING_METAS, WAITING_CREATEWALL, CREATE_REF_MSG, WAITING_MSG };
State mState;
PostReferenceParams mParams;
uint32_t mGrpMetasToken;
RsGxsGroupId mTargetWallGrpId;
uint32_t mWallGrpToken;
uint32_t mRefMsgToken;
void doWork()
{
switch(mState)
{
case BEGIN:
{
// get wall grps for this this target wall owner
mWallService->requestWallGroupMetas(mGrpMetasToken, mParams.mTargetWallOwner);
mState = WAITING_METAS;
mPendingTokens.push_back(mGrpMetasToken);
}
break;
case WAITING_METAS:
{
std::vector<RsGroupMetaData> grpMetas;
mWallService->getWallGroupMetas(mGrpMetasToken, grpMetas);
// search a wall-grp with the right circle settings
for(std::vector<RsGroupMetaData>::iterator vit = grpMetas.begin(); vit != grpMetas.end(); vit++)
{
if(vit->mCircleId == mParams.mCircle)
{
mTargetWallGrpId = vit->mGroupId;
}
}
if(mTargetWallGrpId.isNull())
{
// check if this is our own wall
// then create it
// if this is not our wall, then sharing fails
// have to think about creating wall groups for others
std::list<RsGxsId> ownIds;
mWallService->getRsIdentity()->getOwnIds(ownIds);
if(std::find(ownIds.begin(), ownIds.end(), mParams.mTargetWallOwner) != ownIds.end())
{
// this is our own wall, we can create a group for it
WallGroup grp;
grp.mMeta.mAuthorId = mParams.mTargetWallOwner;
mWallService->createWallGroup(mWallGrpToken, grp);
mState = WAITING_CREATEWALL;
mPendingTokens.push_back(mWallGrpToken);
}
else
{
std::cerr << "FAIL in ReferenceMsgTask2::doWork mState = WAITING_METAS: can't find a wall-grp, can't make a wall-grp for others" << std::endl;
mResult = FAIL;
}
}
else
{
// skip createwall
mState = CREATE_REF_MSG;
}
}
break;
case WAITING_CREATEWALL:
{
mWallService->acknowledgeTokenGrp(mWallGrpToken, mTargetWallGrpId);
mState = CREATE_REF_MSG;
}
break;
// same as old createRefMsgTask
case CREATE_REF_MSG:
{
ReferenceMsgItem* item = new ReferenceMsgItem();
item->meta.mAuthorId = mParams.mAuthor;
item->meta.mGroupId = mTargetWallGrpId;
item->mReferenceMsg.mReferencedGroup = mParams.mReferencedGroupId;
item->mReferenceMsg.mType = mParams.mType;
mWallService->publishMsg(mRefMsgToken, item);
mState = WAITING_MSG;
mPendingTokens.push_back(mRefMsgToken);
}
break;
case WAITING_MSG:
{
// could acknwledge msg to get the id
// maybe later
mResult = COMPLETE;
}
break;
}
}
};
class SearchWallGroupMetasTask: public WallServiceTask
{
public:
SearchWallGroupMetasTask(const RsGxsId &identity):
WallServiceTask(), mIdentity(identity), mState(BEGIN), mGrpMetaToken(0)
{}
RsGxsId mIdentity;
enum State{BEGIN, WAITING_METAS};
State mState;
uint32_t mGrpMetaToken;
std::vector<RsGroupMetaData> mResultData;
virtual void doWork()
{
switch(mState)
{
case BEGIN:
{
// requesting all metas to find those with the matching author id
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_META;
mWallService->RsGenExchange::getTokenService()->requestGroupInfo(mGrpMetaToken, RS_TOKREQ_ANSTYPE_SUMMARY, opts);
mState = WAITING_METAS;
mPendingTokens.push_back(mGrpMetaToken);
}
break;
case WAITING_METAS:
{
std::list<RsGroupMetaData> groupInfo;
mWallService->getGroupMeta(mGrpMetaToken, groupInfo);
// filter metas where author-id == wanted-id
for(std::list<RsGroupMetaData>::iterator it = groupInfo.begin(); it != groupInfo.end(); it++)
{
RsGroupMetaData& grpMeta = *it;
// check if this is a wall-grp
if((grpMeta.mGroupStatus>>24)==RS_PKT_SUBTYPE_WALL_WALL_GRP_ITEM)
{
// allow all metas if identity is not set
if(mIdentity.isNull())
{
mResultData.push_back(*it);
}
else if(grpMeta.mAuthorId == mIdentity)
{
mResultData.push_back(*it);
}
}
}
mResult = COMPLETE;
}
break;
}
}
};
class SearchWallGroupDataTask: public WallServiceTask
{
public:
SearchWallGroupDataTask(const RsGxsId &identity):
WallServiceTask(), mIdentity(identity), mState(BEGIN), mGrpMetaToken(0), mGrpDataToken(0){}
RsGxsId mIdentity;
enum State {BEGIN, WAITING_METAS, WAITING_DATA};
State mState;
uint32_t mGrpMetaToken;
uint32_t mGrpDataToken;
std::vector<WallGroup> mResultData;
virtual void doWork()
{
switch(mState)
{
case BEGIN:
{
// first request another task to search the metas for us
// we need the grp-ids from the metas
mWallService->requestWallGroupMetas(mGrpMetaToken, mIdentity);
mState = WAITING_METAS;
mPendingTokens.push_back(mGrpMetaToken);
}
break;
case WAITING_METAS:
{
std::vector<RsGroupMetaData> groupInfo;
mWallService->getWallGroupMetas(mGrpMetaToken, groupInfo);
std::list<RsGxsGroupId> grpIds;
for(std::vector<RsGroupMetaData>::iterator it = groupInfo.begin(); it != groupInfo.end(); it++)
{
grpIds.push_back(it->mGroupId);
}
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
mWallService->RsGenExchange::getTokenService()->requestGroupInfo(mGrpDataToken, RS_TOKREQ_ANSTYPE_DATA, opts, grpIds);
mState = WAITING_DATA;
mPendingTokens.push_back(mGrpDataToken);
}
break;
case WAITING_DATA:
{
std::vector<WallGroupItem*> grpItems;
// this can really give us wall-grps only, because getGroupDataT can filter items
mWallService->getGroupDataT<WallGroupItem>(mGrpDataToken, grpItems);
for(std::vector<WallGroupItem*>::iterator it = grpItems.begin(); it != grpItems.end(); it++)
{
WallGroupItem* item = *it;
WallGroup wg = item->mWallGroup;
wg.mMeta = item->meta;
mResultData.push_back(wg);
delete item;
}
mResult = COMPLETE;
}
break;
}
}
};
// could optimize this task to not get a public token assigned
// because no one is waiting for this task
// this task copies the grp-item type into the first 8 bits of the group status
// it checks if grp should be subscribed and subscribes if needed
// it then marks the grp as processed
class ProcessGrpTask: public WallServiceTask
{
public:
ProcessGrpTask(RsGxsGroupId grpId):
mGrpId(grpId), mState(BEGIN), grpToken(0), isPostGrp(false)
{}
enum State { BEGIN, WAITING_META, WAITING_SETBITS, WAITING_SUBSCRIBE, WAITING_PROCESSED };
RsGxsGroupId mGrpId;
State mState;
uint32_t grpToken;
RsGxsId authorId;
bool isPostGrp;
void doWork()
{
switch(mState)
{
case BEGIN:
{
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
std::list<RsGxsGroupId> grpIds;
grpIds.push_back(mGrpId);
mWallService->RsGenExchange::getTokenService()->requestGroupInfo(grpToken, RS_TOKREQ_ANSTYPE_DATA, opts, grpIds);
mState = WAITING_META;
mPendingTokens.push_back(grpToken);
}
break;
case WAITING_META:
{
std::vector<RsGxsGrpItem*> grpItems;
mWallService->getGroupData(grpToken, grpItems);
if(grpItems.empty())
{
std::cerr << "Error in ProcessGrpTask WAITING_META: not grp Data" << std::endl;
mResult = FAIL;
return;
}
RsGxsGrpItem* item = grpItems.front();
authorId = item->meta.mAuthorId;
uint8_t packetType = item->PacketSubType();
// gxs handles status and mask in this way:
// value = (currValue & ~mask) | (value & mask);
uint32_t status = (packetType<<24);
uint32_t mask = 0xFF000000;
uint32_t grpStatusToken;
mWallService->setGroupStatusFlags(grpStatusToken, mGrpId, status, mask);
if(dynamic_cast<PostGroupItem*>(item))
{
isPostGrp = true;
}
std::vector<RsGxsGrpItem*>::iterator vit;
for(vit = grpItems.begin(); vit != grpItems.end(); vit++)
{
delete *vit;
}
mState = WAITING_SETBITS;
mPendingTokens.push_back(grpStatusToken);
}
break;
case WAITING_SETBITS:
{
if(isPostGrp)
{
if(mWallService->wantedGrps.find(mGrpId) != mWallService->wantedGrps.end())
{
mWallService->wantedGrps.erase(mGrpId);
uint32_t token;
mWallService->RsGenExchange::subscribeToGroup(token, mGrpId, true);
mPendingTokens.push_back(token);
}
mState = WAITING_SUBSCRIBE;
}
else
{
// this is a wall-grp
bool subscribe = false;
if(mWallService->shouldSubscribeTo(authorId, subscribe))
{
if(subscribe)
{
uint32_t token;
mWallService->RsGenExchange::subscribeToGroup(token, mGrpId, true);
mPendingTokens.push_back(token);
}
mState = WAITING_SUBSCRIBE;
}
else
{
// interesting authors not loaded
// try again later
mState = WAITING_SETBITS;
}
}
}
break;
case WAITING_SUBSCRIBE:
{
uint32_t status = ~GXS_SERV::GXS_GRP_STATUS_UNPROCESSED;
uint32_t mask = GXS_SERV::GXS_GRP_STATUS_UNPROCESSED;
uint32_t token;
mWallService->setGroupStatusFlags(token, mGrpId, status, mask);
mState = WAITING_PROCESSED;
mPendingTokens.push_back(token);
}
break;
case WAITING_PROCESSED:
mResult = COMPLETE;
break;
}
}
};
class ProcessMsgTask: public WallServiceTask
{
public:
ProcessMsgTask(const RsGxsGrpMsgIdPair& id): WallServiceTask(), mState(BEGIN), mGrpMsgId(id){}
enum State { BEGIN, WAITING_MSG_DATA, WAITING_SUBSCRIBE };
State mState;
RsGxsGrpMsgIdPair mGrpMsgId;
uint32_t mMsgDataToken;
RsGxsGroupId mReferencedGroup;
uint32_t mGrpToken;
void doWork()
{
switch(mState)
{
case BEGIN:
{
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA;
GxsMsgReq msgIds;
std::vector<RsGxsMessageId> msgIdVec;
msgIdVec.push_back(mGrpMsgId.second);
msgIds[mGrpMsgId.first] = msgIdVec;
mWallService->RsGenExchange::getTokenService()->requestMsgInfo(mMsgDataToken, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds);
mPendingTokens.push_back(mMsgDataToken);
mState = WAITING_MSG_DATA;
}
break;
case WAITING_MSG_DATA:
{
// this is really crazy, have a vector inside a map to just get one message
GxsMsgDataMap msgItemMap;
mWallService->getMsgData(mMsgDataToken, msgItemMap);
GxsMsgDataMap::iterator mit;
RsGxsMsgItem* item = NULL;
for( mit = msgItemMap.begin(); mit != msgItemMap.end(); mit++)
{
const RsGxsGroupId& grpId = mit->first;
std::vector<RsGxsMsgItem*>& vec = mit->second;
std::vector<RsGxsMsgItem*>::iterator vit;
for(vit = vec.begin(); vit != vec.end(); vit++)
{
if(item == NULL){
item = *vit;
} else {
std::cerr << "ProcessMsgTask WAITING_MSG_DATA: more than one msg item in result. "
"This should not happen." << std::endl;
delete *vit;
}
}
}
if(item == NULL)
{
std::cerr << "ProcessMsgTask WAITING_MSG_DATA: no item" << std::endl;
mResult = FAIL;
break;
}
ReferenceMsgItem* refMsg = dynamic_cast<ReferenceMsgItem*>(item);
if(refMsg)
{
mReferencedGroup = refMsg->mReferenceMsg.mReferencedGroup;
// add grp id to map
mWallService->wantedGrps.insert(mReferencedGroup);
// then try to subscribe
// if the subscribe fails, then this task gets deleted
// in this case, the grpId stays in the list of wanted grps
// if subscribe was successful, then we can delete the id from the list of wanted grps later
mWallService->RsGenExchange::subscribeToGroup(mGrpToken, mReferencedGroup, true);
mPendingTokens.push_back(mGrpToken);
mState = WAITING_SUBSCRIBE;
}
// don't know if have to do something with this
//PostMsgItem* postMsg = dynamic_cast<PostMsgItem*>(item);
// safe msg type in status bits
// don't know if we need to have msg type in meta?
delete item;
}
break;
case WAITING_SUBSCRIBE:
{
// TODO: mark msg as processed
// subscribe was successful, can remove grp-id from wanted list
mWallService->wantedGrps.erase(mReferencedGroup);
mResult = COMPLETE;
}
break;
}
}
};
// room for optimisation:
// request new groups only once an then pass the data to different tasks
// currently the subscribe check and this filter task request grp and msg data on their own
class FilterActivitiesGrpChangeTask: public WallServiceTask
{
public:
FilterActivitiesGrpChangeTask(const RsGxsGroupId& grpId):
WallServiceTask(), mState(BEGIN), mGrpId(grpId), mGrpDataToken(0) {}
enum State { BEGIN, WAITING_DATA };
State mState;
RsGxsGroupId mGrpId;
uint32_t mGrpDataToken;
void doWork()
{
switch(mState)
{
case BEGIN:
{
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
std::list<RsGxsGroupId> groupIds;
groupIds.push_back(mGrpId);
mWallService->RsGenExchange::getTokenService()->requestGroupInfo(mGrpDataToken, RS_TOKREQ_ANSTYPE_DATA, opts, groupIds);
mPendingTokens.push_back(mGrpDataToken);
mState = WAITING_DATA;
}
break;
case WAITING_DATA:
{
std::vector<RsGxsGrpItem*> grpItems;
mWallService->getGroupData(mGrpDataToken, grpItems);
std::vector<RsGxsGrpItem*>::iterator vit;
for(vit = grpItems.begin(); vit != grpItems.end(); vit++)
{
RsGxsGrpItem* item = *vit;
WallGroupItem* wgItem = dynamic_cast<WallGroupItem*>(item);
if(wgItem)
{
// do nothing with a wall group
// maybe display a notice if thes wall group is from a new author?
// like: "new wall found"
}
PostGroupItem* pgItem = dynamic_cast<PostGroupItem*>(item);
if(pgItem)
{
// do nothing with a post group
// then this Task is completely useless?
// currently yes
// maybe i find something to do later
}
delete item;
}
mResult = COMPLETE;
}
break;
}
}
};
class FilterActivitiesMsgChangeTask: public WallServiceTask
{
public:
FilterActivitiesMsgChangeTask(const RsGxsGrpMsgIdPair& grpMsgId):
WallServiceTask(), mState(BEGIN), mGrpMsgId(grpMsgId), mMsgDataToken(0) {}
enum State { BEGIN, WAITING_DATA };
State mState;
RsGxsGrpMsgIdPair mGrpMsgId;
uint32_t mMsgDataToken;
void doWork()
{
switch(mState)
{
case BEGIN:
{
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA;
GxsMsgReq msgIds;
std::vector<RsGxsMessageId> msgIdVec;
msgIdVec.push_back(mGrpMsgId.second);
msgIds[mGrpMsgId.first] = msgIdVec;
mWallService->RsGenExchange::getTokenService()->requestMsgInfo(mMsgDataToken, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds);
mPendingTokens.push_back(mMsgDataToken);
mState = WAITING_DATA;
}
break;
case WAITING_DATA:
{
GxsMsgDataMap msgItemMap;
mWallService->getMsgData(mMsgDataToken, msgItemMap);
GxsMsgDataMap::iterator mit;
for(mit = msgItemMap.begin(); mit != msgItemMap.end(); mit++)
{
const RsGxsGroupId& grpId = mit->first;
std::vector<RsGxsMsgItem*>& vec = mit->second;
std::vector<RsGxsMsgItem*>::iterator vit;
for(vit = vec.begin(); vit != vec.end(); vit++)
{
RsGxsMsgItem* item = *vit;
PostMsgItem* pmItem =dynamic_cast<PostMsgItem*>(item);
if(pmItem)
{
// what could we do here?
}
ReferenceMsgItem* rmItem = dynamic_cast<ReferenceMsgItem*>(item);
if(rmItem)
{
Activity* activity = NULL;
// don't have to lock the activities mutex for read access in the genexchange thread,
// because only the rsgenexchange thread can change the activities
// check if an activity for this root post exists
for(std::vector<Activity>::iterator vit = mWallService->mActivities.begin();
vit != mWallService->mActivities.end(); vit++)
{
if(vit->mReferencedGroup == rmItem->mReferenceMsg.mReferencedGroup)
{
activity = &(*vit);
}
}
// if not create one
if(activity == NULL)
{
// *************** BEGIN ACTIVITIES LOCKED ************************
// WRITE access to activities has to happen in locked state
RsStackMutex stack(mWallService->mActivitiesMutex);
mWallService->mActivities.push_back(Activity());
activity =&mWallService->mActivities.back();
activity->mReferencedGroup = rmItem->mReferenceMsg.mReferencedGroup;
// *************** END ACTIVITIES LOCKED **************************
}
if(rmItem->mReferenceMsg.mType == ReferenceMsg::REFTYPE_SHARE)
{
// check if this author shared it already
// (can happen if someone shares the same root post for different circles)
bool found = false;
std::vector<RsGxsId>::iterator vit;
for(vit = activity->mShared.begin(); vit != activity->mShared.end(); vit++)
{
if(*vit == rmItem->meta.mAuthorId)
{
found = true;
}
}
if(!found)
{
// *************** BEGIN ACTIVITIES LOCKED ************************
RsStackMutex stack(mWallService->mActivitiesMutex);
activity->mShared.push_back(rmItem->meta.mAuthorId);
// *************** END ACTIVITIES LOCKED **************************
}
}
else if(rmItem->mReferenceMsg.mType == ReferenceMsg::REFTYPE_COMMENT)
{
// same for reftype comment
bool found = false;
std::vector<RsGxsId>::iterator vit;
for(vit = activity->mCommented.begin(); vit != activity->mCommented.end(); vit++)
{
if(*vit == rmItem->meta.mAuthorId)
{
found = true;
}
}
if(!found)
{
// *************** BEGIN ACTIVITIES LOCKED ************************
RsStackMutex stack(mWallService->mActivitiesMutex);
activity->mCommented.push_back(rmItem->meta.mAuthorId);
// *************** END ACTIVITIES LOCKED **************************
}
}
}
delete item;
}
}
mResult = COMPLETE;
}
break;
}
}
};
class SearchSubscribedAuthorsTask: public WallServiceTask
{
public:
SearchSubscribedAuthorsTask(): WallServiceTask(), mState(BEGIN), mGrpMetaToken(0) {}
enum State { BEGIN, WAITING_METAS};
State mState;
uint32_t mGrpMetaToken;
void doWork()
{
switch(mState)
{
case BEGIN:
{
// requesting all grp metas
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_META;
// at the moment grp meta does not support flag filtering
// have to filter later
mWallService->RsGenExchange::getTokenService()->requestGroupInfo(mGrpMetaToken, RS_TOKREQ_ANSTYPE_SUMMARY, opts);
mState = WAITING_METAS;
mPendingTokens.push_back(mGrpMetaToken);
}
break;
case WAITING_METAS:
{
std::list<RsGroupMetaData> groupInfo;
mWallService->getGroupMeta(mGrpMetaToken, groupInfo);
// this is a very expensive operation, because we check author of every group
// potential improvement: check wall-grps only and ignore post-grps
for(std::list<RsGroupMetaData>::iterator it = groupInfo.begin(); it != groupInfo.end(); it++)
{
RsGroupMetaData& grpMeta = *it;
// check for null author-id is done in fn
// check if grp is wall-grp and if grp is subscribed
// then add only authors of subscribed wall-grps
if(((grpMeta.mGroupStatus>>24)==RS_PKT_SUBTYPE_WALL_WALL_GRP_ITEM)
&& IS_GROUP_SUBSCRIBED(grpMeta.mSubscribeFlags))
{
mWallService->subscribeToAuthor(grpMeta.mAuthorId, true);
}
}
mWallService->setAuthorsLoaded();
mResult = COMPLETE;
}
break;
}
}
};
class SubscribeAllWallGrpsFromAuthorTask: public WallServiceTask
{
public:
SubscribeAllWallGrpsFromAuthorTask(RsGxsId id, bool subscribe = true):
mState(BEGIN), mGrpMetaToken(0), authorId(id), subscribe(subscribe)
{}
enum State { BEGIN, WAITING_METAS, WAITING_SUBSCRIBE };
State mState;
uint32_t mGrpMetaToken;
RsGxsId authorId;
bool subscribe;
void doWork()
{
switch(mState)
{
case BEGIN:
{
// requesting all grp metas
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_META;
mWallService->RsGenExchange::getTokenService()->requestGroupInfo(mGrpMetaToken, RS_TOKREQ_ANSTYPE_SUMMARY, opts);
mState = WAITING_METAS;
mPendingTokens.push_back(mGrpMetaToken);
}
break;
case WAITING_METAS:
{
std::list<RsGroupMetaData> groupInfo;
std::vector<RsGxsGroupId> grpsToSubscribe;
mWallService->getGroupMeta(mGrpMetaToken, groupInfo);
// this is a very expensive operation, because we check every group
for(std::list<RsGroupMetaData>::iterator it = groupInfo.begin(); it != groupInfo.end(); it++)
{
RsGroupMetaData& grpMeta = *it;
if(((grpMeta.mGroupStatus>>24)==RS_PKT_SUBTYPE_WALL_WALL_GRP_ITEM)
&& (subscribe ^ IS_GROUP_SUBSCRIBED(grpMeta.mSubscribeFlags)) //can handle subscribe and unsubscribe
&& (grpMeta.mAuthorId == authorId))
{
grpsToSubscribe.push_back(grpMeta.mGroupId);
}
}
std::vector<RsGxsGroupId>::iterator vit;
for(vit = grpsToSubscribe.begin(); vit != grpsToSubscribe.end(); vit++)
{
uint32_t token;
mWallService->RsGenExchange::subscribeToGroup(token, *vit, subscribe);
mPendingTokens.push_back(token);
}
mState = WAITING_SUBSCRIBE;
}
break;
case WAITING_SUBSCRIBE:
mResult = COMPLETE;
break;
}
}
};
// ********************** p3WallService **************************
RsWall *rsWall = NULL;
const uint32_t WALL_MSG_STORE_PERIOD = 30*24*60*60; // in seconds
p3WallService::p3WallService(RsGeneralDataService *gds, RsNetworkExchangeService *nes, RsGixs *gixs, RsIdentity* identity):
RsWall(this),
// RS_SERVICE_TYPE_WALL looks redundant, because the serialiser already knows this
RsGenExchange(gds, nes, new WallSerialiser(),
RS_SERVICE_TYPE_WALL, gixs, wallAuthPolicy(), WALL_MSG_STORE_PERIOD),
GxsTokenQueue(this),
/*_mPostTaskMtx("p3WallService _mPostTaskMtx"),*/
authorsMtx("p3WallService authorsMtx"),
authorsLoaded(false),
mActivitiesMutex("p3WallServiceActivitiesMutex"),
_mTaskMtx("p3WallService _mTaskMtx"),
mCommentService(new p3GxsCommentService(this, RS_SERVICE_TYPE_WALL)),
mRsIdentity(identity)
{
// load the ids of authors we are subscribed to
uint32_t token;
_startTask(token, new SearchSubscribedAuthorsTask());
}
p3WallService::~p3WallService()
{
// other services like gxschannels don't do this cleanup?
// or at least i can't find where