Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Client.ExecutionEngine/3.3/Executor.cs @ 4119

Last change on this file since 4119 was 4119, checked in by cneumuel, 14 years ago

HiveExperiment is now able to send IOptimizers of an Experiment and receive the calculated result (#1115)

File size: 4.6 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 HeuristicLab.Hive.Client.Common;
24using HeuristicLab.Hive.Contracts;
25using HeuristicLab.Hive.JobBase;
26using HeuristicLab.Persistence.Core;
27using HeuristicLab.Common;
28using System.Xml.Serialization;
29using HeuristicLab.Persistence.Default.Xml;
30using System.IO;
31using HeuristicLab.Core;
32
33namespace HeuristicLab.Hive.Client.ExecutionEngine {
34  public class Executor : MarshalByRefObject, IDisposable {
35    public Guid JobId { get; set; }
36    public IJob Job { get; set; }
37    public MessageContainer.MessageType CurrentMessage { get; set; }
38    public MessageQueue Queue { get; set; }
39
40    public bool JobIsFinished { get; set; }
41
42    public ExecutionState ExecutionState {
43      get {
44        return Job.ExecutionState;
45      }
46    }
47
48    public double Progress {
49      get {
50        return Job.Progress;
51      }
52    }
53
54    public DateTime CreationTime { get; set; }
55
56    public void Start(byte[] serializedJob) {
57      CreationTime = DateTime.Now;
58      Job = XmlParser.Deserialize<IJob>(new MemoryStream(serializedJob));
59
60      Job.JobStopped += new EventHandler(Job_JobStopped);
61      Job.JobFailed += new EventHandler(Job_JobFailed);
62      Job.Start();
63    }
64
65    void Job_JobFailed(object sender, EventArgs e) {
66      Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobFailed, JobId));
67    }
68
69    public void StartOnlyJob() {
70      Job.Start();
71    }
72
73    public void Abort() {
74      CurrentMessage = MessageContainer.MessageType.AbortJob;
75      Job.Stop();
76    }
77
78    void Job_JobStopped(object sender, EventArgs e) {
79      if (CurrentMessage == MessageContainer.MessageType.NoMessage) {
80        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
81        JobIsFinished = true;
82      } else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot) {
83        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
84      } else if (CurrentMessage == MessageContainer.MessageType.AbortJob) {
85        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
86      }
87    }
88
89    public byte[] GetSnapshot() {
90      //if the job is still running, something went VERY bad.
91      if (Job.ExecutionState == Core.ExecutionState.Started) {
92        return null;
93      } else {
94        // Clear the Status message
95        CurrentMessage = MessageContainer.MessageType.NoMessage;
96        // Pack the whole job inside an xml document
97        byte[] job = SerializeJobObject();
98        // Restart the job
99        // Return the Snapshot
100        return job;
101      }
102    }
103
104    public byte[] GetFinishedJob() {
105      //Job isn't finished!
106      if (Job.ExecutionState == Core.ExecutionState.Started) {
107        throw new InvalidStateException("Job is still running");
108      } else {
109        byte[] jobArr = SerializeJobObject();
110        return jobArr;
111      }
112    }
113
114    public void RequestSnapshot() {
115      CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
116      Job.Stop();
117    }
118
119    private byte[] SerializeJobObject() {
120      MemoryStream stream = new MemoryStream();
121      XmlGenerator.Serialize(Job, stream);
122      return stream.ToArray();
123    }
124
125    private void RestoreJobObject(byte[] sjob) {
126      Job = XmlParser.Deserialize<IJob>(new MemoryStream(sjob));
127    }
128
129    public Executor() {
130      CurrentMessage = MessageContainer.MessageType.NoMessage;
131      JobIsFinished = false;
132      //Job = new TestJob();
133    }
134
135    #region IDisposable Members
136
137    public void Dispose() {
138      Job.JobFailed -= new EventHandler(Job_JobFailed);
139      Job.JobFailed -= new EventHandler(Job_JobStopped);
140      Queue = null;
141      Job = null;
142    }
143
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.