change reader state

This commit is contained in:
YouROK
2020-12-24 12:59:57 +03:00
parent a404545133
commit fcfa6f43ce
3 changed files with 34 additions and 74 deletions

View File

@@ -12,12 +12,18 @@ type CacheState struct {
PiecesCount int PiecesCount int
Torrent *state.TorrentStatus Torrent *state.TorrentStatus
Pieces map[int]ItemState Pieces map[int]ItemState
Readers []*ReaderState
} }
type ItemState struct { type ItemState struct {
Id int Id int
Length int64 Length int64
Size int64 Size int64
Completed bool Completed bool
ReaderType int }
type ReaderState struct {
Start int
End int
Reader int
} }

View File

@@ -114,88 +114,42 @@ func (c *Cache) AdjustRA(readahead int64) {
func (c *Cache) GetState() *state.CacheState { func (c *Cache) GetState() *state.CacheState {
cState := new(state.CacheState) cState := new(state.CacheState)
cState.Capacity = c.capacity
cState.PiecesLength = c.pieceLength
cState.PiecesCount = c.pieceCount
cState.Hash = c.hash.HexString()
stats := make(map[int]state.ItemState, 0) piecesState := make(map[int]state.ItemState, 0)
var fill int64 = 0 var fill int64 = 0
for _, p := range c.pieces { for _, p := range c.pieces {
if p.Size > 0 { if p.Size > 0 {
fill += p.Length fill += p.Length
stats[p.Id] = state.ItemState{ piecesState[p.Id] = state.ItemState{
Id: p.Id, Id: p.Id,
Size: p.Size, Size: p.Size,
Length: p.Length, Length: p.Length,
Completed: p.complete, Completed: p.complete,
ReaderType: 0,
} }
} }
} }
readersState := make([]*state.ReaderState, 0)
c.muReaders.Lock() c.muReaders.Lock()
for r, _ := range c.readers { for r, _ := range c.readers {
rrange := r.getPiecesRange() rng := r.getPiecesRange()
if p, ok := c.pieces[rrange.Start]; ok { pc := r.getReaderPiece()
stats[rrange.Start] = state.ItemState{ readersState = append(readersState, &state.ReaderState{
Id: p.Id, Start: rng.Start,
Size: p.Size, End: rng.End,
Length: p.Length, Reader: pc,
Completed: p.complete, })
ReaderType: 1,
}
} else {
stats[rrange.Start] = state.ItemState{
Id: rrange.Start,
Size: 0,
Length: c.pieceLength,
Completed: false,
ReaderType: 1,
}
}
if p, ok := c.pieces[rrange.End]; ok {
stats[rrange.End] = state.ItemState{
Id: p.Id,
Size: p.Size,
Length: p.Length,
Completed: p.complete,
ReaderType: 2,
}
} else {
stats[rrange.End] = state.ItemState{
Id: rrange.End,
Size: 0,
Length: c.pieceLength,
Completed: false,
ReaderType: 2,
}
}
reader := r.getReaderPiece()
if p, ok := c.pieces[reader]; ok {
stats[reader] = state.ItemState{
Id: p.Id,
Size: p.Size,
Length: p.Length,
Completed: p.complete,
ReaderType: 3,
}
} else {
stats[reader] = state.ItemState{
Id: reader,
Size: 0,
Length: c.pieceLength,
Completed: false,
ReaderType: 3,
}
}
} }
c.muReaders.Unlock() c.muReaders.Unlock()
c.filled = fill c.filled = fill
cState.Filled = c.filled cState.Capacity = c.capacity
cState.Pieces = stats cState.PiecesLength = c.pieceLength
cState.PiecesCount = c.pieceCount
cState.Hash = c.hash.HexString()
cState.Filled = fill
cState.Pieces = piecesState
cState.Readers = readersState
return cState return cState
} }

File diff suppressed because one or more lines are too long