Beispiel #1
0
func (p *Router) Join(group utils.NodeID) error {
	if p.getGroupDht(group) == nil {
		d := dht.NewDHT(10, p.ID(), group, p.listener.RawConn, p.logger)
		for _, n := range p.mainDht.LoadNodes(group.String()) {
			if !n.ID.Match(p.id) {
				d.Discover(n.Addr)
			}
		}
		p.dhtMutex.Lock()
		p.groupDht[group] = d
		p.dhtMutex.Unlock()
		p.mainDht.StoreNodes(group.String(), []utils.NodeInfo{
			utils.NodeInfo{ID: p.id, Addr: p.listener.Addr()},
		})
		return nil
	}
	return errors.New("already joined")
}
Beispiel #2
0
func (p *Router) getSessions(id utils.NodeID) []*session {
	var sessions []*session
	if bytes.Equal(id.NS[:], utils.GlobalNamespace[:]) {
		s := p.getDirectSession(id)
		if s != nil {
			sessions = append(sessions, s)
		}
	} else {
		if d, ok := p.groupDht[id]; ok {
			for _, n := range p.mainDht.LoadNodes(id.String()) {
				if !n.ID.Match(p.id) {
					d.Discover(n.Addr)
				}
			}
			for _, n := range d.FingerNodes() {
				s := p.getDirectSession(n.ID)
				if s != nil {
					sessions = append(sessions, s)
				}
			}
		}
	}
	return sessions
}