mocknet: create a connection on NewStream if we need one
That's what the Swarm does and that's what the function's documentation says it
does.
@@ -330,26 +330,10 @@ func (pn *peernet) Connectedness(p peer.ID) inet.Connectedness {
// NewStream returns a new stream to given peer p.
// If there is no connection to p, attempts to create one.
func (pn *peernet) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error) {
- pn.Lock()
- cs, found := pn.connsByPeer[p]
- if !found || len(cs) < 1 {
- pn.Unlock()
- return nil, fmt.Errorf("no connection to peer")
- }
-
- // if many conns are found, how do we select? for now, randomly...
- // this would be an interesting place to test logic that can measure
- // links (network interfaces) and select properly
- n := rand.Intn(len(cs))
- var c *conn
- for c = range cs {
- if n == 0 {
- break
+ c, err := pn.DialPeer(ctx, p)
+ if err != nil {
+ return nil, err
}
- n--
- }
- pn.Unlock()
-
return c.NewStream()
}
