Ejemplo n.º 1
0
//sets a new router table
// returns the old router table, or error
func (this *Manager) SetRouterTable(table *RouterTable) (*RouterTable, error) {
	this.lock.Lock()
	defer this.lock.Unlock()
	if this.table != nil {
		if this.table.Revision >= table.Revision {
			return nil, fmt.Errorf("Trying to set an older revision %d vs %d", this.table.Revision, table.Revision)
		}
	}

	//create a new map for connections
	c := make(map[string]client.Client)
	for _, e := range table.Entries {
		key := e.Id()
		if key == this.MyEntryId {
			e.Self = true
			table.MyEntry = e
			continue
		}

		conn, ok := this.connections[key]
		if !ok {
			conn = this.createConnection(e)
		}
		delete(this.connections, key)
		c[key] = conn
	}
	//now close any Clients for removed entries
	for _, client := range this.connections {
		client.Close()
	}
	oldTable := this.table
	this.connections = c
	this.table = table
	this.save()

	return oldTable, nil
}
Ejemplo n.º 2
0
// Creates a new manager.  Uses the one or more seed urls to download the
// routing table.
func NewManagerSeed(partitioner Partitioner, serviceName, dataDir, myEntryId string, seedHttpUrls []string) (*Manager, error) {
	//TODO: can we get the servicename from the routing table?

	manager := NewManager(partitioner, serviceName, dataDir, myEntryId)
	var err error
	for _, url := range seedHttpUrls {

		client := client.NewHttp(url)
		tble, err := RequestRouterTable(client)
		client.Close()
		if err != nil {
			//found a table..  woot
			manager.SetRouterTable(tble)
			return manager, nil
		}
	}

	if manager.table != nil {
		//uhh, I guess we sucessfully loaded it elsewhere
		return manager, nil
	}
	//we still return the manager since it is usable just doesnt have a routing table.
	return manager, err
}