1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.IO.Compression;
|
---|
26 | using System.Linq;
|
---|
27 | using System.ServiceModel;
|
---|
28 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.PluginInfrastructure.Advanced {
|
---|
31 | internal class InstallationManager {
|
---|
32 |
|
---|
33 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreUpdatePlugin;
|
---|
34 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreRemovePlugin;
|
---|
35 | internal event EventHandler<PluginInfrastructureCancelEventArgs> PreInstallPlugin;
|
---|
36 |
|
---|
37 | internal event EventHandler<PluginInfrastructureEventArgs> PluginUpdated;
|
---|
38 | internal event EventHandler<PluginInfrastructureEventArgs> PluginRemoved;
|
---|
39 | internal event EventHandler<PluginInfrastructureEventArgs> PluginInstalled;
|
---|
40 |
|
---|
41 | private string pluginDir;
|
---|
42 | public InstallationManager(string pluginDir) {
|
---|
43 | this.pluginDir = pluginDir;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Retrieves a list of plugins available at the remote server
|
---|
48 | /// </summary>
|
---|
49 | /// <returns></returns>
|
---|
50 | public IEnumerable<IPluginDescription> GetRemotePluginList() {
|
---|
51 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
52 | try {
|
---|
53 | List<IPluginDescription> plugins = new List<IPluginDescription>(client.GetPlugins());
|
---|
54 | client.Close();
|
---|
55 | return plugins;
|
---|
56 | }
|
---|
57 | catch (TimeoutException e) {
|
---|
58 | client.Abort();
|
---|
59 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
60 | }
|
---|
61 | catch (FaultException e) {
|
---|
62 | client.Abort();
|
---|
63 | throw new InstallationManagerException("Fault in connection to server.", e);
|
---|
64 | }
|
---|
65 | catch (CommunicationException e) {
|
---|
66 | client.Abort();
|
---|
67 | throw new InstallationManagerException("General communication exception in connection to server.", e);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Retrieves the list of products available at the remote server
|
---|
73 | /// </summary>
|
---|
74 | /// <returns></returns>
|
---|
75 | public IEnumerable<DeploymentService.ProductDescription> GetRemoteProductList() {
|
---|
76 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
77 | try {
|
---|
78 | List<DeploymentService.ProductDescription> products = new List<DeploymentService.ProductDescription>(client.GetProducts());
|
---|
79 | client.Close();
|
---|
80 | return products;
|
---|
81 | }
|
---|
82 | catch (TimeoutException e) {
|
---|
83 | client.Abort();
|
---|
84 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
85 | }
|
---|
86 | catch (FaultException e) {
|
---|
87 | client.Abort();
|
---|
88 | throw new InstallationManagerException("Fault in connection to server.", e);
|
---|
89 | }
|
---|
90 | catch (CommunicationException e) {
|
---|
91 | client.Abort();
|
---|
92 | throw new InstallationManagerException("General communication exception in connection to server.", e);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | /// <summary>
|
---|
97 | /// Installs plugins from remote server
|
---|
98 | /// </summary>
|
---|
99 | /// <param name="plugins"></param>
|
---|
100 | public void Install(IEnumerable<IPluginDescription> plugins, out bool cancelled) {
|
---|
101 | var args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
102 | OnPreInstall(args);
|
---|
103 | if (!args.Cancel) {
|
---|
104 | cancelled = false;
|
---|
105 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
106 | try {
|
---|
107 | foreach (DeploymentService.PluginDescription plugin in plugins) {
|
---|
108 | byte[] zippedPackage = client.GetPlugin(plugin);
|
---|
109 | Unpack(zippedPackage);
|
---|
110 | OnInstalled(new PluginInfrastructureEventArgs(plugin));
|
---|
111 | }
|
---|
112 | client.Close();
|
---|
113 | }
|
---|
114 | catch (TimeoutException e) {
|
---|
115 | client.Abort();
|
---|
116 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
117 | }
|
---|
118 | catch (FaultException e) {
|
---|
119 | client.Abort();
|
---|
120 | throw new InstallationManagerException("Fault in connection to server.", e);
|
---|
121 | }
|
---|
122 | catch (CommunicationException e) {
|
---|
123 | client.Abort();
|
---|
124 | throw new InstallationManagerException("General communication exception in connection to server.", e);
|
---|
125 | }
|
---|
126 | } else {
|
---|
127 | cancelled = true;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | /// <summary>
|
---|
132 | /// Updates plugins from remote server
|
---|
133 | /// </summary>
|
---|
134 | /// <param name="plugins"></param>
|
---|
135 | public void Update(IEnumerable<IPluginDescription> plugins, out bool cancelled) {
|
---|
136 | PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
137 | OnPreUpdate(args);
|
---|
138 | if (!args.Cancel) {
|
---|
139 | cancelled = false;
|
---|
140 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
141 | try {
|
---|
142 | foreach (DeploymentService.PluginDescription plugin in plugins) {
|
---|
143 | byte[] zippedPackage = client.GetPlugin(plugin);
|
---|
144 | Unpack(zippedPackage);
|
---|
145 | OnUpdated(new PluginInfrastructureEventArgs(plugin));
|
---|
146 | }
|
---|
147 | client.Close();
|
---|
148 | }
|
---|
149 | catch (TimeoutException e) {
|
---|
150 | client.Abort();
|
---|
151 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
152 | }
|
---|
153 | catch (FaultException e) {
|
---|
154 | client.Abort();
|
---|
155 | throw new InstallationManagerException("Fault in connection to server.", e);
|
---|
156 | }
|
---|
157 | catch (CommunicationException e) {
|
---|
158 | client.Abort();
|
---|
159 | throw new InstallationManagerException("General communication exception in connection to server.", e);
|
---|
160 | }
|
---|
161 | } else {
|
---|
162 | cancelled = true;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | /// <summary>
|
---|
167 | /// Deletes all plugin files from local installation
|
---|
168 | /// </summary>
|
---|
169 | /// <param name="plugins"></param>
|
---|
170 | public void Remove(IEnumerable<IPluginDescription> plugins) {
|
---|
171 | var fileNames = from pluginToDelete in plugins
|
---|
172 | from file in pluginToDelete.Files
|
---|
173 | select Path.Combine(pluginDir, file.Name);
|
---|
174 | var args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
175 | OnPreDelete(args);
|
---|
176 | if (!args.Cancel) {
|
---|
177 | foreach (string fileName in fileNames) {
|
---|
178 | File.Delete(fileName);
|
---|
179 | OnDeleted(new PluginInfrastructureEventArgs(fileName));
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void Unpack(byte[] zippedPackage) {
|
---|
185 | using (MemoryStream memStream = new MemoryStream(zippedPackage)) {
|
---|
186 | using (ZipArchive zip = new ZipArchive(memStream, ZipArchiveMode.Read)) {
|
---|
187 | foreach (var theEntry in zip.Entries) {
|
---|
188 | string directoryName = pluginDir;
|
---|
189 | string fileName = Path.GetFileName(theEntry.Name);
|
---|
190 | // create directory
|
---|
191 | if (!string.IsNullOrEmpty(directoryName)) {
|
---|
192 | Directory.CreateDirectory(directoryName);
|
---|
193 | }
|
---|
194 | if (!string.IsNullOrEmpty(fileName)) {
|
---|
195 | string fullPath = Path.Combine(directoryName, fileName);
|
---|
196 | string fullDirPath = Path.GetDirectoryName(fullPath);
|
---|
197 | if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
|
---|
198 | using (FileStream streamWriter = File.Create(fullPath)) {
|
---|
199 | int size = 2048;
|
---|
200 | byte[] data = new byte[2048];
|
---|
201 |
|
---|
202 | using (BinaryReader reader = new BinaryReader(theEntry.Open())) {
|
---|
203 | while (true) {
|
---|
204 | size = reader.Read(data, 0, data.Length);
|
---|
205 | if (size > 0) {
|
---|
206 | streamWriter.Write(data, 0, size);
|
---|
207 | } else {
|
---|
208 | break;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | streamWriter.Close();
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void OnPreUpdate(PluginInfrastructureCancelEventArgs args) {
|
---|
221 | if (PreUpdatePlugin != null) PreUpdatePlugin(this, args);
|
---|
222 | }
|
---|
223 |
|
---|
224 | private void OnUpdated(PluginInfrastructureEventArgs args) {
|
---|
225 | if (PluginUpdated != null) PluginUpdated(this, args);
|
---|
226 | }
|
---|
227 |
|
---|
228 | private void OnPreDelete(PluginInfrastructureCancelEventArgs args) {
|
---|
229 | if (PreRemovePlugin != null) PreRemovePlugin(this, args);
|
---|
230 | }
|
---|
231 |
|
---|
232 | private void OnDeleted(PluginInfrastructureEventArgs args) {
|
---|
233 | if (PluginRemoved != null) PluginRemoved(this, args);
|
---|
234 | }
|
---|
235 |
|
---|
236 | private void OnPreInstall(PluginInfrastructureCancelEventArgs args) {
|
---|
237 | if (PreInstallPlugin != null) PreInstallPlugin(this, args);
|
---|
238 | }
|
---|
239 |
|
---|
240 | private void OnInstalled(PluginInfrastructureEventArgs args) {
|
---|
241 | if (PluginInstalled != null) PluginInstalled(this, args);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|