/
Activity.cpp
1995 lines (1611 loc) · 47.6 KB
/
Activity.cpp
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
// @@@LICENSE
//
// Copyright (c) 2009-2013 LG Electronics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// LICENSE@@@
#include "Activity.h"
#include "ActivityManager.h"
#include "PowerActivity.h"
#include "PowerManager.h"
#include "Trigger.h"
#include "Callback.h"
#include "Subscription.h"
#include "Schedule.h"
#include "ActivityJson.h"
#include "Requirement.h"
#include "ActivityAutoAssociation.h"
#include <stdexcept>
#include <functional>
#include <algorithm>
MojLogger Activity::s_log(_T("activitymanager.activity"));
Activity::Activity(activityId_t id, boost::weak_ptr<ActivityManager> am)
: m_id(id)
, m_persistent(false)
, m_explicit(false)
, m_immediate(false)
, m_priority(ActivityBackgroundPriority)
, m_useSimpleType(true) /* Background */
, m_focused(false)
, m_continuous(false)
, m_userInitiated(false)
, m_powerDebounce(false)
, m_bus(PublicBus)
, m_initialized(false)
, m_scheduled(false)
, m_ready(false)
, m_running(false)
, m_ending(false)
, m_terminate(false)
, m_restart(false)
, m_requeue(false)
, m_yielding(false)
, m_released(true)
, m_intCommand(ActivityNoCommand)
, m_extCommand(ActivityNoCommand)
, m_sentCommand(ActivityNoCommand)
, m_am(am)
{
}
Activity::~Activity()
{
MojLogDebug(s_log, _T("[Activity %llu] Cleaning up"), m_id);
}
activityId_t Activity::GetId() const
{
return m_id;
}
void Activity::SetName(const std::string& name)
{
MojLogTrace(s_log);
m_name = name;
}
const std::string& Activity::GetName() const
{
return m_name;
}
bool Activity::IsNameRegistered() const
{
return m_nameTableItem.is_linked();
}
void Activity::SetDescription(const std::string& description)
{
m_description = description;
}
const std::string& Activity::GetDescription() const
{
return m_description;
}
void Activity::SetCreator(const Subscriber& creator)
{
m_creator = creator;
}
void Activity::SetCreator(const BusId& creator)
{
m_creator = creator;
}
const BusId& Activity::GetCreator() const
{
return m_creator;
}
void Activity::SetImmediate(bool immediate)
{
m_immediate = immediate;
}
bool Activity::IsImmediate() const
{
return m_immediate;
}
std::string Activity::GetStateString() const
{
return std::string(ActivityStateNames[GetState()]);
}
void Activity::SendCommand(ActivityCommand_t command, bool internal)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] \"%s\" command received%s"),
m_id, ActivityCommandNames[command],
internal ? " (internally generated)" : "");
/* Cancel, Stop, and Complete are final. Once you go there, you
* can't change your mind. */
if (internal) {
if ((m_intCommand == ActivityCancelCommand) ||
(m_intCommand == ActivityStopCommand)) {
throw std::runtime_error("Activity Manager has already generated "
"a final command to this Activity");
}
m_intCommand = command;
} else {
if ((command == ActivityStartCommand) && !m_initialized &&
!m_ending) {
RequestScheduleActivity();
}
if ((m_extCommand == ActivityCancelCommand) ||
(m_extCommand == ActivityStopCommand) ||
(m_extCommand == ActivityCompleteCommand)) {
throw std::runtime_error("The Activity Manager has already "
"received a command to end the Activity");
}
m_extCommand = command;
}
/* If the Activity is already ending, don't do anything. */
if (m_ending)
return;
ActivityCommand_t sendCommand = ComputeNextExternalCommand();
if (sendCommand != m_sentCommand) {
/* If the Activity has never started externally, only commands
* to start or end it are allowed. */
if ((sendCommand == ActivityCancelCommand) ||
(sendCommand == ActivityStopCommand) ||
(sendCommand == ActivityCompleteCommand)) {
BroadcastCommand(sendCommand);
EndActivity();
} else if (!m_running && (sendCommand == ActivityStartCommand)) {
/* Activity has not yet started running, so only let it run if
* it has been scheduled to run, triggered, and its requirements
* are met. */
if (IsRunnable() && !m_ready) {
RequestRunActivity();
}
} else {
/* Activity has already started running, so this would be
* a transition to or from Pause... just pass on the command */
BroadcastCommand(sendCommand);
}
}
}
MojErr Activity::AddSubscription(boost::shared_ptr<Subscription> sub)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] %s subscribed"),
m_id, sub->GetSubscriber().GetString().c_str());
m_subscriptions.insert(*sub);
SubscriberSet::const_iterator exists = m_subscribers.find(
sub->GetSubscriber());
if (exists != m_subscribers.end()) {
m_subscribers.insert(exists, sub->GetSubscriber());
} else {
m_subscribers.insert(sub->GetSubscriber());
/* If the Activity isn't running, don't pass this information on
* to the Activity Manager. It will query for the unique subscriber
* list immediately before it shifts the Activity to the running
* state. */
if (m_running) {
m_am.lock()->InformActivityGainedSubscriberId(shared_from_this(),
sub->GetSubscriber().GetId());
}
}
return MojErrNone;
}
MojErr Activity::RemoveSubscription(boost::shared_ptr<Subscription> sub)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] %s unsubscribed"),
m_id, sub->GetSubscriber().GetString().c_str());
SubscriptionSet::iterator subscription = m_subscriptions.begin();
for (; subscription != m_subscriptions.end(); ++subscription) {
if (&(*sub) == &(*subscription))
break;
}
if (subscription != m_subscriptions.end()) {
m_subscriptions.erase(subscription);
} else {
MojLogWarning(s_log, _T("[Activity %llu] Unable to find %s on the "
"subscriptions list"), m_id,
sub->GetSubscriber().GetString().c_str());
return MojErrInvalidArg;
}
Subscriber& actor = sub->GetSubscriber();
SubscriberSet::iterator subscriber = m_subscribers.begin();
for (; subscriber != m_subscribers.end(); ++subscriber) {
if (&actor == &(*subscriber))
break;
}
if (subscriber != m_subscribers.end()) {
m_subscribers.erase(subscriber);
} else {
MojLogWarning(s_log, _T("[Activity %llu] Unable to find %s's "
"subscriber entry on the subscribers list"), m_id,
actor.GetString().c_str());
}
/* If all subscriptions by that subscriber are gone, now... */
if (m_subscribers.find(actor) == m_subscribers.end()) {
/* Only if running. If not running, the Activity Manager will
* query the Activity for all unique Subscribers before
* running. */
if (m_running) {
m_am.lock()->InformActivityLostSubscriberId(shared_from_this(),
actor.GetId());
}
}
AdopterQueue::iterator adopter = m_adopters.begin();
for (; adopter != m_adopters.end(); ++adopter) {
if (sub == adopter->lock())
break;
}
if (adopter != m_adopters.end()) {
m_adopters.erase(adopter);
}
if (m_releasedParent.lock() == sub) {
m_releasedParent.reset();
}
if (m_parent.lock() == sub) {
m_parent.reset();
if (m_adopters.empty()) {
/* If there are no remaining subscribers, just let Abandoned clean
* up and call EndActivity. */
if (!m_subscriptions.empty()) {
Orphaned();
}
} else {
m_parent = m_adopters.front().lock();
m_adopters.pop_front();
m_parent.lock()->QueueEvent(ActivityOrphanEvent);
}
}
if (m_subscriptions.empty()) {
Abandoned();
}
return MojErrNone;
}
bool Activity::IsSubscribed() const
{
return !m_subscriptions.empty();
}
Activity::SubscriberIdVec Activity::GetUniqueSubscribers() const
{
SubscriberIdVec subscribers;
subscribers.reserve(m_subscribers.size());
std::unique_copy(m_subscribers.begin(), m_subscribers.end(),
std::back_inserter(subscribers));
return subscribers;
}
MojErr Activity::BroadcastEvent(ActivityEvent_t event)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Broadcasting \"%s\" event"), m_id,
ActivityEventNames[event]);
MojErr err = MojErrNone;
for (SubscriptionSet::iterator iter = m_subscriptions.begin();
iter != m_subscriptions.end(); ++iter) {
MojErr sendErr = iter->QueueEvent(event);
/* Catch the last error, if any */
if (sendErr)
err = sendErr;
}
return MojErrNone;
}
void Activity::PlugAllSubscriptions()
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Plugging all subscriptions"), m_id);
for (SubscriptionSet::iterator iter = m_subscriptions.begin();
iter != m_subscriptions.end(); ++iter) {
iter->Plug();
}
}
void Activity::UnplugAllSubscriptions()
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Unplugging all subscriptions"), m_id);
for (SubscriptionSet::iterator iter = m_subscriptions.begin();
iter != m_subscriptions.end(); ++iter) {
iter->Unplug();
}
}
void Activity::SetTrigger(boost::shared_ptr<Trigger> trigger)
{
m_trigger = trigger;
}
boost::shared_ptr<Trigger> Activity::GetTrigger() const
{
return m_trigger;
}
void Activity::ClearTrigger()
{
m_trigger.reset();
}
bool Activity::HasTrigger() const
{
return m_trigger;
}
void Activity::Triggered(boost::shared_ptr<Trigger> trigger)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Triggered"), m_id);
if (m_trigger == trigger) {
if (!m_running && !m_ready && IsRunnable()) {
RequestRunActivity();
}
}
}
bool Activity::IsTriggered() const
{
if (m_trigger) {
return m_trigger->IsTriggered(shared_from_this());
} else {
return true;
}
}
void Activity::SetCallback(boost::shared_ptr<Callback> callback)
{
m_callback = callback;
}
boost::shared_ptr<Callback> Activity::GetCallback()
{
return m_callback;
}
bool Activity::HasCallback() const
{
return m_callback;
}
void Activity::CallbackFailed(boost::shared_ptr<Callback> callback,
Callback::FailureType failure)
{
MojLogTrace(s_log);
if (failure == Callback::TransientFailure) {
MojLogDebug(s_log, _T("[Activity %llu] Callback experienced transient "
"protocol failure... requeuing Activity"), m_id);
RequestRequeueActivity();
} else {
MojLogDebug(s_log, _T("[Activity %llu] Callback experienced permanent "
"failure... cancelling Activity"), m_id);
SetTerminateFlag(true);
SendCommand(ActivityCancelCommand, true);
/* XXX de-persist here */
}
}
void Activity::CallbackSucceeded(boost::shared_ptr<Callback> callback)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Callback succeeded"), m_id);
/* Nothing special to do here unless the Activity itself implements a
* callback timeout. */
}
void Activity::SetSchedule(boost::shared_ptr<Schedule> schedule)
{
m_schedule = schedule;
}
void Activity::ClearSchedule()
{
m_schedule.reset();
}
void Activity::Scheduled()
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Scheduled"), m_id);
if (!m_running && !m_ready && IsRunnable()) {
RequestRunActivity();
}
}
bool Activity::IsScheduled() const
{
if (m_schedule) {
return m_schedule->IsScheduled();
} else {
return true;
}
}
void Activity::AddRequirement(boost::shared_ptr<Requirement> requirement)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] setting [Requirement %s]"), m_id,
requirement->GetName().c_str());
if (requirement->GetActivity() != shared_from_this()) {
MojLogError(s_log, _T("Attempting to add [Requirement %s] owned by "
"[Activity %llu] to [Activity %llu]"),
requirement->GetActivity()->GetName().c_str(),
requirement->GetActivity()->GetId(), m_id);
throw std::runtime_error("Requirement owner mismatch");
}
m_requirements[requirement->GetName()] = requirement;
if (requirement->m_activityListItem.is_linked()) {
MojLogError(s_log, _T("Found linked requirement adding "
"[Requirement %s] to [Activity %llu]"),
requirement->GetName().c_str(), m_id);
requirement->m_activityListItem.unlink();
}
if (requirement->IsMet()) {
m_metRequirements.push_back(*requirement);
} else {
m_unmetRequirements.push_back(*requirement);
if (!m_running && m_ready) {
m_ready = false;
m_am.lock()->InformActivityNotReady(shared_from_this());
}
}
}
void Activity::RemoveRequirement(boost::shared_ptr<Requirement> requirement)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] removing [Requirement %s]"), m_id,
requirement->GetName().c_str());
if (requirement->GetActivity() != shared_from_this()) {
MojLogError(s_log, _T("Attempting to remove [Requirement %s] owned by "
"[Activity %llu] from [Activity %llu]"),
requirement->GetName().c_str(),
requirement->GetActivity()->GetId(), m_id);
throw std::runtime_error("Requirement owner mismatch");
}
if (!requirement->m_activityListItem.is_linked()) {
MojLogError(s_log, _T("Found unlinked requirement removing "
"[Requirement %s] from [Activity %llu]"),
requirement->GetName().c_str(), m_id);
} else {
requirement->m_activityListItem.unlink();
}
if (!m_running && !m_ready && IsRunnable()) {
RequestRunActivity();
}
}
void Activity::RemoveRequirement(const std::string& name)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] removing [Requirement %s] by name"),
m_id, name.c_str());
RequirementMap::iterator found = m_requirements.find(name);
if (found == m_requirements.end()) {
MojLogWarning(s_log, _T("[Activity %llu] [Requirement %s] not found "
"while trying to remove by name"), m_id, name.c_str());
return;
}
if (found->second->m_activityListItem.is_linked()) {
found->second->m_activityListItem.unlink();
} else {
MojLogError(s_log, _T("Found unlinked requirement removing "
"[Requirement %s] from [Activity %llu] by name"),
found->second->GetName().c_str(), m_id);
}
m_requirements.erase(found);
if (!m_running && !m_ready && IsRunnable()) {
RequestRunActivity();
}
}
bool Activity::IsRequirementSet(const std::string& name) const
{
RequirementMap::const_iterator found = m_requirements.find(name);
return (found != m_requirements.end());
}
bool Activity::HasRequirements() const
{
return (!m_requirements.empty());
}
void Activity::RequirementMet(boost::shared_ptr<Requirement> requirement)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] [Requirement %s] met"), m_id,
requirement->GetName().c_str());
if (requirement->GetActivity() != shared_from_this()) {
MojLogError(s_log, _T("Attempt to mark [Requirement %s] owned by "
"[Activity %llu] as met for [Activity %llu]"),
requirement->GetName().c_str(),
requirement->GetActivity()->GetId(), m_id);
throw std::runtime_error("Requirement owner mismatch");
}
if (requirement->m_activityListItem.is_linked()) {
requirement->m_activityListItem.unlink();
} else {
MojLogError(s_log, _T("Found unlinked requirement marking "
"[Requirement %s] as met for [Activity %llu]"),
requirement->GetName().c_str(), m_id);
}
m_metRequirements.push_back(*requirement);
/* Not obvious from here it'll be time to start.
* XXX use plug/unplug and the fact that an event will only be queued
* if another isn't already... */
BroadcastEvent(ActivityUpdateEvent);
if (m_unmetRequirements.empty()) {
MojLogDebug(s_log, _T("[Activity %llu] All requirements met"), m_id);
if (!m_running && !m_ready && IsRunnable()) {
RequestRunActivity();
}
}
}
void Activity::RequirementUnmet(boost::shared_ptr<Requirement> requirement)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] [Requirement %s] unmet"), m_id,
requirement->GetName().c_str());
if (requirement->GetActivity() != shared_from_this()) {
MojLogError(s_log, _T("Attempt to mark [Requirement %s] owned by "
"[Activity %llu] as unmet for [Activity %llu]"),
requirement->GetName().c_str(),
requirement->GetActivity()->GetId(), m_id);
throw std::runtime_error("Requirement owner mismatch");
}
if (requirement->m_activityListItem.is_linked()) {
requirement->m_activityListItem.unlink();
} else {
MojLogError(s_log, _T("Found unlinked requirement marking "
"[Requirement %s] as unmet for [Activity %llu]"),
requirement->GetName().c_str(), m_id);
}
m_unmetRequirements.push_back(*requirement);
if (!m_running && m_ready) {
m_ready = false;
m_am.lock()->InformActivityNotReady(shared_from_this());
}
BroadcastEvent(ActivityUpdateEvent);
}
void Activity::RequirementUpdated(boost::shared_ptr<Requirement> requirement)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] [Requirement %s] updated"), m_id,
requirement->GetName().c_str());
if (requirement->GetActivity() != shared_from_this()) {
MojLogError(s_log, _T("Updated [Requirement %s] is not owned by "
"[Activity %llu]"), requirement->GetName().c_str(), m_id);
throw std::runtime_error("Requirement owner mismatch");
}
BroadcastEvent(ActivityUpdateEvent);
}
void Activity::SetParent(boost::shared_ptr<Subscription> sub)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Attempting to set %s as parent"),
m_id, sub->GetSubscriber().GetString().c_str());
if (!m_parent.expired())
throw std::runtime_error("Activity already has parent set");
m_parent = sub;
m_released = false;
MojLogDebug(s_log, _T("[Activity %llu] %s assigned as parent"), m_id,
sub->GetSubscriber().GetString().c_str());
}
MojErr Activity::Adopt(boost::shared_ptr<Subscription> sub, bool wait, bool *adopted)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] %s attempting to adopt%s"), m_id,
sub->GetSubscriber().GetString().c_str(),
wait ? "and willing to wait" : "");
m_adopters.push_back(sub);
if (!m_parent.expired()) {
if (adopted)
*adopted = false;
if (!wait) {
MojLogDebug(s_log, _T("[Activity %llu] already has a parent "
"(%s), and %s does not want to wait"), m_id,
m_parent.lock()->GetSubscriber().GetString().c_str(),
sub->GetSubscriber().GetString().c_str());
return MojErrWouldBlock;
}
MojLogDebug(s_log, _T("[Activity %llu] %s added to adopter list"),
m_id, sub->GetSubscriber().GetString().c_str());
} else {
if (adopted)
*adopted = true;
DoAdopt();
}
return MojErrNone;
}
MojErr Activity::Release(const BusId& caller)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] %s attempting to release"), m_id,
caller.GetString().c_str());
/* Can't release if it's already been released. */
if (m_released) {
MojLogDebug(s_log, _T("[Activity %llu] Has already been released"),
m_id);
return MojErrInvalidArg;
}
if (m_parent.lock()->GetSubscriber() != caller) {
MojLogDebug(s_log, _T("[Activity %llu] %s failed to release, as %s "
"is currently the parent"), m_id, caller.GetString().c_str(),
m_parent.lock()->GetSubscriber().GetString().c_str());
return MojErrAccessDenied;
}
m_releasedParent = m_parent;
m_parent.reset();
m_released = true;
MojLogDebug(s_log, _T("[Activity %llu] Released by %s"), m_id,
caller.GetString().c_str());
if (!m_adopters.empty()) {
DoAdopt();
}
return MojErrNone;
}
bool Activity::IsReleased() const
{
return m_released;
}
MojErr Activity::Complete(const BusId& caller, bool force)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] %s attempting to complete"), m_id,
caller.GetString().c_str());
if (!force && (m_released || m_parent.expired() ||
(m_parent.lock()->GetSubscriber() != caller))
&& (caller != m_creator))
return MojErrAccessDenied;
/* XXX Think about this one. As per discussions, allowing complete
* as a signal to other participating processes may be useful */
SendCommand(ActivityCompleteCommand);
MojLogDebug(s_log, _T("[Activity %llu] Completed by %s"), m_id,
caller.GetString().c_str());
return MojErrNone;
}
void Activity::SetPersistent(bool persistent)
{
m_persistent = persistent;
}
bool Activity::IsPersistent() const
{
return m_persistent;
}
void Activity::SetPersistToken(boost::shared_ptr<PersistToken> token)
{
m_persistToken = token;
}
boost::shared_ptr<PersistToken> Activity::GetPersistToken()
{
return m_persistToken;
}
void Activity::ClearPersistToken()
{
m_persistToken.reset();
}
bool Activity::IsPersistTokenSet() const
{
if (m_persistToken)
return true;
else
return false;
}
void Activity::HookPersistCommand(boost::shared_ptr<PersistCommand> cmd)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Hooking [PersistCommand %s]"), m_id,
cmd->GetString().c_str());
if (cmd->GetActivity()->GetId() != m_id) {
MojLogError(s_log, _T("[Activity %llu] Attempt to hook [PersistCommand "
"%s] assigned to [Activity %llu]"), m_id,
cmd->GetString().c_str(), cmd->GetActivity()->GetId());
throw std::runtime_error("Attempt to hook persist command which "
"is currently assigned to a different Activity");
}
m_persistCommands.push_back(cmd);
}
void Activity::UnhookPersistCommand(boost::shared_ptr<PersistCommand> cmd)
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Unhooking [PersistCommand %s]"), m_id,
cmd->GetString().c_str());
if (cmd->GetActivity()->GetId() != m_id) {
MojLogError(s_log, _T("[Activity %llu] Attempt to unhook "
"[PersistCommand %s] assigned to [Activity %llu]"), m_id,
cmd->GetString().c_str(), cmd->GetActivity()->GetId());
throw std::runtime_error("Attempt to unhook persist command which "
"is currently assigned to a different Activity");
}
if (cmd != m_persistCommands.front()) {
CommandQueue::iterator found = std::find(m_persistCommands.begin(),
m_persistCommands.end(), cmd);
if (found != m_persistCommands.end()) {
MojLogError(s_log, _T("[Activity %llu] Request to unhook "
"[PersistCommand %s] which is not the first persist command "
"in the queue"), m_id, cmd->GetString().c_str());
m_persistCommands.erase(found);
} else {
MojLogError(s_log, _T("[Activity %llu] Request to unhook "
"[PersistCommand %s], which is not in the persist command "
"queue"), m_id, cmd->GetString().c_str());
}
} else {
m_persistCommands.pop_front();
/* If any subscriptions were holding events... */
if (m_persistCommands.empty()) {
UnplugAllSubscriptions();
}
}
if (m_ending && m_persistCommands.empty()) {
EndActivity();
}
}
bool Activity::IsPersistCommandHooked() const
{
return (!m_persistCommands.empty());
}
boost::shared_ptr<PersistCommand> Activity::GetHookedPersistCommand()
{
if (m_persistCommands.empty()) {
MojLogError(s_log, _T("[Activity %llu] Attempt to retreive the "
"current hooked persist command, but none is present"), m_id);
throw std::runtime_error("Attempt to retreive hooked persist command, "
"but none is present");
}
return m_persistCommands.front();
}
void Activity::SetPriority(ActivityPriority_t priority)
{
m_priority = priority;
}
ActivityPriority_t Activity::GetPriority() const
{
return m_priority;
}
void Activity::SetFocus(bool focused)
{
if (m_focused == focused) {
return;
}
m_focused = focused;
/* Focus changed, update the sorting of all the Associations */
std::for_each(m_associations.begin(), m_associations.end(),
boost::mem_fn(&ActivitySetAutoAssociation::Reassociate));
if (focused) {
BroadcastEvent(ActivityFocusedEvent);
} else {
BroadcastEvent(ActivityUnfocusedEvent);
}
}
bool Activity::IsFocused() const
{
return m_focused;
}
void Activity::SetExplicit(bool explicitActivity)
{
m_explicit = explicitActivity;
}
bool Activity::IsExplicit() const
{
return m_explicit;
}
void Activity::SetTerminateFlag(bool terminate)
{
m_terminate = terminate;
}
void Activity::SetRestartFlag(bool restart)
{
m_restart = restart;
}
void Activity::SetUseSimpleType(bool useSimpleType)
{
m_useSimpleType = useSimpleType;
}
void Activity::SetBusType(BusType bus)
{
m_bus = bus;
}
Activity::BusType Activity::GetBusType() const
{
return m_bus;
}
void Activity::SetContinuous(bool continuous)
{
m_continuous = continuous;
}
bool Activity::IsContinuous() const
{
return m_continuous;
}
void Activity::SetUserInitiated(bool userInitiated)
{
m_userInitiated = userInitiated;
}
bool Activity::IsUserInitiated() const
{
return m_userInitiated;
}
void Activity::SetPowerActivity(boost::shared_ptr<PowerActivity> powerActivity)
{
m_powerActivity = powerActivity;
}
boost::shared_ptr<PowerActivity> Activity::GetPowerActivity()
{
return m_powerActivity;
}
bool Activity::IsPowerActivity() const
{
return m_powerActivity;
}
void Activity::PowerLockedNotification()
{
MojLogTrace(s_log);
MojLogDebug(s_log, _T("[Activity %llu] Received notification that power "
"has been successfully locked on"), m_id);
m_powerActivity->GetManager()->ConfirmPowerActivityBegin(
shared_from_this());
if (!m_ending) {
DoRunActivity();
}
}
void Activity::PowerUnlockedNotification()
{