MCPcopy
hub / github.com/syncthing/syncthing / AddConnection

Method AddConnection

lib/model/model.go:2290–2349  ·  view source on GitHub ↗

AddConnection adds a new peer connection to the model. An initial index will be sent to the connected peer, thereafter index updates whenever the local folder changes.

(conn protocol.Connection, hello protocol.Hello)

Source from the content-addressed store, hash-verified

2288// be sent to the connected peer, thereafter index updates whenever the local
2289// folder changes.
2290func (m *model) AddConnection(conn protocol.Connection, hello protocol.Hello) {
2291 deviceID := conn.DeviceID()
2292 deviceCfg, ok := m.cfg.Device(deviceID)
2293 if !ok {
2294 slog.Info("Trying to add connection to unknown device")
2295 return
2296 }
2297
2298 connID := conn.ConnectionID()
2299 closed := make(chan struct{})
2300
2301 m.mut.Lock()
2302
2303 m.connections[connID] = conn
2304 m.closed[connID] = closed
2305 m.helloMessages[deviceID] = hello
2306 m.deviceConnIDs[deviceID] = append(m.deviceConnIDs[deviceID], connID)
2307 if m.deviceDownloads[deviceID] == nil {
2308 m.deviceDownloads[deviceID] = newDeviceDownloadState()
2309 }
2310
2311 event := map[string]string{
2312 "id": deviceID.String(),
2313 "deviceName": hello.DeviceName,
2314 "clientName": hello.ClientName,
2315 "clientVersion": hello.ClientVersion,
2316 "type": conn.Type(),
2317 }
2318
2319 addr := conn.RemoteAddr()
2320 if addr != nil {
2321 event["addr"] = addr.String()
2322 }
2323
2324 m.evLogger.Log(events.DeviceConnected, event)
2325
2326 if len(m.deviceConnIDs[deviceID]) == 1 {
2327 slog.Info("New device connection", deviceID.LogAttr(), slogutil.Address(conn.RemoteAddr()), slog.Group("remote", slog.String("name", hello.DeviceName), slog.String("client", hello.ClientName), slog.String("version", hello.ClientVersion)))
2328 } else {
2329 slog.Info("Additional device connection", deviceID.LogAttr(), slogutil.Address(conn.RemoteAddr()), slog.Int("count", len(m.deviceConnIDs[deviceID])-1))
2330 }
2331
2332 m.mut.Unlock()
2333
2334 if (deviceCfg.Name == "" || m.cfg.Options().OverwriteRemoteDevNames) && hello.DeviceName != "" {
2335 m.cfg.Modify(func(cfg *config.Configuration) {
2336 for i := range cfg.Devices {
2337 if cfg.Devices[i].DeviceID == deviceID {
2338 if cfg.Devices[i].Name == "" || cfg.Options.OverwriteRemoteDevNames {
2339 cfg.Devices[i].Name = hello.DeviceName
2340 }
2341 return
2342 }
2343 }
2344 })
2345 }
2346
2347 m.deviceWasSeen(deviceID)

Callers

nothing calls this directly

Calls 15

deviceWasSeenMethod · 0.95
AddressFunction · 0.92
newDeviceDownloadStateFunction · 0.85
UnlockMethod · 0.80
DeviceIDMethod · 0.65
DeviceMethod · 0.65
ConnectionIDMethod · 0.65
StringMethod · 0.65
TypeMethod · 0.65
RemoteAddrMethod · 0.65
LogMethod · 0.65

Tested by

no test coverage detected