コード例 #1
0
ファイル: peer.go プロジェクト: ThingiverseIO/thingiverseio
// 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
// 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
}
コード例 #3
0
func New(cfg *config.Config) (m *Manager, err error) {
	m = &Manager{
		r:                  eventual2go.NewReactor(),
		connected:          typed_events.NewBoolStreamController(),
		peers:              map[config.UUID]*peer.Peer{},
		arrive:             config.NewUUIDStreamController(),
		leave:              config.NewUUIDStreamController(),
		guaranteedMessages: map[messages.Message]*peer.Peer{},
		messageIn:          connection.NewMessageStreamController(),
		logger:             log.New(cfg.Logger(), fmt.Sprintf("%s MANAGER ", cfg.UUID()), 0),
		shutdown:           eventual2go.NewShutdown(),
		shutdownComplete:   eventual2go.NewCompleter(),
	}

	m.r.React(peerLeave{}, m.peerLeave)
	m.r.React(peerConnected{}, m.peerConnected)
	m.r.OnShutdown(m.onShutdown)

	for _, iface := range cfg.Interfaces() {

		var i *connection.Incoming
		i, err = connection.NewIncoming(iface)
		if err != nil {
			m.logger.Println("ERROR setting up incoming:", err)
			m.Shutdown()
			return
		}
		m.shutdown.Register(i)
		m.messageIn.Join(i.In().Where(filterMessages))

		var t *tracker.Tracker
		t, err = tracker.New(iface, i.Port(), cfg)
		if err != nil {
			m.logger.Println("ERROR setting up tracker:", err)
			m.Shutdown()
			return
		}
		m.tracker = append(m.tracker, t)
		m.shutdown.Register(t)
		m.r.AddStream(peerLeave{}, t.Leave().Stream)

		var d *discoverer.Discoverer
		d = discoverer.New(t.Join(), i, cfg)
		m.r.AddStream(peerConnected{}, d.ConnectedPeers().Stream)
	}

	return
}