MatrixDecEncoder::FnBitMap MatrixDecEncoder::operator()(uint8_t x, uint8_t y)
{
    return [x, y, this]() -> std::vector<uint8_t>
    {
        std::vector<uint8_t> digits;
        EncoderType encodedDigit;

        encodedDigit = _encoder(x);
        digits.insert(digits.end(), encodedDigit.begin(), encodedDigit.end());
        digits.push_back(0);

        encodedDigit = _encoder(x / 10);
        digits.insert(digits.end(), encodedDigit.begin(), encodedDigit.end());
        digits.push_back(0);

        encodedDigit = _encoder(y);
        digits.push_back(0);
        digits.insert(digits.end(), encodedDigit.begin(), encodedDigit.end());

        encodedDigit = _encoder(y / 10);
        digits.push_back(0);
        digits.insert(digits.end(), encodedDigit.begin(), encodedDigit.end());

        return digits;
    };
}
DecEncoder::FnBitMap DecEncoder::operator()(uint8_t x, uint8_t y)
{
    return [x, y, this]() -> std::vector<uint8_t>
    {
        std::vector<uint8_t> digits(4);

        digits[3] = _encoder( x      );
        digits[2] = _encoder( x / 10 );
        digits[1] = _encoder( y      );
        digits[0] = _encoder( y / 10 );

        return digits;
    };
}
DecEncoder::FnBitMap DecEncoder::operator()(uint16_t number)
{
    return [number, this]() -> std::vector<uint8_t>
    {
        std::vector<uint8_t> digits(4);

        digits[3] = _encoder( number        );
        digits[2] = _encoder( number / 10   );
        digits[1] = _encoder( number / 100  );
        digits[0] = _encoder( number / 1000 );

        return digits;
    };
}
示例#4
0
文件: episode.cpp 项目: ctguxp/rlcpp
void Episode::encodedState(unsigned int t, std::vector<float> &rs) const
{
    state(t, rs);

    // Encode the state using the encoder
    if (_encoder) {
        _encoder(rs);
    }
}
示例#5
0
std::size_t HexBinaryEncoding::encode(const ByteBuffer& buffer,
                                      ByteBuffer& encodedBuffer)
{
    encodedBuffer.clear();
    ByteBufferOutputStream os(encodedBuffer);
    Poco::HexBinaryEncoder _encoder(os);
    ByteBufferUtils::copyBufferToStream(buffer, _encoder);
    _encoder.close(); // Flush bytes.
    return encodedBuffer.size();
}
MatrixHexEncoder::FnBitMap MatrixHexEncoder::operator()(uint16_t number)
{
    return [number, this]() -> std::vector<uint8_t>
    {
        std::vector<uint8_t> digits;

        auto fnEncodeDigit = [&digits, this](uint8_t n)
        {
            EncoderType encodedDigit(_encoder(n));
            digits.push_back(0);
            digits.insert(digits.end(), encodedDigit.begin(), encodedDigit.end());
        };

        fnEncodeDigit(number);
        fnEncodeDigit(number >> 4);
        fnEncodeDigit(number >> 8);
        fnEncodeDigit(number >> 12);

        return digits;
    };
}
示例#7
0
NetworkCoding_t NetworkCoding::createCodedBlockFromObject(std::string parentObjectName,
		unsigned targetNodeId,long parentObjectFileSize) {

	//check if we already started encoding for this object
	if( this->encoder.find(parentObjectName) == this->encoder.end() ) {
		//create encoder
		printf("creating encoder for parentObjectName=%s\n",parentObjectName.c_str());
		std::string randomFileName = "input-"+gen_random_string(256);;
		size_t blockSize= OBJECT_BLOCK_SIZE;
		gen_random(randomFileName,parentObjectFileSize);
		encoderptr _encoder( new codetorrentencoder(randomFileName.c_str(),blockSize,parentObjectFileSize));
		this->encoder[parentObjectName] = _encoder;
	}

	encoderptr localEncoder = this->encoder[parentObjectName];

	bool isMixingEnabled = false;

	const bool is_in = this->networkcodingobjectsreceived.find(parentObjectName) != this->networkcodingobjectsreceived.end();
	const bool full_cache_code = is_in && m_ncOption==nc_fullobjectonly;
	const bool mixing_full_cache_code = is_in && m_ncOption == nc_mixing;
	const bool is_in_registry = this->objectpublisherregistry.find(parentObjectName) != this->objectpublisherregistry.end();

	//full cache encoding
	if(full_cache_code || mixing_full_cache_code ) {
		//check if ok to send
		printf("full object encoding nodeid=%d\n",this->thisNodeId);
		int blockId = rand() % MAXID;
		NetworkCoding_t networkCoding(parentObjectName, parentObjectFileSize, targetNodeId,blockId,false);

		CodedBlockPtr block = localEncoder->encode();
		block->id = blockId;
		std::string blockStorageId = networkCoding.getName();
		codedblockptr_t _codedblockptr(block,freeCodedBlock);

		writeFile(networkCoding.getBlockIdOnly(),block);
		//boost::lock_guard<boost::mutex> guard(codedblockstoragemutex);
		//this->codedblockstorage[blockStorageId] = _codedblockptr;

		m_stat.totalBlockGenerated++;

		trackCodedBlock(parentObjectName,networkCoding);

		printf("full encoding nodeid=%d name=%s\n",this->thisNodeId,networkCoding.getName().c_str());
		return networkCoding;
	}
	//mixing
	else if(m_ncOption == nc_mixing){
		printf("mixing nodeid=%d\n",this->thisNodeId);

        const bool is_in_rejected = this->rejectedobject.find(parentObjectName) != this->rejectedobject.end();
        //PrintTime(NULL);
        //TODO
        if(is_in_rejected && !is_in ) {
        	bool isStopSend = this->rejectedobject[parentObjectName];
        	if(isStopSend) {
        		printf("reject stopsend=%s\n",parentObjectName.c_str());
    			NetworkCoding_t none;
    			return none;
        	}
        }


		//check if started receiving blocks and have blocks to mix
		if( this->decoder.find(parentObjectName) == this->decoder.end() ) {
			NetworkCoding_t none;
			return none;
		}
		decoderptr localDecoder = this->decoder[parentObjectName];
		CodedBlockPtr blockMix = localDecoder->mix();
		int blockId = rand() % MAXID;
		blockMix->id = blockId;
		NetworkCoding_t networkCoding(parentObjectName, parentObjectFileSize, targetNodeId,blockId,true);


		std::string blockStorageId = networkCoding.getName();
		codedblockptr_t _codedblockptr(blockMix,freeCodedBlock);

		writeFile(networkCoding.getBlockIdOnly(),blockMix);

		//boost::lock_guard<boost::mutex> guard(codedblockstoragemutex);
		//this->codedblockstorage[blockStorageId] = _codedblockptr;

		m_stat.totalBlockGenerated++;

		trackCodedBlock(parentObjectName,networkCoding);

		printf("mixing nodeid=%d name=%s\n",this->thisNodeId,networkCoding.getName().c_str());

		return networkCoding;
	}
	//forward random block not in target's bloomfilter
	else if(m_ncOption == nc_sourceonly && is_in && is_in_registry){
		printf("source only coding nodeid=%d\n",this->thisNodeId);

		//can do source coding
		printf("generaring source block for parentObjectName=%s\n",parentObjectName.c_str());
		int blockId = rand() % MAXID;
		NetworkCoding_t networkCoding(parentObjectName, parentObjectFileSize, targetNodeId,blockId,false);

		CodedBlockPtr block = localEncoder->encode();
		block->print();
		std::string blockStorageId = networkCoding.getName();
		codedblockptr_t _codedblockptr(block,freeCodedBlock);

		writeFile(networkCoding.getBlockIdOnly(),block);

		//boost::lock_guard<boost::mutex> guard(codedblockstoragemutex);
		//this->codedblockstorage[blockStorageId] = _codedblockptr;

		m_stat.totalBlockGenerated++;

		trackCodedBlock(parentObjectName,networkCoding);

		printf("source only coding nodeid=%d name=%s\n",this->thisNodeId,networkCoding.getName().c_str());

		return networkCoding;
	}
	else{

		NetworkCoding_t networkCoding = this->getBlockToSendTarget(parentObjectName,targetNodeId,parentObjectFileSize);
        networkCoding.targetNodeId = targetNodeId;

        printf("forward random  nodeid=%d block=%s\n",this->thisNodeId,networkCoding.getName().c_str());
		return networkCoding;
	}
}