deviceconfiguration.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import "github.com/syncthing/syncthing/lib/protocol"
  8. type DeviceConfiguration struct {
  9. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  10. Name string `xml:"name,attr,omitempty" json:"name"`
  11. Addresses []string `xml:"address,omitempty" json:"addresses"`
  12. Compression protocol.Compression `xml:"compression,attr" json:"compression"`
  13. CertName string `xml:"certName,attr,omitempty" json:"certName"`
  14. Introducer bool `xml:"introducer,attr" json:"introducer"`
  15. SkipIntroductionRemovals bool `xml:"skipIntroductionRemovals,attr" json:"skipIntroductionRemovals"`
  16. IntroducedBy protocol.DeviceID `xml:"introducedBy,attr" json:"introducedBy"`
  17. Paused bool `xml:"paused" json:"paused"`
  18. AllowedNetworks []string `xml:"allowedNetwork,omitempty" json:"allowedNetworks"`
  19. }
  20. func NewDeviceConfiguration(id protocol.DeviceID, name string) DeviceConfiguration {
  21. d := DeviceConfiguration{
  22. DeviceID: id,
  23. Name: name,
  24. }
  25. d.prepare()
  26. return d
  27. }
  28. func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
  29. c := cfg
  30. c.Addresses = make([]string, len(cfg.Addresses))
  31. copy(c.Addresses, cfg.Addresses)
  32. c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
  33. copy(c.AllowedNetworks, cfg.AllowedNetworks)
  34. return c
  35. }
  36. func (cfg *DeviceConfiguration) prepare() {
  37. if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
  38. cfg.Addresses = []string{"dynamic"}
  39. }
  40. if len(cfg.AllowedNetworks) == 0 {
  41. cfg.AllowedNetworks = []string{}
  42. }
  43. }
  44. type DeviceConfigurationList []DeviceConfiguration
  45. func (l DeviceConfigurationList) Less(a, b int) bool {
  46. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  47. }
  48. func (l DeviceConfigurationList) Swap(a, b int) {
  49. l[a], l[b] = l[b], l[a]
  50. }
  51. func (l DeviceConfigurationList) Len() int {
  52. return len(l)
  53. }