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
// 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
}