Skip to content

Commit

Permalink
Merge pull request #929 from subutai-io/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
crioto authored May 2, 2018
2 parents 6904b60 + 9c6da40 commit 56d259d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
16 changes: 9 additions & 7 deletions dht_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ var (

// DHTConnection to a DHT bootstrap node
type DHTConnection struct {
routers []*DHTRouter // Routers
lock sync.Mutex // Mutex for register/unregister
instances map[string]*P2PInstance // Instances
registered []string // List of registered swarm IDs
incoming chan *ptp.DHTPacket // Packets received by routers
ip string // Our outbound IP
isActive bool // Whether DHT connection is active or not
routers []*DHTRouter // Routers
routersList string // Comma-separated list of routers
lock sync.Mutex // Mutex for register/unregister
instances map[string]*P2PInstance // Instances
registered []string // List of registered swarm IDs
incoming chan *ptp.DHTPacket // Packets received by routers
ip string // Our outbound IP
isActive bool // Whether DHT connection is active or not
}

func (dht *DHTConnection) init(routersSrc string) error {
Expand All @@ -50,6 +51,7 @@ func (dht *DHTConnection) init(routersSrc string) error {
router.data = dht.incoming
dht.routers = append(dht.routers, router)
}
dht.routersList = routersSrc
dht.instances = make(map[string]*P2PInstance)
return nil
}
Expand Down
7 changes: 4 additions & 3 deletions lib/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ func (p *PeerToPeer) IsIPv4(ip string) bool {
}

// New is an entry point of a P2P library.
func New(argIP, argMac, argDev, argDirect, argHash, argKeyfile, argKey, argTTL, argLog string, fwd bool, port int, ignoreIPs []string, outboundIP net.IP) *PeerToPeer {
func New(argIP, argMac, argDev, argDirect, argHash, argKeyfile, argKey, argTTL, argLog string, fwd bool, port int, ignoreIPs []string, outboundIP net.IP, routers string) *PeerToPeer {
Log(Debug, "Starting new P2P Instance: %s", argHash)
Log(Debug, "IP: %s", argIP)
Log(Debug, "Mac: %s", argMac)
p := new(PeerToPeer)
p.Routers = routers
p.outboundIP = outboundIP
p.Init()
var err error
Expand Down Expand Up @@ -206,7 +207,7 @@ func New(argIP, argMac, argDev, argDirect, argHash, argKeyfile, argKey, argTTL,
p.UDPSocket = new(Network)
p.UDPSocket.Init("", port)
go p.UDPSocket.Listen(p.HandleP2PMessage)
go p.UDPSocket.KeepAlive(p.retrieveFirstDHTRouter())
go p.UDPSocket.KeepAlive(p.extractBestDHTRouter())
p.waitForRemotePort()

// Create new DHT Client, configure it and initialize
Expand Down Expand Up @@ -268,7 +269,7 @@ func (p *PeerToPeer) waitForRemotePort() {
Log(Warning, "Remote port received: %d", p.UDPSocket.remotePort)
}

func (p *PeerToPeer) retrieveFirstDHTRouter() *net.UDPAddr {
func (p *PeerToPeer) extractBestDHTRouter() *net.UDPAddr {
Log(Debug, "Routers: %s", p.Routers)
routers := strings.Split(p.Routers, ",")
if len(routers) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions lib/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ func TestIsIPv4(t *testing.T) {
}
}

func TestRetrieveFirstDHTRouters(t *testing.T) {
func TestExtractBestDHTRouters(t *testing.T) {
ptp := new(PeerToPeer)
wait, err := net.ResolveUDPAddr("udp4", "192.168.11.5:6882")
if err != nil {
t.Error("error")
}
ptp.Routers = ""
get := ptp.retrieveFirstDHTRouter()
get := ptp.extractBestDHTRouter()
if get != nil {
t.Error("Length of ptp routers is nil")
}
ptp.Routers = "192.168.11.5:24,192.168.22.1:22"
get2 := ptp.retrieveFirstDHTRouter()
get2 := ptp.extractBestDHTRouter()

if bytes.EqualFold(get2.IP, wait.IP) && get2.Port != wait.Port && get2.Zone != wait.Zone {
t.Errorf("Error.Wait %v, get %v", wait, get2)
Expand Down
2 changes: 1 addition & 1 deletion start.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (d *Daemon) run(args *RunArgs, resp *Response) error {
newInst := new(P2PInstance)
newInst.ID = args.Hash
newInst.Args = *args
newInst.PTP = ptp.New(args.IP, args.Mac, args.Dev, "", args.Hash, args.Keyfile, args.Key, args.TTL, "", args.Fwd, args.Port, usedIPs, OutboundIP)
newInst.PTP = ptp.New(args.IP, args.Mac, args.Dev, "", args.Hash, args.Keyfile, args.Key, args.TTL, "", args.Fwd, args.Port, usedIPs, OutboundIP, bootstrap.routersList)
if newInst.PTP == nil {
resp.Output = resp.Output + "Failed to create P2P Instance"
resp.ExitCode = 1
Expand Down

0 comments on commit 56d259d

Please sign in to comment.