Skip to content

Commit

Permalink
Add redundancy factor to encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
divan committed Nov 29, 2018
1 parent c1b3a00 commit dcec889
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 10 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
Expand All @@ -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))
}
Expand Down
5 changes: 2 additions & 3 deletions fountain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dcec889

Please sign in to comment.