Пример #1
0
// New creates a new Peer.
func New(uuid config.UUID, address string, port int, incoming *connection.Incoming, cfg *config.Config) (p *Peer, err error) {
	p = &Peer{
		m:         &sync.Mutex{},
		uuid:      uuid,
		cfg:       cfg,
		incoming:  incoming,
		msgIn:     incoming.MessagesFromSender(uuid),
		connected: NewPeerCompleter(),
		removed:   NewPeerCompleter(),
		logger:    log.New(cfg.Logger(), fmt.Sprintf("%s PEER %s ", cfg.UUID(), uuid), 0),
	}

	p.msgOut, err = connection.NewOutgoing(cfg.UUID(), address, port)
	if err != nil {
		return
	}

	p.msgIn.Where(messages.Is(messages.HELLOOK)).Listen(p.onHelloOk)
	p.msgIn.Where(messages.Is(messages.END)).Listen(p.onEnd)

	if cfg.Exporting() {
		p.msgIn.Where(messages.Is(messages.DOHAVE)).Listen(p.onDoHave)
		p.msgIn.Where(messages.Is(messages.CONNECT)).Listen(p.onConnected)
	}

	p.msgIn.CloseOnFuture(p.removed.Future().Future)
	p.removed.Future().Then(p.closeOutgoing)

	p.initialized = eventual2go.NewTimeoutCompleter(5 * time.Second)
	p.initialized.Future().Err(p.onError)
	return
}
Пример #2
0
func (p *Peer) check(eventual2go.Data) eventual2go.Data {
	msgs := p.Messages().Where(messages.Is(messages.HAVE))
	c := msgs.AsChan()
	p.logger.Println("checking tags")
	for k, v := range p.cfg.Tags() {

		p.logger.Println("checking", k, v)
		p.Send(&messages.DoHave{k, v})

		select {
		case <-time.After(5 * time.Second):
			p.logger.Println("timeout")
			p.Remove()
			return nil
		case m, ok := <-c:
			if !ok {
				p.Remove()
				p.logger.Println("incoming closed")
				return nil
			}
			have := m.(*messages.Have)

			if !have.Have || have.TagKey != k || have.TagValue != v {
				p.Remove()
				p.logger.Printf("Tag %s:%s not supported", k, v)
				return nil
			}
		}
	}
	p.Send(&messages.Connect{})
	p.logger.Println("Connected successfully")
	p.connected.Complete(p)
	return nil
}
Пример #3
0
// New Creates a new Discoverer.
func New(join *tracker.NodeStream, incoming *connection.Incoming, cfg *config.Config) (d *Discoverer) {
	d = &Discoverer{
		cfg:            cfg,
		incoming:       incoming,
		connectedPeers: peer.NewPeerStreamController(),
		seenNodes:      map[config.UUID]*peer.Peer{},
		m:              &sync.Mutex{},
		logger:         log.New(cfg.Logger(), fmt.Sprintf("%s DISCOVERER ", cfg.UUID()), 0),
	}

	//TODO replace with listenwhere
	join.Where(d.isNodeInteresting).Listen(d.onInterestingNode)
	incoming.Messages().Where(messages.Is(messages.HELLO)).Listen(d.onHello)
	return
}
Пример #4
0
func TestInitConnection(t *testing.T) {

	i1, err := connection.NewIncoming("127.0.0.1")
	if err != nil {
		t.Fatal(err)
	}

	i2, err := connection.NewIncoming("127.0.0.1")
	if err != nil {
		t.Fatal(err)
	}

	cfg1 := config.New(true, map[string]string{})
	cfg2 := config.New(false, map[string]string{})

	c := i2.MessagesFromSender(cfg1.UUID()).Where(messages.Is(messages.HELLO)).AsChan()

	p1, err := New(cfg2.UUID(), "127.0.0.1", i2.Port(), i1, cfg1)
	if err != nil {
		t.Fatal(err)
	}
	p1.InitConnection()

	var p2 *Peer
	select {
	case <-time.After(5 * time.Second):
		t.Fatal("Did not received Hello")
	case d := <-c:
		m := d.(*messages.Hello)
		if m.Address != i1.Addr() || m.Port != i1.Port() {
			t.Fatal("Wrong message", m)
		}
		p2, err = NewFromHello(m, i2, cfg2)
		if err != nil {
			t.Fatal(err)
		}
	}

	time.Sleep(5 * time.Second)

	if !p1.initialized.Completed() {
		t.Error("Connection 1 did not initialize")
	}
	if !p2.initialized.Completed() {
		t.Error("Connection 2 did not initialize")
	}

}