1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.IO;
|
---|
6 | using HeuristicLab.Hive.Client.Common;
|
---|
7 | using HeuristicLab.Hive.Client.Communication;
|
---|
8 | using HeuristicLab.Hive.Client.Core.ConfigurationManager;
|
---|
9 | using HeuristicLab.Hive.Contracts;
|
---|
10 | using System.Xml.Serialization;
|
---|
11 | using System.Diagnostics;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Hive.Client.Core.JobStorage {
|
---|
14 | public class JobStorageManager {
|
---|
15 |
|
---|
16 | private static List<JobStorageInfo> storedJobsList = new List<JobStorageInfo>();
|
---|
17 | //Todo: execution path
|
---|
18 | //Todo: Choose a better directory name
|
---|
19 | private static String path = "C:\\Program Files\\HeuristicLab 3.0\\plugins\\Hive.Client.Jobs\\";
|
---|
20 |
|
---|
21 | public static void PersistObjectToDisc(String serverIP, long serverPort, Guid jobId, byte[] job) {
|
---|
22 | String filename = serverIP + "." + serverPort + "." + jobId.ToString();
|
---|
23 | JobStorageInfo info = new JobStorageInfo { JobID = jobId, ServerIP = serverIP, ServerPort = serverPort, TimeFinished = DateTime.Now };
|
---|
24 |
|
---|
25 | if (!Directory.Exists(path))
|
---|
26 | Directory.CreateDirectory(path);
|
---|
27 |
|
---|
28 | Stream jobstream = null;
|
---|
29 | try {
|
---|
30 | jobstream = File.Create(path + filename + ".dat");
|
---|
31 | jobstream.Write(job, 0, job.Length);
|
---|
32 | storedJobsList.Add(info);
|
---|
33 | Debug.WriteLine("Job " + info.JobID + " stored on the harddisc");
|
---|
34 | //Logging.Instance.Info("JobStorageManager", "Job " + info.JobID + " stored on the harddisc");
|
---|
35 | }
|
---|
36 | catch (Exception e) {
|
---|
37 | Logging.Instance.Error("JobStorageManager", "Exception: ", e);
|
---|
38 | }
|
---|
39 | finally {
|
---|
40 | if(jobstream!=null)
|
---|
41 | jobstream.Close();
|
---|
42 | }
|
---|
43 |
|
---|
44 | StoreJobList();
|
---|
45 |
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static void CheckAndSubmitJobsFromDisc() {
|
---|
49 | for(int index=storedJobsList.Count; index > 0; index--) {
|
---|
50 | if (WcfService.Instance.ConnState == NetworkEnum.WcfConnState.Loggedin && (storedJobsList[index-1].ServerIP == WcfService.Instance.ServerIP && storedJobsList[index-1].ServerPort == WcfService.Instance.ServerPort)) {
|
---|
51 | String filename = storedJobsList[index-1].ServerIP + "." + storedJobsList[index-1].ServerPort + "." + storedJobsList[index-1].JobID.ToString();
|
---|
52 | Debug.WriteLine("Sending stored job " + storedJobsList[index - 1].JobID + " to the server");
|
---|
53 | //Logging.Instance.Info("JobStorrageManager", "Sending stored job " + storedJobsList[index - 1].JobID + " to the server");
|
---|
54 | byte[] job = File.ReadAllBytes(path + filename + ".dat");
|
---|
55 |
|
---|
56 | //Todo: ask server first if he really wants the job...
|
---|
57 | ResponseResultReceived res = WcfService.Instance.SendStoredJobResultsSync(ConfigManager.Instance.GetClientInfo().Id, storedJobsList[index-1].JobID, job, 1.00, null, true);
|
---|
58 | ClientStatusInfo.JobsProcessed++;
|
---|
59 | //TODO: has to be fixed from server side
|
---|
60 | //if (res.Success == true) {
|
---|
61 | Debug.WriteLine("Sending of job " + storedJobsList[index - 1].JobID + " done");
|
---|
62 | //Logging.Instance.Info("JobStorrageManager", "Sending of job " + storedJobsList[index - 1].JobID + " done");
|
---|
63 |
|
---|
64 | storedJobsList.Remove(storedJobsList[index - 1]);
|
---|
65 | File.Delete(path + filename + ".dat");
|
---|
66 |
|
---|
67 | // }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static void StoreJobList() {
|
---|
73 | XmlSerializer serializer = new XmlSerializer(typeof(List<JobStorageInfo>));
|
---|
74 | TextWriter writer = new StreamWriter(Path.Combine(path ,"list.xml"));
|
---|
75 | serializer.Serialize(writer, storedJobsList);
|
---|
76 | writer.Close();
|
---|
77 | }
|
---|
78 |
|
---|
79 | }
|
---|
80 | }
|
---|