diff --git a/KodoTest/KodoTest.sdf b/KodoTest/KodoTest.sdf index 2ae811d6a39c71f6dd334b62816184f936519195..70a00d747b5d587a70471d80222107b7322003c4 100644 Binary files a/KodoTest/KodoTest.sdf and b/KodoTest/KodoTest.sdf differ diff --git a/KodoTest/KodoTest.v12.suo b/KodoTest/KodoTest.v12.suo index 4f4fe616e9de58f1ae4850ae54e570068ef6b44b..04293f6e266bbf2f4d01717c3f97f9789007f1a0 100644 Binary files a/KodoTest/KodoTest.v12.suo and b/KodoTest/KodoTest.v12.suo differ diff --git a/KodoTest/KodoTest/Source.cpp b/KodoTest/KodoTest/Source.cpp index d9269310f3b1c0b05c5aa8aa68beb1479f63e9da..c9f39f061ebcb75624958cfd2d2d25f38b8fcf1d 100644 --- a/KodoTest/KodoTest/Source.cpp +++ b/KodoTest/KodoTest/Source.cpp @@ -1,15 +1,7 @@ -// Copyright Steinwurf ApS 2011. -// Distributed under the "STEINWURF RESEARCH LICENSE 1.0". -// See accompanying file LICENSE.rst or -// http://www.steinwurf.com/licensing - - -//! [0] #include <kodo/object/file_encoder.hpp> #include <kodo/object/file_decoder.hpp> #include <kodo/rlnc/full_rlnc_codes.hpp> -//! [1] #include <cassert> #include <cstdint> @@ -17,16 +9,7 @@ #include <iostream> #include <vector> -/// @example encode_decode_file.cpp -/// -/// Often we want to encode / decode data that exceed a single -/// encoding/decoding block. In this case we need to "chop" up -/// the data into manageable chunks and then encode and decode -/// each chuck separately. This example shows how to use the -/// file encoder in Kodo. The file encoder operates directly on -/// a file in the file-system. For decoding we use an object decoder -/// which decodes data to memory, but which is compatible with -/// file encoder. +#include <boost/format.hpp> #define BUFFER_SIZE 1000 @@ -42,6 +25,7 @@ bool isFilesEqual(const std::string& lFilePath, const std::string& rFilePath) char *lBuffer = new char[BUFFER_SIZE](); char *rBuffer = new char[BUFFER_SIZE](); + bool isEqual = true; do { lFile.read(lBuffer, BUFFER_SIZE); @@ -49,95 +33,102 @@ bool isFilesEqual(const std::string& lFilePath, const std::string& rFilePath) if (std::memcmp(lBuffer, rBuffer, BUFFER_SIZE) != 0) { - delete[] lBuffer; - delete[] rBuffer; - return false; + isEqual = false; } - } while (lFile.good() || rFile.good()); + } while (isEqual && (lFile.good() || rFile.good())); delete[] lBuffer; delete[] rBuffer; - return true; + return isEqual; +} + +std::ifstream::pos_type filesize(std::string filename) +{ + std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary); + return in.tellg(); } int main() { - //! [2] - // Set the number of symbols (i.e. the generation size in RLNC - // terminology) and the size of a symbol in bytes - uint32_t max_symbols = 42; - uint32_t max_symbol_size = 64; + uint32_t max_symbols = 10; + uint32_t max_symbol_size = 5000; - uint32_t file_size = 23456; + std::string encode_filename; - std::string encode_filename = "encode-file.bin"; - std::string decode_filename = "decode-file.bin"; + std::cout << "File to encode:" << std::endl; + std::cin >> encode_filename; + + uint32_t file_size = filesize(encode_filename); using file_encoder = kodo::object::file_encoder< kodo::shallow_full_rlnc_encoder<fifi::binary >> ; - using file_decoder = kodo::object::file_decoder< - kodo::shallow_full_rlnc_decoder<fifi::binary >> ; - //! [3] + file_encoder::factory encoder_factory(max_symbols, max_symbol_size); - //! [4] - // Create a test file for encoding. - std::ofstream encode_file; + encoder_factory.set_filename(encode_filename); - encode_file.open(encode_filename, std::ios::binary); - std::vector<char> data_in(file_size, 'x'); + auto encoder = encoder_factory.build(); - encode_file.write(data_in.data(), data_in.size()); - encode_file.close(); - //! [5] + std::cout << "encoder blocks = " << encoder->blocks() << std::endl; - //! [6] - // Actual encoding/decoding of the file - file_encoder::factory encoder_factory(max_symbols, max_symbol_size); - file_decoder::factory decoder_factory(max_symbols, max_symbol_size); + for (uint32_t i = 0; i < encoder->blocks(); ++i) + { + auto e = encoder->build(i); - encoder_factory.set_filename(encode_filename); + std::vector<uint8_t> payload(e->payload_size()); + + for (uint32_t j = 0; j < max_symbols; j++) + { + e->encode(payload.data()); + + std::ofstream fragFile( + (boost::format("frags/frag_%1$02d_%2$02d.frg") % i % j).str(), + std::ios::out | std::ios::binary); + std::copy(payload.begin(), payload.end(), std::ostreambuf_iterator<char>(fragFile)); + fragFile.close(); + } + } + + + std::string decode_filename = "decoded_" + encode_filename; + + using file_decoder = kodo::object::file_decoder< + kodo::shallow_full_rlnc_decoder<fifi::binary >> ; + + file_decoder::factory decoder_factory(max_symbols, max_symbol_size); decoder_factory.set_filename(decode_filename); decoder_factory.set_file_size(file_size); - auto encoder = encoder_factory.build(); auto decoder = decoder_factory.build(); - std::cout << "encoder blocks = " << encoder->blocks() << std::endl; std::cout << "decoder blocks = " << decoder->blocks() << std::endl; - //! [7] - //! [8] - for (uint32_t i = 0; i < encoder->blocks(); ++i) + for (uint32_t i = 0; i < decoder->blocks(); i++) { - // If you want to store the encoder/decoder built. You should use - // the type available as `stack_pointer` in the - // file_encoder/file_decoder types. - // - // file_encoder::stack_pointer e = encoder->build(i); - // file_decoder::stack_pointer d = decoder->build(i); - - auto e = encoder->build(i); auto d = decoder->build(i); - std::vector<uint8_t> payload(e->payload_size()); + std::vector<uint8_t> payload; + uint32_t fragNum = 0; while (!d->is_complete()) { - e->encode(payload.data()); + std::string fileName = (boost::format("frags/frag_%1$02d_%2$02d.frg") % i % fragNum).str(); + std::ifstream fragFile( + fileName, + std::ios::in | std::ios::binary); + std::copy(std::istreambuf_iterator<char>(fragFile), + std::istreambuf_iterator<char>(), + std::back_inserter(payload)); + fragFile.close(); - // Here we would send and receive the payload over a - // network. Lets throw away some packet to simulate. - if (rand() % 2) - { - continue; - } + payload.shrink_to_fit(); d->decode(payload.data()); + fragNum++; + payload.clear(); } } - //! [9] - std::cout << isFilesEqual(encode_filename, decode_filename); + std::cout << "Is this shit working: " << (isFilesEqual(encode_filename, decode_filename) ? "true" : "false"); } \ No newline at end of file diff --git a/KodoTest/KodoTest/test.jpg b/KodoTest/KodoTest/test.jpg new file mode 100644 index 0000000000000000000000000000000000000000..f2fcd75f77ddd25c681506d1ab6b2b56bbc51d08 Binary files /dev/null and b/KodoTest/KodoTest/test.jpg differ