Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive/3.3/Util/PluginUtil.cs @ 12633

Last change on this file since 12633 was 12621, checked in by ascheibe, 9 years ago

#2409 added support for IProgrammableItem in Hive

File size: 6.0 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6976]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;
[12621]28using HeuristicLab.Common;
29using HeuristicLab.Core;
[6976]30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Clients.Hive {
33  public static class PluginUtil {
34
35    /// <summary>
36    /// Checks if plugins are available on Hive Server. If not they are uploaded. Ids are returned.
37    /// </summary>
38    /// <param name="service">An active service-proxy</param>
39    /// <param name="onlinePlugins">List of plugins which are available online</param>
40    /// <param name="alreadyUploadedPlugins">List of plugins which have been uploaded from this Task</param>
41    /// <param name="neededPlugins">List of plugins which need to be uploaded</param>
42    /// <returns></returns>
43    public static List<Guid> GetPluginDependencies(IHiveService service, List<Plugin> onlinePlugins, List<Plugin> alreadyUploadedPlugins, IEnumerable<IPluginDescription> neededPlugins) {
44      var pluginIds = new List<Guid>();
45      Dictionary<IPluginDescription, byte[]> checksumsNeededPlugins = CalcChecksumsForPlugins(neededPlugins);
46
47      foreach (var neededPlugin in checksumsNeededPlugins) {
48        Plugin foundPlugin = alreadyUploadedPlugins.FirstOrDefault(p => p.Hash.SequenceEqual(neededPlugin.Value));
49        if (foundPlugin == null) {
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 (foundPlugin == null) {
59            Plugin p = CreatePlugin(neededPlugin.Key, 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, byte[] hash) {
80      return new Plugin() { Name = plugin.Name, Version = plugin.Version, Hash = hash };
81    }
82
83    public static List<PluginData> CreatePluginDatas(IPluginDescription plugin) {
84      List<PluginData> pluginDatas = new List<PluginData>();
85
86      foreach (IPluginFile pf in plugin.Files) {
87        PluginData pluginData = new PluginData();
88
89        pluginData.Data = File.ReadAllBytes(pf.Name);
90        pluginData.FileName = Path.GetFileName(pf.Name);
91        pluginDatas.Add(pluginData);
92      }
93      return pluginDatas;
94    }
95
[12621]96    public static List<IPluginDescription> GetPluginsForTask(IEnumerable<Type> usedTypes, object task) {
97      if (task.GetObjectGraphObjects().Any(x => typeof(IProgrammableItem).IsInstanceOfType(x))) {
98        //when a programmable item is used all plugins that are currently loaded need to be sent to Hive
99        return ApplicationManager.Manager.Plugins.ToList();
100      } else {
101        return CollectDeclaringPlugins(usedTypes);
102      }
103    }
104
105    private static List<IPluginDescription> CollectDeclaringPlugins(IEnumerable<Type> usedTypes) {
106      List<IPluginDescription> plugins = new List<IPluginDescription>();
[6976]107      foreach (Type type in usedTypes) {
108        var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
109        if (plugin != null && !plugins.Contains(plugin)) {
110          plugins.Add(plugin);
111          CollectPluginDependencies(plugins, plugin);
112        }
113      }
[12621]114      return plugins;
[6976]115    }
116
[12621]117    private static void CollectPluginDependencies(List<IPluginDescription> plugins, IPluginDescription plugin) {
[6976]118      if (plugin == null) return;
119      foreach (var dependency in plugin.Dependencies) {
120        if (!plugins.Contains(dependency)) {
121          plugins.Add(dependency);
122          CollectPluginDependencies(plugins, dependency);
123        }
124      }
125    }
126
127    private static Dictionary<IPluginDescription, byte[]> CalcChecksumsForPlugins(IEnumerable<IPluginDescription> neededPlugins) {
128      Dictionary<IPluginDescription, byte[]> pluginChecksums = new Dictionary<IPluginDescription, byte[]>();
129
130      foreach (IPluginDescription desc in neededPlugins) {
131        byte[] hash;
132        byte[] buffer = new byte[0];
133
134        //calculate checksum over all files belonging to a plugin
135        foreach (IPluginFile pf in desc.Files) {
136          byte[] tmpBuffer = File.ReadAllBytes(pf.Name);
137          byte[] newBuffer = new byte[buffer.Length + tmpBuffer.Length];
138          Array.Copy(buffer, newBuffer, buffer.Length);
139          Array.Copy(tmpBuffer, 0, newBuffer, buffer.Length, tmpBuffer.Length);
140          buffer = newBuffer;
141        }
142
143        using (SHA1 sha1 = SHA1.Create()) {
144          hash = sha1.ComputeHash(buffer);
145        }
146        pluginChecksums.Add(desc, hash);
147      }
148      return pluginChecksums;
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.