[7281] | 1 | using System;
|
---|
[7317] | 2 | using System.Diagnostics;
|
---|
| 3 | using System.Timers;
|
---|
[7281] | 4 | using System.Windows.Forms;
|
---|
[7317] | 5 | using HeuristicLab.Clients.Hive.CloudManager.Model;
|
---|
[7389] | 6 | using HeuristicLab.Common;
|
---|
[7317] | 7 | using HeuristicLab.Core;
|
---|
| 8 | using HeuristicLab.Core.Views;
|
---|
| 9 | using HeuristicLab.MainForm;
|
---|
[7281] | 10 |
|
---|
| 11 | namespace HeuristicLab.Clients.Hive.CloudManager.Views {
|
---|
[7317] | 12 | [View("Cloud Resources View")]
|
---|
| 13 | [Content(typeof(IItemList<Subscription>), IsDefaultView = true)]
|
---|
| 14 | public partial class CloudResourcesView : ItemView, IDisposable {
|
---|
| 15 | private System.Timers.Timer timer = new System.Timers.Timer();
|
---|
| 16 | private int updateInterval = 15000;
|
---|
| 17 | private DateTime dueTime;
|
---|
[7387] | 18 |
|
---|
[7324] | 19 | private const int subscriptionImageIndex = 0;
|
---|
| 20 | private const int serviceImageIndex = 1;
|
---|
[7387] | 21 | private const int deploymentImageIndex = 2;
|
---|
[7317] | 22 |
|
---|
| 23 | public new IItemList<Subscription> Content {
|
---|
| 24 | get { return (IItemList<Subscription>)base.Content; }
|
---|
| 25 | set { base.Content = value; }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[7387] | 28 | private IItemList<HostedService> hostedServices;
|
---|
| 29 | public IItemList<HostedService> HostedServices {
|
---|
| 30 | get { return hostedServices; }
|
---|
| 31 | set { hostedServices = value; }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[7281] | 34 | public CloudResourcesView() {
|
---|
| 35 | InitializeComponent();
|
---|
[7317] | 36 |
|
---|
[7387] | 37 | treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NewFolder);
|
---|
[7324] | 38 | treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NetworkCenterLarge);
|
---|
| 39 | treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.MonitorLarge);
|
---|
| 40 |
|
---|
[7387] | 41 | HostedServices = CloudManagerClient.Instance.HostedServices;
|
---|
[7324] | 42 | Content = CloudManagerClient.Instance.Subscriptions;
|
---|
[7387] | 43 |
|
---|
| 44 |
|
---|
[7317] | 45 | CloudManagerClient.Instance.Refreshing += new EventHandler(Instance_Refreshing);
|
---|
| 46 | CloudManagerClient.Instance.Refreshed += new EventHandler(Instance_Refreshed);
|
---|
| 47 |
|
---|
| 48 | timer.AutoReset = true;
|
---|
| 49 | timer.Interval = 1000;
|
---|
| 50 | timer.Elapsed += PerformUpdate;
|
---|
| 51 | timer.Start();
|
---|
| 52 | dueTime = DateTime.Now.AddMilliseconds(updateInterval);
|
---|
[7281] | 53 | }
|
---|
[7317] | 54 |
|
---|
[7324] | 55 | #region Register Content Events
|
---|
| 56 | protected override void DeregisterContentEvents() {
|
---|
| 57 | Content.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsAdded);
|
---|
| 58 | Content.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsRemoved);
|
---|
| 59 | base.DeregisterContentEvents();
|
---|
| 60 | }
|
---|
| 61 | protected override void RegisterContentEvents() {
|
---|
| 62 | base.RegisterContentEvents();
|
---|
| 63 | Content.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsAdded);
|
---|
| 64 | Content.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsRemoved);
|
---|
| 65 | }
|
---|
| 66 | #endregion
|
---|
| 67 |
|
---|
[7387] | 68 | /// <summary>
|
---|
| 69 | /// Clean up any resources being used.
|
---|
| 70 | /// </summary>
|
---|
| 71 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
---|
| 72 | protected override void Dispose(bool disposing) {
|
---|
| 73 | if (disposing && (components != null)) {
|
---|
| 74 | components.Dispose();
|
---|
| 75 | CloudManagerClient.Instance.Refreshing -= new EventHandler(Instance_Refreshing);
|
---|
| 76 | CloudManagerClient.Instance.Refreshed -= new EventHandler(Instance_Refreshed);
|
---|
| 77 | timer.Stop();
|
---|
| 78 | timer.Dispose();
|
---|
| 79 | timer = null;
|
---|
| 80 | }
|
---|
| 81 | base.Dispose(disposing);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[7324] | 84 | protected override void SetEnabledStateOfControls() {
|
---|
| 85 | base.SetEnabledStateOfControls();
|
---|
| 86 | if (Content == null || Content.Count == 0) {
|
---|
| 87 | btnAddSlaveService.Enabled = false;
|
---|
| 88 | btnDelete.Enabled = false;
|
---|
| 89 | btnSave.Enabled = false;
|
---|
| 90 | } else {
|
---|
| 91 | btnAddSlaveService.Enabled = true;
|
---|
| 92 | btnDelete.Enabled = true;
|
---|
| 93 | btnSave.Enabled = true;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | protected override void OnContentChanged() {
|
---|
| 98 | base.OnContentChanged();
|
---|
| 99 | SetEnabledStateOfControls();
|
---|
| 100 | if (Content == null) {
|
---|
[7403] | 101 | viewHost.Content = null;
|
---|
[7324] | 102 | treeCloudResources.Nodes.Clear();
|
---|
| 103 | } else {
|
---|
[7387] | 104 | treeCloudResources.BeginUpdate();
|
---|
[7324] | 105 | treeCloudResources.Nodes.Clear();
|
---|
| 106 | foreach (Subscription sub in Content) {
|
---|
[7387] | 107 | TreeNode nodeSub = new TreeNode();
|
---|
| 108 | nodeSub.Tag = sub;
|
---|
| 109 | nodeSub.Text = sub.SubscriptionName;
|
---|
| 110 | nodeSub.ImageIndex = subscriptionImageIndex;
|
---|
| 111 | nodeSub.SelectedImageIndex = nodeSub.ImageIndex;
|
---|
| 112 |
|
---|
| 113 | foreach (HostedService s in HostedServices) {
|
---|
[7403] | 114 | if (s.Subscription.SubscriptionID == sub.SubscriptionID) {
|
---|
[7387] | 115 | TreeNode nodeServ = new TreeNode();
|
---|
| 116 | nodeServ.Tag = s;
|
---|
| 117 | nodeServ.Text = s.HostedServiceProperties.Label;
|
---|
| 118 | nodeServ.ImageIndex = serviceImageIndex;
|
---|
| 119 | nodeServ.SelectedImageIndex = nodeServ.ImageIndex;
|
---|
| 120 |
|
---|
| 121 | foreach (Deployment d in s.Deployments) {
|
---|
| 122 | TreeNode nodeDepl = new TreeNode();
|
---|
| 123 | nodeDepl.Tag = d;
|
---|
| 124 | nodeDepl.Text = d.Label + " (" + d.DeploymentSlot + ")";
|
---|
| 125 | nodeDepl.ImageIndex = deploymentImageIndex;
|
---|
| 126 | nodeDepl.SelectedImageIndex = nodeDepl.ImageIndex;
|
---|
| 127 | nodeServ.Nodes.Add(nodeDepl);
|
---|
| 128 | }
|
---|
| 129 | nodeSub.Nodes.Add(nodeServ);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | treeCloudResources.Nodes.Add(nodeSub);
|
---|
[7324] | 133 | }
|
---|
[7387] | 134 | treeCloudResources.EndUpdate();
|
---|
[7324] | 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private void Instance_Refreshing(object sender, EventArgs e) {
|
---|
[7317] | 139 | Debug.WriteLine("Instance_Refreshing");
|
---|
[7387] | 140 | //if (treeCloudResources.InvokeRequired) {
|
---|
| 141 | // treeCloudResources.Invoke((MethodInvoker)delegate { OnContentChanged(); });
|
---|
| 142 | //} else {
|
---|
| 143 | // OnContentChanged();
|
---|
| 144 | //}
|
---|
[7317] | 145 | }
|
---|
| 146 |
|
---|
[7324] | 147 | private void Instance_Refreshed(object sender, EventArgs e) {
|
---|
[7317] | 148 | Debug.WriteLine("Instance_Refreshed");
|
---|
[7387] | 149 | if (treeCloudResources.InvokeRequired) {
|
---|
| 150 | treeCloudResources.Invoke((MethodInvoker)delegate { OnContentChanged(); });
|
---|
| 151 | } else {
|
---|
| 152 | OnContentChanged();
|
---|
| 153 | }
|
---|
[7317] | 154 | }
|
---|
| 155 |
|
---|
[7324] | 156 | private void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Subscription>> e) {
|
---|
| 157 | OnContentChanged();
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | private void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Subscription>> e) {
|
---|
| 161 | OnContentChanged();
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[7317] | 164 | private void PerformUpdate(object sender, ElapsedEventArgs e) {
|
---|
| 165 | TimeSpan timeToRefresh = dueTime - DateTime.Now;
|
---|
| 166 |
|
---|
| 167 | if (timeToRefresh.Seconds >= 0) {
|
---|
| 168 | this.Invoke((MethodInvoker)delegate { lblRefresh.Text = "Done. " + timeToRefresh.Seconds + "s to next refresh."; });
|
---|
| 169 | } else {
|
---|
| 170 | timer.Stop();
|
---|
| 171 | this.Invoke((MethodInvoker)delegate { lblRefresh.Text = "Refreshing data ..."; });
|
---|
| 172 |
|
---|
| 173 | // statistics
|
---|
| 174 | Debug.WriteLine("perform update");
|
---|
[7387] | 175 | CloudManagerClient.Instance.Refresh();
|
---|
[7317] | 176 |
|
---|
| 177 | dueTime = DateTime.Now.AddMilliseconds(updateInterval);
|
---|
[7387] | 178 | if (timer != null) {
|
---|
| 179 | timer.Start();
|
---|
| 180 | }
|
---|
[7317] | 181 | }
|
---|
| 182 | }
|
---|
[7324] | 183 |
|
---|
| 184 | private void btnAddSubscription_Click(object sender, EventArgs e) {
|
---|
| 185 | using (var form = new AddSubscriptionDialog()) {
|
---|
| 186 | form.ShowDialog();
|
---|
| 187 | if (!form.ErrorOccured) {
|
---|
| 188 | if (form.Subscription != null) {
|
---|
| 189 | Subscription sub = form.Subscription;
|
---|
| 190 | CloudManagerClient.Instance.Add(sub);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
[7339] | 195 |
|
---|
| 196 | private void btnAddSlaveService_Click(object sender, EventArgs e) {
|
---|
[7389] | 197 | // When refreshing subscriptions in the dialog,
|
---|
| 198 | // discoverservices and safetoconfig properties are always set to false
|
---|
| 199 | // -> work on deep copy
|
---|
| 200 | Cloner cloner = new Cloner();
|
---|
| 201 | IItemList<Subscription> subscriptions = cloner.Clone(Content);
|
---|
| 202 | using (var form = new AddAzureServiceDialog(subscriptions)) {
|
---|
[7339] | 203 | form.ShowDialog();
|
---|
| 204 |
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
[7389] | 207 |
|
---|
| 208 | private void treeCloudResources_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
|
---|
[7403] | 209 | viewHost.Content = (IContent)e.Node.Tag;
|
---|
[7389] | 210 | }
|
---|
[7281] | 211 | }
|
---|
| 212 | }
|
---|