Ejemplo n.º 1
0
func (nd *Node) Dial(peerHash, protocol string) (net.Conn, error) {
	peerID, err := peer.IDB58Decode(peerHash)
	if err != nil {
		return nil, err
	}

	stream, err := corenet.Dial(nd.ipfsNode, peerID, protocol)
	if err != nil {
		return nil, err
	}

	return wrapStream(stream), nil
}
Ejemplo n.º 2
0
func (nd *Node) PublicKeyFor(peerHash string) (security.PubKey, error) {
	node, err := nd.proc()
	if err != nil {
		return nil, err
	}

	peerID, err := peer.IDB58Decode(peerHash)
	if err != nil {
		return nil, err
	}

	pub := node.Peerstore.PubKey(peerID)
	if pub == nil {
		return nil, fmt.Errorf("No public key for `%s`", peerHash)
	}

	return pub, nil
}
Ejemplo n.º 3
0
// Ping returns a new Pinger. It can be used to
// query the time the remote was last seen. It will be
// constantly updated until close is called on it.
func (nd *Node) Ping(peerHash string) (*Pinger, error) {
	if !nd.IsOnline() {
		return nil, fmt.Errorf("Not online") // TODO: common error?
	}

	node, err := nd.proc()
	if err != nil {
		return nil, err
	}

	peerID, err := peer.IDB58Decode(peerHash)
	if err != nil {
		return nil, err
	}

	ctx, cancel := context.WithCancel(nd.Context)
	pingCh, err := node.Ping.Ping(ctx, peerID)
	if err != nil {
		return nil, err
	}

	pinger := &Pinger{
		lastSeen: time.Now(),
		cancel:   cancel,
	}

	go func() {
		for roundtrip := range pingCh {
			pinger.mu.Lock()
			pinger.roundtrip = roundtrip
			pinger.lastSeen = time.Now()
			pinger.mu.Unlock()
		}
	}()

	return pinger, nil
}