Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Clients.Hive/3.3/Util/PluginUtil.cs @ 14778

Last change on this file since 14778 was 9227, checked in by fschoepp, 12 years ago

#1888:

  • Experiments will be saved as JSON elements within the blob store.
  • Added simple model and JSON converters.
  • Backend stores and runs experiments.
  • Updated interfaces to save/read experiments.
  • Added a binding to automatically map incoming JSON ajax requests to experiment models.
  • Added the javascript DatatypeMapper to map parameter inputs to the right html elements and vice versa.
  • Added smartwizard to generate Wizards for creating new experiments (New.cshtml).
File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
29using System.Diagnostics;
30
31namespace 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, IEnumerable<IPluginDescription> neededPlugins) {
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 => {
50            if (p.Hash != null) {
51              return p.Hash.SequenceEqual(neededPlugin.Value);
52            } else {
53              return false;
54            }
55          });
56
57          if (foundPlugin == null) {
58            Plugin p = CreatePlugin(neededPlugin.Key, neededPlugin.Value);
59            List<PluginData> pd = CreatePluginDatas(neededPlugin.Key);
60            try {
61              p.Id = service.AddPlugin(p, pd);
62              alreadyUploadedPlugins.Add(p);
63              pluginIds.Add(p.Id);
64            }
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 Plugin CreatePlugin(IPluginDescription plugin) {
83      return new Plugin() { Name = plugin.Name, Version = plugin.Version };
84    }
85
86    public static List<PluginData> CreatePluginDatas(IPluginDescription plugin) {
87      List<PluginData> pluginDatas = new List<PluginData>();
88
89      foreach (IPluginFile pf in plugin.Files) {
90        PluginData pluginData = new PluginData();
91
92        pluginData.Data = File.ReadAllBytes(pf.Name);
93        pluginData.FileName = Path.GetFileName(pf.Name);
94        pluginDatas.Add(pluginData);
95      }
96      return pluginDatas;
97    }
98
99    public static void CollectDeclaringPlugins(List<IPluginDescription> plugins, IEnumerable<Type> usedTypes) {
100      foreach (Type type in usedTypes) {
101        var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
102        if (plugin != null && !plugins.Contains(plugin)) {
103          plugins.Add(plugin);
104          CollectPluginDependencies(plugins, plugin);
105        }
106        else if (plugin == null && !type.FullName.StartsWith("System")) {
107          Trace.WriteLine("No plugin available for: " + type.FullName);
108        }
109      }
110    }
111
112    public static void CollectPluginDependencies(List<IPluginDescription> plugins, IPluginDescription plugin) {
113      if (plugin == null) return;
114      foreach (var dependency in plugin.Dependencies) {
115        if (!plugins.Contains(dependency)) {
116          plugins.Add(dependency);
117          CollectPluginDependencies(plugins, dependency);
118        }
119      }
120    }
121
122    private static Dictionary<IPluginDescription, byte[]> CalcChecksumsForPlugins(IEnumerable<IPluginDescription> neededPlugins) {
123      Dictionary<IPluginDescription, byte[]> pluginChecksums = new Dictionary<IPluginDescription, byte[]>();
124
125      foreach (IPluginDescription desc in neededPlugins) {
126        byte[] hash;
127        byte[] buffer = new byte[0];
128
129        //calculate checksum over all files belonging to a plugin
130        foreach (IPluginFile pf in desc.Files) {
131          byte[] tmpBuffer = File.ReadAllBytes(pf.Name);
132          byte[] newBuffer = new byte[buffer.Length + tmpBuffer.Length];
133          Array.Copy(buffer, newBuffer, buffer.Length);
134          Array.Copy(tmpBuffer, 0, newBuffer, buffer.Length, tmpBuffer.Length);
135          buffer = newBuffer;
136        }
137
138        using (SHA1 sha1 = SHA1.Create()) {
139          hash = sha1.ComputeHash(buffer);
140        }
141        pluginChecksums.Add(desc, hash);
142      }
143      return pluginChecksums;
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.