source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ClientPort.cs @ 11468

Last change on this file since 11468 was 11468, checked in by swagner, 8 years ago

#2205: Worked on optimization networks

File size: 6.7 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 ServiceParameterCollection PrepareParameters() {
110      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
111      var parameters = new ServiceParameterCollection();
112      // collect input parameters (take inputs from service and set default values of client)
113      parameters.AddRange(
114        ServicePort.Parameters.
115        Where(p => p.Type == ServiceParameterType.Input).
116        Select(p => (IServiceParameter)p.Clone()).
117        Select(p => {
118          IServiceParameter param;
119          Parameters.TryGetValue(p.Name, out param);
120          if ((param != null) && (param.Value != null))
121            p.Value = (IItem)param.Value.Clone();
122          return p;
123        }));
124      // collect output parameters (take outputs from client)
125      parameters.AddRange(
126        Parameters.
127        Where(p => p.Type == ServiceParameterType.Output).
128        Select(p => (IServiceParameter)p.Clone()));
129      return parameters;
130    }
131    public ServiceParameterCollection CallService(ServiceParameterCollection parameters) {
132      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
133      return ServicePort.Process(parameters);
134    }
135    public ServiceParameterCollection CallService(ServiceParameterCollection parameters, CancellationToken token) {
136      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
137      return ServicePort.Process(parameters, token);
138    }
139    public async Task<ServiceParameterCollection> CallServiceAsync(ServiceParameterCollection parameters) {
140      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
141      return await ServicePort.ProcessAsync(parameters);
142    }
143    public async Task<ServiceParameterCollection> CallServiceAsync(ServiceParameterCollection parameters, CancellationToken token) {
144      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
145      return await ServicePort.ProcessAsync(parameters, token);
146    }
147
148    protected override void OnInterfaceChanged() {
149      Valid = (servicePort != null) && IsValidServicePort(servicePort);
150      base.OnInterfaceChanged();
151    }
152
153    public event EventHandler ServicePortChanged;
154    protected virtual void OnServicePortChanged() {
155      var handler = ServicePortChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
158
159    #region ServicePort Events
160    protected void RegisterServicePortEvents() {
161      if (servicePort != null) {
162        servicePort.InterfaceChanged += ServicePort_InterfaceChanged;
163      }
164    }
165    protected void DeregisterServicePortEvents() {
166      if (servicePort != null) {
167        servicePort.InterfaceChanged -= ServicePort_InterfaceChanged;
168      }
169    }
170    protected void ServicePort_InterfaceChanged(object sender, EventArgs e) {
171      OnInterfaceChanged();
172    }
173    #endregion
174  }
175}
Note: See TracBrowser for help on using the repository browser.