From: Sergey Matveev Date: Tue, 29 Nov 2022 09:41:13 +0000 (+0300) Subject: Sort torrents by add time X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=a06936571c411832ab6067c6f036e18ba6b62e89;p=btrtrc.git Sort torrents by add time --- diff --git a/cmd/btrtrc/fifos.go b/cmd/btrtrc/fifos.go index 28a6ed04..f9aa0117 100644 --- a/cmd/btrtrc/fifos.go +++ b/cmd/btrtrc/fifos.go @@ -38,6 +38,8 @@ type TorrentStat struct { var ( TorrentStats = map[metainfo.Hash]TorrentStat{} TorrentStatsM sync.RWMutex + Torrents []metainfo.Hash + TorrentsM sync.RWMutex ) func recreateFIFO(pth string) { @@ -65,13 +67,18 @@ func fifoList(c *torrent.Client) { time.Sleep(time.Second) continue } - ts := c.Torrents() - sort.Sort(ByInfoHash(ts)) - for _, t := range ts { - if t.Info() == nil { + ts := make([]*torrent.Torrent, 0, len(Torrents)) + TorrentsM.RLock() + for _, h := range Torrents { + t, _ := c.Torrent(h) + if t == nil || t.Info() == nil { fmt.Fprintf(fd, "%s not ready\n", t.Name()) continue } + ts = append(ts, t) + } + TorrentsM.RUnlock() + for _, t := range ts { stats := t.Stats() done := t.BytesCompleted() * 100 / t.Length() percColour := Red @@ -336,6 +343,15 @@ func fifoAdd(c *torrent.Client) { if len(cols) > 1 { t.AddPeers(resolveTestPeers(cols[1:])) } + TorrentsM.Lock() + for _, h := range Torrents { + if h.HexString() == t.InfoHash().HexString() { + goto OldOne + } + } + Torrents = append(Torrents, t.InfoHash()) + OldOne: + TorrentsM.Unlock() go fifoPeerList(t) go fifoFileList(t) log.Println("added:", t.InfoHash().HexString(), t.Name()) @@ -368,9 +384,17 @@ func fifoDel(c *torrent.Client) { } var i infohash.T copy(i[:], raw) + TorrentsM.Lock() + for n, h := range Torrents { + if h.HexString() == i.HexString() { + Torrents = append(Torrents[:n], Torrents[n+1:]...) + break + } + } + TorrentsM.Unlock() t, ok := c.Torrent(i) if !ok { - log.Println("no suck torrent", what) + log.Println("no such torrent", what) continue } txStatsDump(t) diff --git a/cmd/btrtrc/sort.go b/cmd/btrtrc/sort.go index 5b1ad858..3837d1f3 100644 --- a/cmd/btrtrc/sort.go +++ b/cmd/btrtrc/sort.go @@ -31,6 +31,9 @@ func (a ByTxTraffic) Swap(i, j int) { } func (a ByTxTraffic) Less(i, j int) bool { + if a[i].tx == a[j].tx { + return a[i].infoHash.HexString() < a[j].infoHash.HexString() + } return a[i].tx < a[j].tx }