Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- StateMachine in WF4
- public void Initialize(WinSipNetLib.WinSipItf winSipItf, string hostname, string user, string password)
- {
- try
- {
- WinSipItf = winSipItf;
- var Itf = WinSipItf;
- var input = new Dictionary<string, object>
- {
- {"winSipItf", Itf}
- };
- this.WorkFlowHostApp = new WorkflowApplication(new Activity1(), input)
- {
- Idle = this.OnWorkflowIdle,
- Completed = this.OnWorkflowCompleted
- };
- // Setup the Tracking to output in the debug window
- WorkFlowHostApp.Extensions.Add(new Tracker());
- this.WorkFlowHostApp.Extensions.Add(this);
- this.workflowBusy.Reset();
- Console.WriteLine("Starting Workflow");
- this.WorkFlowHostApp.Run();
- }
- catch (Exception ex)
- {
- Console.Write(ex);
- }
- }
- public sealed class Register : NativeActivity<Int32>
- {
- // Define an activity input argument of type string
- [RequiredArgument]
- public InArgument<WinSipNetLib.WinSipItf> winSipItf { get; set; }
- #region Constants and Fields
- private readonly Variable<NoPersistHandle> _noPersistHandle = new Variable<NoPersistHandle>();
- internal const string BookmarkName = "WaitingForAccountStatusEvent";
- /// <summary>
- /// The transition callback.
- /// </summary>
- private BookmarkCallback registrationCallback;
- #endregion
- #region Properties
- ///// <summary>
- ///// Gets or sets PhoneTransition.
- ///// </summary>
- //public PhoneTransition PhoneTransition { get; set; }
- /// <summary>
- /// Gets a value indicating whether CanInduceIdle.
- /// </summary>
- protected override bool CanInduceIdle
- {
- get
- {
- return true;
- }
- }
- /// <summary>
- /// Gets TransitionCallback.
- /// </summary>
- private BookmarkCallback RegistrationCallback
- {
- get
- {
- return this.registrationCallback ?? (this.registrationCallback = new BookmarkCallback(this.OnRegistrationCallback));
- }
- }
- #endregion
- #region Methods
- /// <summary>
- /// The cache metadata.
- /// </summary>
- /// <param name="metadata">
- /// The metadata.
- /// </param>
- protected override void CacheMetadata(NativeActivityMetadata metadata)
- {
- metadata.RequireExtension(typeof(Tracker));
- metadata.RequireExtension(typeof(Phone));
- metadata.RequireExtension(typeof(WaitForRegistrationExt));
- // !!! and one more for GUI
- // Provide a Func<T> to create the extension if it does not already exist
- metadata.AddDefaultExtensionProvider(() => new WaitForRegistrationExt());
- metadata.AddArgument(new RuntimeArgument("winSipItf", typeof(WinSipNetLib.WinSipItf), ArgumentDirection.In, true));
- metadata.AddArgument(new RuntimeArgument("Result", typeof(int), ArgumentDirection.Out, false));
- metadata.AddImplementationVariable(_noPersistHandle);
- }
- protected override void Execute(NativeActivityContext context)
- {
- // Enter a no persist zone to pin this activity to memory since we are setting up a delegate to receive a callback
- var handle = _noPersistHandle.Get(context);
- handle.Enter(context);
- // Get Phone extension needed to call the functions in WinSipItf
- Phone phone = (Phone)context.GetExtension<Phone>();
- // Get (which may create) the extension
- WaitForRegistrationExt regExt = context.GetExtension<WaitForRegistrationExt>();
- // Add the callback
- regExt.AddRegistrationCallback(winSipItf.Get(context));
- bool bRet = phone.WinSipItf.RegisterDBusCallback("WinSipPhone");
- // Obtain the runtime value of the Text input argument
- if (bRet == false)
- {
- this.Result.Set(context, bRet.ToString());
- return;
- }
- string hostname = "demo2.demo.com";
- string username = "406";
- string password = "123123";
- string id = username + "@" + hostname;
- String regUri = hostname;
- if (phone.WinSipItf.DoSipRegister(id, regUri, username, password) == 0)
- {
- this.Result.Set(context, bRet.ToString());
- return;
- }
- // Set a bookmark - the extension will resume when the Gizmo is fired
- context.CreateBookmark(BookmarkName, RegistrationCallback);
- //context.CreateBookmark(this.PhoneTransition.ToString(), this.RegistrationCallback);
- //// Obtain the runtime value of the Text input argument
- //string text = context.GetValue(this.Text);
- //Result.Set(context, string.Format("The text is {0}", text));
- }
- /// <summary>
- /// The on transition callback.
- /// </summary>
- /// <param name="context">
- /// The context.
- /// </param>
- /// <param name="bookmark">
- /// The bookmark.
- /// </param>
- /// <param name="value">
- /// The value.
- /// </param>
- /// <exception cref="InvalidOperationException">
- /// </exception>
- private void OnRegistrationCallback(NativeActivityContext context, Bookmark bookmark, object value)
- {
- if (value is WinSipItf.MSGDATA)
- {
- }
- ////if (value is StateTransitionResult)
- //{
- // this.Result.Set(context, value as StateTransitionResult);
- //}
- //else if (value != null)
- //{
- // // Resumed with something else
- // throw new InvalidOperationException(
- // "You must resume PhoneTransition bookmarks with a result of type StateTransitionResult");
- //}
- // Exit the no persist zone
- var handle = _noPersistHandle.Get(context);
- handle.Exit(context);
- }
- #endregion
- }
Add Comment
Please, Sign In to add comment