File: StateMachineSubscriptionManager.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (395 lines) | stat: -rw-r--r-- 16,256 bytes parent folder | download | duplicates (7)
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
#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;

#endregion Using directives

namespace System.Workflow.Activities
{
    [Serializable]
    internal class StateMachineSubscriptionManager
    {
        private SetStateSubscription _setStateSubscription;
        private List<StateMachineSubscription> _eventQueue = new List<StateMachineSubscription>();
        private Dictionary<IComparable, StateMachineSubscription> _subscriptions = new Dictionary<IComparable, StateMachineSubscription>();
        private StateMachineExecutionState _executionState;

        internal StateMachineSubscriptionManager(StateMachineExecutionState executionState, Guid instanceId)
        {
            _executionState = executionState;
            _setStateSubscription = new SetStateSubscription(instanceId);
        }

        #region Properties
        private List<StateMachineSubscription> EventQueue
        {
            get
            {
                return this._eventQueue;
            }
        }

        internal StateMachineExecutionState ExecutionState
        {
            get
            {
                return _executionState;
            }
        }

        internal Dictionary<IComparable, StateMachineSubscription> Subscriptions
        {
            get
            {
                return this._subscriptions;
            }
        }

        internal SetStateSubscription SetStateSubscription
        {
            get
            {
                return _setStateSubscription;
            }
        }

        #endregion Properties

        internal void UnsubscribeState(ActivityExecutionContext context)
        {
            StateActivity state = (StateActivity)context.Activity;
            foreach (Activity childActivity in state.EnabledActivities)
            {
                EventDrivenActivity eventDriven = childActivity as EventDrivenActivity;
                if (eventDriven != null)
                {
                    if (IsEventDrivenSubscribed(eventDriven))
                        UnsubscribeEventDriven(context, eventDriven);
                }
            }
        }

        internal void ReevaluateSubscriptions(ActivityExecutionContext context)
        {
            Dictionary<IComparable, StateMachineSubscription> subscriptions = this.GetSubscriptionsShallowCopy();
            List<IComparable> subscribed = new List<IComparable>();

            StateActivity state = StateMachineHelpers.GetCurrentState(context);
            while (state != null)
            {

                foreach (Activity activity in state.EnabledActivities)
                {
                    EventDrivenActivity eventDriven = activity as EventDrivenActivity;
                    if (eventDriven == null)
                        continue;

                    IEventActivity eventActivity = StateMachineHelpers.GetEventActivity(eventDriven);
                    IComparable queueName = eventActivity.QueueName;
                    if (queueName == null)
                        continue;

                    StateMachineSubscription subscription;
                    subscriptions.TryGetValue(queueName, out subscription);
                    EventActivitySubscription eventActivitySubscription = subscription as EventActivitySubscription;
                    if (eventActivitySubscription != null)
                    {
                        if (eventActivitySubscription.EventDrivenName.Equals(eventDriven.QualifiedName))
                        {
                            // this EventDriven is already subscribed
                            subscribed.Add(queueName);
                            continue;
                        }
                        else
                        {
                            // Check if this state already subscribe to this event
                            // if so, throws, since it is not valid to subscribe to the
                            // same event twice
                            if (eventActivitySubscription.StateName.Equals(state.QualifiedName))
                                throw new InvalidOperationException(SR.GetStateAlreadySubscribesToThisEvent(state.QualifiedName, queueName));

                            // some other EventDriven is subscribed, so we need to unsubscribe if 
                            // the event driven belongs to one of our parents
                            if (IsParentState(state, eventActivitySubscription.StateName))
                            {
                                UnsubscribeAction unsubscribe = new UnsubscribeAction(eventActivitySubscription.StateName, eventActivitySubscription.EventDrivenName);
                                this.ExecutionState.EnqueueAction(unsubscribe);
                                subscriptions.Remove(queueName);
                            }
                        }
                    }

                    // Tests if a child state already subscribes to this event
                    // is so, skip, since the child takes precedence
                    if (subscribed.Contains(queueName))
                        continue;

                    SubscribeAction subscribe = new SubscribeAction(state.QualifiedName, eventDriven.QualifiedName);
                    this.ExecutionState.EnqueueAction(subscribe);
                    subscribed.Add(queueName);
                }

                state = state.Parent as StateActivity;
            }

            StateActivity currentState = StateMachineHelpers.GetCurrentState(context);
            DisableQueuesAction disableQueues = new DisableQueuesAction(currentState.QualifiedName);
            this.ExecutionState.EnqueueAction(disableQueues);
        }

        private bool IsParentState(StateActivity state, string stateName)
        {
            StateActivity parentState = state.Parent as StateActivity;
            while (parentState != null)
            {
                if (parentState.QualifiedName.Equals(stateName))
                    return true;
                parentState = parentState.Parent as StateActivity;
            }
            return false;
        }

        internal void SubscribeEventDriven(ActivityExecutionContext context, EventDrivenActivity eventDriven)
        {
            IEventActivity eventActivity = StateMachineHelpers.GetEventActivity(eventDriven);
            Activity activity = (Activity)eventActivity;
            IComparable queueName = GetQueueName(eventActivity);
            Debug.Assert(!this.Subscriptions.ContainsKey(queueName));
            SubscribeEventActivity(context, eventActivity);
        }

        internal void UnsubscribeEventDriven(ActivityExecutionContext context, EventDrivenActivity eventDriven)
        {
            Debug.Assert(IsEventDrivenSubscribed(eventDriven));
            IEventActivity eventActivity = StateMachineHelpers.GetEventActivity(eventDriven);
            UnsubscribeEventActivity(context, eventActivity);
        }

        private StateMachineSubscription SubscribeEventActivity(ActivityExecutionContext context,
            IEventActivity eventActivity)
        {
            EventActivitySubscription subscription = new EventActivitySubscription();
            StateActivity state = (StateActivity)context.Activity;
            subscription.Subscribe(context, state, eventActivity);
            WorkflowQueue workflowQueue = GetWorkflowQueue(context, subscription.QueueName);
            if (workflowQueue != null)
                workflowQueue.Enabled = true;

            Debug.Assert(subscription.QueueName != null);
            this.Subscriptions[subscription.QueueName] = subscription;

            return subscription;
        }

        private void UnsubscribeEventActivity(ActivityExecutionContext context,
            IEventActivity eventActivity)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (eventActivity == null)
                throw new ArgumentNullException("eventActivity");

            EventActivitySubscription subscription = GetSubscription(eventActivity);
            WorkflowQueue workflowQueue = GetWorkflowQueue(context, subscription.QueueName);
            if (workflowQueue != null)
                workflowQueue.Enabled = false;
            UnsubscribeEventActivity(context, eventActivity, subscription);
        }

        private void UnsubscribeEventActivity(ActivityExecutionContext context,
            IEventActivity eventActivity,
            EventActivitySubscription subscription)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (eventActivity == null)
                throw new ArgumentNullException("eventActivity");
            if (subscription == null)
                throw new ArgumentNullException("subscription");

            subscription.Unsubscribe(context, eventActivity);
            RemoveFromQueue(subscription.SubscriptionId);

            Debug.Assert(subscription.QueueName != null);
            this.Subscriptions.Remove(subscription.QueueName);
        }

        internal void CreateSetStateEventQueue(ActivityExecutionContext context)
        {
            this.SetStateSubscription.CreateQueue(context);
            this.Subscriptions[this.SetStateSubscription.SubscriptionId] = this.SetStateSubscription;
        }

        internal void DeleteSetStateEventQueue(ActivityExecutionContext context)
        {
            this.Subscriptions[this.SetStateSubscription.SubscriptionId] = null;
            this.SetStateSubscription.DeleteQueue(context);
        }

        internal void SubscribeToSetStateEvent(ActivityExecutionContext context)
        {
            this.SetStateSubscription.Subscribe(context);
            this.Subscriptions[this.SetStateSubscription.SubscriptionId] = this.SetStateSubscription;
        }

        internal void UnsubscribeToSetStateEvent(ActivityExecutionContext context)
        {
            this.Subscriptions[this.SetStateSubscription.SubscriptionId] = null;
            this.SetStateSubscription.Unsubscribe(context);
        }

        private bool IsEventDrivenSubscribed(EventDrivenActivity eventDriven)
        {
            IEventActivity eventActivity = StateMachineHelpers.GetEventActivity(eventDriven);
            EventActivitySubscription subscription = GetSubscription(eventActivity);
            return (subscription != null);
        }

        private EventActivitySubscription GetSubscription(IEventActivity eventActivity)
        {
            IComparable queueName = GetQueueName(eventActivity);
            if ((queueName == null) || (!this.Subscriptions.ContainsKey(queueName)))
                return null;

            EventActivitySubscription subscription = this.Subscriptions[queueName] as EventActivitySubscription;

            Activity activity = (Activity)eventActivity;
            if (subscription == null ||
                subscription.EventActivityName != activity.QualifiedName)
                return null;

            return subscription;
        }

        private StateMachineSubscription GetSubscription(IComparable queueName)
        {
            StateMachineSubscription subscription;
            this.Subscriptions.TryGetValue(queueName, out subscription);
            return subscription;
        }

        /* Currently not used, left here for completeness
        internal static void EnableStateWorkflowQueues(ActivityExecutionContext context, StateActivity state)
        {
            ChangeStateWorkflowQueuesState(context, state, true);
        }
        */

        internal static void DisableStateWorkflowQueues(ActivityExecutionContext context, StateActivity state)
        {
            ChangeStateWorkflowQueuesState(context, state, false);
        }

        private static void ChangeStateWorkflowQueuesState(ActivityExecutionContext context, StateActivity state, bool enabled)
        {
            foreach (Activity activity in state.EnabledActivities)
            {
                EventDrivenActivity eventDriven = activity as EventDrivenActivity;
                if (eventDriven != null)
                    ChangeEventDrivenQueueState(context, eventDriven, enabled);
            }
        }

        internal static void ChangeEventDrivenQueueState(ActivityExecutionContext context, EventDrivenActivity eventDriven, bool enabled)
        {
            IEventActivity eventActivity = StateMachineHelpers.GetEventActivity(eventDriven);
            IComparable queueName = GetQueueName(eventActivity);
            if (queueName == null)
                return; // skip unitialized follower
            WorkflowQueue workflowQueue = GetWorkflowQueue(context, queueName);
            if (workflowQueue != null)
                workflowQueue.Enabled = enabled;
        }

        internal static WorkflowQueue GetWorkflowQueue(ActivityExecutionContext context, IComparable queueName)
        {
            WorkflowQueuingService workflowQueuingService = context.GetService<WorkflowQueuingService>();
            if (workflowQueuingService.Exists(queueName))
            {
                WorkflowQueue workflowQueue = workflowQueuingService.GetWorkflowQueue(queueName);
                return workflowQueue;
            }
            return null;
        }

        private static IComparable GetQueueName(IEventActivity eventActivity)
        {
            IComparable queueName = eventActivity.QueueName;
            return queueName;
        }

        private Dictionary<IComparable, StateMachineSubscription> GetSubscriptionsShallowCopy()
        {
            Dictionary<IComparable, StateMachineSubscription> subscriptions = new Dictionary<IComparable, StateMachineSubscription>();
            foreach (KeyValuePair<IComparable, StateMachineSubscription> dictionaryEntry in this.Subscriptions)
            {
                subscriptions.Add(dictionaryEntry.Key, dictionaryEntry.Value);
            }
            return subscriptions;
        }

        #region Event Queue Methods

        internal void Enqueue(ActivityExecutionContext context, Guid subscriptionId)
        {
            StateMachineSubscription subscription = GetSubscription(subscriptionId);
            if (subscription != null)
            {
                // subscription can be null if we already unsubscribed to 
                // this event
                this.EventQueue.Add(subscription);
            }
            ProcessQueue(context);
        }

        internal void Enqueue(ActivityExecutionContext context, IComparable queueName)
        {
            StateMachineSubscription subscription = GetSubscription(queueName);
            if (subscription != null)
            {
                // subscription can be null if we already unsubscribed to 
                // this event
                this.EventQueue.Add(subscription);
            }
            ProcessQueue(context);
        }

        internal StateMachineSubscription Dequeue()
        {
            StateMachineSubscription subscription = this.EventQueue[0];
            this.EventQueue.RemoveAt(0);
            return subscription;
        }

        private void RemoveFromQueue(Guid subscriptionId)
        {
            this.EventQueue.RemoveAll(delegate(StateMachineSubscription subscription) { return subscription.SubscriptionId.Equals(subscriptionId); });
        }

        internal void ProcessQueue(ActivityExecutionContext context)
        {
            StateActivity currentState = StateMachineHelpers.GetCurrentState(context);
            if (this.EventQueue.Count == 0 ||
                this.ExecutionState.HasEnqueuedActions ||
                this.ExecutionState.SchedulerBusy ||
                currentState == null)
                return;

            StateMachineSubscription subscription = Dequeue();
            subscription.ProcessEvent(context);
        }

        #endregion Event Queue Methods

    }
}