EventServiceClient.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.WCF;
  6. using Aitex.Core.RT.Event;
  7. using System.ServiceModel;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.Util;
  10. using Aitex.Core.Utilities;
  11. using Aitex.Core.WCF.Interface;
  12. using MECF.Framework.Common.Properties;
  13. namespace Aitex.Core.WCF
  14. {
  15. public class EventClient : Singleton<EventClient>
  16. {
  17. public event Action<EventItem> OnEvent;
  18. public event Action OnDisconnectedWithRT;
  19. public bool InProcess { get; set; }
  20. private IEventService _service;
  21. public IEventService Service
  22. {
  23. get
  24. {
  25. if (_service == null)
  26. {
  27. if (InProcess)
  28. {
  29. _service = Singleton<EventManager>.Instance.Service;
  30. }
  31. else
  32. {
  33. _service = new EventServiceClient(new EventServiceCallback());
  34. }
  35. _service.OnEvent += _service_OnEvent;
  36. }
  37. return _service;
  38. }
  39. }
  40. public bool IsConnectedWithRT
  41. {
  42. get { return _retryConnectRT.Result; }
  43. }
  44. Retry _retryConnectRT = new Retry();
  45. FixSizeQueue<EventItem> _queue = new FixSizeQueue<EventItem>(300);
  46. PeriodicJob _fireJob;
  47. Guid _guid = Guid.NewGuid();
  48. private R_TRIG _trigDisconnect = new R_TRIG();
  49. private DeviceTimer _timer = new DeviceTimer();
  50. public bool ConnectRT()
  51. {
  52. _retryConnectRT.Result = InProcess || Service.Register(_guid);
  53. return _retryConnectRT.IsSucceeded;
  54. }
  55. public void Start()
  56. {
  57. if (_fireJob != null)
  58. return;
  59. _fireJob = new PeriodicJob(500, this.FireEvent, "UIEvent", true);
  60. }
  61. public void Stop()
  62. {
  63. if (!InProcess)
  64. {
  65. if (_retryConnectRT.IsSucceeded)
  66. {
  67. Service.UnRegister(_guid);
  68. }
  69. }
  70. if (_fireJob != null)
  71. {
  72. _fireJob.Stop();
  73. }
  74. }
  75. bool FireEvent()
  76. {
  77. _retryConnectRT.Result = InProcess || Service.Register(_guid);
  78. //if (_retryConnectRT.IsSucceeded)
  79. //{
  80. // _service_OnEvent(new EventItem()
  81. // {
  82. // OccuringTime = DateTime.Now,
  83. // Description = "Connected with RT",
  84. // Id = 0,
  85. // Level = EventLevel.Information,
  86. // Type = EventType.EventUI_Notify,
  87. // });
  88. //}
  89. //if (_retryConnectRT.IsErrored)
  90. //{
  91. // _service_OnEvent(new EventItem()
  92. // {
  93. // OccuringTime = DateTime.Now,
  94. // Description = "Disconnect from RT",
  95. // Id = 0,
  96. // Level = EventLevel.Information,
  97. // Type = EventType.EventUI_Notify,
  98. // });
  99. // _timer.Start(0);
  100. //}
  101. if (_timer.GetElapseTime() > 2000 && !_retryConnectRT.Result && OnDisconnectedWithRT!=null)
  102. {
  103. OnDisconnectedWithRT();
  104. }
  105. if (OnEvent != null)
  106. {
  107. EventItem ev;
  108. while (_queue.TryDequeue(out ev))
  109. {
  110. OnEvent(ev);
  111. }
  112. }
  113. return true;
  114. }
  115. private void _service_OnEvent(EventItem ev)
  116. {
  117. _queue.Enqueue(ev);
  118. }
  119. }
  120. public class EventServiceClient : DuplexChannelServiceClientWrapper<IEventService>, IEventService
  121. {
  122. public event Action<EventItem> OnEvent;
  123. public EventServiceClient(EventServiceCallback callbackInstance)
  124. : base(new InstanceContext(callbackInstance), "Client_IEventService", "EventService")
  125. {
  126. callbackInstance.FireEvent += new Action<EventItem>(callbackInstance_FireEvent);
  127. }
  128. void callbackInstance_FireEvent(EventItem obj)
  129. {
  130. if (OnEvent != null)
  131. {
  132. OnEvent(obj);
  133. }
  134. }
  135. public bool Register(Guid id)
  136. {
  137. bool ret = false;
  138. Invoke(svc => { ret = svc.Register(id); });
  139. return ret;
  140. }
  141. public void UnRegister(Guid id)
  142. {
  143. Invoke(svc => { svc.UnRegister(id); });
  144. }
  145. public int Heartbeat()
  146. {
  147. int result = -1;
  148. Invoke(svc => { result = svc.Heartbeat(); });
  149. return result;
  150. }
  151. }
  152. }