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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using System;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Linq;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Optimization.Networks {
|
---|
30 | [Item("ClientPort", "A client port of an optimization network node.")]
|
---|
31 | [StorableClass]
|
---|
32 | public class ClientPort : ClientServicePort, IClientPort {
|
---|
33 | public override Image ItemImage {
|
---|
34 | get {
|
---|
35 | if (Valid) return base.ItemImage;
|
---|
36 | else return HeuristicLab.Common.Resources.VSImageLibrary.Error;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | protected IServicePort servicePort;
|
---|
42 | public IServicePort ServicePort {
|
---|
43 | get { return servicePort; }
|
---|
44 | set {
|
---|
45 | if (servicePort != value) {
|
---|
46 | DeregisterServicePortEvents();
|
---|
47 | servicePort = value;
|
---|
48 | RegisterServicePortEvents();
|
---|
49 | OnServicePortChanged();
|
---|
50 | OnInterfaceChanged();
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 | [Storable]
|
---|
55 | protected bool valid;
|
---|
56 | public bool Valid {
|
---|
57 | get { return valid; }
|
---|
58 | protected set {
|
---|
59 | if (value != valid) {
|
---|
60 | valid = value;
|
---|
61 | OnItemImageChanged();
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | [StorableConstructor]
|
---|
67 | protected ClientPort(bool deserializing) : base(deserializing) { }
|
---|
68 | protected ClientPort(ClientPort original, Cloner cloner)
|
---|
69 | : base(original, cloner) {
|
---|
70 | servicePort = cloner.Clone(original.servicePort);
|
---|
71 | valid = original.valid;
|
---|
72 | RegisterServicePortEvents();
|
---|
73 | }
|
---|
74 | public ClientPort() : base("ClientPort") { }
|
---|
75 | public ClientPort(string name) : base(name) { }
|
---|
76 | public ClientPort(string name, string description) : base(name, description) { }
|
---|
77 |
|
---|
78 | [StorableHook(HookType.AfterDeserialization)]
|
---|
79 | private void AfterDeserialization() {
|
---|
80 | RegisterServicePortEvents();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
84 | return new ClientPort(this, cloner);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public bool IsValidServicePort(IServicePort servicePort) {
|
---|
88 | if (servicePort == null) return false;
|
---|
89 | // check input parameters
|
---|
90 | foreach (var input in servicePort.Parameters.Where(p => p.Type == ServiceParameterType.Input)) {
|
---|
91 | IServiceParameter param;
|
---|
92 | Parameters.TryGetValue(input.Name, out param);
|
---|
93 | if ((param == null) && (input.Value == null)) return false;
|
---|
94 | if (param.Type != input.Type) return false;
|
---|
95 | if (!input.DataType.IsAssignableFrom(param.DataType)) return false;
|
---|
96 | }
|
---|
97 | // check output parameters
|
---|
98 | foreach (var output in Parameters.Where(p => p.Type == ServiceParameterType.Output)) {
|
---|
99 | IServiceParameter param;
|
---|
100 | servicePort.Parameters.TryGetValue(output.Name, out param);
|
---|
101 | if (param == null) return false;
|
---|
102 | if (param.Type != output.Type) return false;
|
---|
103 | if (!output.DataType.IsAssignableFrom(param.DataType)) return false;
|
---|
104 | }
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 | public ServiceParameterCollection PrepareParameters() {
|
---|
108 | if (!Valid) throw new InvalidOperationException("Port configurations do not match");
|
---|
109 | var parameters = new ServiceParameterCollection();
|
---|
110 | // collect input parameters (take inputs from service and set default values of client)
|
---|
111 | parameters.AddRange(
|
---|
112 | ServicePort.Parameters.
|
---|
113 | Where(p => p.Type == ServiceParameterType.Input).
|
---|
114 | Select(p => (IServiceParameter)p.Clone()).
|
---|
115 | Select(p => {
|
---|
116 | IServiceParameter param;
|
---|
117 | Parameters.TryGetValue(p.Name, out param);
|
---|
118 | if ((param != null) && (param.Value != null))
|
---|
119 | p.Value = (IItem)param.Value.Clone();
|
---|
120 | return p;
|
---|
121 | }));
|
---|
122 | // collect output parameters (take outputs from client)
|
---|
123 | parameters.AddRange(
|
---|
124 | Parameters.
|
---|
125 | Where(p => p.Type == ServiceParameterType.Output).
|
---|
126 | Select(p => (IServiceParameter)p.Clone()));
|
---|
127 | return parameters;
|
---|
128 | }
|
---|
129 | public ServiceParameterCollection Call(ServiceParameterCollection parameters) {
|
---|
130 | if (!Valid) throw new InvalidOperationException("Port configurations do not match");
|
---|
131 | return ServicePort.Call(parameters);
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected override void OnInterfaceChanged() {
|
---|
135 | Valid = (servicePort != null) && IsValidServicePort(servicePort);
|
---|
136 | base.OnInterfaceChanged();
|
---|
137 | }
|
---|
138 |
|
---|
139 | public event EventHandler ServicePortChanged;
|
---|
140 | protected virtual void OnServicePortChanged() {
|
---|
141 | var handler = ServicePortChanged;
|
---|
142 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
143 | }
|
---|
144 |
|
---|
145 | #region ServicePort Events
|
---|
146 | protected void RegisterServicePortEvents() {
|
---|
147 | if (servicePort != null) {
|
---|
148 | servicePort.InterfaceChanged += ServicePort_InterfaceChanged;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | protected void DeregisterServicePortEvents() {
|
---|
152 | if (servicePort != null) {
|
---|
153 | servicePort.InterfaceChanged -= ServicePort_InterfaceChanged;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | protected void ServicePort_InterfaceChanged(object sender, EventArgs e) {
|
---|
157 | OnInterfaceChanged();
|
---|
158 | }
|
---|
159 | #endregion
|
---|
160 | }
|
---|
161 | }
|
---|