[2612] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2612] | 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;
|
---|
[2513] | 23 | using System.Collections.Generic;
|
---|
[4068] | 24 | using System.IO;
|
---|
[11650] | 25 | using System.IO.Compression;
|
---|
[2513] | 26 | using System.Linq;
|
---|
[4068] | 27 | using System.ServiceModel;
|
---|
[2513] | 28 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.PluginInfrastructure.Advanced {
|
---|
[2517] | 31 | internal class InstallationManager {
|
---|
[2513] | 32 |
|
---|
[2517] | 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 |
|
---|
[2513] | 41 | private string pluginDir;
|
---|
[2517] | 42 | public InstallationManager(string pluginDir) {
|
---|
| 43 | this.pluginDir = pluginDir;
|
---|
[2513] | 44 | }
|
---|
| 45 |
|
---|
[2922] | 46 | /// <summary>
|
---|
| 47 | /// Retrieves a list of plugins available at the remote server
|
---|
| 48 | /// </summary>
|
---|
| 49 | /// <returns></returns>
|
---|
[3006] | 50 | public IEnumerable<IPluginDescription> GetRemotePluginList() {
|
---|
[4495] | 51 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
[3006] | 52 | try {
|
---|
| 53 | List<IPluginDescription> plugins = new List<IPluginDescription>(client.GetPlugins());
|
---|
| 54 | client.Close();
|
---|
| 55 | return plugins;
|
---|
[2922] | 56 | }
|
---|
[3508] | 57 | catch (TimeoutException e) {
|
---|
[3006] | 58 | client.Abort();
|
---|
[3508] | 59 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
[3006] | 60 | }
|
---|
[3508] | 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 | }
|
---|
[2517] | 69 | }
|
---|
| 70 |
|
---|
[2922] | 71 | /// <summary>
|
---|
| 72 | /// Retrieves the list of products available at the remote server
|
---|
| 73 | /// </summary>
|
---|
| 74 | /// <returns></returns>
|
---|
[3006] | 75 | public IEnumerable<DeploymentService.ProductDescription> GetRemoteProductList() {
|
---|
[4495] | 76 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
[3006] | 77 | try {
|
---|
| 78 | List<DeploymentService.ProductDescription> products = new List<DeploymentService.ProductDescription>(client.GetProducts());
|
---|
| 79 | client.Close();
|
---|
| 80 | return products;
|
---|
[2922] | 81 | }
|
---|
[3508] | 82 | catch (TimeoutException e) {
|
---|
[3006] | 83 | client.Abort();
|
---|
[3508] | 84 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
[3006] | 85 | }
|
---|
[3508] | 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 | }
|
---|
[2513] | 94 | }
|
---|
| 95 |
|
---|
[2922] | 96 | /// <summary>
|
---|
| 97 | /// Installs plugins from remote server
|
---|
| 98 | /// </summary>
|
---|
[3092] | 99 | /// <param name="plugins"></param>
|
---|
[6413] | 100 | public void Install(IEnumerable<IPluginDescription> plugins, out bool cancelled) {
|
---|
[3006] | 101 | var args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
| 102 | OnPreInstall(args);
|
---|
| 103 | if (!args.Cancel) {
|
---|
[6413] | 104 | cancelled = false;
|
---|
[4495] | 105 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
[3006] | 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();
|
---|
[2922] | 113 | }
|
---|
[3508] | 114 | catch (TimeoutException e) {
|
---|
[3006] | 115 | client.Abort();
|
---|
[3508] | 116 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
[3006] | 117 | }
|
---|
[3508] | 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 | }
|
---|
[6413] | 126 | } else {
|
---|
| 127 | cancelled = true;
|
---|
[2922] | 128 | }
|
---|
[2517] | 129 | }
|
---|
| 130 |
|
---|
[2922] | 131 | /// <summary>
|
---|
| 132 | /// Updates plugins from remote server
|
---|
| 133 | /// </summary>
|
---|
[3092] | 134 | /// <param name="plugins"></param>
|
---|
[6413] | 135 | public void Update(IEnumerable<IPluginDescription> plugins, out bool cancelled) {
|
---|
[3006] | 136 | PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
[2922] | 137 | OnPreUpdate(args);
|
---|
| 138 | if (!args.Cancel) {
|
---|
[6413] | 139 | cancelled = false;
|
---|
[4495] | 140 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
[3006] | 141 | try {
|
---|
[2922] | 142 | foreach (DeploymentService.PluginDescription plugin in plugins) {
|
---|
| 143 | byte[] zippedPackage = client.GetPlugin(plugin);
|
---|
| 144 | Unpack(zippedPackage);
|
---|
| 145 | OnUpdated(new PluginInfrastructureEventArgs(plugin));
|
---|
| 146 | }
|
---|
[3006] | 147 | client.Close();
|
---|
[2922] | 148 | }
|
---|
[3508] | 149 | catch (TimeoutException e) {
|
---|
[3006] | 150 | client.Abort();
|
---|
[3508] | 151 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
[3006] | 152 | }
|
---|
[3508] | 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 | }
|
---|
[6413] | 161 | } else {
|
---|
| 162 | cancelled = true;
|
---|
[2922] | 163 | }
|
---|
[2513] | 164 | }
|
---|
| 165 |
|
---|
[2922] | 166 | /// <summary>
|
---|
| 167 | /// Deletes all plugin files from local installation
|
---|
| 168 | /// </summary>
|
---|
[3092] | 169 | /// <param name="plugins"></param>
|
---|
[2922] | 170 | public void Remove(IEnumerable<IPluginDescription> plugins) {
|
---|
| 171 | var fileNames = from pluginToDelete in plugins
|
---|
[2688] | 172 | from file in pluginToDelete.Files
|
---|
| 173 | select Path.Combine(pluginDir, file.Name);
|
---|
[3006] | 174 | var args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
[2517] | 175 | OnPreDelete(args);
|
---|
| 176 | if (!args.Cancel) {
|
---|
| 177 | foreach (string fileName in fileNames) {
|
---|
[2922] | 178 | File.Delete(fileName);
|
---|
| 179 | OnDeleted(new PluginInfrastructureEventArgs(fileName));
|
---|
[2513] | 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[2922] | 184 | private void Unpack(byte[] zippedPackage) {
|
---|
[11650] | 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 | }
|
---|
[4482] | 211 | }
|
---|
[11650] | 212 | streamWriter.Close();
|
---|
[2922] | 213 | }
|
---|
[2527] | 214 | }
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
[2513] | 218 | }
|
---|
[2517] | 219 |
|
---|
[2527] | 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 |
|
---|
[2517] | 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 | }
|
---|
[2513] | 243 | }
|
---|
| 244 | }
|
---|