1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
8 | using HeuristicLab.Hive.Contracts.ResponseObjects;
|
---|
9 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
10 | using HeuristicLab.Hive.Experiment.Properties;
|
---|
11 | using System.ServiceModel;
|
---|
12 | using HeuristicLab.Hive.Contracts;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Hive.Experiment {
|
---|
15 | [Item("Hive Client", "Connects to Hive and lists all submitted experiments by the current user.")]
|
---|
16 | [Creatable("Hive")]
|
---|
17 | public class HiveClient : Item {
|
---|
18 | private static object locker = new object();
|
---|
19 |
|
---|
20 | private ILog log;
|
---|
21 | public ILog Log {
|
---|
22 | get { return log; }
|
---|
23 | }
|
---|
24 |
|
---|
25 | private HiveExperimentList hiveExperiments;
|
---|
26 | public HiveExperimentList HiveExperiments {
|
---|
27 | get { return hiveExperiments; }
|
---|
28 | set {
|
---|
29 | if (hiveExperiments != value) {
|
---|
30 | if (hiveExperiments != null) {
|
---|
31 | DeRegisterHiveExperimentsEvents();
|
---|
32 | }
|
---|
33 | hiveExperiments = value;
|
---|
34 | RegisterHiveExperimentsEvent();
|
---|
35 | OnHiveExperimentsChanged();
|
---|
36 | }
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | private void RegisterHiveExperimentsEvent() {
|
---|
41 | hiveExperiments.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<HiveExperiment>>(hiveExperiments_ItemsAdded);
|
---|
42 | hiveExperiments.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<HiveExperiment>>(hiveExperiments_ItemsRemoved);
|
---|
43 | hiveExperiments.ItemsMoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<HiveExperiment>>(hiveExperiments_ItemsMoved);
|
---|
44 | }
|
---|
45 |
|
---|
46 | private void DeRegisterHiveExperimentsEvents() {
|
---|
47 | hiveExperiments.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<HiveExperiment>>(hiveExperiments_ItemsAdded);
|
---|
48 | hiveExperiments.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<HiveExperiment>>(hiveExperiments_ItemsRemoved);
|
---|
49 | hiveExperiments.ItemsMoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<HiveExperiment>>(hiveExperiments_ItemsMoved);
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | public HiveClient() {
|
---|
54 | this.log = new Log();
|
---|
55 | this.HiveExperiments = new HiveExperimentList();
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
|
---|
60 | HiveClient clone = (HiveClient)base.Clone(cloner);
|
---|
61 | clone.log = (ILog)cloner.Clone(this.log);
|
---|
62 | clone.hiveExperiments = (HiveExperimentList)cloner.Clone(this.hiveExperiments);
|
---|
63 | return clone;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void UpdateExperimentList() {
|
---|
67 | //IClientFacade client = CreateClientFacade();
|
---|
68 | using (Disposable<IClientFacade> service = ServiceLocator.Instance.ClientFacadePool.GetService()) {
|
---|
69 | ResponseObject<JobResultList> response = service.Obj.GetChildJobResults(null, false, false);
|
---|
70 | RefreshExperimentList(response.Obj);
|
---|
71 | }
|
---|
72 | //ServiceLocator.DisposeClientFacade(client);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void RefreshExperimentList(JobResultList jobResultList) {
|
---|
76 | foreach (JobResult jobResult in jobResultList) {
|
---|
77 | HiveExperiment hiveExperiment = GetHiveExperiment(jobResult);
|
---|
78 | if (hiveExperiment == null) {
|
---|
79 | // create new
|
---|
80 | hiveExperiment = new HiveExperiment() { RootJobItem = new JobItem(jobResult) };
|
---|
81 | this.hiveExperiments.Add(hiveExperiment);
|
---|
82 | } else {
|
---|
83 | // update
|
---|
84 | //hiveExperiment.RootJob.UpdateJob(jobResult);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | private HiveExperiment GetHiveExperiment(JobResult jobResult) {
|
---|
90 | //var matches = this.hiveExperiments.Where(exp => exp.RootJob != null && exp.RootJob.JobDto.Id == jobResult.JobId);
|
---|
91 | //if (matches.Count() == 0) {
|
---|
92 | // return null;
|
---|
93 | //} else {
|
---|
94 | // return matches.First();
|
---|
95 | //}
|
---|
96 | return null;
|
---|
97 | }
|
---|
98 |
|
---|
99 | //private IClientFacade CreateClientFacade() {
|
---|
100 | // IClientFacade clientFacade = null;
|
---|
101 | // try {
|
---|
102 | // clientFacade = ServiceLocator.CreateClientFacade(Settings.Default.HiveServerIp);
|
---|
103 | // }
|
---|
104 | // catch (EndpointNotFoundException exception) {
|
---|
105 | // LogMessage("Could not connect to Server: " + exception.Message);
|
---|
106 | // }
|
---|
107 | // return clientFacade;
|
---|
108 | //}
|
---|
109 |
|
---|
110 | private void LogMessage(string message) {
|
---|
111 | // HeuristicLab.Log is not Thread-Safe, so lock on every call
|
---|
112 | lock (locker) {
|
---|
113 | log.LogMessage(message);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | public event EventHandler HiveExperimentsChanged;
|
---|
118 | private void OnHiveExperimentsChanged() {
|
---|
119 | var handler = HiveExperimentsChanged;
|
---|
120 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void hiveExperiments_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<HiveExperiment>> e) {
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 | void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<HiveExperiment>> e) {
|
---|
128 |
|
---|
129 | }
|
---|
130 |
|
---|
131 | void hiveExperiments_ItemsMoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<HiveExperiment>> e) {
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | }
|
---|
136 | }
|
---|