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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Hive.Client.Common;
|
---|
27 | using System.Xml;
|
---|
28 | using System.Xml.Serialization;
|
---|
29 | using System.IO;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Hive.JobBase;
|
---|
32 | using HeuristicLab.Hive.Contracts;
|
---|
33 |
|
---|
34 | namespace 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 | Job.JobStopped += new EventHandler(Job_JobStopped);
|
---|
57 | Job.Start();
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void Abort() {
|
---|
61 | CurrentMessage = MessageContainer.MessageType.AbortJob;
|
---|
62 | Job.Stop();
|
---|
63 | }
|
---|
64 |
|
---|
65 | void Job_JobStopped(object sender, EventArgs e) {
|
---|
66 | if (CurrentMessage == MessageContainer.MessageType.NoMessage)
|
---|
67 | Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.FinishedJob, JobId));
|
---|
68 | else if (CurrentMessage == MessageContainer.MessageType.RequestSnapshot)
|
---|
69 | Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.SnapshotReady, JobId));
|
---|
70 | else if (CurrentMessage == MessageContainer.MessageType.AbortJob)
|
---|
71 | Queue.AddMessage(new MessageContainer(MessageContainer.MessageType.JobAborted, JobId));
|
---|
72 | }
|
---|
73 |
|
---|
74 | public byte[] GetSnapshot() {
|
---|
75 | //if the job is still running, something went VERY bad.
|
---|
76 | if (Job.Running) {
|
---|
77 | return null;
|
---|
78 | } else {
|
---|
79 | // Clear the Status message
|
---|
80 | CurrentMessage = MessageContainer.MessageType.NoMessage;
|
---|
81 | // Pack the whole job inside an xml document
|
---|
82 | byte[] job = SerializeJobObject();
|
---|
83 | // Restart the job
|
---|
84 | Job.Start();
|
---|
85 | // Return the Snapshot
|
---|
86 | return job;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public byte[] GetFinishedJob() {
|
---|
91 | //Job isn't finished!
|
---|
92 | if (Job.Running) {
|
---|
93 | return null;
|
---|
94 | } else {
|
---|
95 | return SerializeJobObject();
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | public void RequestSnapshot() {
|
---|
101 | CurrentMessage = MessageContainer.MessageType.RequestSnapshot;
|
---|
102 | Job.Stop();
|
---|
103 | }
|
---|
104 |
|
---|
105 | private byte[] SerializeJobObject() {
|
---|
106 | return PersistenceManager.SaveToGZip(Job);
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void RestoreJobObject(byte[] sjob) {
|
---|
110 | Job = (IJob)PersistenceManager.RestoreFromGZip(sjob);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public Executor() {
|
---|
114 | CurrentMessage = MessageContainer.MessageType.NoMessage;
|
---|
115 | Job = new TestJob();
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|