Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 993 was 993, checked in by kgrading, 15 years ago

refactored for #437

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