Skip to content

Commit

Permalink
Implement decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Kim committed Nov 20, 2018
1 parent ff36db6 commit 719f9e0
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
114 changes: 114 additions & 0 deletions pkg/impl/libraptorq/decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package libraptorq

import (
"errors"
"github.com/harmony-one/go-raptorq/pkg/impl/libraptorq/swig"
"github.com/harmony-one/go-raptorq/pkg/raptorq"
"runtime"
)

type DecoderFactory struct {
}

func (*DecoderFactory) New(commonOTI uint64, schemeSpecificOTI uint32) (
decoder raptorq.Decoder, err error) {
wrapped := swig.NewBytesDecoder(swig.HostToNet64(commonOTI),
swig.HostToNet32(uint(schemeSpecificOTI)))
if wrapped.Initialized() {
decoder = &Decoder{wrapped, commonOTI, schemeSpecificOTI}
runtime.SetFinalizer(decoder, finalizeDecoder)
} else {
swig.DeleteBytesDecoder(wrapped)
err = errors.New("libRaptorQ decoder failed to initialize")
}
return
}

func finalizeDecoder(decoder *Decoder) {
decoder.Close()
}

type Decoder struct {
wrapped swig.BytesDecoder
commonOTI uint64
schemeSpecificOTI uint32
}

func (dec *Decoder) CommonOTI() uint64 {
return dec.commonOTI
}

func (dec *Decoder) TransferLength() uint64 {
return dec.commonOTI >> 24
}

func (dec *Decoder) SymbolSize() uint16 {
return dec.wrapped.Symbol_size()
}

func (dec *Decoder) SchemeSpecificOTI() uint32 {
return dec.schemeSpecificOTI
}

func (dec *Decoder) NumSourceBlocks() uint8 {
return dec.wrapped.Blocks()
}

func (dec *Decoder) SourceBlockSize(sbn uint8) uint32 {
return uint32(dec.wrapped.Block_size(sbn))
}

func (dec *Decoder) NumSourceSymbols(sbn uint8) uint16 {
return dec.wrapped.Symbols(sbn)
}

func (dec *Decoder) NumSubBlocks() uint16 {
return uint16(dec.schemeSpecificOTI >> 8)
}

func (dec *Decoder) SymbolAlignmentParameter() uint8 {
return uint8(dec.schemeSpecificOTI)
}

func (dec *Decoder) Decode(sbn uint8, esi uint32, symbol []byte) {
dec.wrapped.Add_symbol(symbol, esi, sbn)
}

func (dec *Decoder) IsSourceBlockReady(sbn uint8) bool {
return dec.wrapped.Is_block_ready(sbn)
}

func (dec *Decoder) IsSourceObjectReady() bool {
return dec.wrapped.Is_ready()
}

func (dec *Decoder) SourceBlock(sbn uint8, buf []byte) (n int, err error) {
n = int(dec.wrapped.Decode_block_bytes(buf, 0, sbn))
if n != int(dec.SourceBlockSize(sbn)) {
err = errors.New("decode failure")
}
return
}

func (dec *Decoder) SourceObject(buf []byte) (n int, err error) {
n = int(dec.wrapped.Decode_bytes(buf, 0))
if n != int(dec.TransferLength()) {
err = errors.New("decode failure")
}
return
}

func (dec *Decoder) FreeSourceBlock(sbn uint8) {
dec.wrapped.Free(sbn)
}

func (dec *Decoder) Close() (err error) {
switch wrapped := dec.wrapped.(type) {
case swig.BytesDecoder:
swig.DeleteBytesDecoder(wrapped)
dec.wrapped = nil
default:
err = errors.New("RaptorQ decoder already closed")
}
return
}
8 changes: 6 additions & 2 deletions pkg/impl/libraptorq/swig/swig.swigcxx
Original file line number Diff line number Diff line change
Expand Up @@ -731,13 +731,17 @@ size_t get_local_cache_size();
namespace Impl {
namespace Endian {

template <typename T>
constexpr T b_to_h(T const b);
template <typename T> constexpr T b_to_h(T const b);
template <typename T> constexpr T h_to_b(T const h);

%template(NetToHost16) b_to_h<uint16_t>;
%template(NetToHost32) b_to_h<uint32_t>;
%template(NetToHost64) b_to_h<uint64_t>;

%template(HostToNet16) h_to_b<uint16_t>;
%template(HostToNet32) h_to_b<uint32_t>;
%template(HostToNet64) h_to_b<uint64_t>;

} // namespace Endian
} // namespace Impl
} // namespace RaptorQ__v1
Expand Down

0 comments on commit 719f9e0

Please sign in to comment.