Free cookie consent management tool by TermsFeed Policy Generator

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

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

refactoring of the WCF Service for the Server (#418)

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