Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Util/PluginUtil.cs @ 6407

Last change on this file since 6407 was 6407, checked in by ascheibe, 13 years ago

#1233

  • implemented usage of checksums for comparing assemblies
  • re-added CreateHiveDatabaseApplication.cs to project
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Security.Cryptography;
27using System.ServiceModel;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Clients.Hive {
31  public static class PluginUtil {
32
33    /// <summary>
34    /// Checks if plugins are available on Hive Server. If not they are uploaded. Ids are returned.
35    /// </summary>
36    /// <param name="service">An active service-proxy</param>
37    /// <param name="onlinePlugins">List of plugins which are available online</param>
38    /// <param name="alreadyUploadedPlugins">List of plugins which have been uploaded from this HiveExperiment</param>
39    /// <param name="neededPlugins">List of plugins which need to be uploaded</param>
40    /// <param name="useLocalPlugins">If true, the plugins which are already online are ignored. All local plugins are uploaded, but only once.</param>
41    /// <returns></returns>
42    public static List<Guid> GetPluginDependencies(IHiveService service, List<Plugin> onlinePlugins, List<Plugin> alreadyUploadedPlugins, IEnumerable<IPluginDescription> neededPlugins, bool useLocalPlugins) {
43      var pluginIds = new List<Guid>();
44      Dictionary<IPluginDescription, byte[]> checksumsNeededPlugins = CalcChecksumsForPlugins(neededPlugins);
45
46      foreach (var neededPlugin in checksumsNeededPlugins) {
47        Plugin foundPlugin = alreadyUploadedPlugins.FirstOrDefault(p => p.Hash.SequenceEqual(neededPlugin.Value));
48        if (foundPlugin == null) {
49          //foundPlugin = onlinePlugins.FirstOrDefault(p => p.Hash.SequenceEqual(neededPlugin.Value));
50          foundPlugin = onlinePlugins.FirstOrDefault(p => {
51            if (p.Hash != null) {
52              return p.Hash.SequenceEqual(neededPlugin.Value);
53            } else {
54              return false;
55            }
56          });
57
58          if (useLocalPlugins || foundPlugin == null) {
59            Plugin p = CreatePlugin(neededPlugin.Key, useLocalPlugins, neededPlugin.Value);
60            List<PluginData> pd = CreatePluginDatas(neededPlugin.Key);
61            try {
62              p.Id = service.AddPlugin(p, pd);
63              alreadyUploadedPlugins.Add(p);
64              pluginIds.Add(p.Id);
65            }
66            catch (FaultException<PluginAlreadyExistsFault> fault) {
67              onlinePlugins.Add(service.GetPlugin(fault.Detail.Id));
68            }
69          } else {
70            pluginIds.Add(foundPlugin.Id);
71          }
72        } else {
73          pluginIds.Add(foundPlugin.Id);
74        }
75      }
76      return pluginIds;
77    }
78
79    private static Plugin CreatePlugin(IPluginDescription plugin, bool useLocalPlugins, byte[] hash) {
80      return new Plugin() { Name = plugin.Name, Version = plugin.Version, IsLocal = useLocalPlugins, Hash = hash };
81    }
82
83    public static Plugin CreatePlugin(IPluginDescription plugin, bool useLocalPlugins) {
84      return new Plugin() { Name = plugin.Name, Version = plugin.Version, IsLocal = useLocalPlugins };
85    }
86
87    public static List<PluginData> CreatePluginDatas(IPluginDescription plugin) {
88      List<PluginData> pluginDatas = new List<PluginData>();
89
90      foreach (IPluginFile pf in plugin.Files) {
91        PluginData pluginData = new PluginData();
92
93        pluginData.Data = File.ReadAllBytes(pf.Name);
94        pluginData.FileName = Path.GetFileName(pf.Name);
95        pluginDatas.Add(pluginData);
96      }
97      return pluginDatas;
98    }
99
100    public static void CollectDeclaringPlugins(List<IPluginDescription> plugins, IEnumerable<Type> usedTypes) {
101      foreach (Type type in usedTypes) {
102        var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
103        if (plugin != null && !plugins.Contains(plugin)) {
104          plugins.Add(plugin);
105          CollectPluginDependencies(plugins, plugin);
106        }
107      }
108    }
109
110    public static void CollectPluginDependencies(List<IPluginDescription> plugins, IPluginDescription plugin) {
111      if (plugin == null) return;
112      foreach (var dependency in plugin.Dependencies) {
113        if (!plugins.Contains(dependency)) {
114          plugins.Add(dependency);
115          CollectPluginDependencies(plugins, dependency);
116        }
117      }
118    }
119
120    private static Dictionary<IPluginDescription, byte[]> CalcChecksumsForPlugins(IEnumerable<IPluginDescription> neededPlugins) {
121      Dictionary<IPluginDescription, byte[]> pluginChecksums = new Dictionary<IPluginDescription, byte[]>();
122
123      foreach (IPluginDescription desc in neededPlugins) {
124        byte[] hash;
125        byte[] buffer = new byte[0];
126
127        //calculate checksum over all files belonging to a plugin
128        foreach (IPluginFile pf in desc.Files) {
129          byte[] tmpBuffer = File.ReadAllBytes(pf.Name);
130          byte[] newBuffer = new byte[buffer.Length + tmpBuffer.Length];
131          Array.Copy(buffer, newBuffer, buffer.Length);
132          Array.Copy(tmpBuffer, 0, newBuffer, buffer.Length, tmpBuffer.Length);
133          buffer = newBuffer;
134        }
135
136        using (SHA1 sha1 = SHA1.Create()) {
137          hash = sha1.ComputeHash(buffer);
138        }
139        pluginChecksums.Add(desc, hash);
140      }
141      return pluginChecksums;
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.