Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/### obsolete/ClientPort.cs @ 11713

Last change on this file since 11713 was 11500, checked in by swagner, 10 years ago

#2205: Worked on optimization networks

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using System;
26using System.Drawing;
27using System.Linq;
28using System.Threading;
29using System.Threading.Tasks;
30
31namespace HeuristicLab.Optimization.Networks {
32  [Item("ClientPort", "A client port of an optimization network node.")]
33  [StorableClass]
34  public class ClientPort : ClientServicePort, IClientPort {
35    public override Image ItemImage {
36      get {
37        if (Valid) return base.ItemImage;
38        else return HeuristicLab.Common.Resources.VSImageLibrary.Error;
39      }
40    }
41
42    [Storable]
43    protected IServicePort servicePort;
44    public IServicePort ServicePort {
45      get { return servicePort; }
46      set {
47        if (servicePort != value) {
48          DeregisterServicePortEvents();
49          servicePort = value;
50          RegisterServicePortEvents();
51          OnServicePortChanged();
52          OnInterfaceChanged();
53        }
54      }
55    }
56    [Storable]
57    protected bool valid;
58    public bool Valid {
59      get { return valid; }
60      protected set {
61        if (value != valid) {
62          valid = value;
63          OnItemImageChanged();
64        }
65      }
66    }
67
68    [StorableConstructor]
69    protected ClientPort(bool deserializing) : base(deserializing) { }
70    protected ClientPort(ClientPort original, Cloner cloner)
71      : base(original, cloner) {
72      servicePort = cloner.Clone(original.servicePort);
73      valid = original.valid;
74      RegisterServicePortEvents();
75    }
76    public ClientPort() : base("ClientPort") { }
77    public ClientPort(string name) : base(name) { }
78    public ClientPort(string name, string description) : base(name, description) { }
79
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      RegisterServicePortEvents();
83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new ClientPort(this, cloner);
87    }
88
89    public bool IsValidServicePort(IServicePort servicePort) {
90      if (servicePort == null) return false;
91      // check input parameters
92      foreach (var input in servicePort.Parameters.Where(p => p.Type == ServiceParameterType.Input)) {
93        IServiceParameter param;
94        Parameters.TryGetValue(input.Name, out param);
95        if ((param == null) && (input.Value == null)) return false;
96        if (param.Type != input.Type) return false;
97        if (!input.DataType.IsAssignableFrom(param.DataType)) return false;
98      }
99      // check output parameters
100      foreach (var output in Parameters.Where(p => p.Type == ServiceParameterType.Output)) {
101        IServiceParameter param;
102        servicePort.Parameters.TryGetValue(output.Name, out param);
103        if (param == null) return false;
104        if (param.Type != output.Type) return false;
105        if (!output.DataType.IsAssignableFrom(param.DataType)) return false;
106      }
107      return true;
108    }
109    public void CloneServicePortParameters() {
110      Parameters.Clear();
111      foreach (var p in servicePort.Parameters) {
112        Parameters.Add((IServiceParameter)p.Clone());
113      }
114    }
115    public ServiceParameterCollection PrepareParameters() {
116      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
117      var parameters = new ServiceParameterCollection();
118      // collect input parameters (take inputs from service and set default values of client)
119      parameters.AddRange(
120        ServicePort.Parameters.
121        Where(p => p.Type == ServiceParameterType.Input).
122        Select(p => (IServiceParameter)p.Clone()).
123        Select(p => {
124          IServiceParameter param;
125          Parameters.TryGetValue(p.Name, out param);
126          if ((param != null) && (param.Value != null))
127            p.Value = (IItem)param.Value.Clone();
128          return p;
129        }));
130      // collect output parameters (take outputs from client)
131      parameters.AddRange(
132        Parameters.
133        Where(p => p.Type == ServiceParameterType.Output).
134        Select(p => (IServiceParameter)p.Clone()));
135      return parameters;
136    }
137    public ServiceParameterCollection CallService(ServiceParameterCollection parameters) {
138      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
139      return ServicePort.Process(parameters);
140    }
141    public ServiceParameterCollection CallService(ServiceParameterCollection parameters, CancellationToken token) {
142      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
143      return ServicePort.Process(parameters, token);
144    }
145    public async Task<ServiceParameterCollection> CallServiceAsync(ServiceParameterCollection parameters) {
146      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
147      return await ServicePort.ProcessAsync(parameters);
148    }
149    public async Task<ServiceParameterCollection> CallServiceAsync(ServiceParameterCollection parameters, CancellationToken token) {
150      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
151      return await ServicePort.ProcessAsync(parameters, token);
152    }
153
154    protected override void OnInterfaceChanged() {
155      Valid = (servicePort != null) && IsValidServicePort(servicePort);
156      base.OnInterfaceChanged();
157    }
158
159    public event EventHandler ServicePortChanged;
160    protected virtual void OnServicePortChanged() {
161      var handler = ServicePortChanged;
162      if (handler != null) handler(this, EventArgs.Empty);
163    }
164
165    #region ServicePort Events
166    protected void RegisterServicePortEvents() {
167      if (servicePort != null) {
168        servicePort.InterfaceChanged += ServicePort_InterfaceChanged;
169      }
170    }
171    protected void DeregisterServicePortEvents() {
172      if (servicePort != null) {
173        servicePort.InterfaceChanged -= ServicePort_InterfaceChanged;
174      }
175    }
176    protected void ServicePort_InterfaceChanged(object sender, EventArgs e) {
177      OnInterfaceChanged();
178    }
179    #endregion
180  }
181}
Note: See TracBrowser for help on using the repository browser.