creating...

This commit is contained in:
YouROK
2020-11-09 16:45:06 +03:00
parent 0b09da2490
commit 1d4acc1406
12 changed files with 710 additions and 55 deletions

View File

@@ -99,6 +99,21 @@ func (bt *BTServer) configure() {
log.Println("Configure client:", settings.BTsets)
}
func (bt *BTServer) GetTorrent(hash torrent.InfoHash) *Torrent {
if torr, ok := bt.torrents[hash]; ok {
return torr
}
return nil
}
func (bt *BTServer) ListTorrents() []*Torrent {
var list []*Torrent
for _, t := range bt.torrents {
list = append(list, t)
}
return list
}
func (bt *BTServer) RemoveTorrent(hash torrent.InfoHash) {
if torr, ok := bt.torrents[hash]; ok {
torr.Close()

View File

@@ -33,46 +33,46 @@ func (bt *BTServer) BTState() *BTState {
}
type TorrentStats struct {
Name string
Hash string
Name string `json:"name,omitempty"`
Hash string `json:"hash,omitempty"`
TorrentStatus TorrentStatus
TorrentStatusString string
TorrentStatus TorrentStatus `json:"torrent_status,omitempty"`
TorrentStatusString string `json:"torrent_status_string,omitempty"`
LoadedSize int64
TorrentSize int64
LoadedSize int64 `json:"loaded_size,omitempty"`
TorrentSize int64 `json:"torrent_size,omitempty"`
PreloadedBytes int64
PreloadSize int64
PreloadedBytes int64 `json:"preloaded_bytes,omitempty"`
PreloadSize int64 `json:"preload_size,omitempty"`
DownloadSpeed float64
UploadSpeed float64
DownloadSpeed float64 `json:"download_speed,omitempty"`
UploadSpeed float64 `json:"upload_speed,omitempty"`
TotalPeers int
PendingPeers int
ActivePeers int
ConnectedSeeders int
HalfOpenPeers int
TotalPeers int `json:"total_peers,omitempty"`
PendingPeers int `json:"pending_peers,omitempty"`
ActivePeers int `json:"active_peers,omitempty"`
ConnectedSeeders int `json:"connected_seeders,omitempty"`
HalfOpenPeers int `json:"half_open_peers,omitempty"`
BytesWritten int64
BytesWrittenData int64
BytesRead int64
BytesReadData int64
BytesReadUsefulData int64
ChunksWritten int64
ChunksRead int64
ChunksReadUseful int64
ChunksReadWasted int64
PiecesDirtiedGood int64
PiecesDirtiedBad int64
BytesWritten int64 `json:"bytes_written,omitempty"`
BytesWrittenData int64 `json:"bytes_written_data,omitempty"`
BytesRead int64 `json:"bytes_read,omitempty"`
BytesReadData int64 `json:"bytes_read_data,omitempty"`
BytesReadUsefulData int64 `json:"bytes_read_useful_data,omitempty"`
ChunksWritten int64 `json:"chunks_written,omitempty"`
ChunksRead int64 `json:"chunks_read,omitempty"`
ChunksReadUseful int64 `json:"chunks_read_useful,omitempty"`
ChunksReadWasted int64 `json:"chunks_read_wasted,omitempty"`
PiecesDirtiedGood int64 `json:"pieces_dirtied_good,omitempty"`
PiecesDirtiedBad int64 `json:"pieces_dirtied_bad,omitempty"`
FileStats []TorrentFileStat
FileStats []TorrentFileStat `json:"file_stats,omitempty"`
}
type TorrentFileStat struct {
Id int
Path string
Length int64
Id int `json:"id,omitempty"`
Path string `json:"path,omitempty"`
Length int64 `json:"length,omitempty"`
}
func (t *Torrent) Stats() TorrentStats {
@@ -83,8 +83,8 @@ func (t *Torrent) Stats() TorrentStats {
st.Name = t.Name()
st.Hash = t.hash.HexString()
st.TorrentStatus = t.status
st.TorrentStatusString = t.status.String()
st.TorrentStatus = t.Status
st.TorrentStatusString = t.Status.String()
if t.Torrent != nil {
st.LoadedSize = t.Torrent.BytesCompleted()

View File

@@ -30,6 +30,8 @@ func (t TorrentStatus) String() string {
return "Torrent working"
case TorrentClosed:
return "Torrent closed"
case TorrentInDB:
return "Torrent in db"
default:
return "Torrent unknown status"
}
@@ -41,13 +43,19 @@ const (
TorrentPreload
TorrentWorking
TorrentClosed
TorrentInDB
)
type Torrent struct {
///// info for db
Title string
Poster string
*torrent.TorrentSpec
Status TorrentStatus
/////
*torrent.Torrent
status TorrentStatus
muTorrent sync.Mutex
bt *BTServer
@@ -95,11 +103,11 @@ func NewTorrent(spec *torrent.TorrentSpec, bt *BTServer) (*Torrent, error) {
torr := new(Torrent)
torr.Torrent = goTorrent
torr.status = TorrentAdded
torr.Status = TorrentAdded
torr.lastTimeSpeed = time.Now()
torr.bt = bt
torr.hash = spec.InfoHash
torr.closed = goTorrent.Closed()
torr.TorrentSpec = spec
go torr.watch()
@@ -127,12 +135,12 @@ func (t *Torrent) WaitInfo() bool {
}
func (t *Torrent) GotInfo() bool {
if t.status == TorrentClosed {
if t.Status == TorrentClosed {
return false
}
t.status = TorrentGettingInfo
t.Status = TorrentGettingInfo
if t.WaitInfo() {
t.status = TorrentWorking
t.Status = TorrentWorking
t.expiredTime = time.Now().Add(time.Minute * 5)
return true
} else {
@@ -199,7 +207,7 @@ func (t *Torrent) updateRA() {
}
func (t *Torrent) expired() bool {
return t.cache.ReadersLen() == 0 && t.expiredTime.Before(time.Now()) && (t.status == TorrentWorking || t.status == TorrentClosed)
return t.cache.ReadersLen() == 0 && t.expiredTime.Before(time.Now()) && (t.Status == TorrentWorking || t.Status == TorrentClosed)
}
func (t *Torrent) Files() []*torrent.File {
@@ -214,10 +222,6 @@ func (t *Torrent) Hash() metainfo.Hash {
return t.hash
}
func (t *Torrent) Status() TorrentStatus {
return t.status
}
func (t *Torrent) Length() int64 {
if t.Info() == nil {
return 0
@@ -226,7 +230,7 @@ func (t *Torrent) Length() int64 {
}
func (t *Torrent) NewReader(file *torrent.File, readahead int64) *Reader {
if t.status == TorrentClosed {
if t.Status == TorrentClosed {
return nil
}
reader := NewReader(t, file, readahead)
@@ -243,19 +247,19 @@ func (t *Torrent) GetCache() *torrstor.Cache {
return t.cache
}
func (t *Torrent) Preload(file *torrent.File, size int64) {
func (t *Torrent) Preload(index int, size int64) {
if size < 0 {
return
}
if t.status == TorrentGettingInfo {
if t.Status == TorrentGettingInfo {
t.WaitInfo()
// wait change status
time.Sleep(100 * time.Millisecond)
}
t.muTorrent.Lock()
if t.status != TorrentWorking {
if t.Status != TorrentWorking {
t.muTorrent.Unlock()
return
}
@@ -267,15 +271,20 @@ func (t *Torrent) Preload(file *torrent.File, size int64) {
t.muTorrent.Unlock()
return
}
t.status = TorrentPreload
t.Status = TorrentPreload
t.muTorrent.Unlock()
defer func() {
if t.status == TorrentPreload {
t.status = TorrentWorking
if t.Status == TorrentPreload {
t.Status = TorrentWorking
}
}()
if index < 0 || index >= len(t.Files()) {
index = 0
}
file := t.Files()[index]
buff5mb := int64(5 * 1024 * 1024)
startPreloadLength := size
endPreloadOffset := int64(0)
@@ -312,7 +321,7 @@ func (t *Torrent) Preload(file *torrent.File, size int64) {
t.PreloadSize = size
var lastSize int64 = 0
errCount := 0
for t.status == TorrentPreload {
for t.Status == TorrentPreload {
t.expiredTime = time.Now().Add(time.Minute * 5)
t.PreloadedBytes = t.Torrent.BytesCompleted()
log.Println("Preload:", file.Torrent().InfoHash().HexString(), utils2.Format(float64(t.PreloadedBytes)), "/", utils2.Format(float64(t.PreloadSize)), "Speed:", utils2.Format(t.DownloadSpeed), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
@@ -343,7 +352,7 @@ func (t *Torrent) drop() {
}
func (t *Torrent) Close() {
t.status = TorrentClosed
t.Status = TorrentClosed
t.bt.mu.Lock()
defer t.bt.mu.Unlock()