示例#1
0
func TestActivityMap(t *testing.T) {
	cm := cid.NewMap()
	fooID := cm.Get("foo")
	if fooID == 0 {
		t.Fatal("ID cannot be zero")
	}
	barID := cm.Get("bar")
	if barID == 0 {
		t.Fatal("ID cannot be zero")
	}

	m := make(activityMap)
	if node := m.leastBusyNode(1<<fooID, cm); node != "foo" {
		t.Errorf("Incorrect least busy node %q", node)
	}
	if node := m.leastBusyNode(1<<barID, cm); node != "bar" {
		t.Errorf("Incorrect least busy node %q", node)
	}
	if node := m.leastBusyNode(1<<fooID|1<<barID, cm); node != "foo" {
		t.Errorf("Incorrect least busy node %q", node)
	}
	if node := m.leastBusyNode(1<<fooID|1<<barID, cm); node != "bar" {
		t.Errorf("Incorrect least busy node %q", node)
	}
}
示例#2
0
// NewModel creates and starts a new model. The model starts in read-only mode,
// where it sends index information to connected peers and responds to requests
// for file data without altering the local repository in any way.
func NewModel(indexDir string, cfg *config.Configuration, clientName, clientVersion string) *Model {
	m := &Model{
		indexDir:      indexDir,
		cfg:           cfg,
		clientName:    clientName,
		clientVersion: clientVersion,
		repoCfgs:      make(map[string]config.RepositoryConfiguration),
		repoFiles:     make(map[string]*files.Set),
		repoNodes:     make(map[string][]string),
		nodeRepos:     make(map[string][]string),
		repoState:     make(map[string]repoState),
		suppressor:    make(map[string]*suppressor),
		cm:            cid.NewMap(),
		protoConn:     make(map[string]protocol.Connection),
		rawConn:       make(map[string]io.Closer),
		nodeVer:       make(map[string]string),
		sup:           suppressor{threshold: int64(cfg.Options.MaxChangeKbps)},
	}

	deadlockDetect(&m.rmut, 60*time.Second)
	deadlockDetect(&m.smut, 60*time.Second)
	deadlockDetect(&m.pmut, 60*time.Second)
	go m.broadcastIndexLoop()
	return m
}
示例#3
0
// NewModel creates and starts a new model. The model starts in read-only mode,
// where it sends index information to connected peers and responds to requests
// for file data without altering the local repository in any way.
func NewModel(maxChangeBw int) *Model {
	m := &Model{
		repoDirs:  make(map[string]string),
		repoFiles: make(map[string]*files.Set),
		repoNodes: make(map[string][]string),
		nodeRepos: make(map[string][]string),
		repoState: make(map[string]repoState),
		cm:        cid.NewMap(),
		protoConn: make(map[string]protocol.Connection),
		rawConn:   make(map[string]io.Closer),
		nodeVer:   make(map[string]string),
		sup:       suppressor{threshold: int64(maxChangeBw)},
	}

	go m.broadcastIndexLoop()
	return m
}