Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

Commit

Permalink
Make Connect2Switches blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Dec 6, 2016
1 parent ceb2723 commit 67963ab
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,23 @@ func MakeConnectedSwitches(n int, initSwitch func(int, *Switch) *Switch, connect
}

// Will connect switches i and j via net.Pipe()
// Blocks until a conection is established.
// NOTE: caller ensures i and j are within bounds
func Connect2Switches(switches []*Switch, i, j int) {
switchI := switches[i]
switchJ := switches[j]
c1, c2 := net.Pipe()
go switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake.
go switchJ.AddPeerWithConnection(c2, true)
doneCh := make(chan struct{})
go func() {
switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake.
doneCh <- struct{}{}
}()
go func() {
switchJ.AddPeerWithConnection(c2, true)
doneCh <- struct{}{}
}()
<-doneCh
<-doneCh
}

func StartSwitches(switches []*Switch) error {
Expand Down

0 comments on commit 67963ab

Please sign in to comment.