source: trunk/CrypPlugins/P2PEditor/Distributed/JobListManager.cs @ 1530

Last change on this file since 1530 was 1530, checked in by Paul Lelgemann, 12 years ago

+ P2PEditor: Exception handling, if workspace data cannot be retrieved using P2P

File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Runtime.Serialization.Formatters.Binary;
5using Cryptool.P2P;
6using Cryptool.PluginBase;
7
8namespace Cryptool.P2PEditor.Distributed
9{
10    public class JobListManager
11    {
12        private const string JoblistKey = "Cryptool.P2PEditor.JobList";
13        private const string WorkspaceKeyPrefix = "Workspace";
14        private readonly P2PEditor _p2PEditor;
15
16        public JobListManager(P2PEditor p2PEditor)
17        {
18            _p2PEditor = p2PEditor;
19        }
20
21        public List<DistributedJob> JobList()
22        {
23            _p2PEditor.GuiLogMessage("Fetching DHT job list...", NotificationLevel.Debug);
24
25            if (!P2PManager.Instance.P2PConnected())
26            {
27                _p2PEditor.GuiLogMessage("P2P not connected, cannot fetch job list.", NotificationLevel.Error);
28                return new List<DistributedJob>();
29            }
30
31            byte[] serialisedJobList = P2PManager.Retrieve(JoblistKey);
32
33            if (serialisedJobList == null)
34            {
35                // no job list in DHT, create empty list
36                _p2PEditor.GuiLogMessage("No list in DHT, creating empty list.", NotificationLevel.Debug);
37                return new List<DistributedJob>();
38            }
39
40            var memoryStream = new MemoryStream(serialisedJobList);
41
42            var bformatter = new BinaryFormatter();
43            return (List<DistributedJob>) bformatter.Deserialize(memoryStream);
44        }
45
46        public void AddDistributedJob(DistributedJob distributedJob)
47        {
48            _p2PEditor.GuiLogMessage("Distributing new job...", NotificationLevel.Debug);
49
50            if (!P2PManager.Instance.P2PConnected())
51            {
52                _p2PEditor.GuiLogMessage("P2P not connected, cannot distribute job.", NotificationLevel.Error);
53                return;
54            }
55
56            List<DistributedJob> currentJobList = JobList();
57            currentJobList.Add(distributedJob);
58
59            var memoryStream = new MemoryStream();
60            var bformatter = new BinaryFormatter();
61
62            bformatter.Serialize(memoryStream, currentJobList);
63            P2PManager.Store(JoblistKey, memoryStream.ToArray());
64
65            byte[] workspaceData = File.ReadAllBytes(distributedJob.LocalFilePath);
66            _p2PEditor.GuiLogMessage(
67                "Workspace size: " + workspaceData.Length + ", storing at " + GenerateWorkspaceKey(distributedJob),
68                NotificationLevel.Debug);
69            P2PManager.Store(GenerateWorkspaceKey(distributedJob), workspaceData);
70
71            _p2PEditor.GuiLogMessage("Distributed job " + distributedJob.JobLabel, NotificationLevel.Info);
72        }
73
74        public void CompleteDistributedJob(DistributedJob distributedJob)
75        {
76            if (File.Exists(distributedJob.LocalFilePath))
77            {
78                _p2PEditor.GuiLogMessage("Local file exists, cannot complete job.", NotificationLevel.Error);
79                return;
80            }
81
82            distributedJob.ConvertRawWorkspaceToLocalFile(P2PManager.Retrieve(GenerateWorkspaceKey(distributedJob)));
83        }
84
85        private static string GenerateWorkspaceKey(DistributedJob distributedJob)
86        {
87            return string.Format("{0}.{1}.{2}", JoblistKey, WorkspaceKeyPrefix, distributedJob.JobGuid);
88        }
89    }
90}
Note: See TracBrowser for help on using the repository browser.