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