Skip to content

Commit e6f3004

Browse files
authored
Improve client subscription implementation (#2245)
- shutdown the worker tasks with a ManualResetEvent instead of timer null check - Breaking change: Decorate the PublishStateChanged event handler with state information: - - Stopped/Recovered if publish times out/restarts - Breaking change: PublishStateChange fired for every received publish response, which doesn`t really indicate a state change. -- new behavior: fires only if a Keep alive or republish is triggered.
1 parent 81340c3 commit e6f3004

File tree

6 files changed

+324
-240
lines changed

6 files changed

+324
-240
lines changed

Applications/ClientControls.Net4/Common/Client/SubscribeDataListViewCtrl.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public partial class SubscribeDataListViewCtrl : UserControl
5151
public SubscribeDataListViewCtrl()
5252
{
5353
InitializeComponent();
54-
m_PublishStatusChanged = new EventHandler(OnPublishStatusChanged);
54+
m_PublishStatusChanged = new PublishStateChangedEventHandler(OnPublishStatusChanged);
5555
ResultsDV.AutoGenerateColumns = false;
5656
ImageList = new ClientUtils().ImageList;
57-
57+
5858
m_dataset = new DataSet();
5959
m_dataset.Tables.Add("Requests");
6060

@@ -86,7 +86,7 @@ public SubscribeDataListViewCtrl()
8686
private Subscription m_subscription;
8787
private DisplayState m_state;
8888
private EditComplexValueDlg m_EditComplexValueDlg;
89-
private EventHandler m_PublishStatusChanged;
89+
private PublishStateChangedEventHandler m_PublishStatusChanged;
9090
#endregion
9191

9292
#region Stage Enum
@@ -196,7 +196,7 @@ public void SetSubscription(Subscription subscription)
196196
m_subscription.Handle = this;
197197
}
198198
}
199-
199+
200200
/// <summary>
201201
/// Adds the monitored items to the subscription.
202202
/// </summary>
@@ -217,7 +217,7 @@ public void AddItems(params ReadValueId[] itemsToMonitor)
217217
{
218218
continue;
219219
}
220-
220+
221221
DataRow row = m_dataset.Tables[0].NewRow();
222222

223223
MonitoredItem monitoredItem = new MonitoredItem(m_subscription.DefaultItem);
@@ -421,7 +421,7 @@ private void UpdateRow(DataRow row, MonitoredItemNotification notification)
421421

422422
if (value != null)
423423
{
424-
row[1] = ImageList.Images[ClientUtils.GetImageIndex(Attributes.Value, value.Value)];
424+
row[1] = ImageList.Images[ClientUtils.GetImageIndex(Attributes.Value, value.Value)];
425425
row[12] = (value.WrappedValue.TypeInfo != null) ? value.WrappedValue.TypeInfo.ToString() : String.Empty;
426426
row[13] = value.WrappedValue;
427427
row[14] = value.StatusCode;
@@ -445,13 +445,13 @@ private string GetDisplayString(Subscription subscription)
445445
buffer.Append("/");
446446
buffer.Append(subscription.CurrentLifetimeCount);
447447
buffer.Append("}");
448-
448+
449449
return buffer.ToString();
450450
}
451451
#endregion
452452

453453
#region Event Handlers
454-
private void OnPublishStatusChanged(object sender, EventArgs e)
454+
private void OnPublishStatusChanged(object sender, PublishStateChangedEventArgs e)
455455
{
456456
if (!Object.ReferenceEquals(sender, m_subscription))
457457
{
@@ -466,12 +466,12 @@ private void OnPublishStatusChanged(object sender, EventArgs e)
466466

467467
try
468468
{
469-
if (m_subscription.PublishingStopped)
469+
if ((e.Status & PublishStateChangedMask.Stopped) != 0)
470470
{
471471
SubscriptionStateTB.Text = "STOPPED";
472472
SubscriptionStateTB.ForeColor = Color.Red;
473473
}
474-
else
474+
else if ((e.Status & PublishStateChangedMask.Recovered) != 0)
475475
{
476476
SubscriptionStateTB.Text = GetDisplayString(m_subscription);
477477
SubscriptionStateTB.ForeColor = Color.Empty;
@@ -649,7 +649,7 @@ private void ViewValueMI_Click(object sender, EventArgs e)
649649

650650
m_EditComplexValueDlg = new EditComplexValueDlg();
651651
m_EditComplexValueDlg.Tag = monitoredItem;
652-
652+
653653
m_EditComplexValueDlg.ShowDialog(
654654
m_session,
655655
monitoredItem.ResolvedNodeId,
@@ -706,10 +706,10 @@ private void SetMonitoringModeMI_Click(object sender, EventArgs e)
706706
MonitoringMode oldMonitoringMode = monitoredItems[0].MonitoringMode;
707707
MonitoringMode newMonitoringMode = new EditMonitoredItemDlg().ShowDialog(oldMonitoringMode);
708708

709-
if (oldMonitoringMode != newMonitoringMode)
709+
if (oldMonitoringMode != newMonitoringMode)
710710
{
711711
List<MonitoredItem> itemsToModify = new List<MonitoredItem>();
712-
712+
713713
foreach (MonitoredItem monitoredItem in monitoredItems)
714714
{
715715
DataRow row = (DataRow)monitoredItem.Handle;
@@ -723,7 +723,7 @@ private void SetMonitoringModeMI_Click(object sender, EventArgs e)
723723

724724
monitoredItem.MonitoringMode = newMonitoringMode;
725725
}
726-
726+
727727
if (itemsToModify.Count != 0)
728728
{
729729
m_subscription.SetMonitoringMode(newMonitoringMode, itemsToModify);

Applications/ClientControls.Net4/Common/Client/SubscribeEventsDlg.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public SubscribeEventsDlg()
5656
BrowseCTRL.BrowseTV.CheckBoxes = true;
5757
BrowseCTRL.BrowseTV.AfterCheck += new TreeViewEventHandler(BrowseTV_AfterCheck);
5858

59-
m_PublishStatusChanged = new EventHandler(OnPublishStatusChanged);
59+
m_PublishStatusChanged = new PublishStateChangedEventHandler(OnPublishStatusChanged);
6060
ItemsDV.AutoGenerateColumns = false;
6161
ImageList = new ClientUtils().ImageList;
6262

@@ -74,14 +74,14 @@ public SubscribeEventsDlg()
7474
ItemsDV.DataSource = m_dataset.Tables[0];
7575
}
7676
#endregion
77-
77+
7878
#region Private Fields
7979
private DataSet m_dataset;
8080
private FilterDeclaration m_filter;
8181
private DisplayState m_state;
8282
private Session m_session;
8383
private Subscription m_subscription;
84-
private EventHandler m_PublishStatusChanged;
84+
private PublishStateChangedEventHandler m_PublishStatusChanged;
8585
#endregion
8686

8787
private enum DisplayState
@@ -389,9 +389,9 @@ private string GetDisplayString(Subscription subscription)
389389
buffer.Append(" (");
390390
buffer.Append(subscription.CurrentPublishingInterval);
391391
buffer.Append("ms/");
392-
buffer.Append(subscription.CurrentPublishingInterval*subscription.CurrentKeepAliveCount/1000);
392+
buffer.Append(subscription.CurrentPublishingInterval * subscription.CurrentKeepAliveCount / 1000);
393393
buffer.Append("s/");
394-
buffer.Append(subscription.CurrentPublishingInterval*subscription.CurrentLifetimeCount/1000);
394+
buffer.Append(subscription.CurrentPublishingInterval * subscription.CurrentLifetimeCount / 1000);
395395
buffer.Append("s}");
396396

397397
return buffer.ToString();
@@ -504,7 +504,7 @@ private void UpdateFilter()
504504

505505
fields.Add(field);
506506
}
507-
507+
508508
// update filter.
509509
m_filter.EventTypeId = eventTypeId;
510510
m_filter.Fields = fields;
@@ -644,7 +644,7 @@ private void SetEventTypeChecks(TreeNode node, bool isChecked)
644644
#endregion
645645

646646
#region Event Handlers
647-
private void OnPublishStatusChanged(object sender, EventArgs e)
647+
private void OnPublishStatusChanged(object sender, PublishStateChangedEventArgs e)
648648
{
649649
if (!Object.ReferenceEquals(sender, m_subscription))
650650
{
@@ -659,12 +659,12 @@ private void OnPublishStatusChanged(object sender, EventArgs e)
659659

660660
try
661661
{
662-
if (m_subscription.PublishingStopped)
662+
if ((e.Status & PublishStateChangedMask.Stopped) != 0)
663663
{
664664
SubscriptionStateTB.Text = "STOPPED";
665665
SubscriptionStateTB.ForeColor = Color.Red;
666666
}
667-
else
667+
else if ((e.Status & PublishStateChangedMask.Recovered) != 0)
668668
{
669669
SubscriptionStateTB.Text = GetDisplayString(m_subscription);
670670
SubscriptionStateTB.ForeColor = Color.Empty;

Applications/ClientControls.Net4/Common/DiscoveredServerOnNetworkListCtrl.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ public DiscoveredServerOnNetworkListCtrl()
5757
ItemsLV.MultiSelect = false;
5858
}
5959
#endregion
60-
60+
6161
#region Private Fields
6262
// The columns to display in the control.
63-
private readonly object[][] m_ColumnNames = new object[][]
64-
{
65-
new object[] { "RecordId", HorizontalAlignment.Left, null },
66-
new object[] { "ServerName", HorizontalAlignment.Left, null },
67-
new object[] { "DiscoveryUrl", HorizontalAlignment.Left, null },
68-
new object[] { "ServerCapabilities", HorizontalAlignment.Left, null }
69-
};
70-
63+
private readonly object[][] m_ColumnNames = new object[][]
64+
{
65+
new object[] { "RecordId", HorizontalAlignment.Left, null },
66+
new object[] { "ServerName", HorizontalAlignment.Left, null },
67+
new object[] { "DiscoveryUrl", HorizontalAlignment.Left, null },
68+
new object[] { "ServerCapabilities", HorizontalAlignment.Left, null }
69+
};
70+
7171
private ApplicationConfiguration m_configuration;
7272
private int m_discoveryTimeout;
7373
private int m_discoverCount;
@@ -115,7 +115,7 @@ public void Initialize(string hostname, NumericUpDown startingRecordId, NumericU
115115
{
116116
hostname = System.Net.Dns.GetHostName();
117117
}
118-
118+
119119
this.Instructions = Utils.Format("Discovering servers on host '{0}'.", hostname);
120120
AdjustColumns();
121121

@@ -131,7 +131,7 @@ public void Initialize(string hostname, NumericUpDown startingRecordId, NumericU
131131
{
132132
discoveryUrls = new StringCollection(Utils.DiscoveryUrls);
133133
}
134-
134+
135135
// update the urls with the hostname.
136136
StringCollection urlsToUse = new StringCollection();
137137

@@ -154,7 +154,7 @@ private void OnUpdateServers(object state)
154154
this.BeginInvoke(new WaitCallback(OnUpdateServers), state);
155155
return;
156156
}
157-
157+
158158
ItemsLV.Items.Clear();
159159

160160
ServerOnNetworkCollection servers = state as ServerOnNetworkCollection;
@@ -254,7 +254,7 @@ private bool DiscoverServersOnNetwork(Uri discoveryUrl)
254254
}
255255
catch (Exception e)
256256
{
257-
Utils.LogError("Error retrieving FindServersOnNetwork parameters. Error=({1}){0}", e.Message, e.GetType());
257+
Utils.LogError("Error retrieving FindServersOnNetwork parameters. Error=({0}){1}", e.GetType(), e.Message);
258258
return false;
259259
}
260260

@@ -265,7 +265,7 @@ private bool DiscoverServersOnNetwork(Uri discoveryUrl)
265265
}
266266
catch (Exception e)
267267
{
268-
Utils.LogError("DISCOVERY ERROR - Could not fetch servers from url: {0}. Error=({2}){1}", discoveryUrl, e.Message, e.GetType());
268+
Utils.LogError("DISCOVERY ERROR - Could not fetch servers from url: {0}. Error=({1}){2}", discoveryUrl, e.GetType(), e.Message);
269269
return false;
270270
}
271271
finally
@@ -295,7 +295,7 @@ protected override void UpdateItem(ListViewItem listItem, object item)
295295
listItem.SubItems[0].Text = String.Format("{0}", server.RecordId);
296296
listItem.SubItems[1].Text = String.Format("{0}", server.ServerName);
297297
listItem.SubItems[2].Text = String.Format("{0}", server.DiscoveryUrl);
298-
listItem.SubItems[3].Text = String.Format("{0}", string.Join(",", server.ServerCapabilities));
298+
listItem.SubItems[3].Text = String.Format("{0}", string.Join(",", server.ServerCapabilities));
299299

300300
listItem.ImageKey = GuiUtils.Icons.Service;
301301
}

0 commit comments

Comments
 (0)