Skip to content

Commit

Permalink
Added bias torrent implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
danalex97 committed May 1, 2018
1 parent 01f52b0 commit 0e4e387
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
87 changes: 87 additions & 0 deletions bias_torrent/tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package bias_torrent

import (
"strings"
"math/rand"

. "github.com/danalex97/Speer/interfaces"
"github.com/danalex97/nfsTorrent/torrent"
)

const K int = 1

type Tracker struct {
asId map[string][]string

*torrent.Tracker
}

func (t *Tracker) New(util TorrentNodeUtil) TorrentNode {
tracker := new(Tracker)
tracker.asId = make(map[string][]string)
tracker.Tracker = (tracker.Tracker.New(util)).(*torrent.Tracker)
return tracker
}

func (t *Tracker) OnJoin() {
go t.CheckMessages(t.Recv)
}

func (t *Tracker) Recv(m interface {}) {
switch msg := m.(type) {
/* New Protocol. */
case torrent.Join:
id := msg.Id
as := getAS(id)
if _, ok := t.asId[as]; !ok {
t.asId[as] = []string{}
}
t.asId[as] = append(t.asId[as], id)

t.Join(msg, t.Neighbours)
default:
/* Backward compatibility. */
t.Tracker.Recv(m)
}
}

func getAS(id string) string {
// We assume that ID is of form [AS].[NBR]
return strings.Split(id, ".")[0]
}

func newNeigh(allIds, ids []string) string {
n := allIds[rand.Intn(len(allIds))]
for _, i := range ids {
if i == n {
return newNeigh(allIds, ids)
}
}
return n
}

func (t *Tracker) Neighbours(id string) interface {} {
neighbours := torrent.Neighbours{[]string{}}
as := getAS(id)
B := t.Neigh - K

if nbr, _ :=t.asId[as]; B > len(nbr) {
B = len(nbr) - 1
}

ids := []string{}
for _, newId := range t.asId[as] {
if newId != id {
ids = append(ids, newId)
}
}

for i := 0; i < t.Neigh; i++ {
if i >= B {
ids = t.Ids
}
id := newNeigh(ids, append(neighbours.Ids, id))
neighbours.Ids = append(neighbours.Ids, id)
}
return neighbours
}
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ var extension = flag.Bool(
"Whether we use the extension",
)

var biased = flag.Bool(
"bias",
false,
"Whether we use the biased tracker",
)

var verbose = flag.Bool(
"v",
false,
Expand All @@ -47,7 +53,11 @@ func main() {
// Set extension
var template interface {}
if !*extension {
template = new(simulation.SimulatedNode)
if !*biased {
template = new(simulation.SimulatedNode)
} else {
template = new(simulation.SimulatedBiasedNode)
}
} else {
template = new(simulation.SimulatedCachedNode)
fmt.Println("Running with extension.")
Expand Down
17 changes: 16 additions & 1 deletion simulation/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package simulation

import (
. "github.com/danalex97/Speer/interfaces"
"github.com/danalex97/nfsTorrent/torrent"
"github.com/danalex97/nfsTorrent/config"

"github.com/danalex97/nfsTorrent/torrent"
"github.com/danalex97/nfsTorrent/cache_torrent"
"github.com/danalex97/nfsTorrent/bias_torrent"
)

type SimulatedNode struct {
Expand All @@ -19,6 +21,19 @@ func (s *SimulatedNode) New(util TorrentNodeUtil) TorrentNode {
}
}

type SimulatedBiasedNode struct {
}

func (s *SimulatedBiasedNode) New(util TorrentNodeUtil) TorrentNode {
if util.Join() == "" {
return new(bias_torrent.Tracker).New(util)
} else {
config.Config.SharedInit()
return new(torrent.Peer).New(util)
}
}


type SimulatedCachedNode struct {
}

Expand Down

0 comments on commit 0e4e387

Please sign in to comment.