public void manual_decoding(List <EncodedChunk> encoded_list) { Console.Out.WriteLine("Manual testing in progress..."); /*List<EncodedChunk> encoded_list_copy = new List<EncodedChunk>(encoded_list); * * int i = 0; * * string chunk_filename = "F:/LiveProjects/C#Rookie/ManualNC" + (i + 1) + ".mp3"; * FileStream fp = new FileStream(chunk_filename, FileMode.Create, FileAccess.Write); * * // write the encoded data part of EC to this file * // ready_chunks[z] = 1; * int byte_count = 0; * int total_bytes = (int)encoded_list_copy[1].encoded_data_part.Length; * * * while (byte_count < total_bytes) * { * byte temp = new byte(); * * temp = Convert.ToByte(encoded_list[1].encoded_data_part[byte_count]); * byte_count++; * fp.WriteByte(temp); * } * fp.Close(); * Console.WriteLine("writing NC" + (i + 1) + " done");*/ EncodedChunk ec = new EncodedChunk(); BinaryFormatter bin = new BinaryFormatter(); FileStream fs = new FileStream("F:/LiveProjects/C#Rookie/EncodedChunk.0001.dat", FileMode.Open, FileAccess.ReadWrite); ec = (EncodedChunk)bin.Deserialize(fs); fs.Write(ec.encoded_data_part, 0, ec.encoded_data_part.Length); fs.Close(); fs = new FileStream("F:/LiveProjects/C#Rookie/EncodedChunk.mp3", FileMode.Create, FileAccess.Write); fs.Write(ec.encoded_data_part, 0, (int)ec.encoded_data_part.Length); fs.Close(); }
public void manual_decoding(List<EncodedChunk> encoded_list) { Console.Out.WriteLine("Manual testing in progress..."); /*List<EncodedChunk> encoded_list_copy = new List<EncodedChunk>(encoded_list); int i = 0; string chunk_filename = "F:/LiveProjects/C#Rookie/ManualNC" + (i + 1) + ".mp3"; FileStream fp = new FileStream(chunk_filename, FileMode.Create, FileAccess.Write); // write the encoded data part of EC to this file // ready_chunks[z] = 1; int byte_count = 0; int total_bytes = (int)encoded_list_copy[1].encoded_data_part.Length; while (byte_count < total_bytes) { byte temp = new byte(); temp = Convert.ToByte(encoded_list[1].encoded_data_part[byte_count]); byte_count++; fp.WriteByte(temp); } fp.Close(); Console.WriteLine("writing NC" + (i + 1) + " done");*/ EncodedChunk ec = new EncodedChunk(); BinaryFormatter bin = new BinaryFormatter(); FileStream fs = new FileStream("F:/LiveProjects/C#Rookie/EncodedChunk.0001.dat", FileMode.Open, FileAccess.ReadWrite); ec = (EncodedChunk)bin.Deserialize(fs); fs.Write(ec.encoded_data_part, 0, ec.encoded_data_part.Length); fs.Close(); fs = new FileStream("F:/LiveProjects/C#Rookie/EncodedChunk.mp3", FileMode.Create, FileAccess.Write); fs.Write(ec.encoded_data_part, 0, (int)ec.encoded_data_part.Length); fs.Close(); }
public List<EncodedChunk> EncodeNormalChunks(List<NormalChunk> chunk_list) { /* using LT codes [1] The degree d, 1 ≤ d ≤ n, of the next packet is chosen at random. [2] Exactly d blocks from the message are randomly chosen. [3] If Mi is the ith block of the message, the data portion of the next packet is computed as Mi1 ^ Mi2 ^....Mid, where {i1, i2, …, id} are the randomly chosen indices for the d blocks included in this packet. [4] A prefix is appended to the encoded packet, defining how many blocks n are in the message, how many blocks d have been exclusive-ored into the data portion of this packet, and the list of indices {i1, i2, …, id}. */ int num_chunks = chunk_list.Count; int a = 0, length = 0, num_of_repeats = num_chunks + 2; // (num_of_repeats) signifies how many encoded // chunks need to be generated Console.WriteLine("Begin encoding..."); Console.WriteLine("Num of encoded chunks that will be generated = " + num_of_repeats); for (int x = 0; x < num_of_repeats; x++) { Random rand = new Random(5555 + x); // use a known seed int degree = rand.Next(1, num_chunks); // degree for LT codes Console.WriteLine("degree = " + degree); List<int> rand_chunk_list = new List<int>(); // array which will contain indicies of random chunk_ids EncodedChunk ec = new EncodedChunk(); // generate distinct random chunks for encoding do { int r = rand.Next(num_chunks); if (!rand_chunk_list.Contains(r)) { rand_chunk_list.Add(r); } } while (rand_chunk_list.Count < degree); Console.Write("Chunks that need to be merged in Round " + x + " = "); for (int z = 0; z < degree; z++) { Console.Write(rand_chunk_list[z] + " "); } Console.WriteLine(""); ec.chunk_ids = new List<int>(); for (int z = 0; z < rand_chunk_list.Count; z++) { length = chunk_list[rand_chunk_list[z]].data_part.Length; ec.encoded_data_part = new byte[length]; while (a < length) { // XOr'ing ec.encoded_data_part[a] = Convert.ToByte(ec.encoded_data_part[a] ^ chunk_list[rand_chunk_list[z]].data_part[a]); a++; } a = 0; ec.chunk_ids.Add(rand_chunk_list[z]); } ec.parent_name = chunk_list[0].parent_name; // Now the EncodedChunk is ready. Write it to a file on hard disk BinaryFormatter binaryFormatter = new BinaryFormatter(); string encode_filename = "F:/LiveProjects/C#Rookie/EncodedChunk" + "." + String.Format(@"{0:D4}", x) + ".dat"; Stream fp = new FileStream(encode_filename, FileMode.Create, FileAccess.ReadWrite); try { binaryFormatter.Serialize(fp, ec); } catch (SerializationException se) { Console.WriteLine(se.Message); } fp.Flush(); fp.Close(); ec_chunk_list.Add(ec); Console.WriteLine("Ending Round " + x); } return ec_chunk_list; /* some junk to verify... BinaryFormatter bin = new BinaryFormatter(); FileStream fs = new FileStream("F:/LiveProjects/C#Rookie/EncodedChunk.0001.dat", FileMode.Open, FileAccess.ReadWrite); ec = (EncodedChunk)bin.Deserialize(fs); fs.Write(ec.encoded_data_part, 0, ec.encoded_data_part.Length); fs.Close(); fs = new FileStream("F:/LiveProjects/C#Rookie/EncodedChunk.mp3", FileMode.Create, FileAccess.Write); fs.Write(ec.encoded_data_part, 0, (int)ec.encoded_data_part.Length); fs.Close();*/ }
public List <EncodedChunk> EncodeNormalChunks(List <NormalChunk> chunk_list) { /* using LT codes * [1] The degree d, 1 ≤ d ≤ n, of the next packet is chosen at random. * [2] Exactly d blocks from the message are randomly chosen. * [3] If Mi is the ith block of the message, the data portion of the next packet is computed as * Mi1 ^ Mi2 ^....Mid, where {i1, i2, …, id} are the randomly chosen indices for the d blocks * included in this packet. * [4] A prefix is appended to the encoded packet, defining how many blocks n are in the message, * how many blocks d have been exclusive-ored into the data portion of this packet, and the * list of indices {i1, i2, …, id}. */ int num_chunks = chunk_list.Count; int a = 0, length = 0, num_of_repeats = num_chunks + 2; // (num_of_repeats) signifies how many encoded // chunks need to be generated Console.WriteLine("Begin encoding..."); Console.WriteLine("Num of encoded chunks that will be generated = " + num_of_repeats); for (int x = 0; x < num_of_repeats; x++) { Random rand = new Random(5555 + x); // use a known seed int degree = rand.Next(1, num_chunks); // degree for LT codes Console.WriteLine("degree = " + degree); List <int> rand_chunk_list = new List <int>(); // array which will contain indicies of random chunk_ids EncodedChunk ec = new EncodedChunk(); // generate distinct random chunks for encoding do { int r = rand.Next(num_chunks); if (!rand_chunk_list.Contains(r)) { rand_chunk_list.Add(r); } } while (rand_chunk_list.Count < degree); Console.Write("Chunks that need to be merged in Round " + x + " = "); for (int z = 0; z < degree; z++) { Console.Write(rand_chunk_list[z] + " "); } Console.WriteLine(""); ec.chunk_ids = new List <int>(); for (int z = 0; z < rand_chunk_list.Count; z++) { length = chunk_list[rand_chunk_list[z]].data_part.Length; ec.encoded_data_part = new byte[length]; while (a < length) { // XOr'ing ec.encoded_data_part[a] = Convert.ToByte(ec.encoded_data_part[a] ^ chunk_list[rand_chunk_list[z]].data_part[a]); a++; } a = 0; ec.chunk_ids.Add(rand_chunk_list[z]); } ec.parent_name = chunk_list[0].parent_name; // Now the EncodedChunk is ready. Write it to a file on hard disk BinaryFormatter binaryFormatter = new BinaryFormatter(); string encode_filename = "F:/LiveProjects/C#Rookie/EncodedChunk" + "." + String.Format(@"{0:D4}", x) + ".dat"; Stream fp = new FileStream(encode_filename, FileMode.Create, FileAccess.ReadWrite); try { binaryFormatter.Serialize(fp, ec); } catch (SerializationException se) { Console.WriteLine(se.Message); } fp.Flush(); fp.Close(); ec_chunk_list.Add(ec); Console.WriteLine("Ending Round " + x); } return(ec_chunk_list); /* some junk to verify... * BinaryFormatter bin = new BinaryFormatter(); * FileStream fs = new FileStream("F:/LiveProjects/C#Rookie/EncodedChunk.0001.dat", FileMode.Open, FileAccess.ReadWrite); * ec = (EncodedChunk)bin.Deserialize(fs); * fs.Write(ec.encoded_data_part, 0, ec.encoded_data_part.Length); * fs.Close(); * fs = new FileStream("F:/LiveProjects/C#Rookie/EncodedChunk.mp3", FileMode.Create, FileAccess.Write); * fs.Write(ec.encoded_data_part, 0, (int)ec.encoded_data_part.Length); * fs.Close();*/ }