func Dial(nd *core.IpfsNode, p peer.ID, protocol string) (net.Stream, error) { ctx, cancel := context.WithTimeout(nd.Context(), time.Second*30) defer cancel() err := nd.PeerHost.Connect(ctx, pstore.PeerInfo{ID: p}) if err != nil { return nil, err } return nd.PeerHost.NewStream(nd.Context(), pro.ID(protocol), p) }
func Listen(nd *core.IpfsNode, protocol string) (*ipfsListener, error) { ctx, cancel := context.WithCancel(nd.Context()) list := &ipfsListener{ proto: pro.ID(protocol), conCh: make(chan net.Stream), ctx: ctx, cancel: cancel, } nd.PeerHost.SetStreamHandler(list.proto, func(s net.Stream) { select { case list.conCh <- s: case <-ctx.Done(): s.Close() } }) return list, nil }
if err != nil { res.SetError(err, cmds.ErrNormal) return } out := make(chan interface{}) res.SetOutput((<-chan interface{})(out)) go func() { defer close(out) for { if pfound { stats := nd.Reporter.GetBandwidthForPeer(pid) out <- &stats } else if tfound { protoId := protocol.ID(tstr) stats := nd.Reporter.GetBandwidthForProtocol(protoId) out <- &stats } else { totals := nd.Reporter.GetBandwidthTotals() out <- &totals } if !doPoll { return } select { case <-time.After(interval): case <-req.Context().Done(): return } }