source: trunk/CrypPlugins/KeyPatternJob/KeyPatternJobResult.cs @ 1137

Last change on this file since 1137 was 1137, checked in by arnold, 12 years ago

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.

File size: 8.0 KB
Line 
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;
17using System.Collections.Generic;
18using System.Linq;
19using System.Text;
20using Cryptool.PluginBase.Miscellaneous;
21using System.IO;
22
23namespace Cryptool.Plugins.PeerToPeer.Jobs
24{
25    public class KeyPatternJobResult : JobResult<LinkedList<KeySearcher.KeySearcher.ValueKey>>
26    {
27        private Encoding enc = Encoding.UTF8;
28
29        public KeyPatternJobResult(byte[] serializedKeyPatternJobResult) : base(serializedKeyPatternJobResult)
30        { }
31
32        public KeyPatternJobResult(BigInteger jobId, LinkedList<KeySearcher.KeySearcher.ValueKey> resultList, 
33            TimeSpan processingTime) 
34            : this(jobId, resultList, processingTime, false) { }
35
36        public KeyPatternJobResult(BigInteger jobId, LinkedList<KeySearcher.KeySearcher.ValueKey> resultList, 
37            TimeSpan processingTime, bool isIntermediateResult)
38            : base(jobId,resultList,processingTime, isIntermediateResult)
39        {
40            this.ResultList = resultList;
41        }
42
43        #region access methods on result elements
44
45        LinkedList<KeySearcher.KeySearcher.ValueKey> resultList;
46        public LinkedList<KeySearcher.KeySearcher.ValueKey> ResultList
47        {
48            get { return this.resultList; }
49            set { this.resultList = value; }
50        }
51
52        public KeySearcher.KeySearcher.ValueKey GetElement(int position)
53        {
54            if (this.ResultList != null)
55            {
56                return this.resultList.ElementAt<KeySearcher.KeySearcher.ValueKey>(position);
57            }
58            throw (new Exception("ResultList is empty or the choosen position doesn't exists in this context"));
59        }
60        public KeySearcher.KeySearcher.ValueKey GetFirstElement()
61        {
62            if (this.ResultList != null)
63            {
64                return this.resultList.First.Value;
65            }
66            throw (new Exception("ResultList is empty or the first position doesn't exists in this context"));
67        }
68
69
70        #endregion
71
72        #region Serialization methods -
73
74        /* serialization information: 3 fields per data set in the following order:
75         *  -------------------------------------------------------------------------------------------------------------------------------
76         * | Dataset count | double-value (8 byte) | string len (1 byte) | string-data (n bytes) | byte len (1 byte) | byte-data (n bytes) |
77         *  ------------------------------------------------------------------------------------------------------------------------------- */
78        protected override byte[] SerializeResultType()
79        {
80            MemoryStream memStream = new MemoryStream();
81            byte[] resultCountByte = BitConverter.GetBytes(resultList.Count);
82            memStream.Write(resultCountByte, 0, resultCountByte.Length);
83
84            foreach (KeySearcher.KeySearcher.ValueKey  valKey in resultList)
85            {
86                // serialize value (double)
87                byte[] valueByte = BitConverter.GetBytes(valKey.value);
88                byte[] valueByteLen = BitConverter.GetBytes(valueByte.Length);
89                memStream.Write(valueByteLen, 0, valueByteLen.Length);
90                memStream.Write(valueByte, 0, valueByte.Length);
91
92                // serialize key (string)
93                byte[] keyByte = enc.GetBytes(valKey.key);
94                byte[] keyByteLen = BitConverter.GetBytes(keyByte.Length);
95                memStream.Write(keyByteLen, 0, keyByteLen.Length);
96                memStream.Write(keyByte, 0, keyByte.Length);
97
98                byte[] decryptionByteLen;
99                // serialize decryption (byte[]) - not more than 256 byte
100                if (valKey.decryption.Length >= 255)
101                {
102                    decryptionByteLen = BitConverter.GetBytes(255);
103                    memStream.Write(decryptionByteLen, 0, decryptionByteLen.Length);
104                    memStream.Write(valKey.decryption, 0, 254);
105                }
106                else if (valKey.decryption.Length > 0 && valKey.decryption.Length < 255)
107                {
108                    decryptionByteLen = BitConverter.GetBytes(valKey.decryption.Length);
109                    memStream.Write(decryptionByteLen, 0, decryptionByteLen.Length);
110                    memStream.Write(valKey.decryption, 0, valKey.decryption.Length);
111                }
112                else // if no decryption result exists
113                {
114                    decryptionByteLen = BitConverter.GetBytes((int)0);
115                    memStream.Write(decryptionByteLen, 0, decryptionByteLen.Length);
116                }
117            } // end foreach
118
119            return memStream.ToArray();
120        }
121
122        protected override LinkedList<KeySearcher.KeySearcher.ValueKey> DeserializeResultType(byte[] serializedResultType)
123        {
124            LinkedList<KeySearcher.KeySearcher.ValueKey> lstRet = new LinkedList<KeySearcher.KeySearcher.ValueKey>();
125            MemoryStream memStream = new MemoryStream(serializedResultType);
126
127            // byte[] length of Int32
128            int iInt32 = 4;
129
130            int valueLen = 0;
131            int keyLen = 0;
132            int decryptionLen = 0;
133
134            byte[] byteDatasetCount = new byte[iInt32];
135            memStream.Read(byteDatasetCount, 0, byteDatasetCount.Length);
136            int iDatasetCount = BitConverter.ToInt32(byteDatasetCount, 0);
137
138            KeySearcher.KeySearcher.ValueKey valKey = new KeySearcher.KeySearcher.ValueKey();
139            for (int i = 0; i < iDatasetCount; i++)
140            {
141                // deserialize value (double)
142                byte[] valueByteLen = new byte[iInt32];
143                memStream.Read(valueByteLen, 0, valueByteLen.Length);
144                valueLen = BitConverter.ToInt32(valueByteLen, 0);
145                byte[] valueByte = new byte[valueLen];
146                memStream.Read(valueByte, 0, valueByte.Length);
147               
148                valKey.value = BitConverter.ToDouble(valueByte, 0);
149
150                // deserialize key (string)
151                byte[] keyByteLen = new byte[iInt32];
152                memStream.Read(keyByteLen, 0, keyByteLen.Length);
153                keyLen = BitConverter.ToInt32(keyByteLen, 0);
154                byte[] keyByte = new byte[keyLen];
155                memStream.Read(keyByte, 0, keyByte.Length);
156
157                valKey.key = enc.GetString(keyByte, 0, keyByte.Length);
158
159                // deserialize decryption (byte[])
160                byte[] decryptByteLen = new byte[iInt32];
161                memStream.Read(decryptByteLen, 0, decryptByteLen.Length);
162                decryptionLen = BitConverter.ToInt32(decryptByteLen, 0);
163                byte[] decryptionByte = new byte[decryptionLen];
164                memStream.Read(decryptionByte, 0, decryptionByte.Length);
165
166                valKey.decryption = decryptionByte;
167
168                // add new, deserialize ValueKey to LinkedList
169                lstRet.AddLast(valKey);
170            }
171            if ((memStream.Length - memStream.Position) > 0)
172            {
173                throw(new Exception("The serializedResultType wasn't deserialized correctly, because " 
174                    + "after deserializing there are already some bytes (remaining Length of byte[]: " 
175                    + memStream.Length + ")"));
176            }
177            this.ResultList = lstRet;
178
179            return lstRet;
180        }
181
182        #endregion
183    }
184}
Note: See TracBrowser for help on using the repository browser.