Add redundancy factor to encoder

This commit is contained in:
Ivan Danyliuk
2018-11-29 22:24:10 +02:00
parent c1b3a00f5f
commit dcec889bb8
2 changed files with 12 additions and 6 deletions
+10 -3
View File
@@ -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
View File
@@ -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