Ignore:
Timestamp:
Jul 5, 2010, 3:51:42 PM (12 years ago)
Author:
Paul Lelgemann
Message:

+ P2PEditor can display the status of jobs, if available; Participating displays overlay while loading workspace data
+ KeySearcher can upload status for P2PEditor display

Location:
trunk/CrypPlugins/P2PEditor/Distributed
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/P2PEditor/Distributed/DistributedJob.cs

    r1703 r1727  
    66namespace Cryptool.P2PEditor.Distributed
    77{
    8     [Serializable]
    98    public class DistributedJob : INotifyPropertyChanged
    109    {
    11         [NonSerialized]
    12         private String localFilePath;
    13 
    1410        public DistributedJob()
    1511        {
    16             JobGuid = Guid.NewGuid();
    17             JobOwner = P2PSettings.Default.PeerName;
    18             JobDate = DateTime.Now;
     12            Guid = Guid.NewGuid();
     13            Owner = P2PSettings.Default.PeerName;
     14            CreateDate = DateTime.UtcNow;
    1915        }
    2016
    21         public Guid JobGuid { get; private set; }
     17        public Guid Guid { get; set; }
    2218
    23         public String JobName { get; set; }
     19        public String Name { get; set; }
    2420
    25         public String JobDescription { get; set; }
     21        public String Description { get; set; }
    2622
    27         public String JobOwner { get; set; }
     23        public String Owner { get; set; }
    2824
    29         public DateTime JobDate { get; set; }
     25        public DateTime CreateDate { get; set; }
    3026
    31         public String FileName { get; private set; }
     27        public String FileName { get; set; }
    3228
     29        private DistributedJobStatus status;
     30        public DistributedJobStatus Status
     31        {
     32            get { return status; }
     33            set
     34            {
     35                if (value == status) return;
     36                status = value;
     37                OnPropertyChanged("Status");
     38            }
     39        }
     40
     41        private int downloads;
     42        public int Downloads
     43        {
     44            get { return downloads; }
     45            set
     46            {
     47                if (value == downloads) return;
     48                downloads = value;
     49                OnPropertyChanged("Downloads");
     50            }
     51        }
     52
     53        private DateTime lastDownload;
     54        public DateTime LastDownload
     55        {
     56            get { return lastDownload; }
     57            set
     58            {
     59                if (value == lastDownload) return;
     60                lastDownload = value;
     61                OnPropertyChanged("LastDownload");
     62            }
     63        }
     64
     65        private String localFilePath;
    3366        public String LocalFilePath
    3467        {
     
    4376        }
    4477
    45         #region INotifyPropertyChanged Members
    46 
    47         [field: NonSerialized]
    48         public event PropertyChangedEventHandler PropertyChanged;
    49 
    50         #endregion
     78        private String statusKey;
     79        public String StatusKey
     80        {
     81            get { return statusKey; }
     82            set
     83            {
     84                if (value == statusKey) return;
     85                statusKey = value;
     86                OnPropertyChanged("StatusKey");
     87            }
     88        }
    5189
    5290        public void ConvertRawWorkspaceToLocalFile(byte[] rawWorkspaceData)
    5391        {
    54             string workspacePath = P2PSettings.Default.WorkspacePath;
     92            var workspacePath = P2PSettings.Default.WorkspacePath;
    5593            if (String.IsNullOrEmpty(workspacePath) || !Directory.Exists(workspacePath))
    5694            {
     
    6098            // Avoid overwriting previous versions of this workspace or workspaces with common names by adding an integer prefix
    6199            var originalFileName = FileName;
    62             LocalFilePath = Path.Combine(workspacePath, JobOwner + "_" + originalFileName);
     100            LocalFilePath = Path.Combine(workspacePath, Owner + "_" + originalFileName);
    63101            var counter = 0;
    64102            while (File.Exists(LocalFilePath))
    65103            {
    66                 LocalFilePath = Path.Combine(workspacePath, counter++ + "_" + JobOwner + "_" + originalFileName);
     104                LocalFilePath = Path.Combine(workspacePath, counter++ + "_" + Owner + "_" + originalFileName);
    67105            }
    68106
     
    75113        }
    76114
     115        #region INotifyPropertyChanged Members
     116
     117        public event PropertyChangedEventHandler PropertyChanged;
     118
    77119        private void OnPropertyChanged(string propertyName)
    78120        {
     
    80122                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    81123        }
     124
     125        #endregion
     126
     127        #region Equals and HashCode
    82128
    83129        public override bool Equals(object obj)
     
    98144            }
    99145
    100             return obj.JobGuid == JobGuid;
     146            return obj.Guid == Guid;
    101147        }
    102148
    103149        public override int GetHashCode()
    104150        {
    105             return JobGuid.GetHashCode();
     151            return Guid.GetHashCode();
    106152        }
     153
     154        #endregion
    107155    }
    108156}
  • trunk/CrypPlugins/P2PEditor/Distributed/JobListManager.cs

    r1665 r1727  
    1 using System;
     1using System;
    22using System.Collections.Generic;
    33using System.IO;
    4 using System.Runtime.Serialization.Formatters.Binary;
    54using Cryptool.P2P;
     5using Cryptool.P2P.Internal;
    66using Cryptool.PluginBase;
    77
     
    1212        private const string JoblistKey = "Cryptool.P2PEditor.JobList";
    1313        private const string WorkspaceKeyPrefix = "Workspace";
    14         private readonly P2PEditor _p2PEditor;
     14        private readonly P2PEditor p2PEditor;
    1515
    1616        public JobListManager(P2PEditor p2PEditor)
    1717        {
    18             _p2PEditor = p2PEditor;
     18            this.p2PEditor = p2PEditor;
    1919        }
    2020
    21         public List<DistributedJob> JobList()
     21        public ICollection<DistributedJob> JobList()
    2222        {
    23             _p2PEditor.GuiLogMessage("Fetching DHT job list...", NotificationLevel.Debug);
     23            p2PEditor.GuiLogMessage("Fetching DHT job list...", NotificationLevel.Debug);
    2424
    2525            if (!P2PManager.IsConnected)
    2626            {
    27                 _p2PEditor.GuiLogMessage("P2P not connected, cannot fetch job list.", NotificationLevel.Error);
     27                p2PEditor.GuiLogMessage("P2P not connected, cannot fetch job list.", NotificationLevel.Error);
    2828                return new List<DistributedJob>();
    2929            }
     
    3333            {
    3434                // no job list in DHT, create empty list
    35                 _p2PEditor.GuiLogMessage("No list in DHT, creating empty list.", NotificationLevel.Debug);
     35                p2PEditor.GuiLogMessage("No list in DHT, creating empty list.", NotificationLevel.Debug);
    3636                return new List<DistributedJob>();
    3737            }
    3838
    39             var memoryStream = new MemoryStream(serialisedJobList);
    40 
    41             var bformatter = new BinaryFormatter();
    42             bformatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
    43             return (List<DistributedJob>) bformatter.Deserialize(memoryStream);
     39            return ByteArrayToJobList(serialisedJobList);
    4440        }
    4541
    4642        public void AddDistributedJob(DistributedJob distributedJob)
    4743        {
    48             _p2PEditor.GuiLogMessage("Distributing new job...", NotificationLevel.Debug);
     44            p2PEditor.GuiLogMessage("Distributing new job...", NotificationLevel.Debug);
    4945
    5046            if (!P2PManager.IsConnected)
    5147            {
    52                 _p2PEditor.GuiLogMessage("P2P not connected, cannot distribute job.", NotificationLevel.Error);
     48                p2PEditor.GuiLogMessage("P2P not connected, cannot distribute job.", NotificationLevel.Error);
    5349                return;
    5450            }
     
    5753            currentJobList.Add(distributedJob);
    5854
    59             var memoryStream = new MemoryStream();
    60             var bformatter = new BinaryFormatter();
    61             bformatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
    62 
    63             bformatter.Serialize(memoryStream, currentJobList);
    64             P2PManager.Store(JoblistKey, memoryStream.ToArray());
     55            var serializedJobList = JobListToByteArray(currentJobList);
     56            P2PManager.Store(JoblistKey, serializedJobList);
    6557
    6658            var workspaceData = File.ReadAllBytes(distributedJob.LocalFilePath);
    67             _p2PEditor.GuiLogMessage(
     59            p2PEditor.GuiLogMessage(
    6860                "Workspace size: " + workspaceData.Length + ", storing at " + GenerateWorkspaceKey(distributedJob),
    6961                NotificationLevel.Debug);
    7062            P2PManager.Store(GenerateWorkspaceKey(distributedJob), workspaceData);
    7163
    72             _p2PEditor.GuiLogMessage("Distributed job " + distributedJob.JobName, NotificationLevel.Info);
     64            p2PEditor.GuiLogMessage("Distributed job " + distributedJob.Name, NotificationLevel.Info);
    7365        }
    7466
    7567        public void DeleteDistributedJob(DistributedJob distributedJobToDelete)
    7668        {
    77             _p2PEditor.GuiLogMessage("Deleting job...", NotificationLevel.Debug);
     69            p2PEditor.GuiLogMessage("Deleting job...", NotificationLevel.Debug);
    7870
    7971            if (!P2PManager.IsConnected)
    8072            {
    81                 _p2PEditor.GuiLogMessage("P2P not connected, cannot distribute job.", NotificationLevel.Error);
     73                p2PEditor.GuiLogMessage("P2P not connected, cannot distribute job.", NotificationLevel.Error);
    8274                return;
    8375            }
     
    8678            currentJobList.Remove(distributedJobToDelete);
    8779
    88             var memoryStream = new MemoryStream();
    89             var bformatter = new BinaryFormatter();
    90             bformatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
    91 
    92             bformatter.Serialize(memoryStream, currentJobList);
    93             P2PManager.Store(JoblistKey, memoryStream.ToArray());
     80            var serializedJobList = JobListToByteArray(currentJobList);
     81            P2PManager.Store(JoblistKey, serializedJobList);
    9482
    9583            // Retrieve job first to satify versioned DHT
     
    9785            P2PManager.Remove(GenerateWorkspaceKey(distributedJobToDelete));
    9886
    99             _p2PEditor.GuiLogMessage("Deleted distributed job " + distributedJobToDelete.JobName, NotificationLevel.Info);
     87            p2PEditor.GuiLogMessage("Deleted distributed job " + distributedJobToDelete.Name, NotificationLevel.Info);
    10088        }
    10189
     
    10593        }
    10694
     95        public void RetrieveDownloadCount(DistributedJob distributedJob)
     96        {
     97            var result = P2PManager.Retrieve(GenerateDownloadCounterKey(distributedJob));
     98           
     99            if (result.Status == RequestResultType.KeyNotFound)
     100            {
     101                distributedJob.Downloads = 0;
     102                return;
     103            }
     104
     105            var binaryReader = new BinaryReader(new MemoryStream(result.Data));
     106            distributedJob.Downloads = binaryReader.ReadInt32();
     107            distributedJob.LastDownload = DateTime.FromBinary(binaryReader.ReadInt64());
     108        }
     109
     110        public void RetrieveCurrentStatus(DistributedJob distributedJob)
     111        {
     112            if (string.IsNullOrEmpty(distributedJob.StatusKey)) return;
     113
     114            var result = P2PManager.Retrieve(distributedJob.StatusKey);
     115            if (result.Status != RequestResultType.Success) return;
     116
     117            var status = DistributedJobSerializer.StatusFromReader(new BinaryReader(new MemoryStream(result.Data)));
     118            distributedJob.Status = status;
     119        }
     120
     121        public void IncreaseDownloadCount(DistributedJob distributedJob)
     122        {
     123            RetrieveDownloadCount(distributedJob);
     124            distributedJob.Downloads++;
     125
     126            var memoryStream = new MemoryStream();
     127            var binaryWriter = new BinaryWriter(memoryStream);
     128            binaryWriter.Write(distributedJob.Downloads);
     129            binaryWriter.Write(DateTime.UtcNow.ToBinary());
     130
     131            P2PManager.Store(GenerateDownloadCounterKey(distributedJob), memoryStream.ToArray());
     132        }
     133
    107134        private static string GenerateWorkspaceKey(DistributedJob distributedJob)
    108135        {
    109             return string.Format("{0}.{1}.{2}", JoblistKey, WorkspaceKeyPrefix, distributedJob.JobGuid);
     136            return string.Format("{0}.{1}.{2}", JoblistKey, WorkspaceKeyPrefix, distributedJob.Guid);
     137        }
     138
     139        private static string GenerateDownloadCounterKey(DistributedJob distributedJob)
     140        {
     141            return string.Format("{0}.{1}.{2}.{3}", JoblistKey, WorkspaceKeyPrefix, distributedJob.Guid, "downloads");
     142        }
     143
     144        private static byte[] JobListToByteArray(ICollection<DistributedJob> distributedJobList)
     145        {
     146            var memoryStream = new MemoryStream();
     147            var binaryWriter = new BinaryWriter(memoryStream);
     148            binaryWriter.Write(distributedJobList.Count);
     149
     150            foreach (var distributedJob in distributedJobList)
     151            {
     152                DistributedJobSerializer.ToWriter(distributedJob, binaryWriter);
     153            }
     154
     155            return memoryStream.ToArray();
     156        }
     157
     158        private ICollection<DistributedJob> ByteArrayToJobList(byte[] rawData)
     159        {
     160            var distributedJobList = new List<DistributedJob>();
     161            var binaryReader = new BinaryReader(new MemoryStream(rawData));
     162
     163            var numberOfJobs = binaryReader.ReadInt32();
     164            for (var i = 0; i < numberOfJobs; i++)
     165            {
     166                distributedJobList.Add(DistributedJobSerializer.FromReader(binaryReader));
     167            }
     168
     169            return distributedJobList;
    110170        }
    111171    }
Note: See TracChangeset for help on using the changeset viewer.