Esempio n. 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
}
Esempio n. 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
}
Esempio n. 3
0
// NewMeta creates new meta information for publishing with Memberlist.
func NewMeta(adport int, cfg *config.Config, limit int) (m *Meta) {
	m = &Meta{
		Adport:    adport,
		Exporting: cfg.Exporting(),
	}

	for k, v := range cfg.Tags() {
		m.Tag = fmt.Sprintf("%s:%s", k, v)
		if len(m.Tag)+4 <= limit {
			break
		} else {
			m.Tag = ""
		}
	}
	return
}
Esempio n. 4
0
func New(iface string, adport int, cfg *config.Config) (t *Tracker, err error) {
	t = &Tracker{
		logger:     log.New(cfg.Logger(), fmt.Sprintf("%s TRACKER ", cfg.UUID()), 0),
		cfg:        cfg,
		iface:      iface,
		adport:     adport,
		evtHandler: newEventHandler(),
	}

	t.port, err = getRandomPort(t.iface)

	if err != nil {
		return
	}

	err = t.setupMemberlist()

	return
}
Esempio n. 5
0
// NewInputFromConfig creates a new Input instance for a given configuration.
func NewInputFromConfig(cfg *config.Config) (i *Input, err error) {
	m, err := manager.New(cfg)
	if err != nil {
		return
	}
	i = &Input{
		m:       m,
		cfg:     cfg,
		r:       eventual2go.NewReactor(),
		listen:  map[string]interface{}{},
		logger:  log.New(cfg.Logger(), fmt.Sprintf("%s INPUT ", cfg.UUID()), 0),
		results: &messages.ResultStream{m.MessagesOfType(messages.RESULT).Transform(connection.ToMessage)},
	}

	i.logger.Println("Launching with tagset", cfg.Tags())

	i.r.React(connectionEvent{}, i.onConnection)
	i.r.AddStream(connectionEvent{}, m.Connected().Stream)

	i.r.React(arriveEvent{}, i.sendListenFunctions)
	i.r.AddStream(arriveEvent{}, m.PeerArrive().Stream)

	i.r.React(startListenEvent{}, i.startListen)
	i.r.React(stopListenEvent{}, i.stopListen)

	return
}
Esempio n. 6
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
}
Esempio n. 7
0
// NewOutputFromConfig creates a new Output instance for a given configuration.
func NewOutputFromConfig(cfg *config.Config) (o *Output, err error) {
	m, err := manager.New(cfg)
	o = &Output{
		cfg:       cfg,
		m:         m,
		requests:  &messages.RequestStream{m.MessagesOfType(messages.REQUEST).Transform(connection.ToMessage)},
		listeners: map[string]map[config.UUID]interface{}{},
		logger:    log.New(cfg.Logger(), fmt.Sprintf("%s OUTPUT ", cfg.UUID()), 0),
		r:         eventual2go.NewReactor(),
	}

	o.logger.Println("Launching with tagset", cfg.Tags())

	o.r.React(startListenEvent{}, o.onListen)
	o.r.AddStream(startListenEvent{}, m.MessagesOfType(messages.LISTEN).Stream)

	o.r.React(stopListenEvent{}, o.onStopListen)
	o.r.AddStream(stopListenEvent{}, m.MessagesOfType(messages.STOPLISTEN).Stream)

	o.r.React(leaveEvent{}, o.onPeerGone)

	o.r.React(replyEvent{}, o.deliverResult)
	return
}