1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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.Linq;
|
---|
26 | using System.Security.Cryptography;
|
---|
27 | using System.ServiceModel;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Clients.Hive {
|
---|
32 | public static class PluginUtil {
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Checks if plugins are available on Hive Server. If not they are uploaded. Ids are returned.
|
---|
36 | /// </summary>
|
---|
37 | /// <param name="service">An active service-proxy</param>
|
---|
38 | /// <param name="onlinePlugins">List of plugins which are available online</param>
|
---|
39 | /// <param name="alreadyUploadedPlugins">List of plugins which have been uploaded from this Task</param>
|
---|
40 | /// <param name="neededPlugins">List of plugins which need to be uploaded</param>
|
---|
41 | /// <returns></returns>
|
---|
42 | public static List<Guid> GetPluginDependencies(IHiveService service, List<Plugin> onlinePlugins, List<Plugin> alreadyUploadedPlugins,
|
---|
43 | 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 | } catch (FaultException<PluginAlreadyExistsFault> fault) {
|
---|
66 | onlinePlugins.Add(service.GetPlugin(fault.Detail.Id));
|
---|
67 | }
|
---|
68 | } else {
|
---|
69 | pluginIds.Add(foundPlugin.Id);
|
---|
70 | }
|
---|
71 | } else {
|
---|
72 | pluginIds.Add(foundPlugin.Id);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | return pluginIds;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private static Plugin CreatePlugin(IPluginDescription plugin, byte[] hash) {
|
---|
79 | return new Plugin() { Name = plugin.Name, Version = plugin.Version, Hash = hash };
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static List<PluginData> CreatePluginDatas(IPluginDescription plugin) {
|
---|
83 | List<PluginData> pluginDatas = new List<PluginData>();
|
---|
84 |
|
---|
85 | foreach (IPluginFile pf in plugin.Files) {
|
---|
86 | PluginData pluginData = new PluginData();
|
---|
87 |
|
---|
88 | pluginData.Data = File.ReadAllBytes(pf.Name);
|
---|
89 | pluginData.FileName = Path.GetFileName(pf.Name);
|
---|
90 | pluginDatas.Add(pluginData);
|
---|
91 | }
|
---|
92 | return pluginDatas;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static List<IPluginDescription> GetPluginsForTask(IEnumerable<Type> usedTypes, object task) {
|
---|
96 | if (usedTypes.Any(x => typeof(IProgrammableItem).IsAssignableFrom(x))) {
|
---|
97 | //when a programmable item is used all plugins that are currently loaded need to be sent to Hive
|
---|
98 | return ApplicationManager.Manager.Plugins.ToList();
|
---|
99 | } else {
|
---|
100 | return CollectDeclaringPlugins(usedTypes);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static List<IPluginDescription> CollectDeclaringPlugins(IEnumerable<Type> usedTypes) {
|
---|
105 | List<IPluginDescription> plugins = new List<IPluginDescription>();
|
---|
106 | foreach (Type type in usedTypes) {
|
---|
107 | var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
|
---|
108 | if (plugin != null && !plugins.Contains(plugin)) {
|
---|
109 | plugins.Add(plugin);
|
---|
110 | CollectPluginDependencies(plugins, plugin);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | return plugins;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private static void CollectPluginDependencies(List<IPluginDescription> plugins, IPluginDescription plugin) {
|
---|
117 | if (plugin == null) return;
|
---|
118 | foreach (var dependency in plugin.Dependencies) {
|
---|
119 | if (!plugins.Contains(dependency)) {
|
---|
120 | plugins.Add(dependency);
|
---|
121 | CollectPluginDependencies(plugins, dependency);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | private static Dictionary<IPluginDescription, byte[]> CalcChecksumsForPlugins(IEnumerable<IPluginDescription> neededPlugins) {
|
---|
127 | Dictionary<IPluginDescription, byte[]> pluginChecksums = new Dictionary<IPluginDescription, byte[]>();
|
---|
128 |
|
---|
129 | foreach (IPluginDescription desc in neededPlugins) {
|
---|
130 | byte[] hash;
|
---|
131 | byte[] buffer = new byte[0];
|
---|
132 |
|
---|
133 | //calculate checksum over all files belonging to a plugin
|
---|
134 | foreach (IPluginFile pf in desc.Files) {
|
---|
135 | byte[] tmpBuffer = File.ReadAllBytes(pf.Name);
|
---|
136 | byte[] newBuffer = new byte[buffer.Length + tmpBuffer.Length];
|
---|
137 | Array.Copy(buffer, newBuffer, buffer.Length);
|
---|
138 | Array.Copy(tmpBuffer, 0, newBuffer, buffer.Length, tmpBuffer.Length);
|
---|
139 | buffer = newBuffer;
|
---|
140 | }
|
---|
141 |
|
---|
142 | using (SHA1 sha1 = SHA1.Create()) {
|
---|
143 | hash = sha1.ComputeHash(buffer);
|
---|
144 | }
|
---|
145 | pluginChecksums.Add(desc, hash);
|
---|
146 | }
|
---|
147 | return pluginChecksums;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|