Guest User

Untitled

a guest
Aug 22nd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. StateMachine in WF4
  2. public void Initialize(WinSipNetLib.WinSipItf winSipItf, string hostname, string user, string password)
  3. {
  4. try
  5. {
  6. WinSipItf = winSipItf;
  7. var Itf = WinSipItf;
  8. var input = new Dictionary<string, object>
  9. {
  10. {"winSipItf", Itf}
  11. };
  12. this.WorkFlowHostApp = new WorkflowApplication(new Activity1(), input)
  13. {
  14. Idle = this.OnWorkflowIdle,
  15. Completed = this.OnWorkflowCompleted
  16. };
  17.  
  18. // Setup the Tracking to output in the debug window
  19. WorkFlowHostApp.Extensions.Add(new Tracker());
  20.  
  21. this.WorkFlowHostApp.Extensions.Add(this);
  22.  
  23. this.workflowBusy.Reset();
  24.  
  25. Console.WriteLine("Starting Workflow");
  26. this.WorkFlowHostApp.Run();
  27. }
  28. catch (Exception ex)
  29. {
  30. Console.Write(ex);
  31. }
  32. }
  33.  
  34. public sealed class Register : NativeActivity<Int32>
  35. {
  36. // Define an activity input argument of type string
  37. [RequiredArgument]
  38. public InArgument<WinSipNetLib.WinSipItf> winSipItf { get; set; }
  39.  
  40. #region Constants and Fields
  41.  
  42. private readonly Variable<NoPersistHandle> _noPersistHandle = new Variable<NoPersistHandle>();
  43. internal const string BookmarkName = "WaitingForAccountStatusEvent";
  44.  
  45. /// <summary>
  46. /// The transition callback.
  47. /// </summary>
  48. private BookmarkCallback registrationCallback;
  49.  
  50. #endregion
  51.  
  52. #region Properties
  53.  
  54. ///// <summary>
  55. ///// Gets or sets PhoneTransition.
  56. ///// </summary>
  57. //public PhoneTransition PhoneTransition { get; set; }
  58.  
  59. /// <summary>
  60. /// Gets a value indicating whether CanInduceIdle.
  61. /// </summary>
  62. protected override bool CanInduceIdle
  63. {
  64. get
  65. {
  66. return true;
  67. }
  68. }
  69.  
  70. /// <summary>
  71. /// Gets TransitionCallback.
  72. /// </summary>
  73. private BookmarkCallback RegistrationCallback
  74. {
  75. get
  76. {
  77. return this.registrationCallback ?? (this.registrationCallback = new BookmarkCallback(this.OnRegistrationCallback));
  78. }
  79. }
  80.  
  81. #endregion
  82.  
  83. #region Methods
  84.  
  85. /// <summary>
  86. /// The cache metadata.
  87. /// </summary>
  88. /// <param name="metadata">
  89. /// The metadata.
  90. /// </param>
  91. protected override void CacheMetadata(NativeActivityMetadata metadata)
  92. {
  93. metadata.RequireExtension(typeof(Tracker));
  94. metadata.RequireExtension(typeof(Phone));
  95. metadata.RequireExtension(typeof(WaitForRegistrationExt));
  96. // !!! and one more for GUI
  97.  
  98. // Provide a Func<T> to create the extension if it does not already exist
  99. metadata.AddDefaultExtensionProvider(() => new WaitForRegistrationExt());
  100.  
  101. metadata.AddArgument(new RuntimeArgument("winSipItf", typeof(WinSipNetLib.WinSipItf), ArgumentDirection.In, true));
  102. metadata.AddArgument(new RuntimeArgument("Result", typeof(int), ArgumentDirection.Out, false));
  103.  
  104. metadata.AddImplementationVariable(_noPersistHandle);
  105. }
  106.  
  107. protected override void Execute(NativeActivityContext context)
  108. {
  109. // Enter a no persist zone to pin this activity to memory since we are setting up a delegate to receive a callback
  110. var handle = _noPersistHandle.Get(context);
  111. handle.Enter(context);
  112.  
  113. // Get Phone extension needed to call the functions in WinSipItf
  114. Phone phone = (Phone)context.GetExtension<Phone>();
  115.  
  116. // Get (which may create) the extension
  117. WaitForRegistrationExt regExt = context.GetExtension<WaitForRegistrationExt>();
  118.  
  119. // Add the callback
  120. regExt.AddRegistrationCallback(winSipItf.Get(context));
  121.  
  122. bool bRet = phone.WinSipItf.RegisterDBusCallback("WinSipPhone");
  123.  
  124. // Obtain the runtime value of the Text input argument
  125. if (bRet == false)
  126. {
  127. this.Result.Set(context, bRet.ToString());
  128. return;
  129. }
  130.  
  131. string hostname = "demo2.demo.com";
  132. string username = "406";
  133. string password = "123123";
  134. string id = username + "@" + hostname;
  135. String regUri = hostname;
  136. if (phone.WinSipItf.DoSipRegister(id, regUri, username, password) == 0)
  137. {
  138. this.Result.Set(context, bRet.ToString());
  139. return;
  140. }
  141.  
  142. // Set a bookmark - the extension will resume when the Gizmo is fired
  143. context.CreateBookmark(BookmarkName, RegistrationCallback);
  144.  
  145. //context.CreateBookmark(this.PhoneTransition.ToString(), this.RegistrationCallback);
  146.  
  147. //// Obtain the runtime value of the Text input argument
  148. //string text = context.GetValue(this.Text);
  149.  
  150. //Result.Set(context, string.Format("The text is {0}", text));
  151. }
  152.  
  153. /// <summary>
  154. /// The on transition callback.
  155. /// </summary>
  156. /// <param name="context">
  157. /// The context.
  158. /// </param>
  159. /// <param name="bookmark">
  160. /// The bookmark.
  161. /// </param>
  162. /// <param name="value">
  163. /// The value.
  164. /// </param>
  165. /// <exception cref="InvalidOperationException">
  166. /// </exception>
  167. private void OnRegistrationCallback(NativeActivityContext context, Bookmark bookmark, object value)
  168. {
  169. if (value is WinSipItf.MSGDATA)
  170. {
  171.  
  172. }
  173. ////if (value is StateTransitionResult)
  174. //{
  175. // this.Result.Set(context, value as StateTransitionResult);
  176. //}
  177. //else if (value != null)
  178. //{
  179. // // Resumed with something else
  180. // throw new InvalidOperationException(
  181. // "You must resume PhoneTransition bookmarks with a result of type StateTransitionResult");
  182. //}
  183.  
  184. // Exit the no persist zone
  185. var handle = _noPersistHandle.Get(context);
  186. handle.Exit(context);
  187. }
  188.  
  189. #endregion
  190. }
Add Comment
Please, Sign In to add comment