Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.ExecutionEngine/Executor.cs @ 820

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

implemented the persistance management (#390)

File size: 3.3 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.Common;
27using System.Xml;
28using System.Xml.Serialization;
29using System.IO;
30using HeuristicLab.Hive.Contracts;
31using HeuristicLab.Core;
32
33namespace HeuristicLab.Hive.Client.ExecutionEngine {
34  public class Executor: MarshalByRefObject {
35    public long 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 void Start() {
41      Job.JobStopped += new EventHandler(Job_JobStopped);
42      Job.Start();
43    }
44
45    public void Abort() {
46      CurrentMessage = MessageContainer.MessageType.AbortJob;
47      Job.Stop();     
48    }
49
50    void Job_JobStopped(object sender, EventArgs e) {
51      if (CurrentMessage == MessageContainer.MessageType.NoMessage)
52        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
53      else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot)
54        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
55      else if (CurrentMessage == MessageContainer.MessageType.AbortJob)
56        Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
57    }
58
59    public byte[] GetSnapshot() {
60      //if the job is still running, something went VERY bad.
61      if (Job.Running) {
62        return null;
63      } else {
64        // Clear the Status message
65        CurrentMessage = MessageContainer.MessageType.NoMessage;
66        // Pack the whole job inside an xml document
67        byte[] job = SerializeJobObject();       
68        // Restart the job
69        Job.Start();
70        // Return the Snapshot
71        return job;
72      }
73    }
74
75    public byte[] GetFinishedJob() {
76      //Job isn't finished!
77      if (Job.Running) {
78        return null;
79      } else {
80        return SerializeJobObject();
81      }
82    }
83
84
85    public void RequestSnapshot() {
86      CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
87      Job.Stop();
88    }
89
90    private byte[] SerializeJobObject() {
91      return PersistenceManager.SaveToGZip(Job);
92    }
93
94    private void RestoreJobObject(byte[] sjob) {
95      Job = (IJob)PersistenceManager.RestoreFromGZip(sjob);
96    }
97
98    public Executor() {
99      CurrentMessage = MessageContainer.MessageType.NoMessage;
100      Job = new TestJob();
101    }   
102  }
103}
Note: See TracBrowser for help on using the repository browser.