source: trunk/sources/HeuristicLab.Hive.Client.Core/3.2/JobStorage/JobStorageManager.cs @ 1728

Last change on this file since 1728 was 1728, checked in by kgrading, 13 years ago

implemented persistance for list (#493)

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