Beispiel #1
0
//Connect to a peer at an address and add the connection
func (s *Service) Connect(address string) {

	target, err := peer.IDB58Decode(address)
	if err != nil {
		fmt.Printf("IDB58 error: %s\n ", err)
		return
	}
	fmt.Printf("I am peer %s dialing %s\n", s.node.Identity.Pretty(), target.Pretty())

	con, err := corenet.Dial(s.node, target, s.name)
	if err != nil {
		fmt.Println(err)
		return
	}
	if s.connections == nil {
		s.connections = make(map[string]connection)
	}
	key := con.Conn().RemotePeer().Pretty()
	s.connections[key] = connection{con, &sync.RWMutex{}, make(chan []byte), make(chan []byte), make(chan bool)}
	go s.handleCon(s.connections[key])
	defer func() {
		delete(s.connections, key)
		con.Close()
	}()
}
Beispiel #2
0
//ExchangeRecent sends hash and peers of recent hash and returns received recent key.
func (m *Self) ExchangeRecent(target *Peer, recent key.Key, peers key.Key) (key.Key, error) {
	con, err := corenet.Dial(m.ipfsNode, target.ID, "/app/"+m.RootPath)
	if log.If(err) {
		return "", err
	}
	if err := WriteRequest(con, recent, peers); log.If(err) {
		return "", err
	}
	rrecent, err := ReadResponse(con)
	log.If(err)
	return rrecent, err
}
Beispiel #3
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
}
Beispiel #4
0
func main() {
	if len(os.Args) < 2 {
		fmt.Println("Please give a peer ID as an argument")
		return
	}
	target, err := peer.IDB58Decode(os.Args[1])
	if err != nil {
		panic(err)
	}

	// Basic ipfsnode setup
	r, err := fsrepo.Open("~/.ipfs")
	if err != nil {
		panic(err)
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	cfg := &core.BuildCfg{
		Repo:   r,
		Online: true,
	}

	nd, err := core.NewNode(ctx, cfg)

	if err != nil {
		panic(err)
	}

	fmt.Printf("I am peer %s dialing %s\n", nd.Identity, target)

	con, err := corenet.Dial(nd, target, "/app/whyrusleeping")
	if err != nil {
		fmt.Println(err)
		return
	}

	io.Copy(os.Stdout, con)
}
Beispiel #5
0
func main() {
	if len(os.Args) < 2 {
		fmt.Println("Please give a peer ID as an argument")
		return
	}

	target, err := peer.IDB58Decode(os.Args[1])
	if err != nil {
		panic(err)
	}

	// Basic ipfsnode setup
	r := fsrepo.At("~/.ipfs")
	if err := r.Open(); err != nil {
		panic(err)
	}

	nb := core.NewNodeBuilder().Online()
	nb.SetRepo(r)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	nd, err := nb.Build(ctx)
	if err != nil {
		panic(err)
	}

	fmt.Printf("I am peer %s dialing %s\n", nd.Identity, target)

	con, err := corenet.Dial(nd, target, "/app/whyrusleeping")
	if err != nil {
		fmt.Println(err)
		return
	}

	io.Copy(os.Stdout, con)
}
Beispiel #6
0
func MyDial(n *core.IpfsNode, target peer.ID, protoid string) (net.Stream, error) {

	var con net.Stream
	var err error
	for i := 0; i < MAXTRIES; i++ {
		con, err = corenet.Dial(n, target, protoid)
		if err != nil && i > MAXTRIES {
			fmt.Fprintln(os.Stderr, "Dial failed to connect, tried", i, "times. Giving up.")
			fmt.Fprintln(os.Stderr, fmt.Sprintf("%s", err))
			return nil, err
		} else if err != nil {
			var sleeptime time.Duration
			sleeptime = time.Duration(i)
			time.Sleep(sleeptime * time.Second)
			fmt.Fprintln(os.Stderr, "Dial failed, retrying:", err)
		} else if err == nil {
			return con, err
		}
	}

	return con, err

}
Beispiel #7
0
		}

		lnet, _, err := manet.DialArgs(bindAddr)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		app := cnAppInfo{
			identity: n.Identity,
			protocol: proto,
		}

		n.Peerstore.AddAddr(peer, addr, peerstore.TempAddrTTL)

		remote, err := corenet.Dial(n, peer, proto)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		switch lnet {
		case "tcp", "tcp4", "tcp6":
			listener, err := manet.Listen(bindAddr)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				if err := remote.Close(); err != nil {
					res.SetError(err, cmds.ErrNormal)
				}
				return
			}