diff --git a/encode.go b/encode.go index e8ab77f..bded1eb 100644 --- a/encode.go +++ b/encode.go @@ -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)) } diff --git a/fountain.go b/fountain.go index 6e838e3..d4e4f45 100644 --- a/fountain.go +++ b/fountain.go @@ -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