]> Sergey Matveev's repositories - btrtrc.git/blobdiff - cmd/btrtrc/fifos.go
Sort torrents by add time
[btrtrc.git] / cmd / btrtrc / fifos.go
index 28a6ed0430b47b9717aa8916aea0924386a98b9b..f9aa0117d4d32ba9a8099c09fef6348686f7a100 100644 (file)
@@ -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)