mirror of
https://github.com/divan/txqr.git
synced 2026-09-05 17:47:40 +08:00
Add redundancy factor to encoder
This commit is contained in:
@@ -9,13 +9,15 @@ import (
|
||||
|
||||
// Encoder represents protocol encoder.
|
||||
type Encoder struct {
|
||||
chunkLen int
|
||||
chunkLen int
|
||||
redundancyFactor float64
|
||||
}
|
||||
|
||||
// NewEncoder creates and inits a new encoder for the given chunk length.
|
||||
func NewEncoder(n int) *Encoder {
|
||||
return &Encoder{
|
||||
chunkLen: n,
|
||||
chunkLen: n,
|
||||
redundancyFactor: 1.5,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +32,7 @@ func (e *Encoder) Encode(str string) ([]string, error) {
|
||||
codec := fountain.NewLubyCodec(numChunks, rand.New(fountain.NewMersenneTwister(200)), solitonDistribution(numChunks))
|
||||
|
||||
var msg = []byte(str) // copy of str, as EncodeLTBlock is destructive to msg
|
||||
idsToEncode := ids(numChunks)
|
||||
idsToEncode := ids(int(float64(numChunks) * e.redundancyFactor))
|
||||
lubyBlocks := fountain.EncodeLTBlocks(msg, idsToEncode, codec)
|
||||
|
||||
// TODO(divan): use sync.Pool as this probably will be used many times
|
||||
@@ -41,6 +43,11 @@ func (e *Encoder) Encode(str string) ([]string, error) {
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// SetRedundancyFactor changes the value of redundancy factor.
|
||||
func (e *Encoder) SetRedundancyFactor(rf float64) {
|
||||
e.redundancyFactor = rf
|
||||
}
|
||||
|
||||
func (e *Encoder) frame(blockCode int64, total int, data []byte) string {
|
||||
return fmt.Sprintf("%d/%d/%d|%s", blockCode, e.chunkLen, total, string(data))
|
||||
}
|
||||
|
||||
+2
-3
@@ -22,9 +22,8 @@ func solitonDistribution(n int) []float64 {
|
||||
|
||||
// ids create slice with IDs for 0..n values.
|
||||
func ids(n int) []int64 {
|
||||
N := n + n/2
|
||||
ids := make([]int64, N)
|
||||
for i := int64(0); i < int64(N); i++ {
|
||||
ids := make([]int64, n)
|
||||
for i := int64(0); i < int64(n); i++ {
|
||||
ids[i] = i
|
||||
}
|
||||
return ids
|
||||
|
||||
Reference in New Issue
Block a user