Skip to content

Commit

Permalink
Issue #163 (#166)
Browse files Browse the repository at this point in the history
CHANGE:
  - server/peerStore/peerStore implemented in `peerStore/peerstore.go` All
    other changes accompany these changes.
  • Loading branch information
Ianleeclark authored Nov 28, 2016
1 parent 750c281 commit 3692f72
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 91 deletions.
16 changes: 8 additions & 8 deletions announce/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package announce

import (
"fmt"
r "github.com/GrappigPanda/notorious/kvStoreInterfaces"
"github.com/GrappigPanda/notorious/peerStore/redis"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -69,7 +69,7 @@ func (a *AnnounceData) ParseAnnounceData(req *http.Request) (err error) {
a.Event = "started"
}

a.RequestContext.redisClient = r.OpenClient()
a.RequestContext.redisClient = redisPeerStore.OpenClient()

return
}
Expand Down Expand Up @@ -116,8 +116,8 @@ func (a *AnnounceData) StartedEventHandler() (err error) {
ipport = fmt.Sprintf("%s:%d", a.IP, a.Port)
}

r.RedisSetKeyVal(a.RequestContext.redisClient, keymember, ipport)
if r.RedisSetKeyIfNotExists(a.RequestContext.redisClient, keymember, ipport) {
redisPeerStore.SetKeyVal(a.RequestContext.redisClient, keymember, ipport)
if redisPeerStore.SetKeyIfNotExists(a.RequestContext.redisClient, keymember, ipport) {
fmt.Printf("Adding host %s to %s\n", ipport, keymember)
}

Expand Down Expand Up @@ -152,7 +152,7 @@ func (a *AnnounceData) CompletedEventHandler() {
keymember := fmt.Sprintf("%s:complete", a.InfoHash)
// TODO(ian): DRY!
ipport := fmt.Sprintf("%s:%s", a.IP, a.Port)
if r.RedisSetKeyIfNotExists(a.RequestContext.redisClient, keymember, ipport) {
if redisPeerStore.SetKeyIfNotExists(a.RequestContext.redisClient, keymember, ipport) {
fmt.Printf("Adding host %s to %s:complete\n", ipport, a.InfoHash)
}
}
Expand All @@ -163,15 +163,15 @@ func (a *AnnounceData) removeFromKVStorage(subkey string) {
keymember := fmt.Sprintf("%s:%s", a.InfoHash, subkey)

fmt.Printf("Removing host %s from %v\n", ipport, keymember)
r.RedisRemoveKeysValue(a.RequestContext.redisClient, keymember, ipport)
redisPeerStore.RemoveKeysValue(a.RequestContext.redisClient, keymember, ipport)
}

func (a *AnnounceData) infoHashExists() bool {
return r.RedisGetBoolKeyVal(nil, a.InfoHash)
return redisPeerStore.GetBoolKeyVal(nil, a.InfoHash)
}

func (a *AnnounceData) createInfoHashKey() {
r.CreateNewTorrentKey(nil, a.InfoHash)
redisPeerStore.CreateNewTorrentKey(nil, a.InfoHash)
}

// ParseInfoHash parses the encoded info hash. Such a simple solution for a
Expand Down
54 changes: 54 additions & 0 deletions peerStore/impl/redisPeerStore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package redisPeerStoreImpl

import (
"github.com/GrappigPanda/notorious/peerStore/redis"
"gopkg.in/redis.v3"
)

// RedisStore represents the implementation of a `PeerStore` object.
type RedisStore struct {
client *redis.Client
}

// SetKeyIfNotExists wraps around the generic RedisSetKeyIfNotExists function
func (p *RedisStore) SetKeyIfNotExists(key, value string) (retval bool) {
return redisPeerStore.SetKeyIfNotExists(p.client, key, value)
}

// SetKV wraps around the generic `SetKeyVal` function
func (p *RedisStore) SetKV(key, value string) {
redisPeerStore.SetKeyVal(p.client, key, value)
}

// RemoveKV wraps around the specific `RemoveKeysValue` function
func (p *RedisStore) RemoveKV(key, value string) {
// TODO(ian): Refactor this so we don't have to delete a value from a key
if value != "" || value == "" {
redisPeerStore.RemoveKeysValue(p.client, key, value)
}
}

// KeyExists wraps around the specific `GetBoolKeyVal` function
func (p *RedisStore) KeyExists(key string) (retval bool) {
return redisPeerStore.GetBoolKeyVal(p.client, key)
}

// GetKeyVal wraps around the specific `GetKeyVal` function
func (p *RedisStore) GetKeyVal(key string) []string {
return redisPeerStore.GetKeyVal(p.client, key)
}

// GetAllPeers wraps around the specific `GetAllPeers` function
func (p *RedisStore) GetAllPeers(key string) []string {
return redisPeerStore.GetAllPeers(p.client, key)
}

// SetIPMember wraps around the specific `SetIPMember` function
func (p *RedisStore) SetIPMember(infoHash, ipPort string) (retval int) {
return redisPeerStore.SetIPMember(p.client, infoHash, ipPort)
}

// CreateNewTorrentKey wraps around the specific `CreateNewTorrentKey` function
func (p *RedisStore) CreateNewTorrentKey(infoHash string) {
redisPeerStore.CreateNewTorrentKey(p.client, infoHash)
}
4 changes: 4 additions & 0 deletions server/peerStore/peerstore.go → peerStore/peerstore.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package peerStore

// PeerStore Represents the base implementation of how we store peers. We
// currently use Redis for storing peers, but this interface allows for
// extensions to other third-party systems.
type PeerStore interface {
SetKeyIfNotExists(string, string) bool
SetKV(string, string)
Expand All @@ -8,4 +11,5 @@ type PeerStore interface {
GetKeyVal(string) []string
GetAllPeers(string) []string
SetIPMember(string, string) int
CreateNewTorrentKey(string)
}
48 changes: 24 additions & 24 deletions kvStoreInterfaces/redis.go → peerStore/redis/redis.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kvStoreInterface
package redisPeerStore

import (
"bytes"
Expand All @@ -23,8 +23,8 @@ func OpenClient() (client *redis.Client) {
return
}

// RedisSetIPMember sets a key as a member of an infohash and sets a timeout.
func RedisSetIPMember(c *redis.Client, infoHash, ipPort string) (retval int) {
// SetIPMember sets a key as a member of an infohash and sets a timeout.
func SetIPMember(c *redis.Client, infoHash, ipPort string) (retval int) {
if c == nil {
c = OpenClient()
}
Expand All @@ -46,14 +46,14 @@ func RedisSetIPMember(c *redis.Client, infoHash, ipPort string) (retval int) {
return
}

// RedisSetKeyVal Sets a key to the specified value. Used mostly with adding a
// SetKeyVal Sets a key to the specified value. Used mostly with adding a
// peer into an info_hash
func RedisSetKeyVal(c *redis.Client, keymember string, value string) {
func SetKeyVal(c *redis.Client, keymember string, value string) {
if c == nil {
c = OpenClient()
}

// RedisSetKeyVal sets a key:member's value to value. Returns nothing as of
// SetKeyVal sets a key:member's value to value. Returns nothing as of
// yet.
currTime := int64(time.Now().UTC().Unix())
currTime += EXPIRETIME
Expand All @@ -66,13 +66,13 @@ func RedisSetKeyVal(c *redis.Client, keymember string, value string) {
}
}

// RedisGetKeyVal Lookup a peer in the specified infohash at `key`
func RedisGetKeyVal(c *redis.Client, key string) []string {
// GetKeyVal Lookup a peer in the specified infohash at `key`
func GetKeyVal(c *redis.Client, key string) []string {
if c == nil {
c = OpenClient()
}

// RedisGetKeyVal retrieves a value from the Redis store by looking up the
// GetKeyVal retrieves a value from the store by looking up the
// provided key. If the key does not yet exist, we Create the key in the KV
// storage or if the value is empty, we add the current requester to the
// list.
Expand All @@ -87,8 +87,8 @@ func RedisGetKeyVal(c *redis.Client, key string) []string {
return val
}

// RedisGetAllPeers fetches all peers from the info_hash at `key`
func RedisGetAllPeers(c *redis.Client, key string) []string {
// GetAllPeers fetches all peers from the info_hash at `key`
func GetAllPeers(c *redis.Client, key string) []string {
if c == nil {
c = OpenClient()
}
Expand Down Expand Up @@ -117,28 +117,28 @@ func RedisGetAllPeers(c *redis.Client, key string) []string {
return val
}

// RedisGetCount counts all of the peers at `info_hash`
func RedisGetCount(c *redis.Client, info_hash string, member string) (retval int, err error) {
// GetCount counts all of the peers at `info_hash`
func GetCount(c *redis.Client, infoHash string, member string) (retval int, err error) {
if c == nil {
c = OpenClient()
}

// A generic function which is used to retrieve either the complete count
// or the incomplete count for a specified `info_hash`.
keymember := concatenateKeyMember(info_hash, member)
keymember := concatenateKeyMember(infoHash, member)

x, err := c.SMembers(keymember).Result()
if err != nil {
// TODO(ian): Add actual error checking here.
err = fmt.Errorf("The info hash %s with member %s doesn't exist", info_hash, member)
err = fmt.Errorf("The info hash %s with member %s doesn't exist", infoHash, member)
}

retval = len(x)
return
}

// RedisGetBoolKeyVal Checks if a `key` exists
func RedisGetBoolKeyVal(c *redis.Client, key string) bool {
// GetBoolKeyVal Checks if a `key` exists
func GetBoolKeyVal(c *redis.Client, key string) bool {
if c == nil {
c = OpenClient()
}
Expand All @@ -148,23 +148,23 @@ func RedisGetBoolKeyVal(c *redis.Client, key string) bool {
return ret
}

// RedisSetKeyIfNotExists Set a key if it doesn't exist.
func RedisSetKeyIfNotExists(c *redis.Client, keymember string, value string) (rv bool) {
// SetKeyIfNotExists Set a key if it doesn't exist.
func SetKeyIfNotExists(c *redis.Client, keymember string, value string) (rv bool) {
if c == nil {
c = OpenClient()
}

rv = RedisGetBoolKeyVal(c, keymember)
rv = GetBoolKeyVal(c, keymember)
if !rv {
RedisSetKeyVal(c, keymember, value)
SetKeyVal(c, keymember, value)
}
return
}

// RedisRemoveKeysValue Remove a `value` from `key` in the redis kv storage. `key` is typically
// RemoveKeysValue Remove a `value` from `key` in the redis kv storage. `key` is typically
// a keymember of info_hash:(in)complete and the value is typically the
// ip:port concatenated.
func RedisRemoveKeysValue(c *redis.Client, key string, value string) {
func RemoveKeysValue(c *redis.Client, key string, value string) {
if c == nil {
c = OpenClient()
}
Expand All @@ -174,7 +174,7 @@ func RedisRemoveKeysValue(c *redis.Client, key string, value string) {

// CreateNewTorrentKey Creates a new key. By default, it adds a member
// ":ip". I don't think this ought to ever be generalized, as I just want
// Redis to function in one specific way in notorious.
// to function in one specific way in notorious.
func CreateNewTorrentKey(c *redis.Client, key string) {
if c == nil {
c = OpenClient()
Expand Down
6 changes: 3 additions & 3 deletions reaper/reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package reaper
import (
"fmt"
"github.com/GrappigPanda/notorious/database/mysql"
r "github.com/GrappigPanda/notorious/kvStoreInterfaces"
"github.com/GrappigPanda/notorious/peerStore/redis"
"gopkg.in/redis.v3"
"strconv"
"strings"
Expand Down Expand Up @@ -72,7 +72,7 @@ func StartReapingScheduler(waitTime time.Duration) {
go func() {
for {
// Handle any other cleanup or Notorious-related functions
c := r.OpenClient()
c := redisPeerStore.OpenClient()
_, err := c.Ping().Result()
if err != nil {
panic("No Redis instance detected. If deploying without Docker, install redis-server")
Expand All @@ -86,7 +86,7 @@ func StartReapingScheduler(waitTime time.Duration) {
x, err := mysql.GetWhitelistedTorrents(nil)
for x.Next() {
x.Scan(infoHash, name, addedBy, dateAdded)
r.CreateNewTorrentKey(nil, *infoHash)
redisPeerStore.CreateNewTorrentKey(nil, *infoHash)
}

// Start the actual peer reaper.
Expand Down
6 changes: 3 additions & 3 deletions server/announce_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
. "github.com/GrappigPanda/notorious/announce"
"github.com/GrappigPanda/notorious/bencode"
"github.com/GrappigPanda/notorious/database"
r "github.com/GrappigPanda/notorious/kvStoreInterfaces"
"github.com/GrappigPanda/notorious/peerStore/redis"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -72,8 +72,8 @@ func formatResponseData(ips []string, data *AnnounceData) string {
// string that we respond with.
func EncodeResponse(ipport []string, data *AnnounceData) (resp string) {
ret := ""
completeCount := len(r.RedisGetKeyVal(nil, data.InfoHash))
incompleteCount := len(r.RedisGetKeyVal(nil, data.InfoHash))
completeCount := len(redisPeerStore.GetKeyVal(nil, data.InfoHash))
incompleteCount := len(redisPeerStore.GetKeyVal(nil, data.InfoHash))
ret += bencode.EncodeKV("complete", bencode.EncodeInt(completeCount))

ret += bencode.EncodeKV("incomplete", bencode.EncodeInt(incompleteCount))
Expand Down
49 changes: 0 additions & 49 deletions server/peerStore/redisStore.go

This file was deleted.

8 changes: 4 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/GrappigPanda/notorious/config"
"github.com/GrappigPanda/notorious/database"
"github.com/GrappigPanda/notorious/database/impl"
r "github.com/GrappigPanda/notorious/kvStoreInterfaces"
"github.com/GrappigPanda/notorious/server/peerStore"
"github.com/GrappigPanda/notorious/peerStore"
"github.com/GrappigPanda/notorious/peerStore/impl"
"net/http"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ func (app *applicationContext) worker(data *a.AnnounceData) []string {

}

r.CreateNewTorrentKey(nil, data.InfoHash)
app.peerStoreClient.CreateNewTorrentKey(data.InfoHash)
return app.worker(data)
}

Expand Down Expand Up @@ -159,7 +159,7 @@ func RunServer() {
app := applicationContext{
config: config.LoadConfig(),
trackerLevel: a.RATIOLESS,
peerStoreClient: new(peerStore.RedisStore),
peerStoreClient: new(redisPeerStoreImpl.RedisStore),
sqlObj: new(sqlStoreImpl.MySQLStore),
}

Expand Down

0 comments on commit 3692f72

Please sign in to comment.