-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eugene Kim
committed
Nov 20, 2018
1 parent
ff36db6
commit 719f9e0
Showing
2 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters