diff --git a/client_test.go b/client_test.go index 6182cf3..68d1b29 100644 --- a/client_test.go +++ b/client_test.go @@ -15,7 +15,7 @@ func TestClientRegistrationAndDiscovery(t *testing.T) { hosts := getRendezvousHosts(t, ctx, 5) - svc, err := NewRendezvousService(ctx, hosts[0], ":memory:") + svc, err := makeRendezvousService(ctx, hosts[0], ":memory:") if err != nil { t.Fatal(err) } @@ -74,7 +74,7 @@ func TestClientRegistrationAndDiscoveryAsync(t *testing.T) { hosts := getRendezvousHosts(t, ctx, 5) - svc, err := NewRendezvousService(ctx, hosts[0], ":memory:") + svc, err := makeRendezvousService(ctx, hosts[0], ":memory:") if err != nil { t.Fatal(err) } diff --git a/db/dbi.go b/db/dbi.go new file mode 100644 index 0000000..b6fe8f6 --- /dev/null +++ b/db/dbi.go @@ -0,0 +1,21 @@ +package dbi + +import ( + peer "github.com/libp2p/go-libp2p-peer" +) + +type RegistrationRecord struct { + Id peer.ID + Addrs [][]byte + Ns string + Ttl int +} + +type DB interface { + Close() error + Register(p peer.ID, ns string, addrs [][]byte, ttl int) error + Unregister(p peer.ID, ns string) error + CountRegistrations(p peer.ID) (int, error) + Discover(ns string, cookie []byte, limit int) ([]RegistrationRecord, []byte, error) + ValidCookie(ns string, cookie []byte) bool +} diff --git a/db.go b/db/sqlite/db.go similarity index 96% rename from db.go rename to db/sqlite/db.go index 0265d1f..356ed68 100644 --- a/db.go +++ b/db/sqlite/db.go @@ -1,4 +1,4 @@ -package rendezvous +package db import ( "bytes" @@ -11,11 +11,16 @@ import ( "os" "time" + dbi "github.com/libp2p/go-libp2p-rendezvous/db" + _ "github.com/mattn/go-sqlite3" + logging "github.com/ipfs/go-log" peer "github.com/libp2p/go-libp2p-peer" ) +var log = logging.Logger("rendezvous/db") + type DB struct { db *sql.DB @@ -240,7 +245,7 @@ func (db *DB) Unregister(p peer.ID, ns string) error { return err } -func (db *DB) Discover(ns string, cookie []byte, limit int) ([]RegistrationRecord, []byte, error) { +func (db *DB) Discover(ns string, cookie []byte, limit int) ([]dbi.RegistrationRecord, []byte, error) { now := time.Now().Unix() var ( @@ -278,10 +283,10 @@ func (db *DB) Discover(ns string, cookie []byte, limit int) ([]RegistrationRecor defer rows.Close() - regs := make([]RegistrationRecord, 0, limit) + regs := make([]dbi.RegistrationRecord, 0, limit) for rows.Next() { var ( - reg RegistrationRecord + reg dbi.RegistrationRecord rid string rns string expire int64 diff --git a/db_test.go b/db/sqlite/db_test.go similarity index 99% rename from db_test.go rename to db/sqlite/db_test.go index c5b192e..20087c0 100644 --- a/db_test.go +++ b/db/sqlite/db_test.go @@ -1,4 +1,4 @@ -package rendezvous +package db import ( "bytes" diff --git a/proto.go b/proto.go index c1f004e..b145f00 100644 --- a/proto.go +++ b/proto.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" + db "github.com/libp2p/go-libp2p-rendezvous/db" pb "github.com/libp2p/go-libp2p-rendezvous/pb" logging "github.com/ipfs/go-log" @@ -21,13 +22,6 @@ const ( DefaultTTL = 2 * 3600 // 2hr ) -type RegistrationRecord struct { - Id peer.ID - Addrs [][]byte - Ns string - Ttl int -} - type RendezvousError struct { Status pb.Message_ResponseStatus Text string @@ -120,7 +114,7 @@ func newRegisterResponseError(status pb.Message_ResponseStatus, text string) *pb return r } -func newDiscoverResponse(regs []RegistrationRecord, cookie []byte) *pb.Message_DiscoverResponse { +func newDiscoverResponse(regs []db.RegistrationRecord, cookie []byte) *pb.Message_DiscoverResponse { r := new(pb.Message_DiscoverResponse) r.Status = pb.Message_OK.Enum() diff --git a/svc.go b/svc.go index 14e2de6..b2bb100 100644 --- a/svc.go +++ b/svc.go @@ -1,9 +1,9 @@ package rendezvous import ( - "context" "fmt" + db "github.com/libp2p/go-libp2p-rendezvous/db" pb "github.com/libp2p/go-libp2p-rendezvous/pb" ggio "github.com/gogo/protobuf/io" @@ -21,7 +21,7 @@ const ( ) type RendezvousService struct { - DB *DB + DB db.DB rzs []RendezvousSync } @@ -30,17 +30,7 @@ type RendezvousSync interface { Unregister(p peer.ID, ns string) } -func NewRendezvousService(ctx context.Context, host host.Host, dbpath string, rzs ...RendezvousSync) (*RendezvousService, error) { - db, err := OpenDB(ctx, dbpath) - if err != nil { - return nil, err - } - - rz := NewRendezvousServiceWithDB(host, db, rzs...) - return rz, nil -} - -func NewRendezvousServiceWithDB(host host.Host, db *DB, rzs ...RendezvousSync) *RendezvousService { +func NewRendezvousService(host host.Host, db db.DB, rzs ...RendezvousSync) *RendezvousService { rz := &RendezvousService{DB: db, rzs: rzs} host.SetStreamHandler(RendezvousProto, rz.handleStream) return rz diff --git a/svc_test.go b/svc_test.go index c0631dc..71e3f35 100644 --- a/svc_test.go +++ b/svc_test.go @@ -6,6 +6,7 @@ import ( "math/rand" "testing" + db "github.com/libp2p/go-libp2p-rendezvous/db/sqlite" pb "github.com/libp2p/go-libp2p-rendezvous/pb" ggio "github.com/gogo/protobuf/io" @@ -53,13 +54,22 @@ func getRendezvousClients(t *testing.T, hosts []host.Host) []Rendezvous { return clients } +func makeRendezvousService(ctx context.Context, host host.Host, path string) (*RendezvousService, error) { + dbi, err := db.OpenDB(ctx, path) + if err != nil { + return nil, err + } + + return NewRendezvousService(host, dbi), nil +} + func TestSVCRegistrationAndDiscovery(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() hosts := getRendezvousHosts(t, ctx, 5) - svc, err := NewRendezvousService(ctx, hosts[0], ":memory:") + svc, err := makeRendezvousService(ctx, hosts[0], ":memory:") if err != nil { t.Fatal(err) } @@ -169,7 +179,7 @@ func TestSVCErrors(t *testing.T) { hosts := getRendezvousHosts(t, ctx, 2) - svc, err := NewRendezvousService(ctx, hosts[0], ":memory:") + svc, err := makeRendezvousService(ctx, hosts[0], ":memory:") if err != nil { t.Fatal(err) }