Free cookie consent management tool by TermsFeed Policy Generator

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

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

refactoring for #438

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