Exemplo n.º 1
0
func (pn *ProtoNet) Dial(p pnet.Peer) (io.ReadWriteCloser, error) {
	ln, ok := pn.sw[p.Id()]
	if !ok {
		msg := fmt.Sprintf("%s isn't listening", p.Id())
		return nil, errors.New(msg)
	}
	conn1, conn2 := net.Pipe()
	ln.accept <- conn1
	return conn2, nil
}
Exemplo n.º 2
0
func New(me pnet.Peer, cachesize, shuflen int, protonet pnet.ProtoNet) *Cyclon {
	me.Put(age, 0)
	return &Cyclon{
		me:        me,
		cachesize: cachesize,
		shuflen:   shuflen,
		neighbs:   make(PeerSet),
		protonet:  protonet,
	}
}
Exemplo n.º 3
0
func (s PeerSet) PopOldest() pnet.Peer {
	var oldest pnet.Peer
	for _, p := range s {
		if oldest == nil || oldest.Get(age).(int) < p.Get(age).(int) {
			oldest = p
		}
	}
	delete(s, oldest.Id())
	return oldest
}
Exemplo n.º 4
0
func (gw *Gateway) Dial(dest pnet.Peer, proto string) (io.ReadWriteCloser, error) {
	// See if we already have a connection with this peer.
	s, err := gw.conns.NewStreamWithGroup(dest.Id())
	if err != nil {
		// We don't, let's create a new connection.

		//TODO: Dial many/all addrs, use earliest. Like here:
		//https://github.com/ipfs/go-ipfs/blob/master/p2p/net/swarm/swarm_dial.go
		p, ok := dest.(*PeerInfo)
		if !ok {
			return nil, errors.New("Unknown pnet.Peer type")
		}

		m, err := ma.NewMultiaddrBytes(p.MAddrs[0])
		if err != nil {
			return nil, err
		}

		nc, err := manet.Dial(m)
		if err != nil {
			return nil, err
		}

		c, err := gw.conns.AddConn(nc)
		if err != nil {
			nc.Close()
			return nil, err
		}
		gw.conns.AddConnToGroup(c, p.Id())

		s, err = gw.conns.NewStreamWithGroup(p.Id())
		if err != nil {
			return nil, err
		}
	}

	// Select the protocol on the new stream.
	err = ms.SelectProtoOrFail(proto, s)
	if err != nil {
		s.Close()
	}

	return s, err
}