Ignore:
Timestamp:
Feb 11, 2010, 3:48:49 PM (12 years ago)
Author:
arnold
Message:

Completely redesigned Manager-JobAdmin-Worker-infrastructure to distribute Jobs with a Peer-to-Peer infrastructure to remote CT2-Workspaces.

To test this infrastructure, open 2 instances of CT and load P2P_Manager_NEW_DES.cte and in the other instance P2P_Worker_NEW.cte.
HINT: Working with remote peers isn't possible every time, because the so called "SuperNode", which is necessary for relaying, sometimes goes down. But testing this infrastructure on different computers in the same network should work every time.

Location:
trunk/CrypPlugins/PeerToPeerJobs
Files:
3 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/PeerToPeerJobs/IJobPart.cs

    r1129 r1137  
    1 using System;
     1/* Copyright 2010 Team CrypTool (Christian Arnold), Uni Duisburg-Essen
     2
     3   Licensed under the Apache License, Version 2.0 (the "License");
     4   you may not use this file except in compliance with the License.
     5   You may obtain a copy of the License at
     6
     7       http://www.apache.org/licenses/LICENSE-2.0
     8
     9   Unless required by applicable law or agreed to in writing, software
     10   distributed under the License is distributed on an "AS IS" BASIS,
     11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12   See the License for the specific language governing permissions and
     13   limitations under the License.
     14*/
     15
     16using System;
    217using System.Collections.Generic;
    318using System.Linq;
    419using System.Text;
     20using Cryptool.PluginBase.Miscellaneous;
    521
    622namespace Cryptool.Plugins.PeerToPeer.Jobs
    723{
    8     public interface IJobPart<JobType>
     24    /// <summary>
     25    /// please implement additionally a static Deserialize method and a constructor
     26    /// with a parameter byte[], where you can Deserialize a byte[] representation
     27    /// of this class. The static Deserialize Method could call than the byte[] constructor!!!
     28    /// </summary>
     29    /// <typeparam name="JobType"></typeparam>
     30    public interface IJobPart
    931    {
     32        BigInteger JobId { get; set; }
    1033        byte[] Serialize();
    11         JobType Deserialize(byte[] serializedJobPart);
     34        //IJobPart<JobType> Deserialize(byte[] serializedJobPart);
    1235    }
    1336}
  • trunk/CrypPlugins/PeerToPeerJobs/IJobResult.cs

    r1130 r1137  
    1 using System;
     1/* Copyright 2010 Team CrypTool (Christian Arnold), Uni Duisburg-Essen
     2
     3   Licensed under the Apache License, Version 2.0 (the "License");
     4   you may not use this file except in compliance with the License.
     5   You may obtain a copy of the License at
     6
     7       http://www.apache.org/licenses/LICENSE-2.0
     8
     9   Unless required by applicable law or agreed to in writing, software
     10   distributed under the License is distributed on an "AS IS" BASIS,
     11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12   See the License for the specific language governing permissions and
     13   limitations under the License.
     14*/
     15
     16using System;
    217using System.Collections.Generic;
    318using System.Linq;
    419using System.Text;
     20using Cryptool.PluginBase.Miscellaneous;
    521
    622namespace Cryptool.Plugins.PeerToPeer.Jobs
     
    1228        /// Result is represented by this class
    1329        /// </summary>
    14         int JobId { get; set; }
     30        BigInteger JobId { get; set; }
    1531        /// <summary>
    1632        /// You can allow intermediate results by using this flag
     
    2339        JobResultType Result { get; set; }
    2440        /// <summary>
    25         /// serializes this class, so you can recover all class information by deserializing
     41        /// serializes this class, so you can recover all class information by deserializing.
     42        /// HINT: You can Deserialize a byte[] representation of this class by using the constructor with
     43        /// the byte[] parameter!
    2644        /// </summary>
    2745        /// <returns>serialized byte[] representation of this class</returns>
    2846        byte[] Serialize();
    29         /// <summary>
    30         /// Deserializes a valid byte[] representation of a IJobResult.
    31         /// ATTENTION: Not only deserializes, but additionally recreates the whole class
    32         /// by dint of the byte[]. So all this class information will be overwritten.
    33         /// </summary>
    34         /// <param name="serializedJobResult">a valid byte[] representation of a IJobResult</param>
    35         /// <returns></returns>
    36         bool Deserialize(byte[] serializedJobResult);
    3747    }
    3848}
  • trunk/CrypPlugins/PeerToPeerJobs/JobResult.cs

    r1130 r1137  
    1 using System;
     1/* Copyright 2010 Team CrypTool (Christian Arnold), Uni Duisburg-Essen
     2
     3   Licensed under the Apache License, Version 2.0 (the "License");
     4   you may not use this file except in compliance with the License.
     5   You may obtain a copy of the License at
     6
     7       http://www.apache.org/licenses/LICENSE-2.0
     8
     9   Unless required by applicable law or agreed to in writing, software
     10   distributed under the License is distributed on an "AS IS" BASIS,
     11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12   See the License for the specific language governing permissions and
     13   limitations under the License.
     14*/
     15
     16using System;
    217using System.Collections.Generic;
    318using System.Linq;
     
    520using System.IO;
    621using System.Reflection;
     22using Cryptool.PluginBase.Miscellaneous;
    723
    824namespace Cryptool.Plugins.PeerToPeer.Jobs
    925{
    10     public class JobResult<JobResultType> : IJobResult<JobResultType>
     26    public abstract class JobResult<JobResultType> : IJobResult<JobResultType>
    1127    {
    1228        #region Variables
    1329
    14         private int jobId;
    15         public int JobId
     30        private BigInteger jobId;
     31        public BigInteger JobId
    1632        {
    1733            get {  return this.jobId; }
    1834            set { this.jobId = value; }
    1935        }
    20         private bool isIntermediateJobResult;
     36        private bool isIntermediateJobResult = false;
    2137        public bool IsIntermediateResult
    2238        {
     
    4561        /// <param name="result">result of the job (e.g. simple conclusion, list, complex data structure)</param>
    4662        /// <param name="processingTime">Timespan between begin and end of processing the job</param>
    47         public JobResult(int jobId, JobResultType result, TimeSpan processingTime)
     63        public JobResult(BigInteger jobId, JobResultType result, TimeSpan processingTime)
    4864            :this(jobId,result,processingTime,false)
    4965        { }
     
    5672        /// <param name="processingTime">Timespan between begin and end of processing the job</param>
    5773        /// <param name="isIntermediateResult">Is this is only an intermediate result, set this parameter to true, otherwise choose false</param>
    58         public JobResult(int jobId, JobResultType result, TimeSpan processingTime, bool isIntermediateResult)
     74        public JobResult(BigInteger jobId, JobResultType result, TimeSpan processingTime, bool isIntermediateResult)
    5975        {
    6076            this.JobId = jobId;
     
    6480        }
    6581
     82        // TODO: Test this constructor, if it works fine, delete Deserialize Method
     83        /// <summary>
     84        /// If you have a serialized JobResult class, you can deserialize it by using this constructor!
     85        /// </summary>
     86        /// <param name="serializedJobResult">serialized JobResult class as a byte[]</param>
     87        public JobResult(byte[] serializedJobResult)
     88        {
     89            BigInteger temp_jobId;
     90            JobResultType temp_result;
     91            bool temp_isIntermediateResult;
     92            TimeSpan temp_processingTime;
     93
     94            MemoryStream memStream = new MemoryStream(serializedJobResult, false);
     95            try
     96            {
     97                Int32 testValue = 3000;
     98                byte[] readInt = BitConverter.GetBytes(testValue);
     99
     100                /* Deserialize JobId */
     101                memStream.Read(readInt, 0, readInt.Length);
     102                int jobIdLen = BitConverter.ToInt32(readInt, 0);
     103                byte[] jobIdByte = new byte[jobIdLen];
     104                memStream.Read(jobIdByte, 0, jobIdByte.Length);
     105                temp_jobId = new BigInteger(jobIdByte, jobIdByte.Length);
     106
     107                /* Deserialize Job result data */
     108                //memStream.Read(readInt, 0, readInt.Length);
     109                //int serializedDataLen = BitConverter.ToInt32(readInt, 0);
     110                //byte[] serializedJobResultByte = new byte[serializedDataLen];
     111                //memStream.Read(serializedJobResultByte, 0, serializedJobResultByte.Length);
     112                //temp_result = (JobResultType)GetDeserializationViaReflection(serializedJobResultByte, this.Result);
     113
     114                // right for bool???
     115                temp_isIntermediateResult = Convert.ToBoolean(memStream.ReadByte());
     116
     117                memStream.Read(readInt, 0, readInt.Length);
     118                int days = BitConverter.ToInt32(readInt, 0);
     119                memStream.Read(readInt, 0, readInt.Length);
     120                int hours = BitConverter.ToInt32(readInt, 0);
     121                memStream.Read(readInt, 0, readInt.Length);
     122                int minutes = BitConverter.ToInt32(readInt, 0);
     123                memStream.Read(readInt, 0, readInt.Length);
     124                int seconds = BitConverter.ToInt32(readInt, 0);
     125                memStream.Read(readInt, 0, readInt.Length);
     126                int millisec = BitConverter.ToInt32(readInt, 0);
     127                temp_processingTime = new TimeSpan(days, hours, minutes, seconds, millisec);
     128
     129                // read the rest of the byte[]-stream, this is the specialized JobResulType
     130                byte[] serializedJobResultByte = new byte[memStream.Length - memStream.Position];
     131                memStream.Read(serializedJobResultByte, 0, serializedJobResultByte.Length);
     132                temp_result = DeserializeResultType(serializedJobResultByte);
     133
     134                this.JobId = temp_jobId;
     135                this.IsIntermediateResult = temp_isIntermediateResult;
     136                this.ProcessingTime = temp_processingTime;
     137                this.Result = temp_result;
     138            }
     139            catch (Exception ex)
     140            {
     141                throw ex;
     142            }
     143            finally
     144            {
     145                memStream.Flush();
     146                memStream.Close();
     147                memStream.Dispose();
     148            }
     149        }
     150
     151        #region comparing methods
     152
     153        public override bool Equals(object obj)
     154        {
     155            if(obj == null)
     156                return false;
     157
     158            if (ReferenceEquals(this, obj))
     159                return true;
     160
     161            if (this.GetType() != obj.GetType())
     162                return false;
     163
     164            return this == (JobResult<JobResultType>)obj;
     165        }
     166
     167        public static bool operator ==(JobResult<JobResultType> left, JobResult<JobResultType> right)
     168        {
     169            if ((object)left == (object)right)
     170                return true;
     171
     172            if ((object)left == null || (object)right == null)
     173                return false;
     174
     175            if (left.jobId == right.JobId)
     176                return true;
     177
     178            return false;
     179        }
     180
     181        public static bool operator !=(JobResult<JobResultType> left, JobResult<JobResultType> right)
     182        {
     183            return !(left == right);
     184        }
     185
     186        public override int GetHashCode()
     187        {
     188            return this.JobId.GetHashCode();
     189        }
     190
     191        #endregion
     192
    66193        #region Serialization methods
     194
     195        protected abstract byte[] SerializeResultType();
     196
     197        protected abstract JobResultType DeserializeResultType(byte[] serializedResultType);
    67198
    68199        /* 4 Bytes: serialized JobId
     
    76207         * 4 Bytes: procTime.Milliseconds */
    77208        /// <summary>
    78         /// Serializes the whole class, so you can recreate this instance elsewhere by dint of this byte[]
     209        /// Serializes the whole class, so you can recreate this instance elsewhere by dint of this byte[].
     210        /// HINT: You can Deserialize a byte[] representation of this class by using the constructor with
     211        /// the byte[] parameter!
    79212        /// </summary>
    80213        /// <returns>serialized class as an byte[]</returns>
     
    86219            {
    87220                /* Serialize jobId */
    88                 byte[] intJobId = BitConverter.GetBytes(this.JobId);
    89                 memStream.Write(intJobId, 0, intJobId.Length);
     221                byte[] jobIdByte = this.JobId.getBytes();
     222                byte[] jobIdLen = BitConverter.GetBytes(jobIdByte.Length);
     223                memStream.Write(jobIdLen, 0, jobIdLen.Length);
     224                memStream.Write(jobIdByte, 0, jobIdByte.Length);
    90225
    91226                /* Serialize job result via Reflection */
    92                 byte[] serializedJobResult = GetSerializationViaReflection(this.Result);
    93                 byte[] byJobResultLen = BitConverter.GetBytes(serializedJobResult.Length);
    94                 memStream.Write(byJobResultLen, 0, byJobResultLen.Length);
    95                 memStream.Write(serializedJobResult, 0, serializedJobResult.Length);
     227                //byte[] serializedJobResult = GetSerializationViaReflection(this.Result);
     228                //byte[] byJobResultLen = BitConverter.GetBytes(serializedJobResult.Length);
     229                //memStream.Write(byJobResultLen, 0, byJobResultLen.Length);
     230                //memStream.Write(serializedJobResult, 0, serializedJobResult.Length);
    96231
    97232                byte[] intResultBytes = BitConverter.GetBytes(this.isIntermediateJobResult);
     
    108243                byte[] msecondsBytes = BitConverter.GetBytes(this.processingTime.Milliseconds);
    109244                memStream.Write(msecondsBytes,0,msecondsBytes.Length);
    110                                
     245
     246
     247                byte[] serializedJobResult = SerializeResultType();
     248                memStream.Write(serializedJobResult, 0, serializedJobResult.Length);
     249
     250
    111251                ret = memStream.ToArray();
    112252            }
     
    124264        }
    125265
    126         /// <summary>
    127         /// ATTENTION: Not only deserializes the DistributableJobResult, but additionally recreates the whole class
    128         /// by dint of the byte[]. So all this class information will be overwritten.
    129         /// </summary>
    130         /// <param name="serializedJobResult">a valid DistributableJobResult serialization</param>
    131         public bool Deserialize(byte[] serializedJobResult)
    132         {
    133             bool ret = false;
    134             MemoryStream memStream = new MemoryStream(serializedJobResult,false);
    135             try
    136             {
    137                 /* Deserialize JobId */
    138                 byte[] readInt = new byte[4];
    139                 memStream.Read(readInt, 0, readInt.Length);
    140                 this.JobId = BitConverter.ToInt32(readInt,0);
    141 
    142                 /* Deserialize Job result data */
    143                 memStream.Read(readInt,0,readInt.Length);
    144                 int serializedDataLen = BitConverter.ToInt32(readInt,0);
    145                 byte[] serializedJobResultByte = new byte[serializedDataLen];
    146                 memStream.Read(serializedJobResultByte, 0, serializedJobResultByte.Length);
    147                 this.Result = (JobResultType)GetDeserializationViaReflection(serializedJobResultByte, this.Result);
    148 
    149                 // right for bool???
    150                 memStream.Read(readInt, 0, readInt.Length);
    151                 this.isIntermediateJobResult = BitConverter.ToBoolean(readInt,0);
    152 
    153                 memStream.Read(readInt, 0, readInt.Length);
    154                 int days = BitConverter.ToInt32(readInt,0);
    155                 memStream.Read(readInt, 0, readInt.Length);
    156                 int hours = BitConverter.ToInt32(readInt, 0);
    157                 memStream.Read(readInt, 0, readInt.Length);
    158                 int minutes = BitConverter.ToInt32(readInt, 0);
    159                 memStream.Read(readInt, 0, readInt.Length);
    160                 int seconds = BitConverter.ToInt32(readInt, 0);
    161                 memStream.Read(readInt, 0, readInt.Length);
    162                 int millisec = BitConverter.ToInt32(readInt, 0);
    163                 this.processingTime = new TimeSpan(days, hours, minutes, seconds, millisec);
    164 
    165                 ret = true;
    166             }
    167             catch (Exception ex)
    168             {
    169                 throw ex;
    170             }
    171             finally
    172             {
    173                 memStream.Flush();
    174                 memStream.Close();
    175                 memStream.Dispose();
    176             }
    177             return ret;
    178         }
    179 
    180266        #endregion
    181267
    182         #region Reflection methods
    183 
    184         private byte[] GetSerializationViaReflection(object objectToSerialize)
    185         {
    186             byte[] serializedBytes = null;
    187             try
    188             {
    189                 MethodInfo methInfo = objectToSerialize.GetType().GetMethod("Serialize");
    190                 ParameterInfo[] paramInfo = methInfo.GetParameters();
    191                 ParameterInfo returnParam = methInfo.ReturnParameter;
    192                 Type returnType = methInfo.ReturnType;
    193 
    194                 serializedBytes = methInfo.Invoke(objectToSerialize, null) as byte[];
    195                 if (serializedBytes == null)
    196                     throw (new Exception("Serializing " + objectToSerialize.GetType().ToString() + " canceled!"));
    197             }
    198             catch (Exception ex)
    199             {
    200                 throw (new Exception("Invocing method 'Serialize' of '"
    201                     + objectToSerialize.GetType().ToString() + "' wasn't possible. " + ex.ToString()));
    202             }
    203             return serializedBytes;
    204         }
    205 
    206         private object GetDeserializationViaReflection(object serializedData, object returnType)
    207         {
    208             try
    209             {
    210                 MethodInfo methInfo = returnType.GetType().GetMethod("Deserialize", new[] { serializedData.GetType() });
    211                 object deserializedData = methInfo.Invoke(returnType, new object[] { serializedData });
    212                 if (deserializedData == null)
    213                     throw (new Exception("Deserializing " + returnType.ToString() + " canceled!"));
    214                 return deserializedData;
    215             }
    216             catch (Exception ex)
    217             {
    218                 throw (new Exception("Invocing method 'Deserialize' of '"
    219                     + returnType.ToString() + "' wasn't possible. " + ex.ToString()));
    220             }
    221         }
    222 
    223         #endregion
     268        //#region Reflection methods
     269
     270        //private byte[] GetSerializationViaReflection(object objectToSerialize)
     271        //{
     272        //    byte[] serializedBytes = null;
     273        //    try
     274        //    {
     275        //        MethodInfo methInfo = objectToSerialize.GetType().GetMethod("Serialize");
     276        //        ParameterInfo[] paramInfo = methInfo.GetParameters();
     277        //        ParameterInfo returnParam = methInfo.ReturnParameter;
     278        //        Type returnType = methInfo.ReturnType;
     279
     280        //        serializedBytes = methInfo.Invoke(objectToSerialize, null) as byte[];
     281        //        if (serializedBytes == null)
     282        //            throw (new Exception("Serializing " + objectToSerialize.GetType().ToString() + " canceled!"));
     283        //    }
     284        //    catch (Exception ex)
     285        //    {
     286        //        throw (new Exception("Invocing method 'Serialize' of '"
     287        //            + objectToSerialize.GetType().ToString() + "' wasn't possible. " + ex.ToString()));
     288        //    }
     289        //    return serializedBytes;
     290        //}
     291
     292        //private object GetDeserializationViaReflection(object serializedData, object returnType)
     293        //{
     294        //    try
     295        //    {
     296        //        MethodInfo methInfo = returnType.GetType().GetMethod("Deserialize", new[] { serializedData.GetType() });
     297        //        object deserializedData = methInfo.Invoke(returnType, new object[] { serializedData });
     298        //        if (deserializedData == null)
     299        //            throw (new Exception("Deserializing " + returnType.ToString() + " canceled!"));
     300        //        return deserializedData;
     301        //    }
     302        //    catch (Exception ex)
     303        //    {
     304        //        throw (new Exception("Invocing method 'Deserialize' of '"
     305        //            + returnType.ToString() + "' wasn't possible. " + ex.ToString()));
     306        //    }
     307        //}
     308
     309        //#endregion
    224310    }
    225311}
  • trunk/CrypPlugins/PeerToPeerJobs/PeerToPeerJobs.csproj

    r1130 r1137  
    4848  </ItemGroup>
    4949  <ItemGroup>
     50    <Compile Include="IDistributableJob.cs" />
    5051    <Compile Include="IJobResult.cs" />
     52    <Compile Include="JobMessages.cs" />
     53    <Compile Include="JobPart.cs" />
    5154    <Compile Include="JobResult.cs" />
    52     <Compile Include="IJobStack.cs" />
    5355    <Compile Include="IJobPart.cs" />
    5456    <Compile Include="Properties\AssemblyInfo.cs" />
    5557  </ItemGroup>
    5658  <ItemGroup>
    57     <None Include="All3Classes.cd" />
     59    <ProjectReference Include="..\..\CrypPluginBase\CrypPluginBase.csproj">
     60      <Project>{25DB2B47-A457-4EC2-960B-395CE07AE093}</Project>
     61      <Name>CrypPluginBase</Name>
     62    </ProjectReference>
     63  </ItemGroup>
     64  <ItemGroup>
     65    <None Include="cdJobClasses.cd" />
     66    <None Include="cdJobMessage.cd" />
    5867  </ItemGroup>
    5968  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Note: See TracChangeset for help on using the changeset viewer.