Free cookie consent management tool by TermsFeed Policy Generator

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

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

added licence information and comments (#467)

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