aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2018-03-13 11:29:49 +0000
committerFelix Hanley <felix@userspace.com.au>2018-03-13 11:29:49 +0000
commit79c6a22d81e6ceadb23636e90e5d53f2a86e090d (patch)
tree33776c9bedc99ccd87c1c7061b6195500c05f54d /models
parenta67075e332458002ce4c56c0aa07e03807dc22ea (diff)
downloaddhtsearch-79c6a22d81e6ceadb23636e90e5d53f2a86e090d.tar.gz
dhtsearch-79c6a22d81e6ceadb23636e90e5d53f2a86e090d.tar.bz2
Fix tagging and announce callbacks
Diffstat (limited to 'models')
-rw-r--r--models/infohash.go5
-rw-r--r--models/infohash_test.go9
-rw-r--r--models/peer.go7
-rw-r--r--models/storage.go26
-rw-r--r--models/torrent.go8
5 files changed, 42 insertions, 13 deletions
diff --git a/models/infohash.go b/models/infohash.go
index e8db422..bb1bd81 100644
--- a/models/infohash.go
+++ b/models/infohash.go
@@ -37,6 +37,11 @@ func InfohashFromString(s string) (*Infohash, error) {
func (ih Infohash) String() string {
return hex.EncodeToString(ih)
}
+
+func (ih Infohash) Bytes() []byte {
+ return []byte(ih)
+}
+
func (ih Infohash) Valid() bool {
// TODO
return len(ih) == 20
diff --git a/models/infohash_test.go b/models/infohash_test.go
index e855e35..c1d9cca 100644
--- a/models/infohash_test.go
+++ b/models/infohash_test.go
@@ -30,6 +30,15 @@ func TestInfohashImport(t *testing.T) {
if !ih.Equal(ih2) {
t.Errorf("expected %s to equal %s", ih, ih2)
}
+ if ih.String() != tt.str {
+ t.Errorf("expected ih.String() to equal %s, got %s", tt.str, ih.String())
+ }
+ byt := ih.Bytes()
+ for i := range byt {
+ if byt[i] != []byte(tt.str)[i] {
+ t.Errorf("expected ih.Bytes() to equal %s, got %s", []byte(tt.str), ih.Bytes())
+ }
+ }
} else {
if err == nil {
t.Errorf("FromString should have failed for %s", tt.str)
diff --git a/models/peer.go b/models/peer.go
index 2feee03..220d979 100644
--- a/models/peer.go
+++ b/models/peer.go
@@ -3,12 +3,15 @@ package models
import (
"fmt"
"net"
+ "time"
)
// Peer on DHT network
type Peer struct {
- Addr net.Addr
- Infohash Infohash
+ Addr net.Addr `db:"address"`
+ Infohash Infohash `db:"infohash"`
+ Updated time.Time `json:"updated"`
+ Created time.Time `json:"created"`
}
// String implements fmt.Stringer
diff --git a/models/storage.go b/models/storage.go
index 84ac6aa..c8a8344 100644
--- a/models/storage.go
+++ b/models/storage.go
@@ -1,15 +1,27 @@
package models
+import ()
+
+type migratable interface {
+ MigrateSchema() error
+}
+
type torrentSearcher interface {
- torrentsByHash(hashes Infohash, offset, limit int) (*Torrent, error)
- torrentsByName(query string, offset, limit int) ([]*Torrent, error)
- torrentsByTags(tags []string, offset, limit int) ([]*Torrent, error)
+ TorrentsByHash(hash Infohash) (*Torrent, error)
+ TorrentsByName(query string, offset, limit int) ([]*Torrent, error)
+ TorrentsByTags(tags []string, offset, limit int) ([]*Torrent, error)
+}
+
+type PeerStore interface {
+ SavePeer(*Peer) error
}
-type peerStore interface {
- savePeer(*Peer) error
+type TorrentStore interface {
+ SaveTorrent(*Torrent) error
+ // TODO
+ RemovePeer(*Peer) error
}
-type torrentStore interface {
- saveTorrent(*Torrent) error
+type InfohashStore interface {
+ PendingInfohashes(int) ([]*Peer, error)
}
diff --git a/models/torrent.go b/models/torrent.go
index 4ff3143..6cae23c 100644
--- a/models/torrent.go
+++ b/models/torrent.go
@@ -3,7 +3,6 @@ package models
import (
"bytes"
"crypto/sha1"
- "encoding/hex"
"fmt"
"os"
"strings"
@@ -16,11 +15,12 @@ import (
// Data for persistent storage
type Torrent struct {
ID int `json:"-"`
- Infohash string `json:"infohash"`
+ Infohash Infohash `json:"infohash"`
Name string `json:"name"`
Files []File `json:"files" db:"-"`
Size int `json:"size"`
- Seen time.Time `json:"seen"`
+ Updated time.Time `json:"updated"`
+ Created time.Time `json:"created"`
Tags []string `json:"tags" db:"-"`
}
@@ -52,7 +52,7 @@ func TorrentFromMetadata(ih Infohash, md []byte) (*Torrent, error) {
}
bt := Torrent{
- Infohash: hex.EncodeToString([]byte(ih)),
+ Infohash: ih,
Name: name,
}