Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Core/Core.cs @ 932

Last change on this file since 932 was 932, checked in by kgrading, 16 years ago

implementation for #425

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Linq;
25using System.Text;
26using HeuristicLab.Hive.Client.ExecutionEngine;
27using HeuristicLab.Hive.Client.Common;
28using System.Threading;
29using System.Reflection;
30using System.Diagnostics;
31using System.Security.Permissions;
32using System.Security.Policy;
33using System.Security;
34using HeuristicLab.Hive.Client.Communication;
35using HeuristicLab.Hive.Contracts.BusinessObjects;
36using HeuristicLab.Hive.Contracts;
37using System.Runtime.Remoting.Messaging;
38using HeuristicLab.PluginInfrastructure;
39using System.ServiceModel;
40using System.ServiceModel.Description;
41using HeuristicLab.Hive.Client.Core.ClientConsoleService;
42using HeuristicLab.Hive.Client.Core.ConfigurationManager;
43
44
45namespace HeuristicLab.Hive.Client.Core {
46  public class Core {
47
48    public delegate string GetASnapshotDelegate();
49
50    Dictionary<long, Executor> engines = new Dictionary<long, Executor>();
51    Dictionary<long, AppDomain> appDomains = new Dictionary<long, AppDomain>();
52
53    private WcfService wcfService;
54
55    public void Start() {
56
57      ClientConsoleServer server = new ClientConsoleServer();
58      server.StartClientConsoleServer(new Uri("net.tcp://127.0.0.1:8000/ClientConsole/"));
59
60      ConfigManager manager = ConfigManager.Instance;
61      manager.Core = this;
62
63      wcfService = WcfService.Instance;
64      wcfService.Connect("10.20.53.1", 9000);
65
66      wcfService.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(wcfService_LoginCompleted);
67      wcfService.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(wcfService_PullJobCompleted);
68      wcfService.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(wcfService_SendJobResultCompleted);
69      wcfService.ConnectionRestored += new EventHandler(wcfService_ConnectionRestored);
70      wcfService.ServerChanged += new EventHandler(wcfService_ServerChanged);
71
72      wcfService.LoginAsync(ConfigManager.Instance.GetClientInfo());
73
74      Heartbeat beat = new Heartbeat { Interval = 10000 };
75      beat.StartHeartbeat();     
76
77      MessageQueue queue = MessageQueue.GetInstance();
78      while (true) {
79        MessageContainer container = queue.GetMessage();
80        Debug.WriteLine("Main loop received this message: " + container.Message.ToString());
81        Logging.GetInstance().Info(this.ToString(), container.Message.ToString());
82        DetermineAction(container);
83      }
84    }
85
86    private void DetermineAction(MessageContainer container) {
87      switch (container.Message) {
88        case MessageContainer.MessageType.AbortJob:
89          engines[container.JobId].Abort();
90          break;
91        case MessageContainer.MessageType.JobAborted:
92          Debug.WriteLine("-- Job Aborted Message received");
93          break;
94
95        case MessageContainer.MessageType.RequestSnapshot:
96          engines[container.JobId].RequestSnapshot();
97          break;
98        case MessageContainer.MessageType.SnapshotReady:
99          Thread ssr = new Thread(new ParameterizedThreadStart(GetSnapshot));
100          ssr.Start(container.JobId);         
101          break;
102
103        case MessageContainer.MessageType.FetchJob:
104          wcfService.PullJobAsync(Guid.NewGuid());
105          break;         
106        case MessageContainer.MessageType.FinishedJob:
107          Thread finThread = new Thread(new ParameterizedThreadStart(GetFinishedJob));
108          finThread.Start(container.JobId);         
109          break;     
110      }
111    }
112
113    #region Async Threads for the EE
114   
115    private void GetFinishedJob(object jobId) {
116      long jId = (long)jobId;
117      byte[] sJob = engines[jId].GetFinishedJob();
118     
119      JobResult jobResult = new JobResult { JobId = jId, Result = sJob, Client = ConfigManager.Instance.GetClientInfo() };
120      wcfService.SendJobResultAsync(jobResult, true);
121    }
122
123    private void GetSnapshot(object jobId) {
124      long jId = (long)jobId;
125      byte[] obj = engines[jId].GetSnapshot();
126    }
127
128    #endregion
129
130    #region wcfService Events
131
132    void wcfService_ConnectionRestored(object sender, EventArgs e) {
133      //Do some fancy new things here... e.g: check all appdomains if there are still active Jobs that need to be transmitted
134    }
135
136    void wcfService_LoginCompleted(object sender, LoginCompletedEventArgs e) {
137      if (e.Result.Success) {
138        Logging.GetInstance().Info(this.ToString(), "Login completed to Hive Server @ " + DateTime.Now);
139        ConfigManager.Instance.Loggedin();       
140      } else
141        Logging.GetInstance().Error(this.ToString(), e.Result.StatusMessage);
142    }   
143
144    void wcfService_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
145      bool sandboxed = false;
146
147      PluginManager.Manager.Initialize();
148      AppDomain appDomain =  PluginManager.Manager.CreateAndInitAppDomainWithSandbox(e.Result.JobId.ToString(), sandboxed, typeof(TestJob));
149     
150      appDomains.Add(e.Result.JobId, appDomain);
151
152      Executor engine = (Executor)appDomain.CreateInstanceAndUnwrap(typeof(Executor).Assembly.GetName().Name, typeof(Executor).FullName);
153      engine.JobId = e.Result.JobId;
154      engine.Queue = MessageQueue.GetInstance();
155      engine.Start(e.Result.SerializedJob);
156      engines.Add(e.Result.JobId, engine);
157
158      ClientStatusInfo.JobsFetched++;
159
160      Debug.WriteLine("Increment FetchedJobs to:"+ClientStatusInfo.JobsFetched);
161    }
162
163    void wcfService_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
164      if (e.Result.Success) {
165        AppDomain.Unload(appDomains[e.Result.JobId]);
166        appDomains.Remove(e.Result.JobId);
167        engines.Remove(e.Result.JobId);
168        ClientStatusInfo.JobsProcessed++;
169        Debug.WriteLine("ProcessedJobs to:" + ClientStatusInfo.JobsProcessed);
170      } else {
171        Debug.WriteLine("Job sending FAILED!");
172      }
173    }
174
175    void wcfService_ServerChanged(object sender, EventArgs e) {
176      foreach(KeyValuePair<long, AppDomain> entries in appDomains)
177        AppDomain.Unload(appDomains[entries.Key]);
178      appDomains = new Dictionary<long, AppDomain>();
179      engines = new Dictionary<long, Executor>();
180    }
181
182
183    #endregion
184
185    public Dictionary<long, Executor> GetExecutionEngines() {
186      return engines;
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.