func (m *Model) ClusterConfig(nodeID protocol.NodeID, config protocol.ClusterConfigMessage) { m.pmut.Lock() if config.ClientName == "syncthing" { m.nodeVer[nodeID] = config.ClientVersion } else { m.nodeVer[nodeID] = config.ClientName + " " + config.ClientVersion } m.pmut.Unlock() l.Infof(`Node %s client is "%s %s"`, nodeID, config.ClientName, config.ClientVersion) if name := config.GetOption("name"); name != "" { l.Infof("Node %s hostname is %q", nodeID, name) node := m.cfg.GetNodeConfiguration(nodeID) if node != nil && node.Name == "" { node.Name = name } } }
// clusterConfig returns a ClusterConfigMessage that is correct for the given peer node func (m *Model) clusterConfig(node protocol.NodeID) protocol.ClusterConfigMessage { cm := protocol.ClusterConfigMessage{ ClientName: m.clientName, ClientVersion: m.clientVersion, Options: []protocol.Option{ { Key: "name", Value: m.nodeName, }, }, } m.rmut.RLock() for _, repo := range m.nodeRepos[node] { cr := protocol.Repository{ ID: repo, } for _, node := range m.repoNodes[repo] { // NodeID is a value type, but with an underlying array. Copy it // so we don't grab aliases to the same array later on in node[:] node := node // TODO: Set read only bit when relevant cr.Nodes = append(cr.Nodes, protocol.Node{ ID: node[:], Flags: protocol.FlagShareTrusted, }) } cm.Repositories = append(cm.Repositories, cr) } m.rmut.RUnlock() return cm }
func TestNodeRename(t *testing.T) { ccm := protocol.ClusterConfigMessage{ ClientName: "syncthing", ClientVersion: "v0.9.4", } cfg := config.New("test", node1) cfg.Nodes = []config.NodeConfiguration{ { NodeID: node1, }, } db, _ := leveldb.Open(storage.NewMemStorage(), nil) m := NewModel("/tmp", &cfg, "node", "syncthing", "dev", db) if cfg.Nodes[0].Name != "" { t.Errorf("Node already has a name") } m.ClusterConfig(node1, ccm) if cfg.Nodes[0].Name != "" { t.Errorf("Node already has a name") } ccm.Options = []protocol.Option{ { Key: "name", Value: "tester", }, } m.ClusterConfig(node1, ccm) if cfg.Nodes[0].Name != "tester" { t.Errorf("Node did not get a name") } ccm.Options[0].Value = "tester2" m.ClusterConfig(node1, ccm) if cfg.Nodes[0].Name != "tester" { t.Errorf("Node name got overwritten") } }
// clusterConfig returns a ClusterConfigMessage that is correct for the given peer node func (m *Model) clusterConfig(node protocol.NodeID) protocol.ClusterConfigMessage { cm := protocol.ClusterConfigMessage{ ClientName: m.clientName, ClientVersion: m.clientVersion, Options: []protocol.Option{ { Key: "name", Value: m.nodeName, }, }, } m.rmut.RLock() for _, repo := range m.nodeRepos[node] { cr := protocol.Repository{ ID: repo, } for _, node := range m.repoNodes[repo] { // TODO: Set read only bit when relevant cr.Nodes = append(cr.Nodes, protocol.Node{ ID: node[:], Flags: protocol.FlagShareTrusted, }) } cm.Repositories = append(cm.Repositories, cr) } m.rmut.RUnlock() return cm }